4

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.

MUSTY
  • 55
  • 1
  • 1
  • 5
  • 1
    Can you post some code? And maybe some images you're using? – dom Jun 19 '12 at 06:23
  • 1
    What do you mean by marker? Is it any particular colored object? or can you make it so? Then you won't need template matching. just color extraction is sufficient. If you have a screen shot, upload it in imageshack.us, and provide its link here. – Abid Rahman K Jun 19 '12 at 06:25
  • here is the link for the image http://imageshack.us/photo/my-images/266/piggycard.jpg/ actually this is a image card. im making a educational tool for children.There are more image cards which holds unique markers. so from the marker im going to identify the image card(i thought detecting a marker is easier than detecting the whole image card or which way is Better? im new for this). Thank you for your consideration. – MUSTY Jun 21 '12 at 03:07

1 Answers1

2

There is an OpenCV tutorial on the subject of Template Matching.

Using matchTemplate is a good start, it will provide you with an image containing numbers relating to your matching metric (there is a range of choices for the metric, some of which provide high numbers for better matches, some lower).

To subsequently pick out the best match, you will also need to use the function minMaxLoc which can locate the minimum & maximum values from this matrix.

Chris
  • 8,030
  • 4
  • 37
  • 56