CS 791 E Programming Assignment # 1
3. Write a program that applies an affine transformation on
a given image.
3.1. Methodology
In order to get an affine-transformed image, we have to use the inverse
affine transformation. Furthermore, to make things easy, all the transformations
(rotation, translation) are represented in 3x3 homogeneous matrices, so
the transformation process becomes of the manipluation of the matrices.
Suppose D (3x1) represents the destination image pixel location (coordinates)
as [x, y, 1], O (3x1) the original image coordinates as [x,
y, 1], T (3x3) the translation matrix, R the rotation (around (0,0)) matrix,
C the matrix that represents the rotation center. Thus we have:
1). if translation first and rotation
second:
forward: D = C * R * inverse(C) * T * O.
inverse: O = inverse ( C * R * inverse(C) * T ) * D.
2). if rotation first and then translation:
forward: D = T * C * R * inverse(C) * O.
inverse: O = inverse( T * C * R * inverse(C)
)* D.
If the destination location calculated from above is beyond the
location of the original image size, I just used the edge of the original
image.
3.2. Usage:
-
Download the file affineTransform.tar.gz,
-
This program just receive PGM image format, you can download
beifang.pgm as the test image.
-
gunzip the file, type make affineTrsm
-
type: affineTrsm <original-image-file-name> <distination-file-name>
-
give the affine transformation parameters when asked for:
-
the process looks like:
-
-
-
[b_yi@chicken ./affineTrsm beifang.pgm beifangTr.pgm
The source image size: 232 x 170
---------Input rotation center (X Y): 115 85
---------Input rotation angle: 45
---------Input translate displacement (X Y): 10 20
---------If first do translation, input 1, else, 0: 1
The image has been affine-transformed and written in file: beifangTr.pgm
3.3. Comments:
-
This program "borrows" the "pgm" image read/write C++ classes from Dr.
Bebis' homepage;
-
a class named "matrix" is written (in the file "matrix.cpp") that
can fulfil matrix copy, multiplication, invert, the element get/setValues,
and matrix element output. The invert of the matrix is based on method
I found from website.
-
As of common sense, the lower-left of image is the origin.
-
The progrm contains NO special library packages, and I ran my program in
Linux.
-
Use right-hand rotation rule.
3.4. Results
-
after rotating around the center of the image for 45 degrees, no translation,
-
after first translating for (10, 20) and then rotating around
center for -60 degrees:
-
after first rotating around center for -60 and then translating
for (10, 20:
-
only translating for (20, 30):
-
only rotating around (0, 0) for -15 degrees: