1 \section{Drawing Functions}
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
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])\]
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])\]
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}.
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.
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.
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}
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}
43 The function draws a simple or filled circle with a
44 given center and radius.
47 Clips the line against the image rectangle.
49 \cvdefC{int cvClipLine( \par CvSize imgSize,\par CvPoint* pt1,\par CvPoint* pt2 );}
50 \cvdefPy{ClipLine(imgSize, pt1, pt2) -> (clipped\_pt1, clipped\_pt2)}
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.}}
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.}
61 \cvCPyFunc{DrawContours}
62 Draws contour outlines or interiors in an image.
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 );
67 \cvdefPy{DrawContours(img,contour,external\_color,hole\_color,max\_level,thickness=1,lineType=8,offset=(0,0))-> None}
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
84 \cvarg{lineType}{Type of the contour segments, see \cross{Line} description}
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$.
90 Example: Connected component detection via contour functions
96 int main( int argc, char** argv )
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)
103 IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );
104 CvMemStorage* storage = cvCreateMemStorage(0);
107 cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );
108 cvNamedWindow( "Source", 1 );
109 cvShowImage( "Source", src );
111 cvFindContours( src, storage, &contour, sizeof(CvContour),
112 CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
115 for( ; contour != 0; contour = contour->h_next )
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 );
122 cvNamedWindow( "Components", 1 );
123 cvShowImage( "Components", dst );
131 Draws a simple or thick elliptic arc or an fills ellipse sector.
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}
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}
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.
155 Parameters of Elliptic Arc
157 \includegraphics[width=0.5\textwidth]{pics/ellipse.png}
159 \cvCPyFunc{EllipseBox}
161 Draws a simple or thick elliptic arc or fills an ellipse sector.
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}
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}
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.
177 \cvCPyFunc{FillConvexPoly}
178 Fills a convex polygon.
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}
187 \cvarg{pts}{Array of pointers to a single polygon}
188 \cvarg{npts}{Polygon vertex counter}
190 \cvarg{pn}{List of coordinate pairs}
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}
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.
205 Fills a polygon's interior.
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 );
210 \cvdefPy{FillPoly(img,polys,color,lineType=8,shift=0)-> None}
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}
220 \cvarg{polys}{List of lists of (x,y) pairs. Each list of points is a polygon.}
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}
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.
232 \cvCPyFunc{GetTextSize}
233 Retrieves the width and height of a text string.
236 void cvGetTextSize( \par const char* textString,\par const CvFont* font,\par CvSize* textSize,\par int* baseline );}
237 \cvdefPy{GetTextSize(textString,font)-> (textSize,baseline)}
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}
246 The function calculates the dimensions of a rectangle to enclose a text string when a specified font is used.
249 Initializes font structure.
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}
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:
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}}
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}
276 The function initializes the font structure that can be passed to text rendering functions.
279 \cvCPyFunc{InitLineIterator}
280 Initializes the line iterator.
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 );
285 \cvdefPy{InitLineIterator(image, pt1, pt2, connectivity=8, left\_to\_right=0) -> line\_iterator}
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}}
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.
309 The function returns an iterator over the pixels connecting the two points.
312 The points on the line are
313 calculated one by one using a 4-connected or 8-connected Bresenham
317 Example: Using line iterator to calculate the sum of pixel values along a color line
321 >>> img = cv.LoadImageM("building.jpg", cv.CV_LOAD_IMAGE_COLOR)
322 >>> li = cv.InitLineIterator(img, (100, 100), (125, 150))
326 >>> for (r, g, b) in li:
330 >>> print red_sum, green_sum, blue_sum
331 10935.0 9496.0 7946.0
334 or more concisely using \href{http://docs.python.org/library/functions.html\#zip}{zip}:
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]
346 Example: Using line iterator to calculate the sum of pixel values along the color line.
350 CvScalar sum_line_pixels( IplImage* image, CvPoint pt1, CvPoint pt2 )
352 CvLineIterator iterator;
353 int blue_sum = 0, green_sum = 0, red_sum = 0;
354 int count = cvInitLineIterator( image, pt1, pt2, &iterator, 8, 0 );
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);
362 /* print the pixel coordinates: demonstrates how to calculate the
366 /* assume that ROI is not set, otherwise need to take it
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 );
375 return cvScalar( blue_sum, green_sum, red_sum );
382 Draws a line segment connecting two points.
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 );
387 \cvdefPy{Line(img,pt1,pt2,color,thickness=1,lineType=8,shift=0)-> None}
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:
397 \cvarg{8}{(or omitted) 8-connected line.}
398 \cvarg{4}{4-connected line.}
399 \cvarg{CV\_AA}{antialiased line.}
401 \cvarg{shift}{Number of fractional bits in the point coordinates}
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 )}.
414 Draws simple or thick polygons.
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}
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}
427 \cvarg{polys}{List of lists of (x,y) pairs. Each list of points is a polygon.}
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}
439 The function draws single or multiple polygonal curves.
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}
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}
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.
462 \cvCPyFunc{Rectangle}
463 Draws a simple, thick, or filled rectangle.
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}
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}
478 The function draws a rectangle with two opposite corners \texttt{pt1} and \texttt{pt2}.
480 \cvfunc{CV\_RGB}\label{CV_RGB}
481 Constructs a color value.
483 \cvdefC{\#define CV\_RGB( r, g, b ) cvScalar( (b), (g), (r) )}
484 \cvdefPy{CV\_RGB(red,grn,blu)->CvScalar}
487 \cvarg{red}{Red component}
488 \cvarg{grn}{Green component}
489 \cvarg{blu}{Blue component}
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);
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}
514 The function \texttt{circle} draws a simple or filled circle with a
515 given center and radius.
518 Clips the line against the image rectangle
521 bool clipLine(Size imgSize, Point\& pt1, Point\& pt2);\newline
522 bool clipLine(Rect imgRect, Point\& pt1, Point\& pt2);\newline
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}
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.
537 Draws a simple or thick elliptic arc or an fills ellipse sector.
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
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}
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.
566 Parameters of Elliptic Arc
568 \includegraphics[width=0.5\textwidth]{pics/ellipse.png}
570 \cvCppFunc{ellipse2Poly}
571 Approximates an elliptic arc with a polyline
574 void ellipse2Poly( Point center, Size axes, int angle,\par
575 int startAngle, int endAngle, int delta,\par
576 vector<Point>\& pts );\newline
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}
588 The function \texttt{ellipse2Poly} computes the vertices of a polyline that approximates the specified elliptic arc. It is used by \cvCppCross{ellipse}.
590 \cvCppFunc{fillConvexPoly}
591 Fills a convex polygon.
594 void fillConvexPoly(Mat\& img, const Point* pts, int npts,\par
595 const Scalar\& color, int lineType=8,\par
596 int shift=0);\newline
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}
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).
614 Fills the area bounded by one or more polygons
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() );}
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}
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.
634 \cvCppFunc{getTextSize}
635 Calculates the width and height of a text string.
637 \cvdefCpp{Size getTextSize(const string\& text, int fontFace,\par
638 double fontScale, int thickness,\par
639 int* baseLine);\newline}
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}
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:
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;
658 Mat img(600, 800, CV_8UC3, Scalar::all(0));
661 Size textSize = getTextSize(text, fontFace,
662 fontScale, thickness, &baseline);
663 baseline += thickness;
666 Point textOrg((img.cols - textSize.width)/2,
667 (img.rows + textSize.height)/2);
670 rectangle(img, textOrg + Point(0, baseline),
671 textOrg + Point(textSize.width, -textSize.height),
673 // ... and the baseline first
674 line(img, textOrg + Point(0, thickness),
675 textOrg + Point(textSize.width, thickness),
678 // then put the text itself
679 putText(img, text, textOrg, fontFace, fontScale,
680 Scalar::all(255), thickness, 8);
685 Draws a line segment connecting two points
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}
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:
697 \cvarg{8}{(or omitted) 8-connected line.}
698 \cvarg{4}{4-connected line.}
699 \cvarg{CV\_AA}{antialiased line.}
701 \cvarg{shift}{Number of fractional bits in the point coordinates}
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)}.
714 \cvclass{LineIterator}
715 Class for iterating pixels on a raster line
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
735 // internal state of the iterator
737 int err, count;newline
738 int minusDelta, plusDelta;newline
739 int minusStep, plusStep;newline
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).
745 The number of pixels along the line is store in \texttt{LineIterator::count}.
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);
753 for(int i = 0; i < it.count; i++, ++it)
754 buf[i] = *(const Vec3b)*it;
758 \cvCppFunc{rectangle}
759 Draws a simple, thick, or filled up-right rectangle.
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);}
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}
774 The function \texttt{rectangle} draws a rectangle outline or a filled rectangle, which two opposite corners are \texttt{pt1} and \texttt{pt2}.
777 \cvCppFunc{polylines}
778 Draws several polygonal curves
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}
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}
795 The function \texttt{polylines} draws one or more polygonal curves.
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 );}
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}
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.