% im_registration.m
% Register a transformed ('distorted') image to a reference image
% David Pan, UAH

I = imread('Fig0237(a)(characters test pattern)_POST.tif');
% shear both vertically and horizontally 
tform = maketform('affine', [1 0.3 0; 0.3 1 0; 0 0 1]);
J = imtransform(I, tform);
figure; imshow(J)

% Register J against the reference image I
% Select control points (mp: moving points, fp: fixed points)
[mp,fp] = cpselect(J,I,'Wait',true);
% Create a transformation sturcture for an affine transform using
% the control points
t_struct = cp2tform(mp, fp, 'affine');
info = imfinfo('Fig0237(a)(characters test pattern)_POST.tif');
registered = imtransform(J, t_struct, ...
    'XData',[1 info.Width],'YData',[1 info.Height]);
figure;
imshow(registered);

% Show the difference between I and registered
Diff = abs(double(I) - double(registered));
figure; imshow(uint8(Diff));

