Funding for 'IT Lab' Project, Phase 1: Progress of sticker sales. Purchase a sticker to help us reach our target.Updated: 2010-02-28 11:53
10.7%
Image Processing with OpenCV




by
Lahiru Lakmal Priyadarshana

In the previous article we discussed how to install OpenCV and how to create a ‘Hello World’ project using two popular IDEs. So we hope you have already tried the ‘Hello World’ project and looking forward to do more interesting stuff with OpenCV.

Today, let’s go through the ‘Hello World’ program line by line and take a moment to understand what each command is doing.

Hello World

#include <cv.h>

#include <cxcore.h>

#include <highgui.h>

int main()

{

IplImage* img = cvLoadImage("sample.png");

cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);

cvShowImage("Image", img);

cvWaitKey(0);

cvDestroyWindow("Image");

cvReleaseImage(&img);

return 0;

}

 IplImage* img = cvLoadImage("sample.png");

This line loads the image which is named “sample.png”. In order to specify the image using the name as above, it must be saved in the project execute path.

The function cvLoadImage() is a high-level routine that determines the file format to be loaded based on the file name; it also automatically allocates the memory needed for the image data structure. This high level function can read a wide variety of image formats, including BMP, DIB, JPEG, JPE, PNG, PBM, PGM, PPM, SR, RAS, and TIFF.

A pointer to an allocated image data structure is then returned. This structure, called IplImage, is the OpenCV construct with which we will deal the most. OpenCV uses this structure to handle all kinds of images: single-channel, multichannel, integer-valued, floating-point-valued, etc. We use the pointer that cvLoadImage() returns to manipulate the image and the image data.

 cvNamedWindow("Image", CV_WINDOW_AUTOSIZE);

 cvNamedWindow() is another high-level function provided by the HighGUI library (let’s discuss more about these libraries later). This function opens up a window which can be used to display an image. It also assigns a name to the window so later we can refer that window by its name. In this example we named our first window as “Image”. We can use this method in our program to create more than one window; if we have several images to show up.

The second argument of cvNamedWindow() defines the  properties of the window. CV_WINDOW_AUTOSIZE defines that the window will expand or contract automatically when an image is loaded so as to accommodate the image’s true size.

cvShowImage("Image", img);

 We use this function to display our image (img) inside the window that we created using the previous command. We refer the image using the variable name ‘img’ and refer the window using the name that we given at the time of its creation (‘Image’).

cvWaitKey(0);

 The cvWaitKey()function asks the program to stop and wait for a keystroke. If a positive argument is given, the program will wait for that number of milliseconds and then continue even if nothing is pressed. If the argument is set to 0 or to a negative number, the program will wait indefinitely for a key-press.

cvDestroyWindow("Image");

The function cvDestroyWindow() will close the window and de-allocate any associated memory usage.

cvReleaseImage(&img);

 Once we are through with an image, we can free the allocated memory. OpenCV expects a pointer to the IplImage* pointer for this operation. After the call is completed, the pointer img will be set to NULL.

For a simple program, we don’t really have to call cvDestroyWindow() or cvReleaseImage() because all the resources and windows of the application are closed automatically by the operating system upon exit, but it’s a good habit anyway. J

   

It’s time for a video!

In OpenCV loading a video is almost as simple as loading a single image. The following program will load an AVI video file from the hard disk and play the video within the while loop.

#include <cv.h>

#include <cxcore.h>

#include <highgui.h>

int main() {

cvNamedWindow( “Video”, CV_WINDOW_AUTOSIZE );

CvCapture* capture = cvCreateFileCapture( “video1.avi” );

IplImage* frame;

while(1) {

frame = cvQueryFrame( capture );

if( !frame ) break;

cvShowImage( “Video”, frame );

char c = cvWaitKey(33);

if( c == 27 ) break;

}

cvReleaseCapture( &capture );

cvDestroyWindow( “Video” );

}

First, we create a named window using the cvNamedWindow()function, here it is named as “Video”.

CvCapture* capture = cvCreateFileCapture( “video1.avi” );

This function takes as its argument the name of the AVI file to be loaded and then returns a pointer to a CvCapture structure. This structure contains all of the information about the AVI file being read, including state information. When created in this way, the CvCapture structure is initialized to the beginning of the AVI.

frame = cvQueryFrame( capture );

Once inside of the while(1) loop, we begin reading from the AVI file.

cvQueryFrame() takes as its argument a pointer to a CvCapture structure. It then grabs the next video frame into memory. A pointer is returned to that frame. ‘frame’ is an IplImage* of type.

c = cvWaitKey(33);

if( c == 27 ) break;

Once we have displayed the frame, we then wait for 33 ms.  If the user hits a key, then ‘c’ will be set to the ASCII value of that key; if not, then it will be set to –1. If the user hits the Esc key which has ASCII value of 27, the loop will be stopped. Otherwise, 33 ms will pass and we will just execute the loop again.

cvReleaseCapture( &capture );

When we have exited the read loop (because there was no more video data or because the user hit the Esc key) we can free the memory associated with the CvCapture structure with the above function. This will also close any open file handles to the AVI file.

 In the next article let’s discuss how to add a time slider to our video program and about a simple image transformation. Till then, happy hacking!

                                                                                                              Previous article

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options