You are here : matlabImage Processingnormxcorr2

normxcorr2() - Image Processing

C = normxcorr2(template, A) computes
the normalized cross-correlation of the matrices template and A.
The matrix A must be larger than the matrix template for
the normalization to be meaningful. The values of template cannot
all be the same. The resulting matrix C contains
the correlation coefficients, which can range in value from -1.0 to
1.0.gpuarrayC = normxcorr2(gpuarrayTemplate,
gpuarrayA) performs the normalized cross-correlation operation
on a GPU.


Syntax

C = normxcorr2(template, A)gpuarrayC = normxcorr2(gpuarrayTemplate,
gpuarrayA)


Example

Use Cross-Correlation to Find Template in ImageOpen This Example
Read images into the workspace and display them side-by-side.onion   = rgb2gray(imread('onion.png'));
peppers = rgb2gray(imread('peppers.png'));
imshowpair(peppers,onion,'montage')

Perform cross-correlation and display result as surface.c = normxcorr2(onion,peppers);
figure, surf(c), shading flat

Find peak in cross-correlation.[ypeak, xpeak] = find(c==max(c(:)));
Account for the padding that normxcorr2 adds.yoffSet = ypeak-size(onion,1);
xoffSet = xpeak-size(onion,2);
Display matched area.hFig = figure;
hAx  = axes;
imshow(peppers,'Parent', hAx);
imrect(hAx, [xoffSet+1, yoffSet+1, size(onion,2), size(onion,1)]);

Use cross-correlation to find template in image on a GPURead images into gpuArrays.onion   = gpuArray(imread('onion.png'));
peppers = gpuArray(imread('peppers.png'));
Convert the color images to 2-D. The rgb2gray function
accepts gpuArrays.onion   = rgb2gray(onion);
peppers = rgb2gray(peppers);
Perform cross-correlation and display result as surface.c = normxcorr2(onion,peppers);
figure, surf(c), shading flat
Find peak in cross-correlation.[ypeak, xpeak] = find(c==max(c(:)));Account for the padding that normxcorr2 adds. yoffSet = ypeak-size(onion,1);
xoffSet = xpeak-size(onion,2);Move data back to CPU for display.yoffSet = gather(ypeak-size(onion,1));
xoffSet = gather(xpeak-size(onion,2));
Display matched area.hFig = figure;
hAx  = axes;
imshow(peppers,'Parent', hAx);
imrect(hAx, [xoffSet, yoffSet, size(onion,2), size(onion,1)]);


Output / Return Value


Limitations


Alternatives / See Also


Reference