Thursday, January 1, 2009

OpenCV Image Processing - Template Matching

Matching an external file with video optained

//Standard Header
#include "stdafx.h"
//OpenCV header
#include "cv.h"
#include "highgui.h"

//Start of main function
int main( int argc, char** argv )
{
//Enable camera input
CvCapture* capture = cvCreateCameraCapture(0);
//Terminate Program if camera feed is not available
assert( capture != NULL );
//Create Window named Output
cvNamedWindow( "Output", CV_WINDOW_AUTOSIZE );

//Pointers for tracker
double m,M;
IplImage* image; IplImage* icon;
CvPoint point1;
CvPoint point2;

//continuous loop to obtain frames from the cam and display at output window
while(1)
{
//Bring each frame to image variable
image = cvQueryFrame( capture );
//If no input then terminate
if( !image )
break;
//Tracking Procedures
//Load tracking image
icon=cvLoadImage("a.jpg",1);
int resultW = image->width - icon->width + 1;
int resultH = image->height - icon->height +1;
IplImage* result = cvCreateImage(cvSize(resultW, resultH), IPL_DEPTH_32F, 1);
cvMatchTemplate(image, icon, result, CV_TM_SQDIFF);
cvMinMaxLoc(result, &m, &M, &point2, &point1, NULL);
cvRectangle( image, point2, cvPoint( point2.x + icon->width, point2.y + icon->height ), cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );

cvShowImage( "Output", image );
//Delay to wait before returning to the loop to get next frame
char c = cvWaitKey(33);
//If escape ket pressed then terminate
if( c == 27 )
break;
}
//During program end release camera so it could be used by another application
cvReleaseCapture( &capture );
//Close the Output Window created earlier
cvDestroyWindow( "Output" );
}

1 comment:

Anonymous said...

it gives error
"segmentation fault"