I am using an image which holds a marker in a specific area. I tried to do it using Template matching which is the method defined in opencv as cvMatchTemplate.
I am using a web cam to detect them, currently the program is detecting the marker, because I provided the same marker as template.
But I cannot find a way to check whether it is the best match or just slightly matched. Because in cvMatchTemplate it is not only detecting the best match, it also keeps detecting the areas which are slightly matching.
Can any one please tell me a way to do this. Or if there is any other way for my problem, please let me know!
here is the link for my image card http://imageshack.us/photo/my-images/266/piggycard.jpg/ (I want to detect and check whether its mached)
here is the code
// template_mching_test_2.cpp : Defines the entry point for the console application. //
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
int main()
{
IplImage* imgOriginal = cvLoadImage("D:\\4Yr\\Research\\SRS\\Animations\\Piggycard.jpg", 0);
IplImage* imgTemplate = cvLoadImage("D:\\4Yr\\Research\\MakingOf\\Sample Imageas\\PiggyMarkerStart.jpg", 0);
CvCapture *cap = cvCaptureFromCAM(0);
if(!cap)
return -1;
cvNamedWindow("result");
IplImage* imgOriginal;
IplImage* imgOriginal2;
IplImage* imgResult;
while(true)
{
imgOriginal = cvQueryFrame(cap);//cvCreateImage(cvSize(imgOriginal->width-imgTemplate->width+1, imgOriginal->height-imgTemplate->height+1), IPL_DEPTH_32F, 1);
imgOriginal2 = cvCreateImage(cvSize(imgOriginal->width,imgOriginal->height),imgOriginal->depth,1);
imgResult = cvCreateImage(cvSize(imgOriginal->width-imgTemplate->width + 1,imgOriginal->height-imgTemplate->height+1),IPL_DEPTH_32F,1);
cvZero(imgResult);
cvZero(imgOriginal2);
cvCvtColor(imgOriginal,imgOriginal2,CV_BGR2GRAY);
cvMatchTemplate(imgOriginal2, imgTemplate, imgResult,CV_TM_CCORR_NORMED);
double min_val=0, max_val=0;
CvPoint min_loc, max_loc;
cvMinMaxLoc(imgResult, &min_val, &max_val, &min_loc, &max_loc);
cvRectangle(imgOriginal, max_loc, cvPoint(max_loc.x+imgTemplate->width, max_loc.y+imgTemplate->height), cvScalar(0), 1);
printf("%f \n", max_val);
cvShowImage("result", imgOriginal);
cvWaitKey(10);
cvReleaseImage(&imgOriginal2);
cvReleaseImage(&imgResult);
}
cvDestroyAllWindows();
cvReleaseCapture(&cap);
return 0;
}
and as the template I provided the same marker which cropped from the original image. From minMaxLoc i took the max value to check the best match. but it is keep giving me the same values when the image marker in a position, And when the image marker is not in the frame and slightly matching at a place which previous matched with the marker.Does minMaxloc giving us the coordinates(position) of the marker or matching percentage.Or is there any other way for this. Thank you for your consideration.