Why do I get unexpected results when using the NORMXCORR2 function?
2 views (last 30 days)
Show older comments
Why do I get unexpected results when using the NORMXCORR2 function?
For example, I can create a pair of images of a black cross on a light background, and NORMXCORR2 will detect the shift between the two images in the first case (cross is 18 pixels wide). But when I widen the cross (to 38 pixels) NORMXCORR2 indicates that there has not been a shift
between the two images. It is clear that there is a shift from a visual examination of the images. Is there a bug?
Accepted Answer
MathWorks Support Team
on 27 Jun 2009
The function NORMXCORR2 depends on the template being smaller than the image for the normalization to work properly. Here is a test function that shows how NORMXCORR2 can be used to recover an offset. Note that n must be larger than n_template.
%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [corr_offset] = test_plus(n_template,n,coffset,roffset)
%
% n = the number of pixels per side of the image
% n_template = the number of pixels per side in the template
% coffset = offset in columns
% roffset = offset in rows
if (n<n_template)
error('n must be greater than n_template.')
end
% Make a template image containing a plus
template = .6*ones(n_template);
center = floor((n_template - 1)/2);
half_width = floor(.1*n_template);
cmin = center - half_width;
cmax = center + half_width;
template(1:n_template,cmin:cmax) = .2;
template(cmin:cmax,1:n_template) = .2;
imshow(template)
template_size = size(template);
% Make an n-by-n image containing the template shifted by roffset rows
and coffset columns.
J = .6*ones(n);
rbegin = 1 + roffset;
rend = rbegin + template_size(1) - 1;
cbegin = 1 + coffset;
cend = cbegin + template_size(2) - 1;
J(rbegin:rend,cbegin:cend) = template;
figure;
imshow(J)
%cross-correlate the images
cc = normxcorr2(template,J);
%find the correlation peak coordinates
[max_cc, imax] = max(abs(cc(:)));
[ypeak, xpeak] = ind2sub(size(cc),imax(1));
corr_offset = [ (xpeak-template_size(2)) (ypeak-template_size(1)) ];
0 Comments
More Answers (0)
See Also
Categories
Find more on Geometric Transformation and Image Registration in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!