I1 = imread('C:\Users\Victoras\Desktop\answertest.bmp');
J1= imread('C:\Users\Victoras\Desktop\studenttest.bmp');
I2= rgb2gray(I1); %convert colour images to grayscale
J2= rgb2gray(J1);
BWI = im2bw(I2);% convert grayscale images to binary
BWJ = im2bw(J2);
IMI = imcomplement(BWI); % invert colours, black to white and white to black
IMJ = imcomplement(BWJ);
%imshow(IMI);
%imshow(IMJ);
BWIremove = bwareaopen(IMI, 5000); % remove objects less that 4500 pixels
BWJremove = bwareaopen(IMJ, 5000);
%imshow(BWIremove);
%imshow(BWJremove);
[LI, numI] = bwlabel(BWIremove);
[LJ, numJ] = bwlabel(BWJremove);
centroidsI = regionprops(LI,'Centroid');
centroidsJ = regionprops(LJ,'Centroid');
% Create a transformation structure for an affine % transformation.
t_concord = cp2tform(centroidsI,centroidsJ,'affine');
% Get the width and height of the orthophoto and perform % the transformation.
info = imfinfo(I1);
registered = imtransform(IMJ,t_concord,'XData',[1 info.Width], 'YData',[1 info.Height]);
figure, imshow(registered)