OpenCV4入门教程062:绘图综合测试

索引地址:系列索引

绘制图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

//此程序对于OpenCV3版需要额外包含头文件:
#include <opencv2/imgproc/imgproc.hpp>

//-----------------------------------【宏定义部分】--------------------------------------------
// 描述:定义一些辅助宏
//------------------------------------------------------------------------------------------------
#define WINDOW_NAME1 "Drawing1" //为窗口标题定义的宏
#define WINDOW_NAME2 "Drawing2" //为窗口标题定义的宏
#define WINDOW_WIDTH 600 //定义窗口大小的宏

//--------------------------------【全局函数声明部分】-------------------------------------
// 描述:全局函数声明
//-----------------------------------------------------------------------------------------------
void DrawEllipse(Mat img, double angle); //绘制椭圆
void DrawFilledCircle(Mat img, Point center); //绘制圆
void DrawPolygon(Mat img); //绘制多边形
void DrawLine(Mat img, Point start, Point end); //绘制线段

//---------------------------------------【main( )函数】--------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始执行
//-----------------------------------------------------------------------------------------------
int main(void) {

// 创建空白的Mat图像
Mat atomImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);
Mat rookImage = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

// ---------------------<1>绘制化学中的原子示例图------------------------

//【1.1】先绘制出椭圆
DrawEllipse(atomImage, 90);
DrawEllipse(atomImage, 0);
DrawEllipse(atomImage, 45);
DrawEllipse(atomImage, -45);

//【1.2】再绘制圆心
DrawFilledCircle(atomImage, Point(WINDOW_WIDTH / 2, WINDOW_WIDTH / 2));

// ----------------------------<2>绘制组合图-----------------------------
//【2.1】先绘制出椭圆
DrawPolygon(rookImage);

// 【2.2】绘制矩形
rectangle(rookImage, Point(0, 7 * WINDOW_WIDTH / 8), Point(WINDOW_WIDTH, WINDOW_WIDTH),
Scalar(0, 255, 255), -1, 8);

// 【2.3】绘制一些线段
DrawLine(rookImage, Point(0, 15 * WINDOW_WIDTH / 16),
Point(WINDOW_WIDTH, 15 * WINDOW_WIDTH / 16));
DrawLine(rookImage, Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8),
Point(WINDOW_WIDTH / 4, WINDOW_WIDTH));
DrawLine(rookImage, Point(WINDOW_WIDTH / 2, 7 * WINDOW_WIDTH / 8),
Point(WINDOW_WIDTH / 2, WINDOW_WIDTH));
DrawLine(rookImage, Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8),
Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH));

// ---------------------------<3>显示绘制出的图像------------------------
imshow(WINDOW_NAME1, atomImage);
moveWindow(WINDOW_NAME1, 0, 200);
imshow(WINDOW_NAME2, rookImage);
moveWindow(WINDOW_NAME2, WINDOW_WIDTH, 200);

waitKey(0);
return (0);
}

