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 – Chapter 04


by Lahiru Lakmal Priyadarshana



We apologize our readers for the long silence of openCV article series. After four months the OpenCV article series is back and we hope it is high-time to break the ice!

We believe that you could remember we discussed how to load a video file using OpenCV HighGUI library at the end of the Chapter 03. In that, OpenCV loaded the frames/images one by one and displayed them inside a named window (cvNamedWindow), and since the process repeats inside a while loop we saw it as if a video was running.

May be, you could remember nothing from the 3rd article and couldn’t figure out the head or tail of what we were talking. Don’t worry, because this is common for most of us, just have a quick look at the last article [http://www.digit.lk/09_april_Imageproccesing] and refresh your memory. 

Moving in to today’s article we will be discussing how to add a time slider to our video program and will illustrate some very simple image thresholding program as a kick start to our image processing journey!


4.1 Adding a Slider Bar to the Video Program

We know that HighGUI provides a number of functionalities to work with images rather than simple display functions. One of the most useful utility is the trackbar.

To create a slider, we call cvCreateTrackbar() and indicate which window we want the trackbar to appear in. In order to obtain the desired functionality, we need to supply a callback function that will perform the relocation. Once the user changes the slider value, callback function is called and then passed to the slider’s new value. Refer the following source code.


/**

* Video player with a slider

*

**/

 

#include <cv.h>

#include <cxcore.h>

#include <highgui.h>

 

int g_slider_position = 0;

CvCapture* g_capture = NULL;

 

void onTrackbarSlide(int pos) {

 

      cvSetCaptureProperty(g_capture, CV_CAP_PROP_POS_FRAMES, pos);

}

 

int main() {

 

      cvNamedWindow( "Video", CV_WINDOW_AUTOSIZE );

 

      g_capture = cvCreateFileCapture( "samplevideo.avi" );

     

      int frames = (int) cvGetCaptureProperty(

                              g_capture,

                              CV_CAP_PROP_FRAME_COUNT );

 

      if( frames!= 0 ) {

 

            cvCreateTrackbar(

                  "Position",

                  "Video",

                  &g_slider_position,

                  frames,

                  onTrackbarSlide

            );

 

      }

 

      IplImage* frame;

 

      while(1) {

            frame = cvQueryFrame( g_capture );

            if( !frame ) break;

            cvShowImage( "Video", frame );

            char c = cvWaitKey(33);

            if( c == 27 ) break;

      }

 

      cvReleaseCapture( &g_capture );

      cvDestroyWindow( "Video" );

}

 

Please note that here we have modified the source code from the last chapter that we used to load a video file.

int g_slider_position = 0; is our global variable to track the slider’s value. When the slider is moved and the value is changed, the value of this variable will be automatically updated. It represents the current value of the slider.

 

onTrackbarSlide(int pos) works as the callback function of the slider. Once the slider is moved and the value is changed, this function is called automatically.

 

The function cvCreateTrackbar() is used to create the trackbar and place it inside the named window we created. It allows us to give a label to the trackbar as the first parameter, which is “Position” in this example. Second parameter represents the named window that we want the trackbar to appear in. Third parameter is the variable that will be bound with the trackbar’s current value and finally, the fourth parameter is the callback function. You can pass NULL as the callback function if you do not wish to use one. In that case you can use the variable that we bound to the trackbar (g_slider_position ) to read its current value.

 

cvCreateTrackbar(

                  "Position",

                  "Video",

                  &g_slider_position,

                  frames,

                  onTrackbarSlide

            );

 

Once you run the program you might notice that the slider position will not be updated as the video plays. You can try to add this feature to the program as an exercise.

 

 

4.2 Simple Image Transformation – Thresholding an Image

Since we know how to add a trackbar to a named window and its applicability, we can try out a simple image thresholding with OpenCV. Refer the following source code.

 

/**

* Image Threshold with a Trackbar.

*

**/

 

#include <cv.h>

#include <cxcore.h>

#include <highgui.h>

 

int g_treshold = 0;

 

int main() {

 

      cvNamedWindow( "Example", CV_WINDOW_AUTOSIZE );

 

      IplImage* srcimg = cvLoadImage( "testimage.jpg", 0 );

      IplImage* dstimg = cvCreateImage( cvGetSize(srcimg), srcimg->depth, 1);

 

      cvCreateTrackbar(

                  "Treshold",

                  "Example",

                  &g_treshold,

                  255,

                  NULL

      );

 

      while(1) {

 

            cvThreshold(srcimg, dstimg, g_treshold, 255, CV_THRESH_BINARY);

            cvShowImage("Example", dstimg);

 

            char c = cvWaitKey(33);

            if( c == 27 ) break;

      }

 

      cvReleaseImage(&srcimg);

      cvReleaseImage(&dstimg);

      cvDestroyWindow("Example");

}

 

 

Here, we create a trackbar; labelled as “Threshold” with the max value of 255 inside the “Example” window. We do not need a callback function because; we read the value of g_treshold inside the while loop.

 

The function cvThreshold will do the thresholding of our image. Thresholding is simply rejecting pixels that are below or above some given value while keeping others. It is a simple yet important transformation for lot of complex image processing applications.

 

cvThreshold(srcimg, dstimg, g_treshold, 255, CV_THRESH_BINARY);

 

The first parameter is the source image that we need to apply a threshold. Second parameter is the destination image; the one we use to store the resulted image. We can pass the threshold-value as the third parameter, and according to the threshold type (as in the fourth parameter) the function rejects pixels that are either above or below the threshold-value. Depending on the relationship between the source pixel and the threshold-value, the value of the destination pixel might change. We will discuss more about this relationship and threshold types in our future articles. For the moment, please note that in this example, for every source pixel that is higher than the threshold-value the correspondent destination pixel will be the max value (255 - fourth parameter), or the destination pixel will be 0.

 

Here is a screenshot of our threshold program. Try it out until we meet next month. Happy hacking!

 

References:

Bradski, G., and Kaehler, A. (2008 ). Learning OpenCV. O'Reilly Media, Inc.

 






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