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

4 visualizaciones (últimos 30 días)
Maia2022 el 14 de Sept. de 2022
Respondida: Image Analyst el 6 de Sept. de 2023
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.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Respuestas (2)

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
Hope this helps!
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Image Analyst el 6 de Sept. de 2023
See attached demo.
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Image Quality en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by