Wednesday, May 11, 2011

Episode_8_Template_Matching_with_WebCam

Template matching is a technique in digital image processing for finding small parts of an image which match a template image. It can be used in manufacturing as a part of quality control, a way to navigate a mobile robot, or as a way to detect edges in images. For more information about Template matching, you can see in http://en.wikipedia.org/wiki/Template_matching.

I use some combination such as mouse handler, img ROI, and template matching.
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"

int c;
IplImage *frame,*img, *img1;
IplImage *tpl;
IplImage *res;
double minval, maxval;
int frame_width, frame_height;
int tpl_width, tpl_height;
int res_width, res_height;
int drag = 0;
CvPoint minloc, maxloc;
CvPoint point;
bool pause = FALSE;
bool crop = FALSE;

void mouseHandler(int event, int x, int y, int flags, void* param)
{
/* user press left button */
if (event == CV_EVENT_LBUTTONDOWN && !drag)
{
point = cvPoint(x, y);
drag = 1;
}

/* user drag the mouse */
if (event == CV_EVENT_MOUSEMOVE && drag)
{
cvDestroyWindow("template");
img = cvCloneImage(frame);
cvRectangle(img,point,cvPoint(x, y),CV_RGB(255, 0, 0),1, 8, 0);
cvCopy(img,frame, NULL);
cvShowImage("reference", img);
pause = TRUE;
crop = FALSE;
}

/* user move the mouse */
if (event == CV_EVENT_MOUSEMOVE && !drag)
{
point = cvPoint(x, y);
printf("x = %d y = %d \n",point.x,point.y);
}

/* user release left button */
int a,b;
if (event == CV_EVENT_LBUTTONUP && drag)
{
img = cvCloneImage(frame);
if ((x == point.x) || (y == point.y)) goto jmp;
if ((point.x > x) && (point.y < y)) goto jmp;
if ((point.x < x) && (point.y > y)) goto jmp;
if ((point.x > x) && (point.y > y)) {
 a = point.x;
 b = point.y;
 point.x = x;
 point.y = y;
 x = a;
 y = b;
}
cvSetImageROI(img,cvRect(point.x,point.y,x - point.x,y - point.y));
cvShowImage("template", img);

/* load template image */
cvSaveImage("Temp.jpg",img);
tpl = cvLoadImage("Temp.jpg");
/* get image's properties */
tpl_width = tpl->width;
tpl_height = tpl->height;
res_width = frame_width - tpl_width + 1;
res_height = frame_height - tpl_height + 1;

cvResetImageROI(img);
jmp:
drag = 0;
pause = FALSE;
crop = TRUE;
}
}

int main(int argc, char* argv[])
{
CvCapture* capture = cvCaptureFromCAM(0);
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_WIDTH, 320 );
cvSetCaptureProperty( capture, CV_CAP_PROP_FRAME_HEIGHT, 240 );

cvNamedWindow( "reference", CV_WINDOW_AUTOSIZE );
cvSetMouseCallback("reference", mouseHandler, NULL);

/* get image's properties */
frame=cvQueryFrame(capture);
frame_width = frame->width;
frame_height = frame->height;

while(1)
{
frame=cvQueryFrame(capture);

if (crop){
 /* create new image for template matching computation */
 res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F, 1 );
 /* choose template matching method to be used */
 cvMatchTemplate( frame, tpl, res, CV_TM_SQDIFF );
 cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );
 /* draw red rectangle */
 cvRectangle( frame,cvPoint( minloc.x, minloc.y ),cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ),cvScalar( 0, 0, 255, 0 ), 2, 0, 0 );
}

if (!pause) cvShowImage( "reference", frame );
/* wait until user press a key to exit */
c=cvWaitKey(10);
// escape key terminates program
if(c == 27)
break;
}
/* free memory */
cvDestroyWindow( "reference" );
cvReleaseImage( &frame );


return 0;
}
This my first video,


1 comments:

Shakir Abdullah said...

thank god I found your blog...
you are my time savior....good work you have done here.
I'm new in opencv, I try to compile your code and I found memory leakage in the code(using method you mention in one of your post). Trying to solve it....and it's done...no more memory leakage

change made:
//-------------------------------------
if (crop)
{
/*
some statement
*/
cvReleaseImage( &res );
/*put this to release the image before closing.*/
}
//-------------------------------------

and release all image that have been create(a good practice to avoid memory leakage)

Post a Comment