]> rtime.felk.cvut.cz Git - opencv.git/blob - opencv/doc/cxcore_drawing_functions.tex
9bb0ef2a0631acea457589c50ed0927d618a3ded
[opencv.git] / opencv / doc / cxcore_drawing_functions.tex
1 \section{Drawing Functions}
2
3 Drawing functions work with matrices/images of arbitrary depth.
4 The boundaries of the shapes can be rendered with antialiasing (implemented only for 8-bit images for now).
5 All the functions include the parameter color that uses a rgb value (that may be constructed
6 with \texttt{CV\_RGB} \cvC{macro or the \cvCppCross{cvScalar} function}
7 \cvCpp{or the \cross{Scalar} constructor}) for color
8 images and brightness for grayscale images. For color images the order channel
9 is normally \emph{Blue, Green, Red}, this is what \cvCppCross{imshow}, \cvCppCross{imread} and \cvCppCross{imwrite} expect
10 \ifCpp
11 , so if you form a color using \cross{Scalar} constructor, it should look like:
12 \[\texttt{Scalar}(blue\_component, green\_component, red\_component[, alpha\_component])\]
13 \fi
14 \ifC
15 , so if you form a color using \cvCppCross{cvScalar}, it should look like:
16 \[\texttt{cvScalar}(blue\_component, green\_component, red\_component[, alpha\_component])\]
17 \fi
18
19 If you are using your own image rendering and I/O functions, you can use any channel ordering, the drawing functions process each channel independently and do not depend on the channel order or even on the color space used. The whole image can be converted from BGR to RGB or to a different color space using \cvCppCross{cvtColor}.
20
21 If a drawn figure is partially or completely outside the image, the drawing functions clip it. Also, many drawing functions can handle pixel coordinates specified with sub-pixel accuracy, that is, the coordinates can be passed as fixed-point numbers, encoded as integers. The number of fractional bits is specified by the \texttt{shift} parameter and the real point coordinates are calculated as $\texttt{Point}(x,y)\rightarrow\texttt{Point2f}(x*2^{-shift},y*2^{-shift})$. This feature is especially effective wehn rendering antialiased shapes.
22
23 Also, note that the functions do not support alpha-transparency - when the target image is 4-channnel, then the \texttt{color[3]} is simply copied to the repainted pixels. Thus, if you want to paint semi-transparent shapes, you can paint them in a separate buffer and then blend it with the main image.
24
25 \ifCPy
26
27 \cvCPyFunc{Circle}
28 Draws a circle.
29
30 \cvdefC{void cvCircle( \par CvArr* img,\par CvPoint center,\par int radius,\par CvScalar color,\par int thickness=1,\par int lineType=8,\par int shift=0 );}
31 \cvdefPy{Circle(img,center,radius,color,thickness=1,lineType=8,shift=0)-> None}
32
33 \begin{description}
34 \cvarg{img}{Image where the circle is drawn}
35 \cvarg{center}{Center of the circle}
36 \cvarg{radius}{Radius of the circle}
37 \cvarg{color}{Circle color}
38 \cvarg{thickness}{Thickness of the circle outline if positive, otherwise this indicates that a filled circle is to be drawn}
39 \cvarg{lineType}{Type of the circle boundary, see \cross{Line} description}
40 \cvarg{shift}{Number of fractional bits in the center coordinates and radius value}
41 \end{description}
42
43 The function draws a simple or filled circle with a
44 given center and radius.
45
46 \cvCPyFunc{ClipLine}
47 Clips the line against the image rectangle.
48
49 \cvdefC{int cvClipLine( \par CvSize imgSize,\par CvPoint* pt1,\par CvPoint* pt2 );}
50 \cvdefPy{ClipLine(imgSize, pt1, pt2) -> (clipped\_pt1, clipped\_pt2)}
51 \begin{description}
52 \cvarg{imgSize}{Size of the image}
53 \cvarg{pt1}{First ending point of the line segment. \cvC{It is modified by the function.}}
54 \cvarg{pt2}{Second ending point of the line segment. \cvC{It is modified by the function.}}
55 \end{description}
56
57 The function calculates a part of the line segment which is entirely within the image.
58 \cvC{It returns 0 if the line segment is completely outside the image and 1 otherwise.}
59 \cvPy{If the line segment is outside the image, it returns None. If the line segment is inside the image it returns a new pair of points.}
60
61 \cvCPyFunc{DrawContours}
62 Draws contour outlines or interiors in an image.
63
64 \cvdefC{
65 void cvDrawContours( \par CvArr *img,\par CvSeq* contour,\par CvScalar external\_color,\par CvScalar hole\_color,\par int max\_level,\par int thickness=1,\par int lineType=8 );
66 }
67 \cvdefPy{DrawContours(img,contour,external\_color,hole\_color,max\_level,thickness=1,lineType=8,offset=(0,0))-> None}
68
69 \begin{description}
70 \cvarg{img}{Image where the contours are to be drawn. As with any other drawing function, the contours are clipped with the ROI.}
71 \cvarg{contour}{Pointer to the first contour}
72 \cvarg{external\_color}{Color of the external contours}
73 \cvarg{hole\_color}{Color of internal contours (holes)}
74 \cvarg{max\_level}{Maximal level for drawn contours. If 0, only
75 \texttt{contour} is drawn. If 1, the contour and all contours following
76 it on the same level are drawn. If 2, all contours following and all
77 contours one level below the contours are drawn, and so forth. If the value
78 is negative, the function does not draw the contours following after
79 \texttt{contour} but draws the child contours of \texttt{contour} up
80 to the $|\texttt{max\_level}|-1$ level.}
81 \cvarg{thickness}{Thickness of lines the contours are drawn with.
82 If it is negative (For example, =CV\_FILLED), the contour interiors are
83 drawn.}
84 \cvarg{lineType}{Type of the contour segments, see \cross{Line} description}
85 \end{description}
86
87 The function draws contour outlines in the image if $\texttt{thickness} \ge 0$ or fills the area bounded by the contours if $ \texttt{thickness}<0$.
88
89 \ifC
90 Example: Connected component detection via contour functions
91
92 \begin{lstlisting}
93 #include "cv.h"
94 #include "highgui.h"
95
96 int main( int argc, char** argv )
97 {
98     IplImage* src;
99     // the first command line parameter must be file name of binary 
100     // (black-n-white) image
101     if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)
102     {
103         IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );
104         CvMemStorage* storage = cvCreateMemStorage(0);
105         CvSeq* contour = 0;
106
107         cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );
108         cvNamedWindow( "Source", 1 );
109         cvShowImage( "Source", src );
110
111         cvFindContours( src, storage, &contour, sizeof(CvContour), 
112            CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
113         cvZero( dst );
114
115         for( ; contour != 0; contour = contour->h_next )
116         {
117             CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
118             /* replace CV_FILLED with 1 to see the outlines */
119             cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 );
120         }
121
122         cvNamedWindow( "Components", 1 );
123         cvShowImage( "Components", dst );
124         cvWaitKey(0);
125     }
126 }
127 \end{lstlisting}
128 \fi
129
130 \cvCPyFunc{Ellipse}
131 Draws a simple or thick elliptic arc or an fills ellipse sector.
132
133 \cvdefC{void cvEllipse( \par CvArr* img,\par CvPoint center,\par CvSize axes,\par double angle,\par double start\_angle,\par double end\_angle,\par CvScalar color,\par int thickness=1,\par int lineType=8,\par int shift=0 );}
134 \cvdefPy{Ellipse(img,center,axes,angle,start\_angle,end\_angle,color,thickness=1,lineType=8,shift=0)-> None}
135
136 \begin{description}
137 \cvarg{img}{The image}
138 \cvarg{center}{Center of the ellipse}
139 \cvarg{axes}{Length of the ellipse axes}
140 \cvarg{angle}{Rotation angle}
141 \cvarg{start\_angle}{Starting angle of the elliptic arc}
142 \cvarg{end\_angle}{Ending angle of the elliptic arc.}
143 \cvarg{color}{Ellipse color}
144 \cvarg{thickness}{Thickness of the ellipse arc outline if positive, otherwise this indicates that a filled ellipse sector is to be drawn}
145 \cvarg{lineType}{Type of the ellipse boundary, see \cross{Line} description}
146 \cvarg{shift}{Number of fractional bits in the center coordinates and axes' values}
147 \end{description}
148
149 The function draws a simple or thick elliptic
150 arc or fills an ellipse sector. The arc is clipped by the ROI rectangle.
151 A piecewise-linear approximation is used for antialiased arcs and
152 thick arcs. All the angles are given in degrees. The picture below
153 explains the meaning of the parameters.
154
155 Parameters of Elliptic Arc
156
157 \includegraphics[width=0.5\textwidth]{pics/ellipse.png}
158
159 \cvCPyFunc{EllipseBox}
160
161 Draws a simple or thick elliptic arc or fills an ellipse sector.
162
163 \cvdefC{void cvEllipseBox( \par CvArr* img, \par CvBox2D box, \par CvScalar color,
164                    \par int thickness=1, \par int lineType=8, \par int shift=0 );}
165 \cvdefPy{EllipseBox(img,box,color,thickness=1,lineType=8,shift=0)-> None}
166
167 \begin{description}
168 \cvarg{img}{Image}
169 \cvarg{box}{The enclosing box of the ellipse drawn}
170 \cvarg{thickness}{Thickness of the ellipse boundary}
171 \cvarg{lineType}{Type of the ellipse boundary, see \cross{Line} description}
172 \cvarg{shift}{Number of fractional bits in the box vertex coordinates}
173 \end{description}
174
175 The function draws a simple or thick ellipse outline, or fills an ellipse. The functions provides a convenient way to draw an ellipse approximating some shape; that is what \cross{CamShift} and \cross{FitEllipse} do. The ellipse drawn is clipped by ROI rectangle. A piecewise-linear approximation is used for antialiased arcs and thick arcs.
176
177 \cvCPyFunc{FillConvexPoly}
178 Fills a convex polygon.
179
180 \cvdefC{
181 void cvFillConvexPoly( \par CvArr* img,\par CvPoint* pts,\par int npts,\par CvScalar color,\par int lineType=8,\par int shift=0 );}
182 \cvdefPy{FillConvexPoly(img,pn,color,lineType=8,shift=0)-> None}
183
184 \begin{description}
185 \cvarg{img}{Image}
186 \ifC
187 \cvarg{pts}{Array of pointers to a single polygon}
188 \cvarg{npts}{Polygon vertex counter}
189 \else
190 \cvarg{pn}{List of coordinate pairs}
191 \fi
192 \cvarg{color}{Polygon color}
193 \cvarg{lineType}{Type of the polygon boundaries, see \cross{Line} description}
194 \cvarg{shift}{Number of fractional bits in the vertex coordinates}
195 \end{description}
196
197 The function fills a convex polygon's interior.
198 This function is much faster than the function \texttt{cvFillPoly}
199 and can fill not only convex polygons but any monotonic polygon,
200 i.e., a polygon whose contour intersects every horizontal line (scan
201 line) twice at the most.
202
203
204 \cvCPyFunc{FillPoly}
205 Fills a polygon's interior.
206
207 \cvdefC{
208 void cvFillPoly( \par CvArr* img,\par CvPoint** pts,\par int* npts,\par int contours,\par CvScalar color,\par int lineType=8,\par int shift=0 );
209 }
210 \cvdefPy{FillPoly(img,polys,color,lineType=8,shift=0)-> None}
211
212 \begin{description}
213 \cvarg{img}{Image}
214 \ifC
215 \cvarg{pts}{Array of pointers to polygons}
216 \cvarg{npts}{Array of polygon vertex counters}
217 \cvarg{contours}{Number of contours that bind the filled region}
218 \fi
219 \ifPy
220 \cvarg{polys}{List of lists of (x,y) pairs.  Each list of points is a polygon.}
221 \fi
222 \cvarg{color}{Polygon color}
223 \cvarg{lineType}{Type of the polygon boundaries, see \cross{Line} description}
224 \cvarg{shift}{Number of fractional bits in the vertex coordinates}
225 \end{description}
226
227
228 The function fills an area bounded by several
229 polygonal contours. The function fills complex areas, for example,
230 areas with holes, contour self-intersection, and so forth.
231
232 \cvCPyFunc{GetTextSize}
233 Retrieves the width and height of a text string.
234
235 \cvdefC{
236 void cvGetTextSize( \par const char* textString,\par const CvFont* font,\par CvSize* textSize,\par int* baseline );}
237 \cvdefPy{GetTextSize(textString,font)-> (textSize,baseline)}
238
239 \begin{description}
240 \cvarg{font}{Pointer to the font structure}
241 \cvarg{textString}{Input string}
242 \cvarg{textSize}{Resultant size of the text string. Height of the text does not include the height of character parts that are below the baseline.}
243 \cvarg{baseline}{y-coordinate of the baseline relative to the bottom-most text point}
244 \end{description}
245
246 The function calculates the dimensions of a rectangle to enclose a text string when a specified font is used.
247
248 \cvCPyFunc{InitFont}
249 Initializes font structure.
250
251 \cvdefC{
252 void cvInitFont( \par CvFont* font,\par int fontFace,\par double hscale,\par double vscale,\par double shear=0,\par int thickness=1,\par int lineType=8 );}
253 \cvdefPy{InitFont(fontFace,hscale,vscale,shear=0,thickness=1,lineType=8)-> font}
254
255 \begin{description}
256 \cvarg{font}{Pointer to the font structure initialized by the function}
257 \cvarg{fontFace}{Font name identifier. Only a subset of Hershey fonts \url{http://sources.isc.org/utils/misc/hershey-font.txt} are supported now:
258  \begin{description}
259  \cvarg{CV\_FONT\_HERSHEY\_SIMPLEX}{normal size sans-serif font}
260  \cvarg{CV\_FONT\_HERSHEY\_PLAIN}{small size sans-serif font}
261  \cvarg{CV\_FONT\_HERSHEY\_DUPLEX}{normal size sans-serif font (more complex than \par \texttt{CV\_FONT\_HERSHEY\_SIMPLEX})}
262  \cvarg{CV\_FONT\_HERSHEY\_COMPLEX}{normal size serif font}
263  \cvarg{CV\_FONT\_HERSHEY\_TRIPLEX}{normal size serif font (more complex than \texttt{CV\_FONT\_HERSHEY\_COMPLEX})}
264  \cvarg{CV\_FONT\_HERSHEY\_COMPLEX\_SMALL}{smaller version of \texttt{CV\_FONT\_HERSHEY\_COMPLEX}}
265  \cvarg{CV\_FONT\_HERSHEY\_SCRIPT\_SIMPLEX}{hand-writing style font}
266  \cvarg{CV\_FONT\_HERSHEY\_SCRIPT\_COMPLEX}{more complex variant of \texttt{CV\_FONT\_HERSHEY\_SCRIPT\_SIMPLEX}}
267  \end{description}
268  The parameter can be composited from one of the values above and an optional \texttt{CV\_FONT\_ITALIC} flag, which indicates italic or oblique font.}
269 \cvarg{hscale}{Horizontal scale.  If equal to \texttt{1.0f}, the characters have the original width depending on the font type. If equal to \texttt{0.5f}, the characters are of half the original width.}
270 \cvarg{vscale}{Vertical scale. If equal to \texttt{1.0f}, the characters have the original height depending on the font type. If equal to \texttt{0.5f}, the characters are of half the original height.}
271 \cvarg{shear}{Approximate tangent of the character slope relative to the vertical line.  A zero value means a non-italic font, \texttt{1.0f} means about a 45 degree slope, etc.} 
272 \cvarg{thickness}{Thickness of the text strokes}
273 \cvarg{lineType}{Type of the strokes, see \cross{Line} description}
274 \end{description}
275
276 The function initializes the font structure that can be passed to text rendering functions.
277
278
279 \cvCPyFunc{InitLineIterator}
280 Initializes the line iterator.
281
282 \cvdefC{
283 int cvInitLineIterator( \par const CvArr* image,\par CvPoint pt1,\par CvPoint pt2,\par CvLineIterator* line\_iterator,\par int connectivity=8,\par int left\_to\_right=0 );
284 }
285 \cvdefPy{InitLineIterator(image, pt1, pt2, connectivity=8, left\_to\_right=0) -> line\_iterator}
286
287 \begin{description}
288 \cvarg{image}{Image to sample the line from}
289 \cvarg{pt1}{First ending point of the line segment}
290 \cvarg{pt2}{Second ending point of the line segment}
291 \cvC{\cvarg{line\_iterator}{Pointer to the line iterator state structure}}
292 \cvarg{connectivity}{The scanned line connectivity, 4 or 8.}
293 \cvarg{left\_to\_right}{
294 If ($ \texttt{left\_to\_right} = 0 $ ) then the line is scanned in the specified order, from \texttt{pt1} to \texttt{pt2}.
295 If ($ \texttt{left\_to\_right} \ne 0$) the line is scanned from left-most point to right-most.}
296 \cvPy{\cvarg{line\_iterator}{Iterator over the pixels of the line}}
297 \end{description}
298
299 \ifC
300 The function initializes the line
301 iterator and returns the number of pixels between the two end points.
302 Both points must be inside the image.
303 After the iterator has been
304 initialized, all the points on the raster line that connects the
305 two ending points may be retrieved by successive calls of
306 \texttt{CV\_NEXT\_LINE\_POINT} point.
307 \fi
308 \ifPy
309 The function returns an iterator over the pixels connecting the two points.
310 \fi
311
312 The points on the line are
313 calculated one by one using a 4-connected or 8-connected Bresenham
314 algorithm.
315
316 \ifPy
317 Example: Using line iterator to calculate the sum of pixel values along a color line
318
319 \begin{lstlisting}
320 >>> import cv
321 >>> img = cv.LoadImageM("building.jpg", cv.CV_LOAD_IMAGE_COLOR)
322 >>> li = cv.InitLineIterator(img, (100, 100), (125, 150))
323 >>> red_sum = 0
324 >>> green_sum = 0
325 >>> blue_sum = 0
326 >>> for (r, g, b) in li:
327 ...     red_sum += r
328 ...     green_sum += g
329 ...     blue_sum += b
330 >>> print red_sum, green_sum, blue_sum
331 10935.0 9496.0 7946.0
332 \end{lstlisting}
333
334 or more concisely using \href{http://docs.python.org/library/functions.html\#zip}{zip}:
335
336 \begin{lstlisting}
337 >>> import cv
338 >>> img = cv.LoadImageM("building.jpg", cv.CV_LOAD_IMAGE_COLOR)
339 >>> li = cv.InitLineIterator(img, (100, 100), (125, 150))
340 >>> print [sum(c) for c in zip(*li)]
341 [10935.0, 9496.0, 7946.0]
342 \end{lstlisting}
343 \fi
344
345 \ifC
346 Example: Using line iterator to calculate the sum of pixel values along the color line.
347
348 \begin{lstlisting}
349
350 CvScalar sum_line_pixels( IplImage* image, CvPoint pt1, CvPoint pt2 )
351 {
352     CvLineIterator iterator;
353     int blue_sum = 0, green_sum = 0, red_sum = 0;
354     int count = cvInitLineIterator( image, pt1, pt2, &iterator, 8, 0 );
355
356     for( int i = 0; i < count; i++ ){
357         blue_sum += iterator.ptr[0];
358         green_sum += iterator.ptr[1];
359         red_sum += iterator.ptr[2];
360         CV_NEXT_LINE_POINT(iterator);
361
362         /* print the pixel coordinates: demonstrates how to calculate the 
363                                                         coordinates */
364         {
365         int offset, x, y;
366         /* assume that ROI is not set, otherwise need to take it 
367                                                 into account. */
368         offset = iterator.ptr - (uchar*)(image->imageData);
369         y = offset/image->widthStep;
370         x = (offset - y*image->widthStep)/(3*sizeof(uchar) 
371                                         /* size of pixel */);
372         printf("(%d,%d)\n", x, y );
373         }
374     }
375     return cvScalar( blue_sum, green_sum, red_sum );
376 }
377
378 \end{lstlisting}
379 \fi
380
381 \cvCPyFunc{Line}
382 Draws a line segment connecting two points.
383
384 \cvdefC{
385 void cvLine( \par CvArr* img,\par CvPoint pt1,\par CvPoint pt2,\par CvScalar color,\par int thickness=1,\par int lineType=8,\par int shift=0 );
386 }
387 \cvdefPy{Line(img,pt1,pt2,color,thickness=1,lineType=8,shift=0)-> None}
388
389 \begin{description}
390 \cvarg{img}{The image}
391 \cvarg{pt1}{First point of the line segment}
392 \cvarg{pt2}{Second point of the line segment}
393 \cvarg{color}{Line color}
394 \cvarg{thickness}{Line thickness}
395 \cvarg{lineType}{Type of the line:
396   \begin{description}
397   \cvarg{8}{(or omitted) 8-connected line.}
398   \cvarg{4}{4-connected line.}
399   \cvarg{CV\_AA}{antialiased line.}
400   \end{description}}
401 \cvarg{shift}{Number of fractional bits in the point coordinates}
402 \end{description}
403
404 The function draws the line segment between
405 \texttt{pt1} and \texttt{pt2} points in the image. The line is
406 clipped by the image or ROI rectangle. For non-antialiased lines
407 with integer coordinates the 8-connected or 4-connected Bresenham
408 algorithm is used. Thick lines are drawn with rounding endings.
409 Antialiased lines are drawn using Gaussian filtering. To specify
410 the line color, the user may use the macro
411 \texttt{CV\_RGB( r, g, b )}.
412
413 \cvCPyFunc{PolyLine}
414 Draws simple or thick polygons.
415
416 \cvdefC{
417 void cvPolyLine( \par CvArr* img,\par CvPoint** pts,\par int* npts,\par int contours,\par int is\_closed,\par CvScalar color,\par int thickness=1,\par int lineType=8,\par int shift=0 );}
418 \cvdefPy{PolyLine(img,polys,is\_closed,color,thickness=1,lineType=8,shift=0)-> None}
419
420 \begin{description}
421 \ifC
422 \cvarg{pts}{Array of pointers to polygons}
423 \cvarg{npts}{Array of polygon vertex counters}
424 \cvarg{contours}{Number of contours that bind the filled region}
425 \fi
426 \ifPy
427 \cvarg{polys}{List of lists of (x,y) pairs.  Each list of points is a polygon.}
428 \fi
429 \cvarg{img}{Image}
430 \cvarg{is\_closed}{Indicates whether the polylines must be drawn
431 closed. If closed, the function draws the line from the last vertex
432 of every contour to the first vertex.}
433 \cvarg{color}{Polyline color}
434 \cvarg{thickness}{Thickness of the polyline edges}
435 \cvarg{lineType}{Type of the line segments, see \cross{Line} description}
436 \cvarg{shift}{Number of fractional bits in the vertex coordinates}
437 \end{description}
438
439 The function draws single or multiple polygonal curves.
440
441 \cvCPyFunc{PutText}
442 Draws a text string.
443
444 \cvdefC{
445 void cvPutText( \par CvArr* img,\par const char* text,\par CvPoint org,\par const CvFont* font,\par CvScalar color );}
446 \cvdefPy{PutText(img,text,org,font,color)-> None}
447
448 \begin{description}
449 \cvarg{img}{Input image}
450 \cvarg{text}{String to print}
451 \cvarg{org}{Coordinates of the bottom-left corner of the first letter}
452 \cvarg{font}{Pointer to the font structure}
453 \cvarg{color}{Text color}
454 \end{description}
455
456
457 The function renders the text in the image with
458 the specified font and color. The printed text is clipped by the ROI
459 rectangle. Symbols that do not belong to the specified font are
460 replaced with the symbol for a rectangle.
461
462 \cvCPyFunc{Rectangle}
463 Draws a simple, thick, or filled rectangle.
464
465 \cvdefC{void cvRectangle( \par CvArr* img,\par CvPoint pt1,\par CvPoint pt2,\par CvScalar color,\par int thickness=1,\par int lineType=8,\par int shift=0 );}
466 \cvdefPy{Rectangle(img,pt1,pt2,color,thickness=1,lineType=8,shift=0)-> None}
467
468 \begin{description}
469 \cvarg{img}{Image}
470 \cvarg{pt1}{One of the rectangle's vertices}
471 \cvarg{pt2}{Opposite rectangle vertex}
472 \cvarg{color}{Line color (RGB) or brightness (grayscale image)}
473 \cvarg{thickness}{Thickness of lines that make up the rectangle. Negative values, e.g., CV\_FILLED, cause the function to draw a filled rectangle.}
474 \cvarg{lineType}{Type of the line, see \cross{Line} description}
475 \cvarg{shift}{Number of fractional bits in the point coordinates}
476 \end{description}
477
478 The function draws a rectangle with two opposite corners \texttt{pt1} and \texttt{pt2}.
479
480 \cvfunc{CV\_RGB}\label{CV_RGB}
481 Constructs a color value.
482
483 \cvdefC{\#define CV\_RGB( r, g, b )  cvScalar( (b), (g), (r) )}
484 \cvdefPy{CV\_RGB(red,grn,blu)->CvScalar}
485
486 \begin{description}
487 \cvarg{red}{Red component}
488 \cvarg{grn}{Green component}
489 \cvarg{blu}{Blue component}
490 \end{description}
491
492 \fi
493
494 \ifCpp
495
496 \cvCppFunc{circle}
497 Draws a circle
498
499 \cvdefCpp{
500 void circle(Mat\& img, Point center, int radius,\par
501             const Scalar\& color, int thickness=1,\par
502             int lineType=8, int shift=0);
503 }
504 \begin{description}
505 \cvarg{img}{Image where the circle is drawn}
506 \cvarg{center}{Center of the circle}
507 \cvarg{radius}{Radius of the circle}
508 \cvarg{color}{Circle color}
509 \cvarg{thickness}{Thickness of the circle outline if positive; negative thickness means that a filled circle is to be drawn}
510 \cvarg{lineType}{Type of the circle boundary, see \cvCppCross{line} description}
511 \cvarg{shift}{Number of fractional bits in the center coordinates and radius value}
512 \end{description}
513
514 The function \texttt{circle} draws a simple or filled circle with a
515 given center and radius.
516
517 \cvCppFunc{clipLine}
518 Clips the line against the image rectangle
519
520 \cvdefCpp{
521 bool clipLine(Size imgSize, Point\& pt1, Point\& pt2);\newline
522 bool clipLine(Rect imgRect, Point\& pt1, Point\& pt2);\newline
523 }
524 \begin{description}
525 \cvarg{imgSize}{The image size; the image rectangle will be \texttt{Rect(0, 0, imgSize.width, imgSize.height)}}
526 \cvarg{imgSize}{The image rectangle}
527 \cvarg{pt1}{The first line point}
528 \cvarg{pt2}{The second line point}
529 \end{description}
530
531 The functions \texttt{clipLine} calculate a part of the line
532 segment which is entirely within the specified rectangle.
533 They return \texttt{false} if the line segment is completely outside the rectangle and \texttt{true} otherwise.
534
535
536 \cvCppFunc{ellipse}
537 Draws a simple or thick elliptic arc or an fills ellipse sector.
538
539 \cvdefCpp{
540 void ellipse(Mat\& img, Point center, Size axes,\par
541              double angle, double startAngle, double endAngle,\par
542              const Scalar\& color, int thickness=1,\par
543              int lineType=8, int shift=0);\newline
544 void ellipse(Mat\& img, const RotatedRect\& box, const Scalar\& color,\par
545              int thickness=1, int lineType=8);\newline
546 }
547 \begin{description}
548 \cvarg{img}{The image}
549 \cvarg{center}{Center of the ellipse}
550 \cvarg{axes}{Length of the ellipse axes}
551 \cvarg{angle}{The ellipse rotation angle in degrees}
552 \cvarg{startAngle}{Starting angle of the elliptic arc in degrees}
553 \cvarg{endAngle}{Ending angle of the elliptic arc in degrees}
554 \cvarg{box}{Alternative ellipse representation via a \cross{RotatedRect}, i.e. the function draws an ellipse inscribed in the rotated rectangle}
555 \cvarg{color}{Ellipse color}
556 \cvarg{thickness}{Thickness of the ellipse arc outline if positive, otherwise this indicates that a filled ellipse sector is to be drawn}
557 \cvarg{lineType}{Type of the ellipse boundary, see \cvCppCross{line} description}
558 \cvarg{shift}{Number of fractional bits in the center coordinates and axes' values}
559 \end{description}
560
561 The functions \texttt{ellipse} with less parameters draw an ellipse outline, a filled ellipse, an elliptic
562 arc or a filled ellipse sector. 
563 A piecewise-linear curve is used to approximate the elliptic arc boundary. If you need more control of the ellipse rendering, you can retrieve the curve using \cvCppCross{ellipse2Poly} and then render it with \cvCppCross{polylines} or fill it with \cvCppCross{fillPoly}. If you use the first variant of the function and want to draw the whole ellipse, not an arc, pass \texttt{startAngle=0} and \texttt{endAngle=360}. The picture below
564 explains the meaning of the parameters.
565
566 Parameters of Elliptic Arc
567
568 \includegraphics[width=0.5\textwidth]{pics/ellipse.png}
569
570 \cvCppFunc{ellipse2Poly}
571 Approximates an elliptic arc with a polyline
572
573 \cvdefCpp{
574 void ellipse2Poly( Point center, Size axes, int angle,\par
575                    int startAngle, int endAngle, int delta,\par
576                    vector<Point>\& pts );\newline
577 }
578 \begin{description}
579 \cvarg{center}{Center of the arc}
580 \cvarg{axes}{Half-sizes of the arc. See \cvCppCross{ellipse}}
581 \cvarg{angle}{Rotation angle of the ellipse in degrees. See \cvCppCross{ellipse}}
582 \cvarg{startAngle}{Starting angle of the elliptic arc in degrees}
583 \cvarg{endAngle}{Ending angle of the elliptic arc in degrees}
584 \cvarg{delta}{Angle between the subsequent polyline vertices. It defines the approximation accuracy.}
585 \cvarg{pts}{The output vector of polyline vertices}
586 \end{description}
587
588 The function \texttt{ellipse2Poly} computes the vertices of a polyline that approximates the specified elliptic arc. It is used by \cvCppCross{ellipse}.
589
590 \cvCppFunc{fillConvexPoly}
591 Fills a convex polygon.
592
593 \cvdefCpp{
594 void fillConvexPoly(Mat\& img, const Point* pts, int npts,\par
595                     const Scalar\& color, int lineType=8,\par
596                     int shift=0);\newline
597 }
598 \begin{description}
599 \cvarg{img}{Image}
600 \cvarg{pts}{The polygon vertices}
601 \cvarg{npts}{The number of polygon vertices}
602 \cvarg{color}{Polygon color}
603 \cvarg{lineType}{Type of the polygon boundaries, see \cvCppCross{line} description}
604 \cvarg{shift}{The number of fractional bits in the vertex coordinates}
605 \end{description}
606
607 The function \texttt{fillConvexPoly} draws a filled convex polygon.
608 This function is much faster than the function \texttt{fillPoly}
609 and can fill not only convex polygons but any monotonic polygon without self-intersections,
610 i.e., a polygon whose contour intersects every horizontal line (scan
611 line) twice at the most (though, its top-most and/or the bottom edge could be horizontal).
612
613 \cvCppFunc{fillPoly}
614 Fills the area bounded by one or more polygons
615
616 \cvdefCpp{void fillPoly(Mat\& img, const Point** pts, \par
617               const int* npts, int ncontours,\par
618               const Scalar\& color, int lineType=8,\par
619               int shift=0, Point offset=Point() );}
620 \begin{description}
621 \cvarg{img}{Image}
622 \cvarg{pts}{Array of polygons, each represented as an array of points}
623 \cvarg{npts}{The array of polygon vertex counters}
624 \cvarg{ncontours}{The number of contours that bind the filled region}
625 \cvarg{color}{Polygon color}
626 \cvarg{lineType}{Type of the polygon boundaries, see \cvCppCross{line} description}
627 \cvarg{shift}{The number of fractional bits in the vertex coordinates}
628 \end{description}
629
630 The function \texttt{fillPoly} fills an area bounded by several
631 polygonal contours. The function can fills complex areas, for example,
632 areas with holes, contours with self-intersections (some of thier parts), and so forth.
633
634 \cvCppFunc{getTextSize}
635 Calculates the width and height of a text string.
636
637 \cvdefCpp{Size getTextSize(const string\& text, int fontFace,\par
638                  double fontScale, int thickness,\par
639                  int* baseLine);\newline}
640 \begin{description}
641 \cvarg{text}{The input text string}
642 \cvarg{fontFace}{The font to use; see \cvCppCross{putText}}
643 \cvarg{fontScale}{The font scale; see \cvCppCross{putText}}
644 \cvarg{thickness}{The thickness of lines used to render the text; see \cvCppCross{putText}}
645 \cvarg{baseLine}{The output parameter - y-coordinate of the baseline relative to the bottom-most text point}
646 \end{description}
647
648 The function \texttt{getTextSize} calculates and returns size of the box that contain the specified text.
649 That is, the following code will render some text, the tight box surrounding it and the baseline:
650
651 \begin{lstlisting}
652 // Use "y" to show that the baseLine is about
653 string text = "Funny text inside the box";
654 int fontFace = FONT_HERSHEY_SCRIPT_SIMPLEX;
655 double fontScale = 2;
656 int thickness = 3;
657
658 Mat img(600, 800, CV_8UC3, Scalar::all(0));
659
660 int baseline=0;
661 Size textSize = getTextSize(text, fontFace,
662                             fontScale, thickness, &baseline);
663 baseline += thickness;
664
665 // center the text
666 Point textOrg((img.cols - textSize.width)/2,
667               (img.rows + textSize.height)/2);
668
669 // draw the box
670 rectangle(img, textOrg + Point(0, baseline),
671           textOrg + Point(textSize.width, -textSize.height),
672           Scalar(0,0,255));
673 // ... and the baseline first
674 line(img, textOrg + Point(0, thickness),
675      textOrg + Point(textSize.width, thickness),
676      Scalar(0, 0, 255));
677
678 // then put the text itself
679 putText(img, text, textOrg, fontFace, fontScale,
680         Scalar::all(255), thickness, 8);
681 \end{lstlisting}
682         
683         
684 \cvCppFunc{line}
685 Draws a line segment connecting two points
686
687 \cvdefCpp{void line(Mat\& img, Point pt1, Point pt2, const Scalar\& color,\par
688           int thickness=1, int lineType=8, int shift=0);\newline}
689 \begin{description}
690 \cvarg{img}{The image}
691 \cvarg{pt1}{First point of the line segment}
692 \cvarg{pt2}{Second point of the line segment}
693 \cvarg{color}{Line color}
694 \cvarg{thickness}{Line thickness}
695 \cvarg{lineType}{Type of the line:
696   \begin{description}
697   \cvarg{8}{(or omitted) 8-connected line.}
698   \cvarg{4}{4-connected line.}
699   \cvarg{CV\_AA}{antialiased line.}
700   \end{description}}
701 \cvarg{shift}{Number of fractional bits in the point coordinates}
702 \end{description}
703
704 The function \texttt{line} draws the line segment between
705 \texttt{pt1} and \texttt{pt2} points in the image. The line is
706 clipped by the image boundaries. For non-antialiased lines
707 with integer coordinates the 8-connected or 4-connected Bresenham
708 algorithm is used. Thick lines are drawn with rounding endings.
709 Antialiased lines are drawn using Gaussian filtering. To specify
710 the line color, the user may use the macro
711 \texttt{CV\_RGB(r, g, b)}.
712
713
714 \cvclass{LineIterator}
715 Class for iterating pixels on a raster line
716
717 \begin{lstlisting}
718 class LineIterator
719 {
720 public:
721     // creates iterators for the line connecting pt1 and pt2
722     // the line will be clipped on the image boundaries
723     // the line is 8-connected or 4-connected
724     // If leftToRight=true, then the iteration is always done
725     // from the left-most point to the right most,
726     // not to depend on the ordering of pt1 and pt2 parameters
727     LineIterator(const Mat& img, Point pt1, Point pt2,
728                  int connectivity=8, bool leftToRight=false);newline
729     // returns pointer to the current line pixel
730     uchar* operator *();newline
731     // move the iterator to the next pixel
732     LineIterator& operator ++();newline
733     LineIterator operator ++(int);newline
734
735     // internal state of the iterator
736     uchar* ptr;newline
737     int err, count;newline
738     int minusDelta, plusDelta;newline
739     int minusStep, plusStep;newline
740 };
741 \end{lstlisting}
742
743 The class \texttt{LineIterator} is used to get each pixel of a raster line. It can be treated as versatile implementation of the Bresenham algorithm, where you can stop at each pixel and do some extra processing, for example, grab pixel values along the line, or draw a line with some effect (e.g. with XOR operation).
744
745 The number of pixels along the line is store in \texttt{LineIterator::count}.
746
747 \begin{lstlisting}
748 // grabs pixels along the line (pt1, pt2)
749 // from 8-bit 3-channel image to the buffer
750 LineIterator it(img, pt1, pt2, 8);
751 vector<Vec3b> buf(it.count);
752
753 for(int i = 0; i < it.count; i++, ++it)
754     buf[i] = *(const Vec3b)*it;
755 \end{lstlisting}
756
757
758 \cvCppFunc{rectangle}
759 Draws a simple, thick, or filled up-right rectangle.
760
761 \cvdefCpp{void rectangle(Mat\& img, Point pt1, Point pt2,\par
762                const Scalar\& color, int thickness=1,\par
763                int lineType=8, int shift=0);}
764 \begin{description}
765 \cvarg{img}{Image}
766 \cvarg{pt1}{One of the rectangle's vertices}
767 \cvarg{pt2}{Opposite to \texttt{pt1} rectangle vertex}
768 \cvarg{color}{Rectangle color or brightness (grayscale image)}
769 \cvarg{thickness}{Thickness of lines that make up the rectangle. Negative values, e.g. \texttt{CV\_FILLED}, mean that the function has to draw a filled rectangle.}
770 \cvarg{lineType}{Type of the line, see \cvCppCross{line} description}
771 \cvarg{shift}{Number of fractional bits in the point coordinates}
772 \end{description}
773
774 The function \texttt{rectangle} draws a rectangle outline or a filled rectangle, which two opposite corners are \texttt{pt1} and \texttt{pt2}.
775                
776
777 \cvCppFunc{polylines}
778 Draws several polygonal curves
779
780 \cvdefCpp{void polylines(Mat\& img, const Point** pts, const int* npts,\par
781                int ncontours, bool isClosed, const Scalar\& color,\par
782                int thickness=1, int lineType=8, int shift=0 );\newline}
783 \begin{description}
784 \cvarg{img}{The image}
785 \cvarg{pts}{Array of polygonal curves}
786 \cvarg{npts}{Array of polygon vertex counters}
787 \cvarg{ncontours}{The number of curves}
788 \cvarg{isClosed}{Indicates whether the drawn polylines are closed or not. If they are closed, the function draws the line from the last vertex of each curve to its first vertex}
789 \cvarg{color}{Polyline color}
790 \cvarg{thickness}{Thickness of the polyline edges}
791 \cvarg{lineType}{Type of the line segments, see \cvCppCross{line} description}
792 \cvarg{shift}{The number of fractional bits in the vertex coordinates}
793 \end{description}
794
795 The function \texttt{polylines} draws one or more polygonal curves.
796
797 \cvCppFunc{putText}
798 Draws a text string
799
800 \cvdefCpp{void putText( Mat\& img, const string\& text, Point org,\par
801               int fontFace, double fontScale, Scalar color,\par
802               int thickness=1, int lineType=8,\par
803               bool bottomLeftOrigin=false );}
804 \begin{description}
805 \cvarg{img}{The image}
806 \cvarg{text}{The text string to be drawn}
807 \cvarg{org}{The bottom-left corner of the text string in the image}
808 \cvarg{fontFace}{The font type, one of \texttt{FONT\_HERSHEY\_SIMPLEX}, \texttt{FONT\_HERSHEY\_PLAIN},
809  \texttt{FONT\_HERSHEY\_DUPLEX}, \texttt{FONT\_HERSHEY\_COMPLEX}, \texttt{FONT\_HERSHEY\_TRIPLEX},
810  \texttt{FONT\_HERSHEY\_COMPLEX\_SMALL}, \texttt{FONT\_HERSHEY\_SCRIPT\_SIMPLEX} or \texttt{FONT\_HERSHEY\_SCRIPT\_COMPLEX},
811    where each of the font id's can be combined with \texttt{FONT\_HERSHEY\_ITALIC} to get the slanted letters.}
812 \cvarg{fontScale}{The font scale factor that is multiplied by the font-specific base size}
813 \cvarg{thickness}{Thickness of the lines used to draw the text}
814 \cvarg{lineType}{The line type; see \texttt{line} for details}
815 \cvarg{bottomLeftOrigin}{When true, the image data origin is at the bottom-left corner, otherwise it's at the top-left corner}
816 \end{description}
817
818 The function \texttt{putText} draws a text string in the image.
819 Symbols that can not be rendered using the specified font are
820 replaced question marks. See \cvCppCross{getTextSize} for a text rendering code example.
821
822 \fi