Sometimes the program needs to draw some basic graphics, such as lines, rectangles, ellipses, and polygons. Of course there are such functions in OPENCV.
1. Function Introduction
Straight line:
void Const int thickness=1int linetype=8int shift=0) img– image pt1– Line start pt2– line end color– color thickness– thickness linetype– line type, can be the following value 88-connected line 4
4--antialiasing line. Point coordinates of shift–
Oval Ellipse:
voidEllipse (mat& img, point Center, Size axes,DoubleAngleDoubleStartAngle,DoubleEndangle,Constscalar& Color,intthickness=1,intLinetype=8,intshift=0)voidEllipse (mat& img,Constrotatedrect& box,Constscalar& Color,intthickness=1,intLinetype=8) Parameter description: img– image center– Ellipse Center axes– ellipse main half axis length angle– rotation angle startangle– Elliptical arc Start angle endangle– Elliptical arc End angle box–alternative ellipse re Presentation via Rotatedrect or cvbox2d. This means, the function draws an ellipse inscribedinchThe rotated rectangle.color– color thickness– thickness, if less than 0, indicates that the fill ellipse linetype– and line functions, straight type shift– partial point coordinates
Rectangle Rectangle:
voidRectangle (mat& img, point pt1, point Pt2,Constscalar& Color,intthickness=1,intLinetype=8,intshift=0)voidRectangle (mat& img, Rect Rec,Constscalar& Color,intthickness=1,intLinetype=8,intshift=0) Parameter description: img– image pt1– Vertex coordinates pt2– relative to p1 vertex coordinates rec– rectangle selection Specification color– the color or brightness thickness– the same as the Ellipse function linetype– the same as the line function shift– part Point-of-contact coordinates
Circle Circle:
voidCircle (mat& img, point Center,intRadiusConstscalar& Color,intthickness=1,intLinetype=8,intshift=0) Parameter description: Img–imagewhereThe Circle isDrawn.center–center of the Circle.radius–radius of the circle.color–circle color.thickness–thickness of the C Ircle Outline,ifPositive. Negative thickness means that a filled circle isTo be drawn.linetype–type of the circle boundary. See the line () Description.shift–number of fractional bitsinchThe coordinates of the center andinchThe RADIUS value.
Polygon Fillpoly:
voidFillpoly (mat& img,Constpoint** pts,Const int* NPTs,intNcontours,Constscalar& Color,intLinetype=8,intshift=0, point offset=Point ()) parameter description: Img–image.pts–array of polygonswhereEach polygon isRepresented asAn array of Points.npts–array of polygon vertex counters.ncontours–number of contours. Bind the filled region . Color–polygon Color.linetype–type of the Polygon boundaries. See the line () Description.shift–number of fractional bitsinchThe vertex coordinates.offset–optional offset of all points of the contours.
2. Code
#defineW 400///function DefinitionvoidMyellipse (Mat img,Doubleangle);voidmyfilledcircle (Mat img, point Center);voidmypolygon (Mat img);voidmyline (Mat img, point Start, point end);intBasicdraw (void ){ CharAtom_window[] ="Drawing 1:atom"; CharRook_window[] ="Drawing 2:rook"; Mat Atom_image=Mat::zeros (W, W, CV_8UC3); Mat Rook_image=Mat::zeros (W, W, CV_8UC3); Myellipse (Atom_image, - ); Myellipse (Atom_image,0 ); Myellipse (Atom_image, $ ); Myellipse (Atom_image,- $ ); Myfilledcircle (Atom_image, point (w/2, w/2) ); MyPolygon (Rook_image); Rectangle (rook_image, point (0,7*w/8), Point (W, W), Scalar (0,255,255 ), -1, 8 ); Myline (Rook_image, point (0, the*w/ -), Point (W, the*w/ - ) ); Myline (Rook_image, point (w/4,7*w/8), point (w/4, W)); Myline (Rook_image, point (w/2,7*w/8), point (w/2, W)); Myline (Rook_image, point (3*w/4,7*w/8), point (3*w/4, W)); Imshow (Atom_window, atom_image); MoveWindow (Atom_window,0, $ ); Imshow (Rook_window, rook_image); MoveWindow (Rook_window, W, $ ); Waitkey (0 ); return(0);}//the function of drawing an ellipsevoidMyellipse (Mat img,Doubleangle) { intThickness =2; intLinetype =8; Ellipse (IMG, point (w/2, w/2), Size (w/4, w/ -), Angle,0, the, Scalar (255,0,0), thickness, linetype);}//Draw a circlevoidmyfilledcircle (Mat img, point center) {intThickness =-1; intLinetype =8; Circle (IMG, center, W/ +, Scalar (0,0,255), thickness, linetype);}//Draw Polygonsvoidmypolygon (Mat img) {intLinetype =8; /** Create some points*/Point rook_points[1][ -]; rook_points[0][0] = Point (w/4,7*w/8 ); rook_points[0][1] = Point (3*w/4,7*w/8 ); rook_points[0][2] = Point (3*w/4, -*w/ - ); rook_points[0][3] = Point ( One*w/ -, -*w/ - ); rook_points[0][4] = Point ( +*w/ +,3*w/8 ); rook_points[0][5] = Point (3*w/4,3*w/8 ); rook_points[0][6] = Point (3*w/4, w/8 ); rook_points[0][7] = Point ( -*w/ +, w/8 ); rook_points[0][8] = Point ( -*w/ +, w/4 ); rook_points[0][9] = Point ( A*w/ +, w/4 ); rook_points[0][Ten] = Point ( A*w/ +, w/8 ); rook_points[0][ One] = Point ( -*w/ +, w/8 ); rook_points[0][ A] = Point ( -*w/ +, w/4 ); rook_points[0][ -] = Point ( -*w/ +, w/4 ); rook_points[0][ -] = Point ( -*w/ +, w/8 ); rook_points[0][ the] = Point (w/4, w/8 ); rook_points[0][ -] = Point (w/4,3*w/8 ); rook_points[0][ -] = Point ( -*w/ +,3*w/8 ); rook_points[0][ -] = Point (5*w/ -, -*w/ - ); rook_points[0][ +] = Point (w/4, -*w/ - ); Constpoint* ppt[1] = {rook_points[0] }; intNpt[] = { - }; Fillpoly (IMG, PPT, NPT,1, Scalar (255,255,255), Linetype);}//the function of drawing a straight linevoidmyline (Mat img, point Start, point end) {intThickness =2; intLinetype =8; Line (IMG, start, End, Scalar (0,0,0), thickness, linetype);}
3. Results
4. Other Notes
Point
Structure:
Define a "point", x parameter, and y parameter.
Scalar structure:
A scalar is a container with four elements that can use only a subset of its elements, such as the RGB that represents the color using scalar (a,b,c) above.
5. End
OPENCV Introduction: (vi: basic drawing function)