1Basic Drawing {#tutorial_basic_geometric_drawing}
2=============
3
4@tableofcontents
5
6@next_tutorial{tutorial_random_generator_and_text}
7
8|    |    |
9| -: | :- |
10| Original author | Ana Huamán |
11| Compatibility | OpenCV >= 3.0 |
12
13Goals
14-----
15
16In this tutorial you will learn how to:
17
18-   Draw a **line** by using the OpenCV function **line()**
19-   Draw an **ellipse** by using the OpenCV function **ellipse()**
20-   Draw a **rectangle** by using the OpenCV function **rectangle()**
21-   Draw a **circle** by using the OpenCV function **circle()**
22-   Draw a **filled polygon** by using the OpenCV function **fillPoly()**
23
24@add_toggle_cpp
25OpenCV Theory
26-------------
27
28For this tutorial, we will heavily use two structures: @ref cv::Point and @ref cv::Scalar :
29
30### Point
31
32It represents a 2D point, specified by its image coordinates \f$x\f$ and \f$y\f$. We can define it as:
33@code{.cpp}
34Point pt;
35pt.x = 10;
36pt.y = 8;
37@endcode
38or
39@code{.cpp}
40Point pt =  Point(10, 8);
41@endcode
42### Scalar
43
44-   Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel
45    values.
46-   In this tutorial, we will use it extensively to represent BGR color values (3 parameters). It is
47    not necessary to define the last argument if it is not going to be used.
48-   Let's see an example, if we are asked for a color argument and we give:
49    @code{.cpp}
50    Scalar( a, b, c )
51    @endcode
52    We would be defining a BGR color such as: *Blue = a*, *Green = b* and *Red = c*
53@end_toggle
54
55@add_toggle_java
56OpenCV Theory
57-------------
58
59For this tutorial, we will heavily use two structures: @ref cv::Point and @ref cv::Scalar :
60
61### Point
62
63It represents a 2D point, specified by its image coordinates \f$x\f$ and \f$y\f$. We can define it as:
64@code{.java}
65Point pt = new Point();
66pt.x = 10;
67pt.y = 8;
68@endcode
69or
70@code{.java}
71Point pt = new Point(10, 8);
72@endcode
73### Scalar
74
75-   Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel
76    values.
77-   In this tutorial, we will use it extensively to represent BGR color values (3 parameters). It is
78    not necessary to define the last argument if it is not going to be used.
79-   Let's see an example, if we are asked for a color argument and we give:
80    @code{.java}
81    Scalar( a, b, c )
82    @endcode
83    We would be defining a BGR color such as: *Blue = a*, *Green = b* and *Red = c*
84@end_toggle
85
86Code
87----
88
89@add_toggle_cpp
90-   This code is in your OpenCV sample folder. Otherwise you can grab it from
91    [here](https://raw.githubusercontent.com/opencv/opencv/master/samples/cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp)
92    @include samples/cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp
93@end_toggle
94
95@add_toggle_java
96-   This code is in your OpenCV sample folder. Otherwise you can grab it from
97    [here](https://raw.githubusercontent.com/opencv/opencv/master/samples/java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java)
98    @include samples/java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java
99@end_toggle
100
101@add_toggle_python
102-   This code is in your OpenCV sample folder. Otherwise you can grab it from
103    [here](https://raw.githubusercontent.com/opencv/opencv/master/samples/python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py)
104    @include samples/python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py
105@end_toggle
106
107Explanation
108-----------
109
110Since we plan to draw two examples (an atom and a rook), we have to create two images and two
111windows to display them.
112@add_toggle_cpp
113@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp create_images
114@end_toggle
115
116@add_toggle_java
117@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java create_images
118@end_toggle
119
120@add_toggle_python
121@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py create_images
122@end_toggle
123
124We created functions to draw different geometric shapes. For instance, to draw the atom we used
125**MyEllipse** and **MyFilledCircle**:
126@add_toggle_cpp
127@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp draw_atom
128@end_toggle
129
130@add_toggle_java
131@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java draw_atom
132@end_toggle
133
134@add_toggle_python
135@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py draw_atom
136@end_toggle
137
138And to draw the rook we employed **MyLine**, **rectangle** and a **MyPolygon**:
139@add_toggle_cpp
140@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp draw_rook
141@end_toggle
142
143@add_toggle_java
144@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java draw_rook
145@end_toggle
146
147@add_toggle_python
148@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py draw_rook
149@end_toggle
150
151
152Let's check what is inside each of these functions:
153@add_toggle_cpp
154@end_toggle
155
156<H4>MyLine</H4>
157@add_toggle_cpp
158@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp my_line
159@end_toggle
160
161@add_toggle_java
162@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java my_line
163@end_toggle
164
165@add_toggle_python
166@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py my_line
167@end_toggle
168
169-   As we can see, **MyLine** just call the function **line()** , which does the following:
170    -   Draw a line from Point **start** to Point **end**
171    -   The line is displayed in the image **img**
172    -   The line color is defined by <B>( 0, 0, 0 )</B> which is the RGB value correspondent
173        to **Black**
174    -   The line thickness is set to **thickness** (in this case 2)
175    -   The line is a 8-connected one (**lineType** = 8)
176
177<H4>MyEllipse</H4>
178@add_toggle_cpp
179@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp my_ellipse
180@end_toggle
181
182@add_toggle_java
183@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java my_ellipse
184@end_toggle
185
186@add_toggle_python
187@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py my_ellipse
188@end_toggle
189
190-   From the code above, we can observe that the function **ellipse()** draws an ellipse such
191    that:
192
193    -   The ellipse is displayed in the image **img**
194    -   The ellipse center is located in the point <B>(w/2, w/2)</B> and is enclosed in a box
195        of size <B>(w/4, w/16)</B>
196    -   The ellipse is rotated **angle** degrees
197    -   The ellipse extends an arc between **0** and **360** degrees
198    -   The color of the figure will be <B>( 255, 0, 0 )</B> which means blue in BGR value.
199    -   The ellipse's **thickness** is 2.
200
201<H4>MyFilledCircle</H4>
202@add_toggle_cpp
203@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp my_filled_circle
204@end_toggle
205
206@add_toggle_java
207@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java my_filled_circle
208@end_toggle
209
210@add_toggle_python
211@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py my_filled_circle
212@end_toggle
213
214-   Similar to the ellipse function, we can observe that *circle* receives as arguments:
215
216    -   The image where the circle will be displayed (**img**)
217    -   The center of the circle denoted as the point **center**
218    -   The radius of the circle: **w/32**
219    -   The color of the circle: <B>( 0, 0, 255 )</B> which means *Red* in BGR
220    -   Since **thickness** = -1, the circle will be drawn filled.
221
222<H4>MyPolygon</H4>
223@add_toggle_cpp
224@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp my_polygon
225@end_toggle
226
227@add_toggle_java
228@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java my_polygon
229@end_toggle
230
231@add_toggle_python
232@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py my_polygon
233@end_toggle
234
235-   To draw a filled polygon we use the function **fillPoly()** . We note that:
236
237    -   The polygon will be drawn on **img**
238    -   The vertices of the polygon are the set of points in **ppt**
239    -   The color of the polygon is defined by <B>( 255, 255, 255 )</B>, which is the BGR
240        value for *white*
241
242<H4>rectangle</H4>
243@add_toggle_cpp
244@snippet cpp/tutorial_code/ImgProc/basic_drawing/Drawing_1.cpp rectangle
245@end_toggle
246
247@add_toggle_java
248@snippet java/tutorial_code/ImgProc/BasicGeometricDrawing/BasicGeometricDrawing.java rectangle
249@end_toggle
250
251@add_toggle_python
252@snippet python/tutorial_code/imgProc/BasicGeometricDrawing/basic_geometric_drawing.py rectangle
253@end_toggle
254
255-   Finally we have the @ref cv::rectangle function (we did not create a special function for
256    this guy). We note that:
257
258    -   The rectangle will be drawn on **rook_image**
259    -   Two opposite vertices of the rectangle are defined by <B>( 0, 7*w/8 )</B>
260        and <B>( w, w )</B>
261    -   The color of the rectangle is given by <B>( 0, 255, 255 )</B> which is the BGR value
262        for *yellow*
263    -   Since the thickness value is given by **FILLED (-1)**, the rectangle will be filled.
264
265Result
266------
267
268Compiling and running your program should give you a result like this:
269
270![](images/Drawing_1_Tutorial_Result_0.png)
271