title: How to detect and match a marker using OpenCV (Template Matching)
body: <p>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. </p>
  
  <p>I am using a web cam to detect them, currently the program is detecting the marker, because I provided the same marker as template. </p>
  
  <p>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. </p>
  
  <p>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!</p>
  
  <p>here is the link for my image card
  <a href="http://imageshack.us/photo/my-images/266/piggycard.jpg/" rel="nofollow">http://imageshack.us/photo/my-images/266/piggycard.jpg/</a>
  (I want to detect and check whether its mached)</p>
  
  <p>here is the code</p>
  
  <p>// template_mching_test_2.cpp : Defines the entry point for the console application.
  //</p>
  
  <pre><code>#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-&gt;width-imgTemplate-&gt;width+1, imgOriginal-&gt;height-imgTemplate-&gt;height+1), IPL_DEPTH_32F, 1);
  imgOriginal2 = cvCreateImage(cvSize(imgOriginal-&gt;width,imgOriginal-&gt;height),imgOriginal-&gt;depth,1);
  imgResult = cvCreateImage(cvSize(imgOriginal-&gt;width-imgTemplate-&gt;width + 1,imgOriginal-&gt;height-imgTemplate-&gt;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, &amp;min_val, &amp;max_val, &amp;min_loc, &amp;max_loc);
  
  cvRectangle(imgOriginal, max_loc, cvPoint(max_loc.x+imgTemplate-&gt;width,  max_loc.y+imgTemplate-&gt;height), cvScalar(0), 1);
  printf("%f \n", max_val);
  
  cvShowImage("result", imgOriginal);
  
  cvWaitKey(10);
  
  cvReleaseImage(&amp;imgOriginal2);
  cvReleaseImage(&amp;imgResult);
  }
  
  cvDestroyAllWindows();
  cvReleaseCapture(&amp;cap);
  
  return 0;
  
  }
  </code></pre>
  
  <p>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.</p>