为了说明如何使用
normxcorr2,请看下面的示例(改编自
这个页面)。
template = 0.2*ones(11);
template(6,3:9) = 0.6;
template(3:9,6) = 0.6;
BW = single(template > 0.5);
imtool(template, 'InitialMagnification','fit')
offsetTemplate = 0.2*ones(81);
offset = [30 50];
offsetTemplate( (1:size(template,1))+offset(1), ...
(1:size(template,2))+offset(2) ) = template;
imtool(offsetTemplate, 'InitialMagnification',400)
cc_norm = normxcorr2(BW, offsetTemplate);
imtool(cc_norm, 'InitialMagnification',400)
[max_cc_norm, imax] = max( abs(cc_norm(:)) );
[ypeak, xpeak] = ind2sub(size(cc_norm), imax(1));
corr_offset = [ (ypeak-size(template,1)) (xpeak-size(template,2)) ];
fprintf('Input offset: %d,%d\nRecovered offset: %d,%d\n', offset, corr_offset)