function aligned_photo=align2(photo, template, rows, columns) % align2(photo, template, rows, columns) % returns a shifted version of the photo % to make it best align with the template. % If template is the same size as the photo, % then align2 merely "rotates" some of the rows of the photo % from the top to the bottom (or vice versa) (and columns) % to make it best align with the template. % % usage: % » aligned_photo = align2(photo, template, 100:200, 1:40) % » aligned_photo = align2(photo, template, 1:size(photo,1), 1:size(photo,2) ) % 1998-08-31:DAV: fixed correlation/convolution bug. % 1998-08-31:DAV: subtracted mean intensity, % so it wouldn't always give shifts of 0,0. % 1998-08-29:DAV: fixed so it works with both odd and even size photos. % 1998-08-29:DAV: written by David Cary % Is this made practically obsolete with XCORR2 ? % See also XCORR2, CONV2, FILTER2, % FIXME: % My code uses a photo and template of the the *same* size. % It probably does the Right Thing if % template is much smaller than photo; but this hasn't been tested. % speedup: Reduce the amount of data, to make it go a bit faster % FIXME: this assumes there's a nice hot spot somewhere in % this region. This is true for all the boards I've seen so far... % I picked the column of small chips on the left edge. small_photo = photo( rows, columns); small_photo = small_photo - mean(small_photo(:)); small_template = template( rows, columns); % rotate so we do correlation, not convolution. small_template = rot90(small_template,2) - mean(small_template(:)); % end of speedup. Height=size(small_photo,1)+size(small_template,1)-1; Width=size(small_photo,2)+size(small_template,2)-1; %'doing correlation' padded_correlation = abs( ifft2(... fft2(small_photo, Height, Width) .* ... fft2(small_template, Height, Width)... )); %'finding shift' %correlation=padded_correlation(... % (0:(size(small_photo,1)-1)) + floor(size(small_template,1)/2)+1, ... % (0:(size(small_photo,2)-1)) + floor(size(small_template,2)/2)+1 ... % ); % to reduce false hits, only allow +-6 pixel shifts in y direction. %trimmed_correlation = correlation; %half_size = floor(size(small_photo,1)/2 ); %top = 1:(half_size-6); %trimmed_correlation( top, :) = 0; %bottom = (half_size+6):size(small_photo,1); %trimmed_correlation( bottom, :) = 0; % max_match = max( trimmed_correlation(:) ); max_match = max( padded_correlation(:) ); [y,x] = find( max_match == padded_correlation ); % arbitrarily pick the 1st match -- % since a few pathological cases have multiple matches. yshift = y(1)- size(small_photo,1) xshift = x(1)- size(small_photo,2) % positive shift values mean that % photo is shifted to the right/down relative to template, % and now needs to be shifted to the left/up to make it line up. %'shifting image' % shuffle columns reordered_columns = 1 + mod( (0:size(photo,2)-1) + xshift, size(photo,2) ); columns_aligned = photo( :, reordered_columns ); % shuffle rows reordered_rows = 1 + mod( (0:size(photo,1)-1) + yshift, size(photo,1) ); aligned_photo = columns_aligned( reordered_rows, : ); % end align2.m