matchTemplate Resultspace Padding

asked 2017-06-22 02:20:49 -0600

Franz Kaiser gravatar image

updated 2017-06-22 05:57:48 -0600

Hello everyone, in the matchTemplate function there is a picture illustrating the slinding-window-approach (the one with the two black arrows). First of all: as I understand the code, the coordinates in this pictures are not right, are they? I came to this assumption, because the result-variable contains the match between left upper corner from template with left upper corner from queryimage (not the center of template with left upper corner from query as shown in the picture). If this is correct it means that the whole template has to fit on the searchimage. A situation as shown (half dogs had correlate to nothing) is not possible. Could someone confirm this assumption, please?
edit: see @berak 's post. I also did an example with should explain the situation: see the pictures below where I used berak's suggested cv::copyMakeBorder. This solution leads me to the following Question: What kind of borders seems to be the more general sultion? A black or a borderInterpolate?

I also worked with the matlab version of the normalized cross-correlation normxcorr2. In this function the result space is in the dimensions of (rows.template + rows.query +1) x (cols.template + cols.query +1). Can you give me a hint, if or how such an output is possible as well with openCV? could adding a black border do the trick? -> it does see the pictures

Template matching without border Template matching with border Template

edit retag flag offensive close merge delete

Comments

  • result_cols = img.cols - templ.cols + 1; (- , not +, same for rows)
  • the 2nd image is slightly misleading, as we have to scan the image using the top-left corner of the template image, too (not the center, as shown in the image)
berak gravatar imageberak ( 2017-06-22 02:28:05 -0600 )edit

Thank you, berak. The dimension you mentioned are for opencv, i was referring to the matlab implementation. Is the result i am trying to get clear?

Franz Kaiser gravatar imageFranz Kaiser ( 2017-06-22 02:47:57 -0600 )edit

why would that differ ? (unless they're padding) also, where does it say so ?

you could use copyMakeBorder for the padding

berak gravatar imageberak ( 2017-06-22 02:51:27 -0600 )edit

Which flags do you use for matchTemplate ?

LBerger gravatar imageLBerger ( 2017-06-22 04:24:03 -0600 )edit

At the moment i work with CV_TM_SQDIFF_NORMED

Franz Kaiser gravatar imageFranz Kaiser ( 2017-06-22 05:56:47 -0600 )edit

I think you should use an image an a template with zero means

LBerger gravatar imageLBerger ( 2017-06-22 07:14:21 -0600 )edit

@LBerger, I am sorry, but I don't really understand what you mean by "template with zero means". Is it a constant black border around the image, or something I have do do with the template?

Franz Kaiser gravatar imageFranz Kaiser ( 2017-06-23 02:20:49 -0600 )edit

Before processing a signal x, we offen transform it in xe such that : xe = x - mean(x)

LBerger gravatar imageLBerger ( 2017-06-23 02:46:00 -0600 )edit