//-------------------------------【DrawEllipse( )函数】--------------------------------
// 描述:自定义的绘制函数,实现了绘制不同角度、相同尺寸的椭圆
//-----------------------------------------------------------------------------------------
void DrawEllipse(Mat img, double angle) {
int thickness = 2;
int lineType = 8;

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

//-----------------------------------【DrawFilledCircle( )函数】---------------------------
// 描述:自定义的绘制函数,实现了实心圆的绘制
//-----------------------------------------------------------------------------------------
void DrawFilledCircle(Mat img, Point center) {
int thickness = -1;
int lineType = 8;

circle(img, center, WINDOW_WIDTH / 32, Scalar(0, 0, 255), thickness, lineType);
}

//-----------------------------------【DrawPolygon( )函数】--------------------------
// 描述:自定义的绘制函数,实现了凹多边形的绘制
//--------------------------------------------------------------------------------------
void DrawPolygon(Mat img) {
int lineType = 8;

//创建一些点
Point rookPoints[ 1 ][ 20 ];
rookPoints[ 0 ][ 0 ] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 1 ] = Point(3 * WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 2 ] = Point(3 * WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
rookPoints[ 0 ][ 3 ] = Point(11 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rookPoints[ 0 ][ 4 ] = Point(19 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 5 ] = Point(3 * WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 6 ] = Point(3 * WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 7 ] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 8 ] = Point(26 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[ 0 ][ 9 ] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[ 0 ][ 10 ] = Point(22 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 11 ] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 12 ] = Point(18 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[ 0 ][ 13 ] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
rookPoints[ 0 ][ 14 ] = Point(14 * WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 15 ] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 16 ] = Point(WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 17 ] = Point(13 * WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
rookPoints[ 0 ][ 18 ] = Point(5 * WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
rookPoints[ 0 ][ 19 ] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);

const Point *ppt[ 1 ] = {rookPoints[ 0 ]};
int npt[] = {20};

fillPoly(img, ppt, npt, 1, Scalar(255, 255, 255), lineType);
}

//-----------------------------------【DrawLine( )函数】--------------------------
// 描述:自定义的绘制函数,实现了线的绘制
//---------------------------------------------------------------------------------
void DrawLine(Mat img, Point start, Point end) {
int thickness = 2;
int lineType = 8;
line(img, start, end, Scalar(0, 0, 0), thickness, lineType);
}

测试结果:

testing6

动态绘制图形

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//------------------------------------------------------------------------------------------------
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <stdio.h>
using namespace cv;

//-----------------------------------【全局变量声明部分】---------------------------------------
// 描述:声明全局变量
//---------------------------------------------------------------------------------------------------
const int NUMBER = 100;
const int DELAY = 5;

const int window_width = 900;
const int window_height = 600;
int x_1 = -window_width / 2;
int x_2 = window_width * 3 / 2;
int y_1 = -window_width / 2;
int y_2 = window_width * 3 / 2;

//-----------------------------------【函数声明部分】-------------------------------------------
// 描述:函数原型声明
//---------------------------------------------------------------------------------------------------
static Scalar randomColor(RNG &rng);
int Drawing_Random_Lines(Mat image, char *window_name, RNG rng);
int Drawing_Random_Rectangles(Mat image, char *window_name, RNG rng);
int Drawing_Random_Ellipses(Mat image, char *window_name, RNG rng);
int Drawing_Random_Polylines(Mat image, char *window_name, RNG rng);
int Drawing_Random_Filled_Polygons(Mat image, char *window_name, RNG rng);
int Drawing_Random_Circles(Mat image, char *window_name, RNG rng);
int Displaying_Random_Text(Mat image, char *window_name, RNG rng);
int Displaying_Big_End(Mat image, char *window_name, RNG rng);

//-----------------------------------【main( )函数】-------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-----------------------------------------------------------------------------------------------
int main(void) {
int c;

//创建窗口
char window_name[] = "Drawing2";

// 创建随机的RNG
RNG rng(0xFFFFFFFF);

//初始化矩阵
Mat image = Mat::zeros(window_height, window_width, CV_8UC3);

// 在 DELAY ms内显示窗口
imshow(window_name, image);
waitKey(DELAY);

//线条的绘制
c = Drawing_Random_Lines(image, window_name, rng);
if (c != 0)
return 0;

// 矩形的绘制
c = Drawing_Random_Rectangles(image, window_name, rng);
if (c != 0)
return 0;

// 椭圆的绘制
c = Drawing_Random_Ellipses(image, window_name, rng);
if (c != 0)
return 0;

// 折线的绘制
c = Drawing_Random_Polylines(image, window_name, rng);
if (c != 0)
return 0;

// 多边形的绘制
c = Drawing_Random_Filled_Polygons(image, window_name, rng);
if (c != 0)
return 0;

// 绘制圆形
c = Drawing_Random_Circles(image, window_name, rng);
if (c != 0)
return 0;

// 随机绘制文字
c = Displaying_Random_Text(image, window_name, rng);
if (c != 0)
return 0;

//绘制最终的谢幕
c = Displaying_Big_End(image, window_name, rng);
if (c != 0)
return 0;

waitKey(0);
return 0;
}

//-----------------------------------【randomColor( )函数】-----------------------------------
// 描述:生成随机颜色
//---------------------------------------------------------------------------------------------------
static Scalar randomColor(RNG &rng) {
int icolor = (unsigned)rng;
return Scalar(icolor & 255, (icolor >> 8) & 255, (icolor >> 16) & 255);
}

//---------------------------【Drawing_Random_Lines( )函数】-------------------------------
// 描述:绘制随机的线条
//---------------------------------------------------------------------------------------------------
int Drawing_Random_Lines(Mat image, char *window_name, RNG rng) {
Point pt1, pt2;

for (int i = 0; i < NUMBER; i++) {
pt1.x = rng.uniform(x_1, x_2);
pt1.y = rng.uniform(y_1, y_2);
pt2.x = rng.uniform(x_1, x_2);
pt2.y = rng.uniform(y_1, y_2);

line(image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), 8);
imshow(window_name, image);
if (waitKey(DELAY) >= 0) {
return -1;
}
}

return 0;
}

//---------------------------【Drawing_Random_Ellipses( )函数】----------------------------
// 描述:绘制随机的矩形
//---------------------------------------------------------------------------------------------------
int Drawing_Random_Rectangles(Mat image, char *window_name, RNG rng) {
Point pt1, pt2;
int lineType = 8;
int thickness = rng.uniform(-3, 10);

for (int i = 0; i < NUMBER; i++) {
pt1.x = rng.uniform(x_1, x_2);
pt1.y = rng.uniform(y_1, y_2);
pt2.x = rng.uniform(x_1, x_2);
pt2.y = rng.uniform(y_1, y_2);

rectangle(image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType);

imshow(window_name, image);
if (waitKey(DELAY) >= 0) {
return -1;
}
}

return 0;
}

//---------------------------【Drawing_Random_Ellipses( )函数】----------------------------
// 描述:绘制随机的椭圆
//---------------------------------------------------------------------------------------------------
int Drawing_Random_Ellipses(Mat image, char *window_name, RNG rng) {
int lineType = 8;

for (int i = 0; i < NUMBER; i++) {
Point center;
center.x = rng.uniform(x_1, x_2);
center.y = rng.uniform(y_1, y_2);

Size axes;
axes.width = rng.uniform(0, 200);
axes.height = rng.uniform(0, 200);

double angle = rng.uniform(0, 180);

ellipse(image, center, axes, angle, angle - 100, angle + 200, randomColor(rng),
rng.uniform(-1, 9), lineType);

imshow(window_name, image);

if (waitKey(DELAY) >= 0) {
return -1;
}
}

return 0;
}

//---------------------------【Drawing_Random_Polylines( )函数】--------------------------
// 描述:绘制随机的折线
//---------------------------------------------------------------------------------------------------
int Drawing_Random_Polylines(Mat image, char *window_name, RNG rng) {
int lineType = 8;

for (int i = 0; i < NUMBER; i++) {
Point pt[ 2 ][ 3 ];
pt[ 0 ][ 0 ].x = rng.uniform(x_1, x_2);
pt[ 0 ][ 0 ].y = rng.uniform(y_1, y_2);
pt[ 0 ][ 1 ].x = rng.uniform(x_1, x_2);
pt[ 0 ][ 1 ].y = rng.uniform(y_1, y_2);
pt[ 0 ][ 2 ].x = rng.uniform(x_1, x_2);
pt[ 0 ][ 2 ].y = rng.uniform(y_1, y_2);
pt[ 1 ][ 0 ].x = rng.uniform(x_1, x_2);
pt[ 1 ][ 0 ].y = rng.uniform(y_1, y_2);
pt[ 1 ][ 1 ].x = rng.uniform(x_1, x_2);
pt[ 1 ][ 1 ].y = rng.uniform(y_1, y_2);
pt[ 1 ][ 2 ].x = rng.uniform(x_1, x_2);
pt[ 1 ][ 2 ].y = rng.uniform(y_1, y_2);

const Point *ppt[ 2 ] = {pt[ 0 ], pt[ 1 ]};
int npt[] = {3, 3};

polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1, 10), lineType);

imshow(window_name, image);
if (waitKey(DELAY) >= 0) {
return -1;
}
}
return 0;
}

