How does Normalized Cross Correlation work?
4 views (last 30 days)
Show older comments
I'm trying to understand how NCC works. For example I have used normxcorr2 for template matching. I understand how the size of the correlation image is configured. But I'm trying to figure out how the function works. How does the actual matching occur? And what is the correlation image used for?
Thank you very much for any information.
I have included the figures I have been working with as well as the code. .
.
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/145149/image.png)
.
Here is the code I have used:
%% Normalized Cross Correlation
load('DataSet04.mat');
TargetImage= imread('DataSet04-04.bmp');
figure, imshow(TargetImage), title('Target Image for Matching (size )');
sampleTemplate2= zeros(168, 168);
% Y, X1:X2 Top
sampleTemplate2(5, 5:163) = 255;
% Y, X1:X2 Bottom
sampleTemplate2(163, 5:163) = 255;
% Y1:Y2, X Right
sampleTemplate2(5:163, 163) = 255;
% Y1:Y2 X Left
sampleTemplate2(5:163, 5) = 255;
sampleTemplate2 = imfill(sampleTemplate2, 'holes');
figure, imshow(sampleTemplate2), title('Template used for Matching');
imwrite(sampleTemplate2, 'tmplt2.bmp', 'bmp');
tmplt2= imread('tmplt2.bmp');
%figure, surf(sampleT2), shading flat;
[Rowtmplt, Coltmplt] = size(tmplt2);
CorrelationImage= normxcorr2(tmplt2,TargetImage);
figure, imshow(CorrelationImage), title('Normalized Cross Correlation Image');
[RowCorr, ColCorr] = size(CorrelationImage);
sprintf('Size= %d%d', RowCorr, ColCorr);
[maxCorr, imax] = max(abs(CorrelationImage(:)));
[ypeak, xpeak] = ind2sub(size(CorrelationImage), imax(1));
sprintf('Row = %d%d', ypeak, xpeak);
BestRow = ypeak - (Rowtmplt - 1);
BestCol = xpeak - (Coltmplt - 1);
sprintf(' Best Match %d%d' , BestCol, BestRow);
figure, imshow(TargetImage), title('Template Matched');
h= imrect(gca, [ BestCol BestRow (Coltmplt - 1) (Rowtmplt - 1)]);
Thank You!
% Amanda Johnson
% ap-johnson3@wiu.edu
0 Comments
Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!