samples/cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp#
An example using drawing functions Check the corresponding tutorial for more details
1/**
2 * @file Drawing_1.cpp
3 * @brief Simple geometric drawing
4 * @author OpenCV team
5 */
6#include <opencv2/core.hpp>
7#include <opencv2/imgproc.hpp>
8#include <opencv2/highgui.hpp>
9
10#define w 400
11
12using namespace cv;
13
14/// Function headers
15void MyEllipse( Mat img, double angle );
16void MyFilledCircle( Mat img, Point center );
17void MyPolygon( Mat img );
18void MyLine( Mat img, Point start, Point end );
19
20/**
21 * @function main
22 * @brief Main function
23 */
24int main( void ){
25
26 //![create_images]
27 /// Windows names
28 char atom_window[] = "Drawing 1: Atom";
29 char rook_window[] = "Drawing 2: Rook";
30
31 /// Create black empty images
32 Mat atom_image = Mat::zeros( w, w, CV_8UC3 );
33 Mat rook_image = Mat::zeros( w, w, CV_8UC3 );
34 //![create_images]
35
36 /// 1. Draw a simple atom:
37 /// -----------------------
38
39 //![draw_atom]
40 /// 1.a. Creating ellipses
41 MyEllipse( atom_image, 90 );
42 MyEllipse( atom_image, 0 );
43 MyEllipse( atom_image, 45 );
44 MyEllipse( atom_image, -45 );
45
46 /// 1.b. Creating circles
47 MyFilledCircle( atom_image, Point( w/2, w/2) );
48 //![draw_atom]
49
50 /// 2. Draw a rook
51 /// ------------------
52
53 //![draw_rook]
54 /// 2.a. Create a convex polygon
55 MyPolygon( rook_image );
56
57 //![rectangle]
58 /// 2.b. Creating rectangles
59 rectangle( rook_image,
60 Point( 0, 7*w/8 ),
61 Point( w, w),
62 Scalar( 0, 255, 255 ),
63 FILLED,
64 LINE_8 );
65 //![rectangle]
66
67 /// 2.c. Create a few lines
68 MyLine( rook_image, Point( 0, 15*w/16 ), Point( w, 15*w/16 ) );
69 MyLine( rook_image, Point( w/4, 7*w/8 ), Point( w/4, w ) );
70 MyLine( rook_image, Point( w/2, 7*w/8 ), Point( w/2, w ) );
71 MyLine( rook_image, Point( 3*w/4, 7*w/8 ), Point( 3*w/4, w ) );
72 //![draw_rook]
73
74 /// 3. Display your stuff!
75 imshow( atom_window, atom_image );
76 moveWindow( atom_window, 0, 200 );
77 imshow( rook_window, rook_image );
78 moveWindow( rook_window, w, 200 );
79
80 waitKey( 0 );
81 return(0);
82}
83
84/// Function Declaration
85
86/**
87 * @function MyEllipse
88 * @brief Draw a fixed-size ellipse with different angles
89 */
90//![my_ellipse]
91void MyEllipse( Mat img, double angle )
92{
93 int thickness = 2;
94 int lineType = 8;
95
96 ellipse( img,
97 Point( w/2, w/2 ),
98 Size( w/4, w/16 ),
99 angle,
100 0,
101 360,
102 Scalar( 255, 0, 0 ),
103 thickness,
104 lineType );
105}
106//![my_ellipse]
107
108/**
109 * @function MyFilledCircle
110 * @brief Draw a fixed-size filled circle
111 */
112//![my_filled_circle]
113void MyFilledCircle( Mat img, Point center )
114{
115 circle( img,
116 center,
117 w/32,
118 Scalar( 0, 0, 255 ),
119 FILLED,
120 LINE_8 );
121}
122//![my_filled_circle]
123
124/**
125 * @function MyPolygon
126 * @brief Draw a simple concave polygon (rook)
127 */
128//![my_polygon]
129void MyPolygon( Mat img )
130{
131 int lineType = LINE_8;
132
133 /** Create some points */
134 Point rook_points[1][20];
135 rook_points[0][0] = Point( w/4, 7*w/8 );
136 rook_points[0][1] = Point( 3*w/4, 7*w/8 );
137 rook_points[0][2] = Point( 3*w/4, 13*w/16 );
138 rook_points[0][3] = Point( 11*w/16, 13*w/16 );
139 rook_points[0][4] = Point( 19*w/32, 3*w/8 );
140 rook_points[0][5] = Point( 3*w/4, 3*w/8 );
141 rook_points[0][6] = Point( 3*w/4, w/8 );
142 rook_points[0][7] = Point( 26*w/40, w/8 );
143 rook_points[0][8] = Point( 26*w/40, w/4 );
144 rook_points[0][9] = Point( 22*w/40, w/4 );
145 rook_points[0][10] = Point( 22*w/40, w/8 );
146 rook_points[0][11] = Point( 18*w/40, w/8 );
147 rook_points[0][12] = Point( 18*w/40, w/4 );
148 rook_points[0][13] = Point( 14*w/40, w/4 );
149 rook_points[0][14] = Point( 14*w/40, w/8 );
150 rook_points[0][15] = Point( w/4, w/8 );
151 rook_points[0][16] = Point( w/4, 3*w/8 );
152 rook_points[0][17] = Point( 13*w/32, 3*w/8 );
153 rook_points[0][18] = Point( 5*w/16, 13*w/16 );
154 rook_points[0][19] = Point( w/4, 13*w/16 );
155
156 const Point* ppt[1] = { rook_points[0] };
157 int npt[] = { 20 };
158
159 fillPoly( img,
160 ppt,
161 npt,
162 1,
163 Scalar( 255, 255, 255 ),
164 lineType );
165}
166//![my_polygon]
167
168/**
169 * @function MyLine
170 * @brief Draw a simple line
171 */
172//![my_line]
173void MyLine( Mat img, Point start, Point end )
174{
175 int thickness = 2;
176 int lineType = LINE_8;
177
178 line( img,
179 start,
180 end,
181 Scalar( 0, 0, 0 ),
182 thickness,
183 lineType );
184}
185//![my_line]