//---------------------------【Drawing_Random_Filled_Polygons( )函数】------------------
// 描述:绘制随机的多边形
//---------------------------------------------------------------------------------------------------
int Drawing_Random_Filled_Polygons(Mat image, char *window_name, RNG rng) {
int lineType = 8;

for (int i = 0; i < NUMBER; i++) {
Point pt[ 2 ][ 3 ];
pt[ 0 ][ 0 ].x = rng.uniform(x_1, x_2);
pt[ 0 ][ 0 ].y = rng.uniform(y_1, y_2);
pt[ 0 ][ 1 ].x = rng.uniform(x_1, x_2);
pt[ 0 ][ 1 ].y = rng.uniform(y_1, y_2);
pt[ 0 ][ 2 ].x = rng.uniform(x_1, x_2);
pt[ 0 ][ 2 ].y = rng.uniform(y_1, y_2);
pt[ 1 ][ 0 ].x = rng.uniform(x_1, x_2);
pt[ 1 ][ 0 ].y = rng.uniform(y_1, y_2);
pt[ 1 ][ 1 ].x = rng.uniform(x_1, x_2);
pt[ 1 ][ 1 ].y = rng.uniform(y_1, y_2);
pt[ 1 ][ 2 ].x = rng.uniform(x_1, x_2);
pt[ 1 ][ 2 ].y = rng.uniform(y_1, y_2);

const Point *ppt[ 2 ] = {pt[ 0 ], pt[ 1 ]};
int npt[] = {3, 3};

fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);

imshow(window_name, image);
if (waitKey(DELAY) >= 0) {
return -1;
}
}
return 0;
}

