This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

normxcorr2

Normalized 2-D cross-correlation

Syntax

C = normxcorr2(template,A)

Description

example

C = normxcorr2(template,A) computes the normalized cross-correlation of the matrices template and A. The resulting matrix C contains the correlation coefficients.

You optionally can compute the normalized cross-correlation using a GPU (requires Parallel Computing Toolbox™). For more information, see Image Processing on a GPU.

Examples

collapse all

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 normxcorr2 adds.

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)]);

Read two images into gpuArrays.

onion   = gpuArray(imread('onion.png'));
peppers = gpuArray(imread('peppers.png'));

Convert the color images to 2-D grayscale. The rgb2gray function accepts gpuArrays.

onion   = rgb2gray(onion);
peppers = rgb2gray(peppers);

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 normxcorr2 adds.

yoffSet = ypeak-size(onion,1);
xoffSet = xpeak-size(onion,2);

Move the data back to the CPU for display.

yoffSet = gather(ypeak-size(onion,1));
xoffSet = gather(xpeak-size(onion,2));

Display the matched area.

figure
imshow(peppers);
imrect(gca, [xoffSet+1, yoffSet+1, size(onion,2), size(onion,1)]);

Input Arguments

collapse all

Input template, specified as a numeric matrix. The values of template cannot all be the same.

To perform the computation using a GPU, specify template as a gpuArray that contains a numeric matrix.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Input image, specified as a numeric image. A must be larger than the matrix template for the normalization to be meaningful.

Normalized cross-correlation is an undefined operation in regions where A has zero variance over the full extent of the template. In these regions, normxcorr2 assigns correlation coefficients of zero to the output C.

To perform the computation using a GPU, specify A as a gpuArray that contains a numeric matrix.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical

Output Arguments

collapse all

Correlation coefficients, returned as a numeric matrix with values in the range [-1, 1].

If the correlation coefficients are computed using a GPU, then C is returned as a gpuArray containing a numeric matrix with values in the range [-1, 1].

Data Types: double

Algorithms

normxcorr2 uses the following general procedure [1], [2]:

  1. Calculate cross-correlation in the spatial or the frequency domain, depending on size of images.

  2. Calculate local sums by precomputing running sums [1].

  3. Use local sums to normalize the cross-correlation to get correlation coefficients.

The implementation closely follows the formula from [1]:

γ(u,v)=x,y[f(x,y)f¯u,v][t(xu,yv)t¯]{x,y[f(x,y)f¯u,v]2x,y[t(xu,yv)t¯]2}0.5

where

  • f is the image.

  • t¯ is the mean of the template

  • f¯u,v is the mean of f(x,y) in the region under the template.

References

[2] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume II, Addison-Wesley, 1992, pp. 316-317.

Extended Capabilities

See Also

|

Introduced before R2006a