How to get the coordinates of the peak from normxcorr2 between a ROI and a template

3 visualizaciones (últimos 30 días)
Hi,
I am using normxcorr2() between a ROI and a binary template, e.g.
template = [ 0 1 0 0 ; 0 1 1 1; 0 1 0 0; 1 1 1 1]
My ROI is cropped from a full image with this bounding box :
BB = [x,y,w,h];
BB is the size of the bounding box of the ROI.
subImage = FullImage(BB(2):BB(2)+BB(4)-1, BB(1):BB(1)+BB(3)-1);
The size of the subImage is the same as the template.
I compute the correlation image between subImage, template:
subCorrelationImage = normxcorr2(subImage, template);
The resulting size of this subCorrelationImage is not the same as subImage and template size.
I'd like to get position of the maximum of the correlation in the full image.
I have:
[ypeak, xpeak] = find(subCorrelationImage==max(subCorrelationImage(:)));
I tried:
ypeak = ypeak + y
xpeak = xpeak + x
But it does not work.
Thank you for your help.

Respuestas (2)

Neha
Neha el 6 de Sept. de 2023
Hi,
I understand that you want to obtain the position of the maximum correlation of the full image. After finding the peak, you also need to account for the padding that "normxcorr2" adds. Here's an updated version of your code that correctly calculates the position of the maximum correlation:
% Compute the correlation image between subImage and template
subCorrelationImage = normxcorr2(template, subImage);
% Calculate the position of the maximum correlation in subCorrelationImage
[ypeak, xpeak] = find(subCorrelationImage == max(subCorrelationImage(:)));
% Adjust the position to the full image coordinates
y = ypeak - size(template, 1);
x = xpeak - size(template, 2);
% y and x are the positions of the maximum correlation in the full image
You can refer to the following documentation link for more information on "normxcorr2":
Hope this helps!

Image Analyst
Image Analyst el 6 de Sept. de 2023
See attached demo.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by