%Function for alignment

% Img1IN is the reference image
% Img2IN will be align based on Img1IN and saved on ImgOUT

function [ImgOUT]=align2D(Img_ref,Img,showim)

if nargin<3
    showim=0;
end

%Img1IN = Img1IN/max(max(max(Img1IN)));
%Img2IN = Img2IN/max(max(max(Img2IN)));

error = Inf;
L = size(Img_ref);

C = fftshift(ifftn(fftn(Img_ref/max(max(max(Img_ref)))).*conj(fftn(Img)))); % cross correlation
ind = find(C == max(max(max(C))),1,'first'); % maximum cross correlation
[Y,X] = ind2sub(size(C),ind);
X = X - floor(L(2)/2) - 1;
Y = Y - floor(L(1)/2) - 1;
ImgTemp1 = circshift(Img,[Y X]);

errorTemp = sum(abs(ImgTemp1(:)-Img_ref(:))); %error


if errorTemp < error
    ImgOUT = ImgTemp1; %save better align
    error = errorTemp;
end

Img = rot90(rot90(Img));   
% C = fftshift(ifftn(fftn(Img1IN/max(max(max(Img1IN)))).*conj(fftn(Img)))); % cross correlation
% ind = find(C == max(max(max(C))),1,'first'); % maximum cross correlation
% [Y,X] = ind2sub(size(C),ind);
% X = X - floor(L(2)/2) - 1;
% Y = Y - floor(L(1)/2) - 1;
% ImgTemp = circshift(Img,[Y X]);
% 
% errorTemp = nansum(nansum(nansum(abs(ImgTemp-Img1IN)))); %error
% 
% if errorTemp < error
%     ImgOUT = ImgTemp; %save better align
%     error = errorTemp;
% 
% end

C = fftshift(ifftn(fftn(Img_ref/max(max(max(Img_ref)))).*conj(fftn(Img))));% cross correlation
ind = find(C == max(max(max(C))),1,'first'); % maximum cross correlation
[Y,X] = ind2sub(size(C),ind);
X = X - floor(L(2)/2) - 1;
Y = Y - floor(L(1)/2) - 1;
ImgTemp2 = circshift(Img,[Y X]);

errorTemp = sum(abs(ImgTemp2(:)-Img_ref(:))); %error


if errorTemp < error
    ImgOUT = ImgTemp2; %save better align
    error = errorTemp;
end

%figure(102); img(ImgTemp1,'1',ImgTemp2,'2');

if showim~=0
figure(1)
subplot(1,2,1), imagesc(Img_ref), axis image, title('Model');
subplot(1,2,2), imagesc(ImgOUT), axis image, title('Aligned');
end
