Opencv Tutorial

OpenCV is a computer vision library widely used with many functions that enable a computer program to “see” and to make decisions based on what it finds. This tutorial will introduce the basic types of OpenCV, how to use them and how to do some simple image transforms, all with documented sample C code. After reading you should be able to compile an OpenCV program, to open an image as gray scale, smooth it, save the result as another image and show it on the screen.

Installing OpenCV

To install OpenCV visit its site and download the appropriate version for your system. If you are a Windows user, some people reported problems with the 2.0 version, so you should install 1.0 if the problem isn’t solved yet. Then install it to the default location on your system.

I’ll give instructions to use the gcc compiler. If you use Windows, you can download it on the Mingw project. We’ll also use the pkg-config program. There is also a Windows version available under the Third Party Dependencies section. Under linux, both gcc and pkg-config should be available in the default install.

Compiling OpenCV programs

The easiest way to compile OpenCV programs is to the pkg-config to give us the necessary flags to gcc. To use pkg-config we pass two flags cflags and libs and the name of the library. This will return the flags needed by gcc to compile a program using that library. Try executing the command below. It will print the flags to compile a program using OpenCV with gcc.

pkg-config –libs –cflags opencv

To pass the result of pkg-config as arguments to gcc we write the command between ` `. The command line to compile a prog.c program would be:

gcc `pkg-config –libs –cflags opencv` prog.c -o prog

Now we can code our first program using OpenCV

The First OpenCV program

Our first OpenCV program in this tutorial will simply open an image and save a copy of it as result.png. Then we will expand this program to apply some simple filter to the image. We will need to learn about the IplImage type and two functions, cvLoadImage and cvSaveImage.

IplImage is an image in OpenCV. It has some important attributes: width, height, nChannels, depth and imageData (another would be widthStep. Its used with imageData to access a pixel in the image). But we will use it as a container for loading and storing an image. The basic OpenCV functions are in the cv.h header.

Both cvLoadImage and cvSaveImage are from HighGUI, an OpenCV module that contains some user interface functions, access to cameras and some filesystem facilities. To use it we have to include the highgui.h header.

So let’s begin our first OpenCV program including the headers required and starting our main function:

#include int main(int argc, char *argv[]) {

Then we declare an image and load the file passed as the first argument to our program (if you don’t know about command line arguments click here). The second argument of cvLoadImage is a flag indicating if we want a color image. Passing 0 gets us an automagically converted gray scale image.

     IplImage *img;
     img = cvLoadImage(argv[1], 0);

With this we only need to save the image with another name return:

     cvSaveImage("result.png", img);
     return 0;

The source code for this example is here. First you need to compile it using “gcc `pkg-config –libs –cflags opencv` prog1.c” and run it using “./prog1 (image_to_test) “. A result.png image will appear in the folder of the code with a copy of the image passed as argument.

Gaussian blur filter with OpenCV

Now we will apply a Gaussian Blur to the image. This filter makes the image blurry and is used as preprocessing in some image processing procedures. We will do this filtering in-place, witch means that the original image will not be available after our smoothing operation.

To do the Gaussian blur we use the cvSmooth function with the CV_GAUSSIAN flag. (Note that are other types of smoothing and that they can or cannot be applied in place) This function receives 7 values: the input image, the output image, the type of smoothing and 4 parameters. In our case, the input and output image are the same, the type is CV_GAUSSIAN and only three arguments are used: the two first are the size of the kernel to use and the third is the standard deviation of the Gaussian. If you don’t know what this means, just think that param1 and param2 are the size of the window to blur and param3 should always be zero (this way the value is obtained from the kernel size). We will apply a 7×7 Gaussian blur on the image, so the function call is this:

cvSmooth(img, img, CV_GAUSSIAN, 7, 7, 0, 0);

Insert the line above before the cvSaveImage function, save it, compile and run the program. The result.png image now should be a blurred version of the input image. The code is here.

Showing the image in a window with HighGUI

Its good to save the result image of the smoothing, but it would be nicer if we showed the image to the user when the smoothing is completed. Luckily, instead of having to deal with some specifics on how to code windows on your operating system, the OpenCV module HighGUI does this for us. It offers a simple set of os independent functions to show images on a window. We will use 3 of them: cvNamedWindow, cvShowImage and cvWaitKey. The first creates a window to show images on, the second shows an image on some window and the third freezes the program waiting for a key and is used to let the user control when to close the window. Lets complete the example above by inserting the following code below cvSaveImage:

    cvNamedWindow("Smoothing", 1);
    cvShowImage("Smoothing", img);
    cvWaitKey(-1); // wait forever for a key

The code of this example is here.


This is a simple example of how to use OpenCV, but if you are able to run this then you can run any other OpenCV program. You can find more advanced examples in thesetwo articles. Thanks for reading. If you have any doubts just leave a comment and I’ll answer as soon as possible.