loops - Using MATLAB to calculate offset between successive images -
i'm taking images using tunneling microscope. however, scope drifting between successive images. i'm trying use matlab calculate offset between images. code below calculates in seconds small images (e.g. 64x64 pixels), takes >2 hrs handle 512x512 pixel images i'm dealing with. have suggestions speeding code? or know of better ways track images in matlab? help!
%test templates template = .5*ones(32); template(25:32,:) = 0; template(:,25:64) = 0; data_a = template; close imshow(data_a); template(9:32,41:64) = .5; template(:,1:24) = 0; data_b = template; figure, imshow(data_b); tic [m n] = size(data_b); z = []; % loop on possible displacements x = -n:n y = -m:m paddata_b = data_b; ax = abs(x); zerocols = zeros(m,ax); if x > 0 paddata_b(:,1:ax) = []; paddata_b = [paddata_b zerocols]; else paddata_b(:,(n-ax+1):n) = []; paddata_b = [zerocols paddata_b]; end ay = abs(y); zerorows = zeros(ay,n); if y < 0 paddata_b(1:ay,:) = []; paddata_b = vertcat(paddata_b, zerorows); else paddata_b((m-ay+1):m,:) = []; paddata_b = vertcat(zerorows, paddata_b); end % full matrix sum after array multiplication c = paddata_b.*data_a; matsum = sum(sum(c)); % populate array of matrix sums each displacement z(x+n+1, y+m+1) = matsum; end end toc % plot matrix sums figure, surf(z), shading flat % find maximum value of z matrix [max_z, imax] = max(abs(z(:))); [xpeak, ypeak] = ind2sub(size(z),imax(1)) % calculate displacement in pixels corr_offset = [(xpeak-n-1) (ypeak-m-1)]; xoffset = corr_offset(1) yoffset = corr_offset(2)
what you're calculating known cross-correlation of 2 images. can calculate cross-correlation of offsets @ once using discrete fourier transforms (dft or fft). try like
z = ifft2( fft2(dataa) .* fft2(datab).' );
if pad zeros in fourier domain, can use sort of math offsets in fractions of pixel, , apply offsets of fractions of pixel image.
Comments
Post a Comment