Open CV 學習筆記:基本圖形繪製

1.用於繪製直線的line函數

2.用於繪製圓的circle函數

3.用於繪製橢圓的ellipse函數

4.用於繪製矩形的rectangle函數

5.用於繪製填充多邊形的fillPoly函數


Line

C++: void line(Mat& img, Point pt1,Point pt2, const Scalar& color, int thickness=1, int lineType=8,int shift=0)

Parameters:

  • img – 圖像.
  • pt1 – 線條起點.
  • pt2 – 線條終點.
  • color – 線條顏色.
  • thickness – 線條寬度.
  • lineType – 線型

Type of the line:

    • 8 (or omitted) - 8-connected line.
    • 4 - 4-connected line.
    • CV_AA - antialiased line.
  • shift – 座標點小數點位數.

Circle

C++: void circle(Mat&img, Point center, intradius, const Scalar&color,intthickness=1, intlineType=8, intshift=0)

Parameters:

  • img – 要畫圓的那個矩形.
  • center – 圓心座標.
  • radius – 半徑.
  • color – 圓邊框顏色,scalar類型的
  • thickness – 正值表示圓邊框寬度. 負值表示畫一個填充圓形
  • lineType – 圓邊框線型
  • shift – 圓心座標和半徑的小數點位數

Ellipse

C++: void ellipse(Mat& img, Point center,Size axes, double angle, double startAngle, double endAngle, const Scalar& color,int thickness=1, int lineType=8, int shift=0)

C++: void ellipse(Mat& img, constRotatedRect& box, const Scalar& color, int thickness=1, int lineType=8)

Parameters: 

  • img – 橢圓所在圖像.
  • center – 橢圓中心.
  • axes – 橢圓主軸一半的長度
  • angle – 橢圓旋轉角度
  • startAngle – 橢圓弧起始角度
  • endAngle –橢圓弧終止角度
  • box – 指定橢圓中心和旋轉角度的信息,通過 RotatedRect 或 CvBox2D. 這表示橢圓畫在旋轉矩形上(矩形是不可見的,只是指定了一個框而已)
  • color – 橢圓邊框顏色.
  • thickness – 正值代表橢圓邊框寬度,負值代表填充的橢圓
  • lineType – 線型
  • shift – 橢圓中心座標和座標軸的小數點位數


Rectangle

C++: void rectangle(Mat& img,Point pt1, Pointpt2, const Scalar&color, intthickness=1,intlineType=8, intshift=0)

C++: void rectangle(Mat& img,Rect rec, const Scalar&color, intthickness=1, intlineType=8,intshift=0 )

Parameters:

  • img – 畫矩形的對象
  • pt1 – 矩形的一個頂點,左上角的.
  • pt2 – 另一個頂點,右下角的.
  • rec – 確定矩形的另一種方式,給左上角座標和長寬
  • color – 指定矩形的顏色或亮度(灰度圖像),scalar(255,0,255)既可指定.
  • thickness – 矩形邊框的粗細. 負值(like CV_FILLED)表示要畫一個填充的矩形
  • lineType – 邊框線型. (   

                8 (or 0) - 8-connected line(8鄰接)連接 線。

                4 - 4-connected line(4鄰接)連接線。

                CV_AA - antialiased 線條。)

  • shift –座標點的小數點位數

fillPoly

C++: void fillPoly(Mat& img, const Point** pts, const int* npts, int ncontours, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

C++: void fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )


Parameters: 

  • img – 折線所在圖像
  • pts – 折線中拐點座標指針.
  • npts – 折線拐點個數指針.
  • ncontours – 折線線段數量.
  • color – 折線顏色.
  • thickness – 折線寬度.
  • lineType – 線型.
  • shift – 橢圓中心座標和座標軸的小數點位數       


#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#define WINDOW_NAME "CHUANG"
#define WINDOW_WIDTH 600
using namespace cv;

void DrawEllipse(Mat img,double angle){//繪製橢圓形
	int thickness = 7;
	int lineType = 8;

	ellipse(img,
			Point(WINDOW_WIDTH/2,WINDOW_WIDTH/2),
			Size(WINDOW_WIDTH/4,WINDOW_WIDTH/16),
			angle,
			0,
			60,
			Scalar(25,129,240),
			thickness,
			lineType);
}

void DrawFilledCircle(Mat& img,Point center){//繪製圓形
	int thickness = -1;
	int lineType = 8;

	circle(img,
		   center,
		   WINDOW_WIDTH/32,
		   Scalar(43,223,34),
		   thickness,
		   lineType);
}

void DrawPolygon(Mat img){//繪製多邊形
	int lineType = 8;
	Point pp[2][4];
	pp[0][0] = Point(WINDOW_WIDTH/4,WINDOW_WIDTH/2);
	pp[0][1] = Point(WINDOW_WIDTH/2,WINDOW_WIDTH/4);
	pp[0][2] = Point(WINDOW_WIDTH,WINDOW_WIDTH/2);
	pp[0][3] = Point(WINDOW_WIDTH/2,WINDOW_WIDTH);


	pp[1][0] = Point(0,0);
	pp[1][1] = Point(WINDOW_WIDTH/2,0);
	pp[1][2] = Point(0,WINDOW_WIDTH/2);

	const Point* ppt[1] = {pp[0]};
	int npt[] = {4};
 
	fillPoly(img,
			 ppt,
			 npt,
			 1,
			 Scalar(33,134,45),
			 lineType);


	ppt[0] = pp[1];
	npt[0] = 3;
	fillPoly(img,
		ppt,
		npt,
		1,
		Scalar(133,34,145),
		lineType);
}
void DrawLine(Mat img,Point start,Point end){//繪製直線
	int thickness = 2;
	int lingType = 8;

	line(img,
		 start,
		 end,
		 Scalar(34,122,66),
		 thickness,
		 lingType);
}

int main(int argc,char** argv){
	
	Mat atomImage = Mat::zeros(WINDOW_WIDTH,WINDOW_WIDTH,CV_8UC3);
	Mat rookImage = Mat::zeros(WINDOW_WIDTH,WINDOW_WIDTH,CV_8UC3);

	//繪製橢圓
	for(int i = 0;i <= 360;i += 45)
		DrawEllipse(atomImage,i);
	//繪製圓形
	DrawFilledCircle(atomImage,Point(WINDOW_WIDTH/2,WINDOW_WIDTH/2));
	//繪製矩形
	rectangle(atomImage,Point(WINDOW_WIDTH/16,WINDOW_WIDTH/16),Point(WINDOW_WIDTH/4,WINDOW_WIDTH/4),Scalar(43,46,44),5,8);
	imshow("矩形、圓和橢圓",atomImage);

    //繪製直線
	DrawLine(rookImage,Point(0,WINDOW_WIDTH/2),Point(WINDOW_WIDTH,0));
	//繪製多邊形
	DrawPolygon(rookImage);
	imshow("多邊形、直線",rookImage);

	waitKey(0);
	return 0;
}





發佈了229 篇原創文章 · 獲贊 12 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章