I am trying to use normxcorr2 (normalized cross-correlation) from MATLAB for calculating velocity of moving shapes in a developing embryo. I have 3 questions:
1) My image size is 260x360 pixels. I give a template size of 10x10 pixels and I ask the command to search for this template in the subsequent frame in a search window of 50x50 pixels. I get a correlation matrix of size 59x59. So this means that the command moves the template pixel by pixel within the search window looking for the best correlation. Right?
2) Each value in the correlation matrix represents a template matrix in the search window. Right?
3) Say I get a max at the 10th row and 16th column in the correlation matrix. This means that the best correlated template lies in the 10th matrix in the y direction and 16th matrix in the x direction in the image. Right?
To illustrate the use of normxcorr2, consider the following example (adapted from this page)
%# Make light gray plus on dark gray background
template = 0.2*ones(11);
template(6,3:9) = 0.6;
template(3:9,6) = 0.6;
BW = single(template > 0.5); %# Make white plus on black background
imtool(template, 'InitialMagnification','fit')
%# Make new image that offsets the template
offsetTemplate = 0.2*ones(81);
offset = [30 50]; %# Shift by 30 rows, 50 columns
offsetTemplate( (1:size(template,1))+offset(1), ...
(1:size(template,2))+offset(2) ) = template;
imtool(offsetTemplate, 'InitialMagnification',400)
%# Cross-correlate BW and offsetTemplate to recover offset
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With