In the current r2015a online documentation's example of template matching using normxcorr2 ( mathworks.com/help/images/ref/normxcorr2.html ), the offset of correlation array, because of zero padding is assumed to be the size of template:
yoffSet = ypeak-size(onion,1);xoffSet = xpeak-size(onion,2);
However, when I run through the example on my machine (r2011b), and extract the rectangle matching region of 'peppers' based on the above values …
match = peppers(yoffSet:yoffSet+size(onion,1)-1, xoffSet:xoffSet+size(onion,2)-1);
… the resulting crop is not equal to 'onion'. It is in fact offset by 1*1 pixel. Don't know if this can be caused by my older Matlab version.
From what I can understand from normxcorr2 code and conv2 help, the padding is in fact of width (size(T)-1)/2 on each side for a template with an odd size (for an even template size, the padding is size(T)/2 on the left and top, and size(T)/2 – 1 on the right and bottom). These would be the correct offset values for the central pixel of template (when there is one, i.e. odd template size).
In the end, the offset for the top-left pixel of the template-matching region should rather be size(T) -1. Am I right ?
Best Answer