OpenCV 2.0 Tutorial 01

For research purpouses I needed to get to know the openCV 2.0 library in detail, and they’ve recently introduced a C++ interface. There was no real tutorial, so I’m starting one myself. Keep in mind that I am still learning the computer vision stuff myself, but I’ll be as clear as I can be at everything I use.

I’m using the Visual Studio 9 compiler and the focus for now is the openCV library, although there will be some work done with the VideoInput library and OpenSceneGraph.

I prefer to work with stuff that you can see working, so let’s start with a trivial program that reads and displays a single image:

#include 
#include 

void main() {
 cv::Mat& loadedImage = cv::imread("picture.jpg", 1);
 cv::namedWindow("picture window", CV_WINDOW_AUTOSIZE);
 cv::imshow("picture window", loadedImage);
 cv::waitKey(0);
}

Nice and simple, although some details may be of interest. The picture is stored in loadedImage through the use of the imread function. The function takes a filename and a flag and produces a Mat object. There are three possible flags, which are used to determine the color type of the resulting Mat. A flag of 0 results in a grayscale image, anything above 0 results in a 3-channel color image, and anything below 0 retains the original image format. Note that if your image has an additional alpha channel (RGBA format or similar), you will need to use a negative flag to actually load it correctly. If the loading fails for whatever reason, it returns an empty Mat, which has a NULL data field. By default openCV supports many common file formats, such as .bmp, .jpg, .png, .ppm and more.

Also, the waitKey function is slightly misleading, as it does more than just wait for keys. It also handles the window message pump, enabling ‘default behaviour’ for the window. The argument indicates the number of milliseconds the function waits for a key, with 0 indicating an indefinate wait. The return value of waitKey is equal to the keycode of the pressed key, or -1 if no key was pressed in the alotted time. This means that for example a result of 27 means that the escape key was pressed. This type of setup makes it convenient to create simple keyboard interactions, but some key (combinations) may give you trouble (for example pressing the shift alone doesn’t register anything).

Let’s leave it at that for this one. If you get this to work, you’re ready to go and explore a bit. Next time, live video input.

2 thoughts on “OpenCV 2.0 Tutorial 01

  1. Hi there!

    I’m using Visual Studio as well for openCV but i’m encountering some problems. When I compile the code in debug mode, everything works fine. But when I compile it in release mode, I get loads of errors. The .exe file created by compiling in debug mode can’t run on other computers. Any idea what I should do to make my applications run on other computers?

    Kind regards,
    Henk

    1. Visual studio maintains separate settings for debug and release mode, including library and include paths – the errors in release mode probably come from there. Running the executable on other computers usually requires the visual c++ runtime to be installed. Depending on which version of visual studio you need a different one, but I’m guessing you need [this one].

      Hope this helps,
      – Gijsbert

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.