Contents
  1. I. Drawing Basic Shapes
  2. 1. Rectangle
  3. 2. Circle
  4. 3. Line
  5. 4. Ellipse
  6. II. Methods for Drawing Polygons
  7. 2.1 Polygon Border
  8. 2.2 Filling Polygons
  9. 2.3 Drawing Multiple Polygons

  Before starting the main text, first note that the (0, 0) coordinate point on the screen is at the upper-left corner, and the maximum coordinates are at the lower-right corner.

I. Drawing Basic Shapes

1. Rectangle

  In OpenCV, the function for drawing a rectangle is rectangle() .

void rectangle(InputOutputArray img, Rect rec, const Scalar& color, int thickness = 1, int lineType = LINE_8, int shift = 0);
ParameterPurpose
InputOutputArray imgTarget image to draw on
Rect recRect class object
const Scalar& colorLine color
int thickness = 1Positive values mean line width; -1 means fill the rectangle
int lineType = LINE_8Line type
int shift = 0Number of fractional bits in the coordinates

Example program for drawing a rectangle:

void MyDemo::drawing_Demo(Mat& image) {
	//绘制矩形
	Rect rect;
	rect.x = 250;	//左上端点x坐标
	rect.y = 170;	//左上端点x坐标
	rect.width = 100;	//宽度
	rect.height = 100;	//高度
	rectangle(image, rect, Scalar(0, 0, 255), 2, 8, 0);
	imshow("Drawing", image);
}

Rectangle

2. Circle

  In OpenCV, the function for drawing a circle is rectangle() .

void cv::circle (InputOutputArray img, Point center, int radius, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0);
ParameterPurpose
InputOutputArray imgTarget image to draw on
Point centerCenter coordinates of the circle
int radiusRadius of the circle
const Scalar &colorColor of the circle
int thickness=1Positive values mean line width; -1 means fill the circle
int lineType = LINE_8Line type
int shift = 0Number of fractional bits in the coordinates

Example program for drawing a circle:

void MyDemo::drawing_Demo(Mat& image) {
	//绘制圆
	circle(image, Point(250, 170), 20, Scalar(255, 0, 0), -1, 8, 0);
	imshow("Drawing", image);
}

Circle

  

3. Line

  In OpenCV, the function for drawing a line is rectangle() .

void cv::line(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0);
ParameterPurpose
InputOutputArray imgTarget image to draw on
Point pt1rCoordinates of endpoint 1
Point pt2Coordinates of endpoint 2
const Scalar &colorLine color
int thickness=1Line width
int lineType = LINE_8Line type
int shift = 0Number of fractional bits in the coordinates

Example program for drawing a circle:

void MyDemo::drawing_Demo(Mat& image) {
	//绘制直线
	line(image, Point(250, 170), Point(350, 270), Scalar(0, 255, 0), 2, LINE_AA, 0);
	imshow("Drawing", image);
}

Line  

4. Ellipse

  In OpenCV, the function for drawing an ellipse is ellipse() .

void ellipse(Mat&img, const RotatedRect&box, const Scalar& color, int thickness=1, int lineType=8);
ParameterPurpose
Mat&imgTarget image to draw on
const RotatedRect&boxEllipse class
const Scalar &colorColor of the ellipse
int thickness=1Positive values mean line width; -1 means fill the circle
int lineType = LINE_8Line type

The properties of the ellipse class RotatedRect are as follows (example):

RotatedRect rrt;	//创建对象
rrt.center = Point(100, 200);	//椭圆中心点
rrt.size = Size(100, 200);		//椭圆大小(横轴,纵轴长度)
rrt.angle = 0;		//旋转角度

Example program for drawing a circle:

void MyDemo::drawing_Demo(Mat& image) {
	//绘制椭圆
	RotatedRect rrt;
	rrt.center = Point(100, 200);
	rrt.size = Size(100, 200);
	rrt.angle = 0;
	ellipse(image, rrt, Scalar(0, 255, 255), 1, 8);
	imshow("Drawing", image);
}

Ellipse

  

II. Methods for Drawing Polygons

  The essence of drawing a polygon is to define a point set composed of multiple points, then connect the points in that set to form a polygon.

2.1 Polygon Border

  The function used to draw a polygon border is ploylines() .

