| Image Processing Toolbox™ | ![]() |
C = normxcorr2(template, A)
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.
The input matrices can be numeric. The output matrix C is double.
normxcorr2 uses the following general procedure [1], [2]:
Calculate cross-correlation in the spatial or the frequency domain, depending on size of images.
Calculate local sums by precomputing running sums. [1]
Use local sums to normalize the cross-correlation to get correlation coefficients.
The implementation closely follows following formula from [1]:

where
is the image.
is the mean of
the template
is the mean of
in the region under the template.
template = .2*ones(11); % Make light gray plus on dark gray background
template(6,3:9) = .6;
template(3:9,6) = .6;
BW = template > 0.5; % Make white plus on black background
figure, imshow(BW), figure, imshow(template)
% Make new image that offsets the template
offsetTemplate = .2*ones(21);
offset = [3 5]; % Shift by 3 rows, 5 columns
offsetTemplate( (1:size(template,1))+offset(1),...
(1:size(template,2))+offset(2) ) = template;
figure, imshow(offsetTemplate)
% Cross-correlate BW and offsetTemplate to recover offset
cc = normxcorr2(BW,offsetTemplate);
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (ypeak-size(template,1)) (xpeak-size(template,2)) ];
isequal(corr_offset,offset) % 1 means offset was recovered
[1] Lewis, J. P., "Fast Normalized Cross-Correlation," Industrial Light & Magic
[2] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume II, Addison-Wesley, 1992, pp. 316-317.
![]() | nlfilter | ntsc2rgb | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |