Renato Garcia

Visualizing OpenCV Images on GDB

On debugging programs, be it with GDB or any other debugger, if some variable represents a picture, to inspect it printing its content as a number sequence or character sequence is not that useful.

Except if you can see a woman in red above, that output is not much clarifying about the state of matImg variable.

A little better option than simply to print an image as a byte array, would be a variation of “printf debugging”. Using the functions from OpenCV library as an example, the code bellow will open a window with the image on mat variable, and wait until some key be pressed.

cv::imshow("window", mat);


It’s simple, it’s works, but it has all the same drawbacks of the conventional “printf debugging” method. The ideal solution would be to get the same result as above, but through an internal GDB command.

Trying don’t to reinvent the wheel, I have managed to found a GDB extension, created by Stefano Pellegrini, that implements much of the desired functionality to cv::Mat objects. Great! Starting from this version, I have added IplImage support, ROIs support, and some other features, having now a pretty good way of debugging programs which uses OpenCV images.

Since the extension is installed, will be a new GDB command to show the image in a window.

This extension will register a new GDB command, cv_imshow, which receives as argument a variable containing the image that must be shown. Firstly I had considered to use the pretty printing mechanism in place of to create a new command, but that would not be the better choice. Even restricted to OpenCV image types, not all formats are compatible to be shown. Therefore is yet useful to have at least the traditional GDB print command as a last resource to inspect a variable, if it cannot be graphically inspected.

The source code, as well as the instructions to compile and install the extension can be found at the project github repository.