This example shows how to use phase correlation as a preliminary step for automatic image registration. In this process, you perform phase correlation, using
imregcorr, and then pass the result of that registration as the initial condition of an optimization-based registration, using
imregister. Phase correlation and optimization-based registration are complementary algorithms. Phase correlation is good for finding gross alignment, even for severely misaligned images. Optimization-based registration is good for finding precise alignment, given a good initial condition.
Read image that will be reference image in the registration.
fixed = imread('cameraman.tif'); imshow(fixed);
Create an unregistered image by deliberately distorting this image using rotation, scale, and shear. Display the image.
theta = 170; S = 2.3; ShearY = 1.3; tform = affine2d([S.*cosd(theta) -S.*ShearY*sind(theta) 0;... S.*sind(theta) S.*cosd(theta) 0; 0 0 1]); moving = imwarp(fixed,tform); moving = moving + uint8(10*rand(size(moving))); figure, imshow(moving)
Estimate the registration required to bring these two images into alignment.
imregcorr returns an
affine2d object that defines the transformation.
tformEstimate = imregcorr(moving,fixed);
Apply the estimated geometric transform to the misaligned image. Specify
'OutputView' to make sure the registered image is the same size as the reference image. Display the original image and the registered image side-by-side. You can see that
imregcorr has done a good job handling the rotation and scaling differences between the images. The registered image,
movingReg, is very close to being aligned with the original image,
fixed. But there is some misalignment left.
imregcorr can handle rotation and scale distortions well, but not shear distortion.
Rfixed = imref2d(size(fixed)); movingReg = imwarp(moving,tformEstimate,'OutputView',Rfixed); figure, imshowpair(fixed,movingReg,'montage');
View the aligned image overlaid on the original image, using
imshowpair. In this view,
imshowpair uses color to highlight areas of misalignment that remain.
To finish the registration, use
imregister, passing the estimated transformation returned by
imregcorr as the initial condition.
imregister is more effective if the two images are roughly in alignment at the start of the operation. The transformation estimated by
imregcorr provides this information for
imregister. The example uses the default optimizer and metric values for a registration of two images taken with the same sensor (
[optimizer, metric] = imregconfig('monomodal'); movingRegistered = imregister(moving, fixed,... 'affine', optimizer, metric,'InitialTransformation',tformEstimate);
Display the result of this registration. Note that
imregister has achieved a very accurate registration, given the good initial condition provided by
figure imshowpair(fixed, movingRegistered,'Scaling','joint');