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);
Post new comment