Lets take the Lena image to perform the transformation

- In this post I am going to discuss image projective transforms.

- These transform are easy to understand and useful in many applications like object detection, 3 D view geometry and ADAS.

- Suppose image I(x,y) as input image and our output (transform) image is O(u,v)

- So we can write a Transform matrix T such that :

O(u,v) = T [I(x,y)]

- Suppose image is in homogeneous co-ordinate system we can add 1

O(u,v,1) = T [I(x,y,1)]

- Where T is 3 x 3 matrix, according to T matrix we can able to transform the image many ways.

Lets write a code which can take input image multiply it with transform matrix and produce transform image

%%%%%%%%%%%%%%%%%%%%%%%% Code Start%%%%%%%%%%%%%%%%

function transform_img = trasform_2d(T, input_img)

%function perform the 2 D projective transform

% input_image : gray input image

% T is 3 x 3 trasnform matrix

%transform_image : output gray transform image

[row,col] = size(input_img);

transform_img = zeros(2*row,2*col);

for i=-row:2*row

for j=-col:2*col

U = inv(T)*[i j 1]’; % where ‘ is transpose

if(U(3)>1) % in case of 8 dof projective transform

U(1) = U(1)/U(3); %% taking care homogeneous co ordinate

U(2) = U(2)/U(3);

end

if((U(1)>0)&&(U(1)<row-1)&&(U(2)>0)&&(U(2)<col-1)) %% consider only points %which lie inside of input image

transform_img(round(i+row+1),round(j+col+1)) = input_img(round(U(1)+1),round(U(2)+1));

end

end

end

end

%%%%%%%%%%%% Code End %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Following are the main 2 D project transform :

- Euclidean Transform
- Similarity Transform
- Affine Transform
- Projective Transform

**Euclidean Transform :**

Let define transformation matrix for Euclidean transform

T = [ R11 R12 Tx ; R21 R22 Ty ; 0 0 1]

Where R11 , R12, R21 and R22 are rotation matrix and Tx and Ty are translation. So it have have 3 degree of freedom 1 in rotation and 2 in translation.

lets take 30 degree rotation angle and -100 pixel translation in both direction

Result of Euclidean Transform

Euclidean Transform dont have property to scale the images.

**Similarity Transform:**

Let define transformation matrix for Similarity transform

T = [ s*R11 s*R12 Tx ; s* R21 s*R22 Ty ; 0 0 1]

It have 4 degree of freedom.

if in Euclidean transform we added the feature of scale (s) its become similarity tranform

lets take 40 degree rotation angle and -100 pixel translation in both direction and 1.5 scaling factor

**Affine Transform :**

Let define transformation matrix for Affine transform

T = [ a11 a12 Tx ; a21 a22 Ty ; 0 0 1]

In Similarity transform if we can able to take different angle as well as different scale it will become affine transform.

lets take 40 degree rotation angle in x direction and 30 degree rotation angle in y direction , -100 pixel translation in both direction, 0.7 scaling factor in x direction and 1.5 scaling factor in y direction.

**Projective Transform : **

Define the transformation full matrix which include the homogeneous factor as well.

T = [h11 h12 h13 ; h21 h22 h23 ; h31 h32 h33]

Projective transform have 8 degree of freedom because we added 2 coefficient h31 h32 in affine transform matrix which take care of image homogeneous co-ordinate geometry. (might be in furure calibration post i will discuss it about in detail)

%%%%%%%%%%%%%%%%%% Code Start %%%%%%%%%%%%%%%%%%%%%%%

clc % clear the screen

clear all % clear all variables

close all % close all MATLAB windows

%% read Image

% set path to read image from system

ImagePath = ‘D:\DSUsers\uidp6927\image_processingCode\lena.jpg’;

img_RGB = imread(ImagePath); % read image from path

img_RGB = im2double(img_RGB); % Convert image to double precision

img_gray = rgb2gray(img_RGB); % convert image to gray

[row, col] = size(img_gray);

figure,imshow(img_gray)

%For a nonreflective similarity,

%[u,v] = [x,y,1]T

%% euclidean transformation.

scale = 1; % scale factor

angle = 30*pi/180; % rotation angle

tx = -100; % x translation

ty = -100; % y translation

sc = scale*cos(angle);

ss = scale*sin(angle);

T = [ sc ss tx;

-ss sc ty;

0 0 1];

% call the function transform_2d

transform_img = trasform_2d(T, img_gray);

figure, imshow(transform_img)

title(‘Euclidean transform’)

%% Similarity transform

scale = 1.5; % scale factor

angle = 40*pi/180; % rotation angle

tx = -100; % x translation

ty = -50; % y translation

sc = scale*cos(angle);

ss = scale*sin(angle);

T = [ sc ss tx;

-ss sc ty;

0 0 1];

transform_img = trasform_2d(T, img_gray);

figure, imshow(transform_img)

title(‘Similarity transform’)

%% Affine transform

scale1 = .7; % scale factor

scale2 = 1.7;

angle1 = 40*pi/180; % rotation angle

angle2 = 30*pi/180; % rotation angle

tx = -100; % x translation

ty = -50; % y translation

sc1 = scale1*cos(angle1);

ss1 = scale1*sin(angle2);

sc2 = scale2*cos(angle1);

ss2 = scale2*sin(angle2);

T = [ sc1 ss1 tx;

-ss2 sc2 ty;

0 0 1];

transform_img = trasform_2d(T, img_gray);

figure, imshow(transform_img)

title(‘Affine transform’)

%% projective transform

scale1 = .7; % scale factor

scale2 = 1.7;

angle1 = 40*pi/180; % rotation angle

angle2 = 30*pi/180; % rotation angle

tx = -100; % x translation

ty = -50; % y translation

sc1 = scale1*cos(angle1);

ss1 = scale1*sin(angle2);

sc2 = scale2*cos(angle1);

ss2 = scale2*sin(angle2);

T = [ sc1 ss1 tx;

-ss2 sc2 ty;

.01 .02 1];

transform_img = trasform_2d(T, img_gray);

figure, imshow(transform_img)

title(‘Projective transform’)

%%%%%%%%%%%%%%%%%%%%% Code End %%%%%%%%%%%%%%%%%%%%%%%

Happy Learning

Cheers