Drawing Functions

Drawing functions work with matrices/images of arbitrary depth. The boundaries of the shapes can be rendered with antialiasing (implemented only for 8-bit images for now). All the functions include the parameter color that uses a rgb value (that may be constructed with CV_RGB macro or the cvScalar() function ) for color images and brightness for grayscale images. For color images the order channel is normally Blue, Green, Red , this is what imshow() , imread() and imwrite() expect , so if you form a color using cvScalar() , it should look like:

\texttt{cvScalar} (blue \_ component, green \_ component, red \_ component[, alpha \_ component])

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 cvtColor() .

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 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.

Also, note that the functions do not support alpha-transparency - when the target image is 4-channnel, then the 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.

Circle

Comments from the Wiki

void cvCircle(CvArr* img, CvPoint center, int radius, CvScalar color, int thickness=1, int lineType=8, int shift=0)

Draws a circle.

Parameters:
  • img – Image where the circle is drawn
  • center – Center of the circle
  • radius – Radius of the circle
  • color – Circle color
  • thickness – Thickness of the circle outline if positive, otherwise this indicates that a filled circle is to be drawn
  • lineType – Type of the circle boundary, see Line description
  • shift – Number of fractional bits in the center coordinates and radius value

The function draws a simple or filled circle with a given center and radius.

ClipLine

Comments from the Wiki

int cvClipLine(CvSize imgSize, CvPoint* pt1, CvPoint* pt2)

Clips the line against the image rectangle.

Parameters:
  • imgSize – Size of the image
  • pt1 – First ending point of the line segment. It is modified by the function.
  • pt2 – Second ending point of the line segment. It is modified by the function.

The function calculates a part of the line segment which is entirely within the image. It returns 0 if the line segment is completely outside the image and 1 otherwise.

DrawContours

Comments from the Wiki

void cvDrawContours(CvArr *img, CvSeq* contour, CvScalar external_color, CvScalar hole_color, int max_level, int thickness=1, int lineType=8)

Draws contour outlines or interiors in an image.

Parameters:
  • img – Image where the contours are to be drawn. As with any other drawing function, the contours are clipped with the ROI.
  • contour – Pointer to the first contour
  • external_color – Color of the external contours
  • hole_color – Color of internal contours (holes)
  • max_level – Maximal level for drawn contours. If 0, only contour is drawn. If 1, the contour and all contours following it on the same level are drawn. If 2, all contours following and all contours one level below the contours are drawn, and so forth. If the value is negative, the function does not draw the contours following after contour but draws the child contours of contour up to the |\texttt{max\_level}|-1 level.
  • thickness – Thickness of lines the contours are drawn with. If it is negative (For example, =CV _ FILLED), the contour interiors are drawn.
  • lineType – Type of the contour segments, see Line description

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 .

Example: Connected component detection via contour functions

#include "cv.h"
#include "highgui.h"

