Transform rotated image to the original image

For solving part of my problem I needed to find the transformation matrix between the rotated image and its original so I told myself why not write the post in my blog about this problem. For this post I am going to show you how we can transform rotated image to the original image. Let’s start:

close all
clear all
%% Input images.

Detect features in both images and match the features:

ptsOriginal  = detectSURFFeatures(original_lena);
ptsDistorted = detectSURFFeatures(rotated_lena);

%% Extract feature.

[featuresOriginal,   validPtsOriginal]  = extractFeatures(original_lena,  ptsOriginal);
[featuresDistorted, validPtsDistorted]  = extractFeatures(rotated_lena, ptsDistorted);

%% Match features

indexPairs = matchFeatures(featuresOriginal, featuresDistorted);

Retrieve locations of corresponding points for each image:

Show point matches

Show point matches

matchedOriginal  = validPtsOriginal(indexPairs(:,1));
matchedDistorted = validPtsDistorted(indexPairs(:,2));

%% Show point matches.
showMatchedFeatures(image2, rotated_lena, matchedOriginal,matchedDistorted);

%% Estimate Transformation.

[tform, inlierDistorted, inlierOriginal] = estimateGeometricTransform(...
    matchedDistorted, matchedOriginal, 'similarity');

Display matching point pairs used in transformation matrix:

Display matching point pairs used in transformation matrix.

Display matching point pairs used in transformation matrix.

showMatchedFeatures(original_lena, rotated_lena, inlierOriginal, inlierDistorted);

To solve for Scale and Angle, we compute the inverse transformation matrix:

Tinv  = tform.invert.T;

ss = Tinv(2,1);
sc = Tinv(1,1);
scale_recovered = sqrt(ss*ss + sc*sc);
theta_recovered = atan2(ss,sc)*180/pi;

Recover the Original Image:

Compare recovered to original.

Compare recovered to original.

outputView = imref2d(size(original_lena));
recovered  = imwarp(rotated_lena,tform,'OutputView',outputView);

%% Compare recovered to original 

figure, imshowpair(original_lena,recovered,'montage')

imDiff = original_lena-recovered;

Deblur the Recover image:

Compare recovered to original after deblurring.

Compare recovered to original after deblurring.

deblurSigma = 0.7; % Adjust this to get the most visually pleasing results.
motion_noise = fspecial('gaussian', 15, deblurSigma);
de_recovered = deconvlucy(recovered,motion_noise);
figure, imshowpair(original_lena,de_recovered,'montage')

Leave a Reply

Your email address will not be published. Required fields are marked *