Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/drawing.cpp
16337 views
1
#include "opencv2/core.hpp"
2
#include "opencv2/imgproc.hpp"
3
#include "opencv2/highgui.hpp"
4
#include <stdio.h>
5
6
using namespace cv;
7
8
static void help()
9
{
10
printf("\nThis program demonstrates OpenCV drawing and text output functions.\n"
11
"Usage:\n"
12
" ./drawing\n");
13
}
14
static Scalar randomColor(RNG& rng)
15
{
16
int icolor = (unsigned)rng;
17
return Scalar(icolor&255, (icolor>>8)&255, (icolor>>16)&255);
18
}
19
20
int main()
21
{
22
help();
23
char wndname[] = "Drawing Demo";
24
const int NUMBER = 100;
25
const int DELAY = 5;
26
int lineType = LINE_AA; // change it to LINE_8 to see non-antialiased graphics
27
int i, width = 1000, height = 700;
28
int x1 = -width/2, x2 = width*3/2, y1 = -height/2, y2 = height*3/2;
29
RNG rng(0xFFFFFFFF);
30
31
Mat image = Mat::zeros(height, width, CV_8UC3);
32
imshow(wndname, image);
33
waitKey(DELAY);
34
35
for (i = 0; i < NUMBER * 2; i++)
36
{
37
Point pt1, pt2;
38
pt1.x = rng.uniform(x1, x2);
39
pt1.y = rng.uniform(y1, y2);
40
pt2.x = rng.uniform(x1, x2);
41
pt2.y = rng.uniform(y1, y2);
42
43
int arrowed = rng.uniform(0, 6);
44
45
if( arrowed < 3 )
46
line( image, pt1, pt2, randomColor(rng), rng.uniform(1,10), lineType );
47
else
48
arrowedLine(image, pt1, pt2, randomColor(rng), rng.uniform(1, 10), lineType);
49
50
imshow(wndname, image);
51
if(waitKey(DELAY) >= 0)
52
return 0;
53
}
54
55
for (i = 0; i < NUMBER * 2; i++)
56
{
57
Point pt1, pt2;
58
pt1.x = rng.uniform(x1, x2);
59
pt1.y = rng.uniform(y1, y2);
60
pt2.x = rng.uniform(x1, x2);
61
pt2.y = rng.uniform(y1, y2);
62
int thickness = rng.uniform(-3, 10);
63
int marker = rng.uniform(0, 10);
64
int marker_size = rng.uniform(30, 80);
65
66
if (marker > 5)
67
rectangle(image, pt1, pt2, randomColor(rng), MAX(thickness, -1), lineType);
68
else
69
drawMarker(image, pt1, randomColor(rng), marker, marker_size );
70
71
imshow(wndname, image);
72
if(waitKey(DELAY) >= 0)
73
return 0;
74
}
75
76
for (i = 0; i < NUMBER; i++)
77
{
78
Point center;
79
center.x = rng.uniform(x1, x2);
80
center.y = rng.uniform(y1, y2);
81
Size axes;
82
axes.width = rng.uniform(0, 200);
83
axes.height = rng.uniform(0, 200);
84
double angle = rng.uniform(0, 180);
85
86
ellipse( image, center, axes, angle, angle - 100, angle + 200,
87
randomColor(rng), rng.uniform(-1,9), lineType );
88
89
imshow(wndname, image);
90
if(waitKey(DELAY) >= 0)
91
return 0;
92
}
93
94
for (i = 0; i< NUMBER; i++)
95
{
96
Point pt[2][3];
97
pt[0][0].x = rng.uniform(x1, x2);
98
pt[0][0].y = rng.uniform(y1, y2);
99
pt[0][1].x = rng.uniform(x1, x2);
100
pt[0][1].y = rng.uniform(y1, y2);
101
pt[0][2].x = rng.uniform(x1, x2);
102
pt[0][2].y = rng.uniform(y1, y2);
103
pt[1][0].x = rng.uniform(x1, x2);
104
pt[1][0].y = rng.uniform(y1, y2);
105
pt[1][1].x = rng.uniform(x1, x2);
106
pt[1][1].y = rng.uniform(y1, y2);
107
pt[1][2].x = rng.uniform(x1, x2);
108
pt[1][2].y = rng.uniform(y1, y2);
109
const Point* ppt[2] = {pt[0], pt[1]};
110
int npt[] = {3, 3};
111
112
polylines(image, ppt, npt, 2, true, randomColor(rng), rng.uniform(1,10), lineType);
113
114
imshow(wndname, image);
115
if(waitKey(DELAY) >= 0)
116
return 0;
117
}
118
119
for (i = 0; i< NUMBER; i++)
120
{
121
Point pt[2][3];
122
pt[0][0].x = rng.uniform(x1, x2);
123
pt[0][0].y = rng.uniform(y1, y2);
124
pt[0][1].x = rng.uniform(x1, x2);
125
pt[0][1].y = rng.uniform(y1, y2);
126
pt[0][2].x = rng.uniform(x1, x2);
127
pt[0][2].y = rng.uniform(y1, y2);
128
pt[1][0].x = rng.uniform(x1, x2);
129
pt[1][0].y = rng.uniform(y1, y2);
130
pt[1][1].x = rng.uniform(x1, x2);
131
pt[1][1].y = rng.uniform(y1, y2);
132
pt[1][2].x = rng.uniform(x1, x2);
133
pt[1][2].y = rng.uniform(y1, y2);
134
const Point* ppt[2] = {pt[0], pt[1]};
135
int npt[] = {3, 3};
136
137
fillPoly(image, ppt, npt, 2, randomColor(rng), lineType);
138
139
imshow(wndname, image);
140
if(waitKey(DELAY) >= 0)
141
return 0;
142
}
143
144
for (i = 0; i < NUMBER; i++)
145
{
146
Point center;
147
center.x = rng.uniform(x1, x2);
148
center.y = rng.uniform(y1, y2);
149
150
circle(image, center, rng.uniform(0, 300), randomColor(rng),
151
rng.uniform(-1, 9), lineType);
152
153
imshow(wndname, image);
154
if(waitKey(DELAY) >= 0)
155
return 0;
156
}
157
158
for (i = 1; i < NUMBER; i++)
159
{
160
Point org;
161
org.x = rng.uniform(x1, x2);
162
org.y = rng.uniform(y1, y2);
163
164
putText(image, "Testing text rendering", org, rng.uniform(0,8),
165
rng.uniform(0,100)*0.05+0.1, randomColor(rng), rng.uniform(1, 10), lineType);
166
167
imshow(wndname, image);
168
if(waitKey(DELAY) >= 0)
169
return 0;
170
}
171
172
Size textsize = getTextSize("OpenCV forever!", FONT_HERSHEY_COMPLEX, 3, 5, 0);
173
Point org((width - textsize.width)/2, (height - textsize.height)/2);
174
175
Mat image2;
176
for( i = 0; i < 255; i += 2 )
177
{
178
image2 = image - Scalar::all(i);
179
putText(image2, "OpenCV forever!", org, FONT_HERSHEY_COMPLEX, 3,
180
Scalar(i, i, 255), 5, lineType);
181
182
imshow(wndname, image2);
183
if(waitKey(DELAY) >= 0)
184
return 0;
185
}
186
187
waitKey();
188
return 0;
189
}
190
191