% im_transform.m
% Image Transformation examples using imtranslate and imtransform

% Translation
I = imread('coins.png');
J = imtranslate(I, [20, 20]);
imshow(J)
J = imtranslate(I, [20, 20], 'Outputview','full');
figure; imshow(J)
J = imtranslate(I, [20, 20], 'Outputview','full','FillValues',255);
figure; imshow(J)

% Translation using imtransform function
tform = maketform('affine',[1 0 0; 0 1 0; 20 20 1]);

J = imtransform(I, tform, 'XData',[1 size(I,2)+20],'YData',...
      [1 size(I,1)+20], 'FillValue', 255);
      
% Other Transformations
I = imread('coins.png');
% use maketform to create a transformation structure
% Note in Matlab, x, v: horizontal, y, w: vertical
% while in the book, x, v: vertical, y, w: horizontal

% Identity
tform = maketform('affine', [1 0 0; 0 1 0; 0 0 1]);
% Scaling
tform = maketform('affine', [0.5 0 0; 0 0.5 0; 0 0 1]);
% Rotation
theta = pi/2;
tform = maketform('affine', [cos(theta) sin(theta) 0;
    -sin(theta) cos(theta) 0; 0 0 1]);
% Translation
tform = maketform('affine', [1 0 0; 0 1 0; 100 100 1]);
J = imtransform(I, tform);
% Warning: You just called IMTRANSFORM with a pure translation. The output
%image will be the same as the input image unless you specify 'XData' and
%'YData' values in your call to IMTRANSFORM. 

J = imtransform(I, tform, 'XData',[1 size(I,2)],'YData',[1 size(I,1)]);
figure; imshow(J)

I = imread('Fig0236(a)(letter_T).tif');

% Horizontal Shear 
tform = maketform('affine', [1 0 0; 0.5 1 0; 0 0 1]);
% x = v + 0.5w, y = w

% Vertical Shear
tform = maketform('affine', [1 0.5 0; 0 1 0; 0 0 1]);
% x = v, y = 0.5v + w

J = imtransform(I, tform);
figure; imshow(J)

% Combinations of transformations
theta = pi/4;
T1 = [cos(theta) -sin(theta) 0; ...
     sin(theta) cos(theta) 0; 0 0 1];
T2 = [1 0.5 0; 0 1 0; 0 0 1];
% Rotatation followed by vertical shear
T = T1*T2;
tform = maketform('affine',T);
J = imtransform(I, tform);
figure; imshow(J)



    

