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