C++Opencv image geometry drawing

1. The function rectangle() to draw a rectangle

The function rectangle() for drawing rectangles is provided in opencv

void cv::rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar & color, int  thickness = 1,
                   int  lineType = LINE_8, int  shift = 0)
 
void cv::rectangle(InputOutputArray img, Rect rec, const Scalar & color, int  thickness = 1,
                   int  lineType = LINE_8, int  shift = 0)            

Parameter explanation:
pt1: a vertex of the rectangle
pt2: the vertex opposite to pt1 in the rectangle, that is, two points are on the diagonal.
rec: The fixed point and length and width of the upper left corner of the rectangle.
Scalar: color bgr
thickness: line width
lineType: line type
shift: number of decimal places for coordinate points

2. The function line() for drawing lines

This function uses two points to determine a straight line to draw a straight line in the image

void cv::line(InputOutputArray img,Point pt1, Point pt2, const Scalar & color, int  thickness = 1, int  lineType = LINE_8, int  shift = 0)

Parameter explanation:
pt1: The coordinates of the starting point of the line in the image.
pt2: The coordinates of the end point of the line in the image.
color: the color of the circle, represented by three channels.

3. The function rectangle() to draw a circle

void cv::circle(InputOutputArray img, Point center,  int  radius, const Scalar & color, int  thickness = 1, int  lineType = LINE_8, int  shift = 0)

Parameter explanation:
img: the image that needs to draw a circle
center: the coordinates of the center of the circle.
radius: The radius length of the circle, in pixels.
color: the color of the circle.
thickness: the width of the outline, if the value is negative, draw a solid circle.
lineType: the type of boundary, the possible values ​​are FILLED, LINE_4, LINE_8 and LINE_AA
shift: the number of decimal places in the center coordinates and radius values.

4. The function ellipse() to draw an ellipse

void cv::ellipse(InputOutputArray img, Point center, Size axes, double  angle, double  startAngle, double  endAngle,
                 const Scalar & color, int  thickness = 1, int  lineType = LINE_8, int  shift = 0)

Parameter explanation:
center: the center coordinate of the ellipse.
axes: Half the size of the ellipse's axes.
angle: The angle at which the ellipse is rotated, in degrees.
startAngle: The angle at which the ellipse arc starts, in degrees.
endAngle: The angle at which the ellipse arc ends, in degrees

In OpenCV 4, another function ellipse2Poly() is provided to output the pixel coordinates of the boundary of the ellipse, but the ellipse will not be drawn in the image. The function prototype is given in Listing 3-43.

void cv::ellipse2Poly(Point center, Size axes, int angle, int arcStart, int arcEnd, int delta,
std::vector< Point > & pts)
Parameter explanation:
This function is consistent with the parameters that need to be input when drawing an ellipse, just different The ellipse is output to the image, but the coordinate points of the edge of the ellipse are stored through the vector vector, which is convenient for subsequent processing
delta: the angle between the vertices of the subsequent polyline, which defines the approximate accuracy.
pts: ellipse edge pixel coordinate vector set.

Sample code:

	Rect rect;
	rect.x = 100;
	rect.y = 100;
	rect.width = 250;
	rect.height = 300;

	Mat bg = Mat::zeros(image.size(), image.type());
	rectangle(bg, rect, Scalar(0, 0, 255), -1, 8, 0);
	circle(bg, Point(350,400),15,Scalar(0,255,0),-1,LINE_AA, 0);
	line(bg, Point(100, 100), Point(350, 400), Scalar(0, 255, 0), 4, LINE_AA, 0);

	RotatedRect rrt;
	rrt.center = Point(200,200);
	rrt.size = Size(100,200);
	rrt.angle = 90.0;
	ellipse(bg, rrt, Scalar(0, 255, 255), 2, 8);
	
	Mat dst;
	addWeighted(image, 0.7, bg, 0.3, 0, dst);
	imshow("绘制演示", bg);

insert image description here

5. Functions for drawing polygons

5.1 fillPoly() fill drawing

void cv::fillPoly(     
         InputOutputArray  img,
         InputArrayOfArrays       pts,
         const Scalar &        color,
         int   lineType = LINE_8,
         int   shift = 0,
         Point       offset = Point()

Parameter explanation:
img means drawing canvas, image
pts polygon vertex array, can store multiple polygon vertex coordinate arrays.
color means color
lineType means line rendering type
shift means relative displacement
offset: optional offset for all vertices

5.2 polylines() filling drawing

void cv::polylines(
         InputOutputArray  img,
         InputArrayOfArrays       pts,
         bool        isClosed,
         const Scalar &        color,
         int   thickness = 1,
         int   lineType = LINE_8,
         int   shift = 0
)

Parameter explanation:
img means drawing canvas, image
pts means polygon point
isClosed means closed, default closed
color means color
thickness means line width, must be a positive number
lineType means line rendering type
shift means relative displacement

5.3 drawContours() function

 1void cv::drawContours (        
 2    InputOutputArray  image,
 3    InputArrayOfArrays  contours,
 4    int   contourIdx,
 5    const Scalar &        color,
 6    int   thickness = 1,
 7    int   lineType = LINE_8,
 8    InputArray      hierarchy = noArray(),
 9    int   maxLevel = INT_MAX,
10    Point       offset = Point()
11)

drawContours() can complete the filling and drawing of multiple polygons. It essentially treats the vertex set of each polygon as a contour, and can easily complete the drawing and filling of contours, as well as the drawing of points, lines, circles, ellipses and rectangles. Like filling, drawing and filling are realized by setting the thickness parameter. It just needs to be changed appropriately when entering. A collection of multiple polygon points represented by the parameter controus, contourIdx greater than zero means to draw the contour of the specified index, -1 means to draw all, a positive thickness means to draw, and a non-positive number means to fill

Sample code:

	Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3);
	Point p1(100,100);
	Point p2(350, 100);
	Point p3(450, 280);
	Point p4(320, 450);
	Point p5(80, 400);
	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(canvas, pts, Scalar(255, 0, 255), 8, 0);     //多边形填充
	//polylines(canvas, pts, true, Scalar(0,0,255),2,8,0);  //多边形绘制

	// 方法二
	std::vector<std::vector<Point>> contours;  //多边形集合
	contours.push_back(pts);
	drawContours(canvas, contours, -1, Scalar(255,0,0), -1);
	imshow("多边形绘制", canvas);

insert image description here

Guess you like

Origin blog.csdn.net/qq_45373844/article/details/127830525