close all
[imgName, path] = picimage('./', 0, 'pick up 1 gray scale image');
img = imread([path imgName]);

 
%% PURE TRANSLATION

T  = maketform('affine',[1 0 0; 0 1 0; 100 20 1]);
J  = imtransform(img,T,'XData',[1,size(img,2)],'YData',[1 size(img,1)]);

%pure translation. The output image will be the same as the input image
 
subplot(1,2,1);
imshow(img, []);
title('Original');
subplot(1,2,2);
imshow(J, []);
title('Translation (100,20)');
pause
%% SCALING
 
T = affine2d([1.5 0 0; 0 .5 0;  0 0 1]);
J = imwarp(img,T);

 subplot(2,1,1);
imshow(img, []);
title('Original');
subplot(2,1,2);
imshow(J, []);
title('Scaled (1.5, 0.5)');
pause

%%ROTATION
Theta= pi/6;
T = affine2d([cos(Theta) sin(Theta) 0; -sin(Theta) cos(Theta) 0;  0 0 1]);
J = imwarp(img,T);

 subplot(1,2,1);
imshow(img, []);
title('Original');
subplot(1,2,2);
imshow(J, []);
title('Rotation of pi/6 ');
pause

%% Affine transform based on 3 control points

X= [ 0 0;
     6 3;
    -2 5]; 
 Y = [-1 -1;
       0 -10;
       4 4]; 

%tform = maketform('affine',[u v],[x y]);
T =fitgeotrans(X,Y, 'affine');
J = imwarp(img,T);

 subplot(1,2,1);
imshow(img, []);
title('Original');
subplot(1,2,2);
imshow(J, []);
title('Control Points ');
pause

%%Shearing: %% Affine transform based on the T matrix definition
 
tform = affine2d([1 0 0; .5 1 0; 0 0 1]);
J = imwarp(img,tform);
subplot(1,2,1);
imshow(img);
title('Original');
subplot(1,2,2);
imshow(J);
title('Sheared');
pause

 
 
%% Wave1

T =fitgeotrans( X,Y, 'polynomial', 4);
J = imwarp(img,T);


for k=1:size(img,1)
    for l=1:size(img,2)
    x = min([max([1, k + 20 *sin((2*pi*l)/128)]), size(img,1)]);
    y = l;
    Wave1(k,l) = img(round(x),round(y));
    end
end
   
subplot(1,2,1);
imshow(img);
title('Original');
subplot(1,2,2);
imshow(Wave1);
title('Wave1');
pause

%% Wave 2


for k=1:size(img,1)
    for l=1:size(img,2)
    x = min([max([1, k + 20 *sin((2*pi*k)/30)]), size(img,1)]);
    y = l;
    Wave2(k,l) = img(round(x),round(y));
    end
end
   
subplot(1,2,1);
imshow(img);
title('Original');
subplot(1,2,2);
imshow(Wave2);
title('Wave2');
pause

%% Warp

x0 = size(img,1)/2;
 
for k=1:size(img,1)
    for l=1:size(img,2)
        x = min([max([1,sign(k - x0)/x0 *  (k-x0)^2 + x0]), size(img,1)]);
        y = l;
        Warp(k,l) = img(round(x),round(y));
    end
end
   
subplot(1,2,1);
imshow(img);
title('Original');
subplot(1,2,2);
imshow(Warp);
title('Warp');
pause

%% Swirl

x0 = size(img,1)/2;
y0 = size(img,2)/2;
 
for k=1:size(img,1)
    for l=1:size(img,2)
        r = sqrt((k -x0)^2 + (l- y0)^2);
        Theta = r* pi/512;
        x = min([max([1,(k - x0)*cos(Theta) + (l - y0)*sin(Theta) + x0]), size(img,1)]);
        y = min([max([1,-(k - x0)*sin(Theta) + (l - y0)*cos(Theta) + y0]), size(img,2)]);
        Swirl(k,l) = img(round(x),round(y));
    end
end
   
subplot(1,2,1);
imshow(img);
title('Original');
subplot(1,2,2);
imshow(Swirl);
title('Swirl');