1
#include "opencv2/core.hpp"
2
#include "opencv2/imgproc.hpp"
3
#include "opencv2/highgui.hpp"
9
printf("\nThis program demonstrates OpenCV drawing and text output functions.\n"
13
static Scalar randomColor(RNG& rng)
15
int icolor = (unsigned)rng;
16
return Scalar(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
19
int main(int argc, char** argv)
21
cv::CommandLineParser parser(argc, argv, "{help h||}");
22
if (parser.has("help"))
27
char wndname[] = "Drawing Demo";
28
const int NUMBER = 100;
30
int lineType = LINE_AA; // change it to LINE_8 to see non-antialiased graphics
31
int i, width = 1000, height = 700;
32
int x1 = -width/2, x2 = width*3/2, y1 = -height/2, y2 = height*3/2;
35
Mat image = Mat::zeros(height, width, CV_8UC3);
36
imshow(wndname, image);
39
for (i = 0; i < NUMBER; i++)
42
pt1.x = rng.uniform(x1, x2);
43
pt1.y = rng.uniform(y1, y2);
44
pt2.x = rng.uniform(x1, x2);
45
pt2.y = rng.uniform(y1, y2);
47
line( image, pt1, pt2, randomColor(rng), rng.uniform(1,10), lineType );
49
imshow(wndname, image);
50
if(waitKey(DELAY) >= 0)
54
for (i = 0; i < NUMBER; i++)
57
pt1.x = rng.uniform(x1, x2);
58
pt1.y = rng.uniform(y1, y2);
59
pt2.x = rng.uniform(x1, x2);
60
pt2.y = rng.uniform(y1, y2);
61
int thickness = rng.uniform(-3, 10);
63
rectangle( image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType );
65
imshow(wndname, image);
66
if(waitKey(DELAY) >= 0)
70
for (i = 0; i < NUMBER; i++)
73
center.x = rng.uniform(x1, x2);
74
center.y = rng.uniform(y1, y2);
76
axes.width = rng.uniform(0, 200);
77
axes.height = rng.uniform(0, 200);
78
double angle = rng.uniform(0, 180);
80
ellipse( image, center, axes, angle, angle - 100, angle + 200,
81
randomColor(rng), rng.uniform(-1,9), lineType );
83
imshow(wndname, image);
84
if(waitKey(DELAY) >= 0)
88
for (i = 0; i< NUMBER; i++)
91
pt[0][0].x = rng.uniform(x1, x2);
92
pt[0][0].y = rng.uniform(y1, y2);
93
pt[0][1].x = rng.uniform(x1, x2);
94
pt[0][1].y = rng.uniform(y1, y2);
95
pt[0][2].x = rng.uniform(x1, x2);
96
pt[0][2].y = rng.uniform(y1, y2);
97
pt[1][0].x = rng.uniform(x1, x2);
98
pt[1][0].y = rng.uniform(y1, y2);
99
pt[1][1].x = rng.uniform(x1, x2);
100
pt[1][1].y = rng.uniform(y1, y2);
101
pt[1][2].x = rng.uniform(x1, x2);
102
pt[1][2].y = rng.uniform(y1, y2);
103
const Point* ppt[2] = {pt[0], pt[1]};
106
polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType);
108
imshow(wndname, image);
109
if(waitKey(DELAY) >= 0)
113
for (i = 0; i< NUMBER; i++)
116
pt[0][0].x = rng.uniform(x1, x2);
117
pt[0][0].y = rng.uniform(y1, y2);
118
pt[0][1].x = rng.uniform(x1, x2);
119
pt[0][1].y = rng.uniform(y1, y2);
120
pt[0][2].x = rng.uniform(x1, x2);
121
pt[0][2].y = rng.uniform(y1, y2);
122
pt[1][0].x = rng.uniform(x1, x2);
123
pt[1][0].y = rng.uniform(y1, y2);
124
pt[1][1].x = rng.uniform(x1, x2);
125
pt[1][1].y = rng.uniform(y1, y2);
126
pt[1][2].x = rng.uniform(x1, x2);
127
pt[1][2].y = rng.uniform(y1, y2);
128
const Point* ppt[2] = {pt[0], pt[1]};
131
fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);
133
imshow(wndname, image);
134
if(waitKey(DELAY) >= 0)
138
for (i = 0; i < NUMBER; i++)
141
center.x = rng.uniform(x1, x2);
142
center.y = rng.uniform(y1, y2);
144
circle(image, center, rng.uniform(0, 300), randomColor(rng),
145
rng.uniform(-1, 9), lineType);
147
imshow(wndname, image);
148
if(waitKey(DELAY) >= 0)
152
for (i = 1; i < NUMBER; i++)
155
org.x = rng.uniform(x1, x2);
156
org.y = rng.uniform(y1, y2);
158
putText(image, "Testing text rendering", org, rng.uniform(0,8),
159
rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);
161
imshow(wndname, image);
162
if(waitKey(DELAY) >= 0)
166
Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
167
Point org((width - textsize.width)/2, (height - textsize.height)/2);
170
for( i = 0; i < 255; i += 2 )
172
image2 = image - Scalar::all(i);
173
putText(image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
174
Scalar(i, i, 255), 5, lineType);
176
imshow(wndname, image2);
177
if(waitKey(DELAY) >= 0)