int main( int argc, char** argv )
{
    IplImage* src;
    // the first command line parameter must be file name of binary
    // (black-n-white) image
    if( argc == 2 && (src=cvLoadImage(argv[1], 0))!= 0)
    {
        IplImage* dst = cvCreateImage( cvGetSize(src), 8, 3 );
        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* contour = 0;

        cvThreshold( src, src, 1, 255, CV_THRESH_BINARY );
        cvNamedWindow( "Source", 1 );
        cvShowImage( "Source", src );

        cvFindContours( src, storage, &contour, sizeof(CvContour),
           CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
        cvZero( dst );

        for( ; contour != 0; contour = contour->h_next )
        {
            CvScalar color = CV_RGB( rand()&255, rand()&255, rand()&255 );
            /* replace CV_FILLED with 1 to see the outlines */
            cvDrawContours( dst, contour, color, color, -1, CV_FILLED, 8 );
        }

        cvNamedWindow( "Components", 1 );
        cvShowImage( "Components", dst );
        cvWaitKey(0);
    }
}

Ellipse

Comments from the Wiki

void cvEllipse(CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, CvScalar color, int thickness=1, int lineType=8, int shift=0)

Draws a simple or thick elliptic arc or an fills ellipse sector.

Parameters:
  • img – The image
  • center – Center of the ellipse
  • axes – Length of the ellipse axes
  • angle – Rotation angle
  • start_angle – Starting angle of the elliptic arc
  • end_angle – Ending angle of the elliptic arc.
  • color – Ellipse color
  • thickness – Thickness of the ellipse arc outline if positive, otherwise this indicates that a filled ellipse sector is to be drawn
  • lineType – Type of the ellipse boundary, see Line description
  • shift – Number of fractional bits in the center coordinates and axes’ values

The function draws a simple or thick elliptic arc or fills an ellipse sector. The arc is clipped by the ROI rectangle. A piecewise-linear approximation is used for antialiased arcs and thick arcs. All the angles are given in degrees. The picture below explains the meaning of the parameters.

Parameters of Elliptic Arc

_images/ellipse.png

EllipseBox

Comments from the Wiki

void cvEllipseBox(CvArr* img, CvBox2D box, CvScalar color, int thickness=1, int lineType=8, int shift=0)

Draws a simple or thick elliptic arc or fills an ellipse sector.

Parameters:
  • img – Image
  • box – The enclosing box of the ellipse drawn
  • thickness – Thickness of the ellipse boundary
  • lineType – Type of the ellipse boundary, see Line description
  • shift – Number of fractional bits in the box vertex coordinates

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 CamShift and FitEllipse do. The ellipse drawn is clipped by ROI rectangle. A piecewise-linear approximation is used for antialiased arcs and thick arcs.

FillConvexPoly

Comments from the Wiki

void cvFillConvexPoly(CvArr* img, CvPoint* pts, int npts, CvScalar color, int lineType=8, int shift=0)

Fills a convex polygon.

Parameters:
  • img – Image
  • pts – Array of pointers to a single polygon
  • npts – Polygon vertex counter
  • color – Polygon color
  • lineType – Type of the polygon boundaries, see Line description
  • shift – Number of fractional bits in the vertex coordinates

The function fills a convex polygon’s interior. This function is much faster than the function cvFillPoly and can fill not only convex polygons but any monotonic polygon, i.e., a polygon whose contour intersects every horizontal line (scan line) twice at the most.

FillPoly

Comments from the Wiki

void cvFillPoly(CvArr* img, CvPoint** pts, int* npts, int contours, CvScalar color, int lineType=8, int shift=0)

Fills a polygon’s interior.

Parameters:
  • img – Image
  • pts – Array of pointers to polygons
  • npts – Array of polygon vertex counters
  • contours – Number of contours that bind the filled region
  • color – Polygon color
  • lineType – Type of the polygon boundaries, see Line description
  • shift – Number of fractional bits in the vertex coordinates

The function fills an area bounded by several polygonal contours. The function fills complex areas, for example, areas with holes, contour self-intersection, and so forth.

GetTextSize

Comments from the Wiki

void cvGetTextSize(const char* textString, const CvFont* font, CvSize* textSize, int* baseline)

Retrieves the width and height of a text string.

Parameters:
  • font – Pointer to the font structure
  • textString – Input string
  • textSize – Resultant size of the text string. Height of the text does not include the height of character parts that are below the baseline.
  • baseline – y-coordinate of the baseline relative to the bottom-most text point

The function calculates the dimensions of a rectangle to enclose a text string when a specified font is used.

InitFont

Comments from the Wiki

void cvInitFont(CvFont* font, int fontFace, double hscale, double vscale, double shear=0, int thickness=1, int lineType=8)

Initializes font structure.

Parameters:
  • font – Pointer to the font structure initialized by the function
  • fontFace

    Font name identifier. Only a subset of Hershey fonts http://sources.isc.org/utils/misc/hershey-font.txt are supported now:

    • CV_FONT_HERSHEY_SIMPLEX normal size sans-serif font
    • CV_FONT_HERSHEY_PLAIN small size sans-serif font
    • CV_FONT_HERSHEY_DUPLEX normal size sans-serif font (more complex than CV_FONT_HERSHEY_SIMPLEX )
    • CV_FONT_HERSHEY_COMPLEX normal size serif font
    • CV_FONT_HERSHEY_TRIPLEX normal size serif font (more complex than CV_FONT_HERSHEY_COMPLEX )
    • CV_FONT_HERSHEY_COMPLEX_SMALL smaller version of CV_FONT_HERSHEY_COMPLEX
    • CV_FONT_HERSHEY_SCRIPT_SIMPLEX hand-writing style font
    • CV_FONT_HERSHEY_SCRIPT_COMPLEX more complex variant of CV_FONT_HERSHEY_SCRIPT_SIMPLEX

    The parameter can be composited from one of the values above and an optional CV_FONT_ITALIC flag, which indicates italic or oblique font.

  • hscale – Horizontal scale. If equal to 1.0f , the characters have the original width depending on the font type. If equal to 0.5f , the characters are of half the original width.
  • vscale – Vertical scale. If equal to 1.0f , the characters have the original height depending on the font type. If equal to 0.5f , the characters are of half the original height.
  • shear – Approximate tangent of the character slope relative to the vertical line. A zero value means a non-italic font, 1.0f means about a 45 degree slope, etc.
  • thickness – Thickness of the text strokes
  • lineType – Type of the strokes, see Line description

The function initializes the font structure that can be passed to text rendering functions.

InitLineIterator

Comments from the Wiki

int cvInitLineIterator(const CvArr* image, CvPoint pt1, CvPoint pt2, CvLineIterator* line_iterator, int connectivity=8, int left_to_right=0)

Initializes the line iterator.

Parameters:
  • image – Image to sample the line from
  • pt1 – First ending point of the line segment
  • pt2 – Second ending point of the line segment
  • line_iterator – Pointer to the line iterator state structure
  • connectivity – The scanned line connectivity, 4 or 8.
  • left_to_right – If ( \texttt{left\_to\_right} = 0 ) then the line is scanned in the specified order, from pt1 to pt2 . If ( \texttt{left\_to\_right} \ne 0 ) the line is scanned from left-most point to right-most.

The function initializes the line iterator and returns the number of pixels between the two end points. Both points must be inside the image. After the iterator has been initialized, all the points on the raster line that connects the two ending points may be retrieved by successive calls of CV_NEXT_LINE_POINT point. The points on the line are calculated one by one using a 4-connected or 8-connected Bresenham algorithm.

Example: Using line iterator to calculate the sum of pixel values along the color line.

CvScalar sum_line_pixels( IplImage* image, CvPoint pt1, CvPoint pt2 )
{
    CvLineIterator iterator;
    int blue_sum = 0, green_sum = 0, red_sum = 0;
    int count = cvInitLineIterator( image, pt1, pt2, &iterator, 8, 0 );

    for( int i = 0; i < count; i++ ){
        blue_sum += iterator.ptr[0];
        green_sum += iterator.ptr[1];
        red_sum += iterator.ptr[2];
        CV_NEXT_LINE_POINT(iterator);

        /* print the pixel coordinates: demonstrates how to calculate the
                                                        coordinates */
        {
        int offset, x, y;
        /* assume that ROI is not set, otherwise need to take it
                                                into account. */
        offset = iterator.ptr - (uchar*)(image->imageData);
        y = offset/image->widthStep;
        x = (offset - y*image->widthStep)/(3*sizeof(uchar)
                                        /* size of pixel */);
        printf("(
        }
    }
    return cvScalar( blue_sum, green_sum, red_sum );
}

Line

Comments from the Wiki

void cvLine(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int lineType=8, int shift=0)

Draws a line segment connecting two points.

Parameters:
  • img – The image
  • pt1 – First point of the line segment
  • pt2 – Second point of the line segment
  • color – Line color
  • thickness – Line thickness
  • lineType

    Type of the line:

    • 8 (or omitted) 8-connected line.
    • 4 4-connected line.
    • CV_AA antialiased line.
  • shift – Number of fractional bits in the point coordinates

The function draws the line segment between pt1 and pt2 points in the image. The line is clipped by the image or ROI rectangle. For non-antialiased lines with integer coordinates the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering. To specify the line color, the user may use the macro CV_RGB( r, g, b ) .

PolyLine

Comments from the Wiki

void cvPolyLine(CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed, CvScalar color, int thickness=1, int lineType=8, int shift=0)

Draws simple or thick polygons.

Parameters:
  • pts – Array of pointers to polygons
  • npts – Array of polygon vertex counters
  • contours – Number of contours that bind the filled region
  • img – Image
  • is_closed – Indicates whether the polylines must be drawn closed. If closed, the function draws the line from the last vertex of every contour to the first vertex.
  • color – Polyline color
  • thickness – Thickness of the polyline edges
  • lineType – Type of the line segments, see Line description
  • shift – Number of fractional bits in the vertex coordinates

The function draws single or multiple polygonal curves.

PutText

Comments from the Wiki

void cvPutText(CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color)

Draws a text string.

Parameters:
  • img – Input image
  • text – String to print
  • org – Coordinates of the bottom-left corner of the first letter
  • font – Pointer to the font structure
  • color – Text color

The function renders the text in the image with the specified font and color. The printed text is clipped by the ROI rectangle. Symbols that do not belong to the specified font are replaced with the symbol for a rectangle.

Rectangle

Comments from the Wiki

void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int lineType=8, int shift=0)

Draws a simple, thick, or filled rectangle.

Parameters:
  • img – Image
  • pt1 – One of the rectangle’s vertices
  • pt2 – Opposite rectangle vertex
  • color – Line color (RGB) or brightness (grayscale image)
  • thickness – Thickness of lines that make up the rectangle. Negative values, e.g., CV _ FILLED, cause the function to draw a filled rectangle.
  • lineType – Type of the line, see Line description
  • shift – Number of fractional bits in the point coordinates

The function draws a rectangle with two opposite corners pt1 and pt2 .

CV_RGB

Comments from the Wiki

#define CV_RGB(r, g, b ) cvScalar( (b), (g), (r))

Constructs a color value.

Parameters:
  • red – Red component
  • grn – Green component
  • blu – Blue component

Table Of Contents

Previous topic

Dynamic Structures

Next topic

XML/YAML Persistence

This Page