function Lab07_GeometricTransformations()
    
    clc; clear; close all;    
    
    %% affine2d and imwarp
    clc; clear; close all;
    I = imread('cameraman.tif');
    [m,n] = size(I);
    
    % Reference 2-D image to world coordinates
    RA = imref2d([m,n]);
    
    % Create a 2-D geometric transformation object.
    % Traslation   *** transpose matrix ***
    t = [1 0 50; 0 1 100; 0 0 1];
    tform = affine2d(t.'); 
        
    % Apply the transformation to the image.
    % RB — Spatial referencing information associated with the transformed image
    [J,RB] = imwarp(I,RA,tform, 'nearest', 'FillValues',0);
    % 'OutputView' — Size and location of output image in world coordinate system
    [J2,RB2] = imwarp(I,RA,tform, 'nearest', 'FillValues',0,'OutputView',RA);
    RB
    RB2
    
    figure;
    nr = 1;
    nc = 3; 
    subplot(nr, nc, 1); imshow(I); title('Image (I)'); axis on;    
    subplot(nr, nc, 2); imshow(J); title('Transformation (J)'); axis on;  
    subplot(nr, nc, 3); imshow(J2); title('Transformation (J2)'); axis on; 
    
    %%%%% ******************************************** Solutions ******************************************** %%%%%
    
    %% affine2d and imwarp Concatenation 
    clc; clear; close all;
    I = imread('faceBW.png');
    [m,n] = size(I);
    
    % Reference 2-D image to world coordinates
    RA = imref2d([m,n]);
    
    % Create a 2-D geometric transformation object.
    % Traslation  
    t = [1 0 -n/2; 0 1 -m/2; 0 0 1];
    tform = affine2d(t.'); 
    [J,RB] = imwarp(I,RA,tform);
	% Rotation
    theta = -45;
    t = [cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1];
    tform = affine2d(t.');
    [J2,RB2] = imwarp(J,RB,tform);
    % Traslation  
    t = [1 0 n/2; 0 1 m/2; 0 0 1];
    tform = affine2d(t.'); 
    [J3,RB3] = imwarp(J2,RB2,tform,'OutputView',RA);
        
    figure;
    nr = 1;
    nc = 2; 
    subplot(nr, nc, 1); imshow(I); title('Image (I)'); axis on;    
    subplot(nr, nc, 2); imshow(J3); title('Transformation (J3)'); axis on; 
    
    %% affine2d and imwarp Concatenation multiplication  
    clc; clear; close all;
    I = imread('faceBW.png');
    [m,n] = size(I);
    
    % Reference 2-D image to world coordinates
    RA = imref2d([m,n]);
    
    % Create a 2-D geometric transformation object.
    % Traslation*Rotation*Traslation 
    theta = -45;
    t = [1 0 n/2; 0 1 m/2; 0 0 1]*[cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]*[1 0 -n/2; 0 1 -m/2; 0 0 1];
    tform = affine2d(t.'); 
    [J,RB] = imwarp(I,RA,tform,'OutputView',RA);
	
        
    figure;
    nr = 1;
    nc = 2; 
    subplot(nr, nc, 1); imshow(I); title('Image (I)'); axis on;    
    subplot(nr, nc, 2); imshow(J); title('Transformation (J)'); axis on; 
    
    %% Find T - two sets of linear equations
    clc; clear; close all;
    I1 = im2double( imread('faceBW.png') );  
    I2 = im2double( imread('faceRTBW.png') );  
    [m,n] = size(I1);
    np = 4;
    
%     Select np points 
    imshow(I1);    
    [x1,y1] = ginput(np);
    
    imshow(I2);
    [x2,y2] = ginput(np);
    
    vecOnes = ones(np,1);       
    M2 = [x1,y1,vecOnes];
    bx=x2;
    by=y2;
    
    ax = inv(M2.'*M2)*M2.'*bx;
    ay = inv(M2.'*M2)*M2.'*by;
    
    T = [ax.';ay.';[0,0,1]];
    
    % Reference 2-D image to world coordinates
    RA = imref2d([m,n]);
    
    % Create a 2-D geometric transformation object.
    tform = affine2d(T.'); 
        
    % Apply the transformation to the image.
    [J,RB] = imwarp(I1,RA,tform, 'nearest', 'FillValues',1,'OutputView',RA);
           
    dif = I2-J;
    
    figure;
    nr = 1;
    nc = 4; 
    subplot(nr, nc, 1); imshow(I1); title('Image (I1)'); axis on; 
    subplot(nr, nc, 2); imshow(I2); title('Image (I2)'); axis on;  
    subplot(nr, nc, 3); imshow(J); title('Transformation (J)'); axis on; 
    subplot(nr, nc, 4); imshow(dif); title('dif = I2-J'); axis on; 
    
    %% Find T - x' and y' in the same matrix 
    clc; clear; close all;
    I1 = im2double( imread('faceBW.png') );  
    I2 = im2double( imread('faceRTBW.png') );  
    [m,n] = size(I1);
    np = 4;
    
%     Select np points 
    imshow(I1);    
    [x1,y1] = ginput(np);
    
    imshow(I2);
    [x2,y2] = ginput(np);
    
    vecOnes = ones(np,1);
    vecZeros = zeros(np,1);
    M = [[x1;vecZeros],[y1;vecZeros],[vecOnes;vecZeros],[vecZeros;x1],[vecZeros;y1],[vecZeros;vecOnes]];
    b = [x2;y2];
    a = inv(M.'*M)*M.'*b;
    
    T = [a(1:3).';a(4:6).';[0,0,1]];
    
    % Reference 2-D image to world coordinates
    RA = imref2d([m,n]);
    
    % Create a 2-D geometric transformation object.
    tform = affine2d(T.'); 
        
    % Apply the transformation to the image.
    [J,RB] = imwarp(I1,RA,tform, 'nearest', 'FillValues',1,'OutputView',RA);
           
    dif = I2-J;
    
    figure;
    nr = 1;
    nc = 4; 
    subplot(nr, nc, 1); imshow(I1); title('Image (I1)'); axis on; 
    subplot(nr, nc, 2); imshow(I2); title('Image (I2)'); axis on;  
    subplot(nr, nc, 3); imshow(J); title('Transformation (J)'); axis on; 
    subplot(nr, nc, 4); imshow(dif); title('dif = I2-J'); axis on; 
    
    %% myImwarp - test 
    clc; clear; close all;
    I = im2double(imread('faceBW.png'));
    [m,n] = size(I);
    
    % Reference 2-D image to world coordinates
    RA = imref2d([m,n]);
    
    % Rotate an image around its center
    % Create a 2-D geometric transformation object.
    theta = -45;
    %t = [1 0 39; 0 1 0; 0 0 1]*[1 0 n/2; 0 1 m/2; 0 0 1]*[cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]*[1 0 -n/2; 0 1 -m/2; 0 0 1];
    
    % Sequence of transformations that generates I2 
    t = [1 0 39; 0 1 0; 0 0 1]*[1 0 n/2; 0 1 m/2; 0 0 1]*[cosd(theta) -sind(theta) 0; sind(theta) cosd(theta) 0; 0 0 1]*[1 0 -n/2; 0 1 -m/2; 0 0 1];
    
    %myImwarp 
    J=myImwarp(I, t);        
    
    % Matlab transformation  *** transpose matrix ***
    tform = affine2d(t.');         
    % Apply the transformation to the image.
    [J2,RB2] = imwarp(I,RA,tform, 'nearest', 'FillValues',0,'OutputView',RA);
    
    dif = J-J2;
    sum(sum(dif)) 
    
%     figure;   
    nr = 1;     nc = 4; 
    subplot(nr, nc, 1); imshow(I); title('Image I1'); axis on; 
    subplot(nr, nc, 2); imshow(J); title({'Image J','(myImwarp)'}); axis on; 
    subplot(nr, nc, 3); imshow(J2); title({'Image J','(Matlab imwarp)'}); axis on; 
    subplot(nr, nc, 4); imshow(J-J2); title({'Image J-J2','(Matlab imwarp)'}); axis on; 
    
end


