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
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");
cvDestroyWindow("Image");
return 0;
}
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.
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);
cvWaitKey(0);
cvDestroyWindow("Image");
The function cvDestroyWindow() will close the window and de-allocate any associated memory usage.
cvReleaseImage(&img);
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.
Post new comment