void cv::polylines(
		InputOutputArray	img,
		InputArrayOfArrays	pts,
		bool	isClosed,
		const Scalar & color,
		int		thickness = 1,
		int		lineType = LINE_8,
		int		shift = 0
)
ParameterPurpose
imgTarget image to draw on
ptsPoint set constructed in advance
isClosedWhether the polygon is a closed shape
colorColor of the polygon border
thicknessLine width (must be a positive number)
lineTypeLine type
shiftNumber of fractional bits in the coordinates

Example program: drawing a pentagon

void MyDemo::polyDrawing_Demo() {
	//创建画布
	Mat bg = Mat::zeros(Size(512, 512), CV_8UC3);

	//创建五个点,作为多边形五个顶点
	Point p1(100, 100);
	Point p2(350, 100);
	Point p3(450, 300);
	Point p4(250, 450);
	Point p5(80, 200);

	//将五个点依次添加到点集中
	std::vector<Point> pts;
	pts.push_back(p1);
	pts.push_back(p2);
	pts.push_back(p3);
	pts.push_back(p4);
	pts.push_back(p5);

	//绘制多边形边框
	polylines(bg, pts, true, Scalar(0, 255, 255), 3, LINE_AA, 0);
	imshow("Poly Drawing!", bg);
}

Here pts.push_back is used to add points to the point-set array, for cases where the number of points is unknown. This program already knows there are five points in total, so you can also create the point set with the following code:

	std::vector<Point> pts(5);
	pts[0] = p1;
	pts[1] = p2;
	pts[2] = p3;
	pts[3] = p4;
	pts[4] = p5;

Polygon Border

2.2 Filling Polygons

  The function used to draw a filled polygon is fillPoly() .

void cv::fillPoly(
		InputOutputArray	img,
		InputArrayOfArrays	pts,
		const Scalar & color,
		int		lineType = LINE_8,
		int		shift = 0
)
ParameterPurpose
imgTarget image to draw on
ptsPoint set constructed in advance
colorColor of the polygon border
lineTypeLine type
shiftNumber of fractional bits in the coordinates

Example program: drawing a filled pentagon

void MyDemo::polyDrawing_Demo() {
	//创建画布
	Mat bg = Mat::zeros(Size(512, 512), CV_8UC3);

	//创建五个点,作为多边形五个顶点
	Point p1(100, 100);
	Point p2(350, 100);
	Point p3(450, 300);
	Point p4(250, 450);
	Point p5(80, 200);

	//将五个点依次添加到点集中
	std::vector<Point> pts;
	pts.push_back(p1);
	pts.push_back(p2);
	pts.push_back(p3);
	pts.push_back(p4);
	pts.push_back(p5);

	//绘制封闭的五边形
	fillPoly(bg, pts, Scalar(255, 255, 0), 8, 0);
	imshow("Poly Drawing!", bg);
}

Filling Polygons

2.3 Drawing Multiple Polygons

  The function used to draw multiple polygons is drawContours() .

void drawContours(
	InputOutputArray 	image,
	InputArrayOfArrays 	contours,
	int 	contourIdx,
	const Scalar& color,
	int 	thickness=1,
	int 	lineType=8,
	InputArray 	hierarchy=noArray(),
	int 		maxLevel=INT_MAX,
	Point 		offset=Point() )
ParameterPurpose
imgTarget image to draw on
contoursInput contour groups; each contour group consists of a point vector
contourIdxWhich contour to draw; if this parameter is negative, draw all contours
colorLine color
thicknessLine width; a negative value or CV_FILLED means fill the interior of the contour
lineTypeLine type

Example program: drawing a pentagon

void MyDemo::polyDrawing_Demo() {
	//创建画布
	Mat bg = Mat::zeros(Size(512, 512), CV_8UC3);

	//创建五个点,作为多边形五个顶点
	Point p1(100, 100);
	Point p2(350, 100);
	Point p3(450, 300);
	Point p4(250, 450);
	Point p5(80, 200);

	//将五个点依次添加到点集中
	std::vector<Point> pts;
	pts.push_back(p1);
	pts.push_back(p2);
	pts.push_back(p3);
	pts.push_back(p4);
	pts.push_back(p5);

	//添加点集到contours
	//(相当于contours是点集的集合)
	std::vector<std::vector<Point>> contours;
	contours.push_back(pts);

	//绘制多边形
	drawContours(bg, contours, -1, Scalar(255, 0, 255), -1);
	imshow("Poly Drawing!", bg);
}

Drawing Multiple Polygons