%This script demonstrates how to perform manual registration of two images
%by using cpselect
%by Lin ZHANG, SSE, Tongji University, Mar. 2013
     clc
     close all
     clear all
imNeedsTrans = imread('gate2.bmp');
imBase = imread('gate1.bmp');

[xyinput_out, xybase_out] = cpselect(imNeedsTrans,imBase,'Wait',true);

tform = cp2tform(xyinput_out,xybase_out,'projective');

[imTransformed,transformedXData,transformedYData] = imtransform(imNeedsTrans, tform);
[rows, cols, junk] = size(imBase);
base_XData = [1 cols];
base_YData = [1 rows];

top_image = imTransformed;
top_XData = transformedXData;
top_YData = transformedYData;

alpha = 1.0;
[rowsOfInputImage, colsOfInputImage, junk] = size(imNeedsTrans);
top_alpha = imtransform(alpha*ones(rowsOfInputImage, colsOfInputImage), tform);
bottom_image = imBase;
bottom_XData = base_XData;
bottom_YData = base_YData;

figure;
imshow(imBase,'XData', bottom_XData, 'YData', bottom_YData);
hold on
h_top = imshow(imTransformed,'XData', transformedXData, 'YData', transformedYData);
set(h_top,'AlphaData',top_alpha);
axis auto
axis on