# matchTemplate function, result processing

I'am using opencv for some project. And have a task which should be solved. Task is quite simple. I have a main picture and have a template(s) than I compare main pic with template. I use matchTemplate() function. I'am just curiosity about one moment. In documentation I found following info: To identify the matching area, we have to compare the template image against the source image by sliding it. By sliding, we mean moving the patch one pixel at a time (left to right, up to down). At each location, a metric is calculated so it represents how “good” or “bad” the match at that location is (or how similar the patch is to that particular area of the source image). For each location of T over I, you store the metric in the result matrix (R). Each location in R contains the match metric.

So, for example you have the main picture and the template. And you know that there is one entry of template at least in main pic. And it works fine. Also it works fine if you have more then one entry, but you still know how many entries do you have. But if you do not know how many templates are in the main picture 0 or 10? Is any way to calculate it ? Which algorithm should be used? Thanks in advance.

edit retag close merge delete

Sort by » oldest newest most voted

You have to filter your answer. For example, don't used minMaxloc, but process your image manually. Otherwise, you can use a binary mask (greater than a threshold it is your answer) and find all one in the binary image. The threshold isn't easy to find (at one or two pixels of the better values, the value in the answer is high too), optionally, you could find only center of connected components in your binary result.

more

Thanks for answer. As I understood, I should to find this threshold by myself with experimentally way? And still got a chance to have false-positive reaction? Could you please explain a little bit more about method of finding centers of connected components?

( 2013-02-11 08:15:14 -0600 )edit

Suppose we have a linear results (easy to explain) like that: r=[0,0.1,0.4,0.9,1,0.9,0.3,0,0.3,0.9,0.4] Obviously, your centers are 1 and 0.9 at r[4] and r[9]. If you use the threshold >= 0.9 to have all results, then your have 4 different results (r[3],r[4],r[5],r[9]). You see that r[3-5] are contiguous. Then you select the center. Center can be selected by average, means, center of mass,... all you want. You know the size of your template, so use it (for example find center in a window of size (templateX,templateY). You can use a manual approach or any machine learning approach to find the center (the first one will be easiest...).

( 2013-02-11 10:26:23 -0600 )edit

more

I think I found a easy way. For example i use this peace of code

       img = cv2.imread("main.bmp", cv.CV_LOAD_IMAGE_GRAYSCALE)
result = cv2.matchTemplate(img,template,cv.CV_TM_SQDIFF)
(min,max,minloc,maxloc) = cv2.minMaxLoc(result)
(x,y) = minloc


But it's not enough. How I can get from result all values and coordinetes one by one from min to max ?

( 2013-02-14 09:20:38 -0600 )edit

Official site

GitHub

Wiki

Documentation