//---------------------------【Drawing_Random_Circles( )函数】-----------------------------
// 描述:绘制随机的圆
//---------------------------------------------------------------------------------------------------
int Drawing_Random_Circles(Mat image, char *window_name, RNG rng) {
int lineType = 8;

for (int i = 0; i < NUMBER; i++) {
Point center;
center.x = rng.uniform(x_1, x_2);
center.y = rng.uniform(y_1, y_2);

circle(image, center, rng.uniform(0, 300), randomColor(rng), rng.uniform(-1, 9), lineType);

imshow(window_name, image);
if (waitKey(DELAY) >= 0) {
return -1;
}
}

return 0;
}

//---------------------------【Displaying_Random_Text( )函数】-----------------------------
// 描述:绘制随机的文字
//---------------------------------------------------------------------------------------------------
int Displaying_Random_Text(Mat image, char *window_name, RNG rng) {
int lineType = 8;

for (int i = 1; i < NUMBER; i++) {
Point org;
org.x = rng.uniform(x_1, x_2);
org.y = rng.uniform(y_1, y_2);

putText(image, "Testing text rendering", org, rng.uniform(0, 8),
rng.uniform(0, 100) * 0.05 + 0.1, randomColor(rng), rng.uniform(1, 10), lineType);

imshow(window_name, image);
if (waitKey(DELAY) >= 0) {
return -1;
}
}

return 0;
}

//---------------------------【Displaying_Big_End( )函数】------------------------------------
// 描述:绘制最终的结束效果
//---------------------------------------------------------------------------------------------------
int Displaying_Big_End(Mat image, char *window_name, RNG) {
Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
Point org((window_width - textsize.width) / 2, (window_height - textsize.height) / 2);
int lineType = 8;

Mat image2;

for (int i = 0; i < 255; i += 2) {
image2 = image - Scalar::all(i);
putText(image2, "OpenCV forever!", org, FONT_HERSHEY_PLAIN, 3, Scalar(50, i, i), 5,
lineType);

imshow(window_name, image2);
if (waitKey(DELAY) >= 0) {
return -1;
}
}

return 0;
}

测试效果如图:

dyna


OpenCV4入门教程062:绘图综合测试
https://blog.jackeylea.com/opencv/opencv-drawing/
作者
JackeyLea
发布于
2020年6月16日
许可协议