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