Chapter 6. Drawing and Annotating

Drawing Things

We often want to draw some kind of picture, or to draw something on top of an image obtained from somewhere else. Toward this end, OpenCV provides a menagerie of functions that will allow us to make lines, squares, circles, and the like.

OpenCV’s drawing functions work with images of any depth, but most of them affect only the first three channels—defaulting to only the first channel in the case of single-channel images. Most of the drawing functions support a color, a thickness, a line type (which really refers to whether to anti-alias lines), and subpixel alignment of objects.

When you specify colors, the convention is to use the cv::Scalar object, even though only the first three values are used most of the time. (It is sometimes convenient to be able to use the fourth value in a cv::Scalar to represent an alpha channel, but the drawing functions do not currently support alpha blending.) Also, by convention, OpenCV uses BGR ordering1 for converting multichannel images to color renderings (this is what is used by the draw function imshow(), which actually paints images onto your screen for viewing). Of course, you don’t have to use this convention, and it might not be ideal if you are using data from some other library with OpenCV headers on top of it. In any case, the core functions of the library are always agnostic to any “meaning” you might assign to a channel.

Line Art and Filled Polygons

Functions that draw lines of one ...

Get Learning OpenCV 3 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.