Image Alignment Code in MATLAB
Show older comments
I've been trying everything I can find, but I can't seem to come up with a code that aligns my images very well. I have been using the pressure taps on the image to align the images manually, so using those again would be great. I've gotten to within a few pixels, but I need it to be with at least pixel accuracy, ideally subpixel accuracy. Here's an example of one of the codes that seems to do semi well.
img1 = 255-mean(imread('01_wing.png'),3);
img2 = 255-mean(imread('02_wing.png'),3);
c = normxcorr2(img2,img1);
[y x] = find(c==max(c(:)));
y = y-size(img2,1);
x = x-size(img2,2);
TFORM = maketform('affine',[1 0 x;0 1 y; 0 0 1]');
NEWimg2 = imtransform(img2,TFORM,'xdata',[1 size(img1,2)],'ydata',[1 size(img1,1)]);
I've attached the wing images in a zip file. The image below is an example of the pressure taps not aligning correctly.
What should I do differently?

Accepted Answer
More Answers (2)
Image Analyst
on 11 Oct 2013
0 votes
Why not use imregister()? It's in the Image Processing Toolbox.
8 Comments
Joe
on 12 Oct 2013
Image Analyst
on 12 Oct 2013
Other than increasing resolution to reduce the quantization error, I don't know of a way to improve on imregister. Can you increase the spatial and intensity resolution at all?
Joe
on 12 Oct 2013
Image Analyst
on 13 Oct 2013
Why do you think they're not right? Take what it gives you and try to shift it a pixel in all 8 directions. Is any of those directions better than that it suggested?
Joe
on 14 Oct 2013
Image Analyst
on 15 Oct 2013
So did you just say that you were able to manually shift an image that gave a better alignment than the shift that imregister() gave? I'd like to see your images and code that proves it.
Joe
on 15 Oct 2013
I can imagine imregister() having difficulty if the pressure taps in image A do not have any initial overlap with the taps in image B. The initial gradient of the registration metric will be zero in that case and the algorithm won't be able to move.
With normxcorr2, you would hopefully be able to obtain that initial overlap, but since the taps are so small, it is unclear.
Eric
on 15 Oct 2013
Here's what works well for me:
1. Get dftregistration.m from www.mathworks.com/matlabcentral/fileexchange/18401-efficient-subpixel-image-registration-by-cross-correlation
2. Register the gradients of the images, not the images themselves.
Here's my code:
%Load data
im1Orig = double(imread('01_wing.png'));
im2Orig = double(imread('02_wing.png'));
%Calculate gradients
im1 = imgradient(im1Orig);
im2 = imgradient(im2Orig);
%Get registration values
[output, Greg] = dftregistration(fft2(im1),fft2(im2),50);
%Translate image
imOut = TranslateImage(im2, output(3), output(4),'method','Fourier');
I get a row shift of -0.36 pixels and a column shift of +3.06 pixels. My function TranslateImage is basically:
img_out = TranslateImage(img_in, rowshift, colshift)
%%Create frequency space sampling vectors
[numrows, numcols] = size(img_in);
[n,m] = meshgrid(-fix(numcols/2):fix((numcols-1)/2),-fix(numrows/2):fix((numrows-1)/2));
m = m/numrows;
n = n/numcols;
%%Perform translation in the Fourier domain
img_fft = fftshift(fft2(ifftshift(double(img_in))));%Cast the input image to a double
shiftOtf = exp(-1i*2*pi*m*rowshift) .* exp(-1i*2*pi*n*colshift);
img_fft_trans = img_fft .* shiftOtf;
img_out = real(fftshift(ifft2(ifftshift(img_fft_trans))));
return
end
For monomodal image registration with pure translation, there are many algorithms that work better than imregister().
Good luck,
Eric
1 Comment
Eric
on 15 Oct 2013
I should point out that pure translation doesn't appear to be totally valid for these images, but it's close. Some of the features line up very well, while others are still off by a pixel.
-Eric
Categories
Find more on 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!