Skip to main content
Drawing functions work with matrices/images of arbitrary depth. The boundaries of shapes can be rendered with antialiasing (implemented only for 8-bit images for now).

Color Convention

For color images, the channel ordering is normally Blue, Green, Red (BGR). This is what imshow, imread, and imwrite expect. If you form a color using the Scalar constructor, it should look like:

Line Drawing

line

Draws a line segment connecting two points.
InputOutputArray
Image.
Point
First point of the line segment.
Point
Second point of the line segment.
Scalar
Line color.
int
default:"1"
Line thickness.
int
default:"LINE_8"
Type of the line. See LineTypes.
int
default:"0"
Number of fractional bits in the point coordinates.
The function line draws the line segment between pt1 and pt2 points in the image. The line is clipped by the image boundaries.

arrowedLine

Draws an arrow segment pointing from the first point to the second one.
InputOutputArray
Image.
Point
The point the arrow starts from.
Point
The point the arrow points to.
Scalar
Line color.
int
default:"1"
Line thickness.
int
default:"8"
Type of the line. See LineTypes.
int
default:"0"
Number of fractional bits in the point coordinates.
double
default:"0.1"
The length of the arrow tip in relation to the arrow length.

Shape Drawing

rectangle

Draws a simple, thick, or filled up-right rectangle.
InputOutputArray
Image.
Point
Vertex of the rectangle.
Point
Vertex of the rectangle opposite to pt1.
Rect
Alternative rectangle specification.
Scalar
Rectangle color or brightness (grayscale image).
int
default:"1"
Thickness of lines that make up the rectangle. Negative values, like FILLED, mean that the function has to draw a filled rectangle.
int
default:"LINE_8"
Type of the line. See LineTypes.
int
default:"0"
Number of fractional bits in the point coordinates.

circle

Draws a circle.
InputOutputArray
Image where the circle is drawn.
Point
Center of the circle.
int
Radius of the circle.
Scalar
Circle color.
int
default:"1"
Thickness of the circle outline, if positive. Negative values, like FILLED, mean that a filled circle is to be drawn.
int
default:"LINE_8"
Type of the circle boundary. See LineTypes.
int
default:"0"
Number of fractional bits in the coordinates of the center and in the radius value.

ellipse

Draws a simple or thick elliptic arc or fills an ellipse sector.
InputOutputArray
Image.
Point
Center of the ellipse.
Size
Half of the size of the ellipse main axes.
double
Ellipse rotation angle in degrees.
double
Starting angle of the elliptic arc in degrees.
double
Ending angle of the elliptic arc in degrees.
RotatedRect
Alternative ellipse representation via RotatedRect. This means that the function draws an ellipse inscribed in the rotated rectangle.
Scalar
Ellipse color.
int
default:"1"
Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
int
default:"LINE_8"
Type of the ellipse boundary. See LineTypes.
int
default:"0"
Number of fractional bits in the coordinates of the center and values of axes.
To draw the whole ellipse, not an arc, pass startAngle=0 and endAngle=360. If startAngle is greater than endAngle, they are swapped.

Polygon Drawing

polylines

Draws several polygonal curves.
InputOutputArray
Image.
InputArrayOfArrays
Array of polygonal curves.
bool
Flag indicating whether the drawn polylines are closed or not. If they are closed, the function draws a line from the last vertex of each curve to its first vertex.
Scalar
Polyline color.
int
default:"1"
Thickness of the polyline edges.
int
default:"LINE_8"
Type of the line segments. See LineTypes.
int
default:"0"
Number of fractional bits in the vertex coordinates.

fillPoly

Fills the area bounded by one or more polygons.
InputOutputArray
Image.
InputArrayOfArrays
Array of polygons where each polygon is represented as an array of points.
Scalar
Polygon color.
int
default:"LINE_8"
Type of the polygon boundaries. See LineTypes.
int
default:"0"
Number of fractional bits in the vertex coordinates.
Point
default:"Point()"
Optional offset of all points of the contours.
The function fillPoly fills an area bounded by several polygonal contours. The function can fill complex areas, for example, areas with holes, contours with self-intersections, and so forth.

fillConvexPoly

Fills a convex polygon.
InputOutputArray
Image.
InputArray
Polygon vertices.
Scalar
Polygon color.
int
default:"LINE_8"
Type of the polygon boundaries. See LineTypes.
int
default:"0"
Number of fractional bits in the vertex coordinates.
This function is much faster than fillPoly. It can fill not only convex polygons but any monotonic polygon without self-intersections.

Text Rendering

putText

Draws a text string.
InputOutputArray
Image.
String
Text string to be drawn.
Point
Bottom-left corner of the text string in the image.
int
Font type. See HersheyFonts.
double
Font scale factor that is multiplied by the font-specific base size.
Scalar
Text color.
int
default:"1"
Thickness of the lines used to draw a text.
int
default:"LINE_8"
Line type. See LineTypes.
bool
default:"false"
When true, the image data origin is at the bottom-left corner. Otherwise, it is at the top-left corner.
The function putText renders the specified text string in the image. Symbols that cannot be rendered using the specified font are replaced by question marks.

getTextSize

Calculates the width and height of a text string.
String
Input text string.
int
Font to use. See HersheyFonts.
double
Font scale factor that is multiplied by the font-specific base size.
int
Thickness of lines used to render the text.
int*
y-coordinate of the baseline relative to the bottom-most text point.
The function calculates and returns the size of a box that contains the specified text.

Enumerations

LineTypes

Types of line:
  • FILLED (-1) - Filled shape
  • LINE_4 (4) - 4-connected line
  • LINE_8 (8) - 8-connected line
  • LINE_AA (16) - Antialiased line

HersheyFonts

Hershey font types:
  • FONT_HERSHEY_SIMPLEX (0) - Normal size sans-serif font
  • FONT_HERSHEY_PLAIN (1) - Small size sans-serif font
  • FONT_HERSHEY_DUPLEX (2) - Normal size sans-serif font (more complex than SIMPLEX)
  • FONT_HERSHEY_COMPLEX (3) - Normal size serif font
  • FONT_HERSHEY_TRIPLEX (4) - Normal size serif font (more complex than COMPLEX)
  • FONT_HERSHEY_COMPLEX_SMALL (5) - Smaller version of COMPLEX
  • FONT_HERSHEY_SCRIPT_SIMPLEX (6) - Hand-writing style font
  • FONT_HERSHEY_SCRIPT_COMPLEX (7) - More complex variant of SCRIPT_SIMPLEX
  • FONT_ITALIC (16) - Flag for italic font

MarkerTypes

Marker types used for the drawMarker function:
  • MARKER_CROSS (0) - A crosshair marker shape
  • MARKER_TILTED_CROSS (1) - A 45 degree tilted crosshair marker shape
  • MARKER_STAR (2) - A star marker shape
  • MARKER_DIAMOND (3) - A diamond marker shape
  • MARKER_SQUARE (4) - A square marker shape
  • MARKER_TRIANGLE_UP (5) - An upwards pointing triangle marker shape
  • MARKER_TRIANGLE_DOWN (6) - A downwards pointing triangle marker shape