Visual Servoing Platform  version 3.3.0
Tutorial: How to modify an image to insert basic drawings

Introduction

In this tutorial you will learn how to modify the content of an image adding basic drawings without the need of an image display window. This functionality could be useful if none of the following 3rd parties are available: X11, GDI, OpenCV, GTK, Direct3D.

Modify an image with basic drawings

There is the vpImageDraw class that allows to modify an image by inserting basic drawings like point, circle, line, rectangle, polygon, frame. There is also vpFont class that allows to modify an image to insert text. These classes are used in testImageDraw.cpp.

If you run the corresponding binary:

$ cd $VISP_WS/visp-build/modules/core
$ ./testImageDraw

it will create canvas_color.png and canvas_gray.png images that give a good overview.

  • Content of canvas_color.png image that shows basic drawings inserted in a color image implemented as a vpImage<vpRGBa> is the following:
  • Content of canvas_gray.png image that shows basic drawings inserted in a gray level image implemented as a vpImage<unsigned char> is the following:

Draw a point in an image

The following snippet shows how to modify color image I drawing a red point at pixel location (100, 200).

The following snippet shows how to modify a gray level image I drawing a white point at pixel location (100, 200).

vpImagePoint ip(100, 200);
unsigned char color = 255; // white
vpImageDraw::drawPoint(I, ip, color);

Draw a line between 2 points

The following snippet shows how to modify color image I drawing an orange line with thickness 3 between pixels with coordinates (100, 200) and (300, 400).

vpImage<vpRGBa> I(480, 640);
vpImagePoint ip1(100, 200);
vpImagePoint ip2(300, 400);

The following snippet shows how to modify gray level image I drawing a black line with thickness 3 between pixels with coordinates (100, 200) and (300, 400).

vpImagePoint ip1(100, 200);
vpImagePoint ip2(300, 400);
unsigned char color = 0; // black
vpImageDraw::drawLine(I, ip1, ip2, color, 3);

Draw a circle

The following snippet shows how to modify color image I drawing a green cercle with thickness 3, centered at pixel location (100, 200) and with radius 80 pixels.

vpImage<vpRGBa> I(480, 640);
vpImagePoint ip(100, 200);

The following snippet shows how to modify gray level image I drawing a gray cercle with thickness 3, centered at pixel location (100, 200) and with radius 80 pixels.

vpImagePoint ip(100, 200);
unsigned char color = 128; // gray
vpImageDraw::drawCircle(I, ip, 80, color, 3);

Draw a rectangle

The following snippet shows how to modify color image I drawing a yellow rectangle with thickness 3, with top left corner location (100, 200), and rectangle width and height set to 150, 80 respectively.

vpImage<vpRGBa> I(480, 640);
vpImagePoint ip(100, 200);
int w = 150;
int h = 80;

The following snippet shows how to modify gray level image I drawing a light gray rectangle with thickness 3, with top left corner location (100, 200), and rectangle width and height set to 150, 80 respectively.

vpImagePoint ip(100, 200);
int w = 150;
int h = 80;
unsigned char color = 200; // light gray
vpImageDraw::drawRectangle(I, vpRect(ip, w, h), color, false, 3);

Draw a cross

The following snippet shows how to modify color image I drawing a blue cross with thickness 3, location (100, 200), and size 15 pixels.

vpImage<vpRGBa> I(480, 640);
vpImagePoint ip(100, 200);
int w = 150;
int h = 80;

The following snippet shows how to modify gray level image I drawing a dark gray cross with thickness 3, location (100, 200), and size 15 pixels.

vpImagePoint ip(100, 200);
int w = 150;
int h = 80;
unsigned char color = 50; // dark gray
vpImageDraw::drawCross(I, ip, 15, color, 1);

Insert text in an image

The following snippet shows how to modify color image I drawing "Hello world" in white over a black background at location (100, 200).

vpImage<vpRGBa> I(480, 640);
vpImagePoint ip(100, 200);
font.drawText(I, "Test...", ip, vpColor::white, vpColor::black);

The following snippet shows how to modify gray level image I drawing "Hello world" in white over a black background at location (100, 200).

vpImagePoint ip(100, 200);
unsigned char color = 255; // white
unsigned char background = 0; // black
font.drawText(I, "Test...", ip, color, background);

Next tutorial

You are now ready to see how to continue with Tutorial: Image frame grabbing.

vpImageDraw::drawCircle
static void drawCircle(vpImage< unsigned char > &I, const vpImagePoint &center, unsigned int radius, unsigned char color, unsigned int thickness=1)
Definition: vpImageDraw.cpp:326
vpColor::orange
static const vpColor orange
Definition: vpColor.h:188
vpColor::yellow
static const vpColor yellow
Definition: vpColor.h:186
vpImageDraw::drawPoint
static void drawPoint(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned char color, unsigned int thickness=1)
Definition: vpImageDraw.cpp:869
vpColor::green
static const vpColor green
Definition: vpColor.h:181
vpImageDraw::drawCross
static void drawCross(vpImage< unsigned char > &I, const vpImagePoint &ip, unsigned int size, unsigned char color, unsigned int thickness=1)
Definition: vpImageDraw.cpp:354
vpColor::black
static const vpColor black
Definition: vpColor.h:172
vpImagePoint
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:87
vpColor::white
static const vpColor white
Definition: vpColor.h:173
vpColor::blue
static const vpColor blue
Definition: vpColor.h:184
vpImage< vpRGBa >
vpColor::red
static const vpColor red
Definition: vpColor.h:178
vpImageDraw::drawRectangle
static void drawRectangle(vpImage< unsigned char > &I, const vpRect &rectangle, unsigned char color, bool fill=false, unsigned int thickness=1)
Definition: vpImageDraw.cpp:926
vpRect
Defines a rectangle in the plane.
Definition: vpRect.h:77
vpImageDraw::drawLine
static void drawLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
Definition: vpImageDraw.cpp:840