Normalized 2-D cross-correlation
Read two images into the workspace, and convert them to grayscale for use with
normxcorr2. Display the images side-by-side.
onion = rgb2gray(imread('onion.png')); peppers = rgb2gray(imread('peppers.png')); imshowpair(peppers,onion,'montage')
Perform cross-correlation, and display the result as a surface.
c = normxcorr2(onion,peppers); figure, surf(c), shading flat
Find the peak in cross-correlation.
[ypeak, xpeak] = find(c==max(c(:)));
Account for the padding that
yoffSet = ypeak-size(onion,1); xoffSet = xpeak-size(onion,2);
Display the matched area.
figure imshow(peppers); imrect(gca, [xoffSet+1, yoffSet+1, size(onion,2), size(onion,1)]);
template— Input template
Input template, specified as a numeric matrix. The values of
cannot all be the same.
A— Input image
Input image, specified as a numeric image.
A must be larger than the
template for the normalization to be
Normalized cross-correlation is an undefined operation in regions where
has zero variance over the full extent of the template. In these regions,
normxcorr2 assigns correlation coefficients of zero
to the output
C— Correlation coefficients
Correlation coefficients, returned as a numeric matrix with values in the range [-1, 1].
Calculate cross-correlation in the spatial or the frequency domain, depending on size of images.
Calculate local sums by precomputing running sums .
Use local sums to normalize the cross-correlation to get correlation coefficients.
The implementation closely follows the formula from :
is the image.
is the mean of the template
is the mean of in the region under the template.
 Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume II, Addison-Wesley, 1992, pp. 316-317.
This function fully supports GPU arrays. For more information, see Image Processing on a GPU.