OpenCV——基本图形绘制(椭圆、圆、多边形、直线、矩形)

 1 //绘制椭圆
 2 void DrawEllipse(Mat img, double angle)
 3 {
 4     int thickhness = 2;
 5     int lineType = 8;
 6 
 7     ellipse(img, 
 8         Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2), 
 9         Size(WINDOW_WIDTH / 4, WINDOW_WIDTH / 16),
10         angle, 
11         0, 360, 
12         Scalar(255, 129, 0), 
13         thickhness,
14         lineType);
15 
16 }

 1 //绘制实心圆
 2 void DrawFilledCircle(Mat img, Point center)
 3 {
 4     int thickness = -1;
 5     int lineType = 8;
 6 
 7     circle(img,
 8         center,
 9         WINDOW_WIDTH / 32,
10         Scalar(0, 0, 255),
11         thickness,
12         lineType);
13 }

当thickness为其他>0的值时为正常的空心圆

 1 void DrawPolygon(Mat img)
 2 {
 3     int lineType = 8;
 4 
 5     Point rookPoints[1][20];
 6     rookPoints[0][0] = Point(WINDOW_WIDTH/4,7* WINDOW_WIDTH/8);
 7     rookPoints[0][1] = Point(3* WINDOW_WIDTH/4,7* WINDOW_WIDTH/8);
 8     rookPoints[0][2] = Point(3* WINDOW_WIDTH/4,13* WINDOW_WIDTH/16);
 9     rookPoints[0][3] = Point(11* WINDOW_WIDTH/16,13* WINDOW_WIDTH/16);
10     rookPoints[0][4] = Point(19* WINDOW_WIDTH/32,3* WINDOW_WIDTH/8);
11     rookPoints[0][5] = Point(3* WINDOW_WIDTH/4,3* WINDOW_WIDTH/8);
12     rookPoints[0][6] = Point(3*WINDOW_WIDTH/4, WINDOW_WIDTH/8);
13     rookPoints[0][7] = Point(26* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
14     rookPoints[0][8] = Point(26* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
15     rookPoints[0][9] = Point(22* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
16     rookPoints[0][10] = Point(22* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
17     rookPoints[0][11] = Point(18* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
18     rookPoints[0][12] = Point(18* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
19     rookPoints[0][13] = Point(14* WINDOW_WIDTH/40, WINDOW_WIDTH/4);
20     rookPoints[0][14] = Point(14* WINDOW_WIDTH/40, WINDOW_WIDTH/8);
21     rookPoints[0][15] = Point(WINDOW_WIDTH/4, WINDOW_WIDTH/8);
22     rookPoints[0][16] = Point(WINDOW_WIDTH/4,3* WINDOW_WIDTH/8);
23     rookPoints[0][17] = Point(13* WINDOW_WIDTH/32,3* WINDOW_WIDTH/8);
24     rookPoints[0][18] = Point(5* WINDOW_WIDTH/16,13* WINDOW_WIDTH/16);
25     rookPoints[0][19] = Point(WINDOW_WIDTH/4,13* WINDOW_WIDTH/16);
26 
27     const Point* ppt[1] = { rookPoints[0] };
28     int npt[] = { 20 };
29 
30     fillPoly(img,
31         ppt,
32         npt,
33         1,//好像只能为1,填其他数程序出错
34         Scalar(255, 255, 255),
35         lineType);
36 
37 }

1、cvPolyLine 绘制简单或多样的多边形。
void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed,
                          CvScalar color, int thickness=1, int line_type=8, int shift=0 );
img       图像。
pts       折线的顶点指针数组。
npts     折线的定点个数数组。也可以认为是pts指针数组的大小
contours   折线的线段数量。
is_closed  指出多边形是否封闭。如果封闭,函数将起始点和结束点连线。
color         折线的颜色。
thickness  线条的粗细程度。
line_type  线段的类型。参见cvLine。
shift          顶点的小数点位数。

2、cvFillPoly用于一个单独被多边形轮廓所限定的区域内进行填充。

函数可以填充复杂的区域,例如,有漏洞的区域和有交叉点的区域等等。
void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours,CvScalar color, int line_type=8, int shift=0 );
img

          图像。
pts           指向多边形的数组指针。
npts         多边形的顶点个数的数组。
contours   组成填充区域的线段的数量。
color         多边形的颜色。
line_type  组成多边形的线条的类型。
shift          顶点坐标的小数点位数

绘制直线

1 int thickness = 2;
2     int lineType = 8;
3     line(rookImage,//要绘制的图
4         Point(0, 0),//起始点
5         Point(WINDOW_WIDTH, WINDOW_WIDTH),//终点
6         Scalar(255, 255, 255),
7         thickness,
8         lineType);

绘制矩形

1 rectangle (image3,  rec1,Scalar(0, 0, 255), -1, 8, 0)

rectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,

                  int thickness=1, int line_type=8, int shift=0 );
img
图像.
pt1
矩形的一个顶点。
pt2
矩形对角线上的另一个顶点
color
线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。
thickness
组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形。
line_type
线条的类型。见cvLine的描述
shift
坐标点的小数点位数。

猜你喜欢

转载自www.cnblogs.com/long5683/p/9649356.html