Affine Transformation- Image Processing In TensorFlow- Part 1

Paras Patidar
MLAIT
Published in
5 min readJan 3, 2020

Affine Transformation helps to modify the geometric structure of the image, preserving parallelism of lines but not the lengths and angles.

What You Will Learn?

  • Introduction to Affine Transformation
  • Different Affine Transformations like Rotation, Shear, Scale and Translation
  • Math Behind Affine Transformation
  • Implementation of Affine Transformation In Tensorflow

Let’s Start…

Introduction to Affine Transformation

Affine Transformation helps to modify the geometric structure of the image, preserving parallelism of lines but not the lengths and angles. It preserves collinearity and ratios of distances. It is one type of method we can use in Machine Learning and Deep Learning for Image Processing and also for Image Augmentation.

This technique is also used to correct Geometric Distortions and Deformations that occur with non-ideal camera angles. Ex: Satellite Imagery.

The Affine Transformation relies on matrices to handle rotation, shear, translation and scaling.

We will be using an image as a reference to understand the things more clearly.

Source: https://pixlr.com/photo/image-design-11-1-pw.jpg

I will be using TensorFlow's Image Processing method called apply_affine_transformation to perform the following operations.

tf.keras.preprocessing.image.apply_affine_transform(
x,
theta=0,
tx=0,
ty=0,
shear=0,
zx=1,
zy=1,
row_axis=0,
col_axis=1,
channel_axis=2,
fill_mode='nearest',
cval=0.0,
order=1
)

Argument Definition

x: 2D numpy array, single image.
theta: Rotation angle in degrees.
tx: Width shift.
ty: Heigh shift.
shear: Shear angle in degrees.
zx: Zoom in x direction.
zy: Zoom in y direction
row_axis: Index of axis for rows in the input image.
col_axis: Index of axis for columns in the input image.
channel_axis: Index of axis for channels in the input image.
fill_mode: Points outside the boundaries of the input
are filled according to the given mode
(one of `{'constant', 'nearest', 'reflect', 'wrap'}`).
cval: Value used for points outside the boundaries
of the input if `mode='constant'`.
order: int, order of interpolation

Returns

The transformed version of the input.

Different Affine Transformation and Implementation

1. Translation

A translation is a function that moves every point with a constant distance in a specified direction. In TensorFlow, it is specified as tx and ty which will provide the orientation and the distance.

tx: Width shift.
ty: Heigh shift.

The mathematical transformation is the following…

So after specifying the values of tx and ty, we can get our desired result.

transformation = tf.keras.preprocessing.image.apply_affine_transform(
img,
tx=30,
ty=30
)
plt.imshow(transformation)
plt.axis('off')

2. Rotation

Rotation is a circular transformation around a point or an axis. We can specify the angle of rotation to rotate our image around a point or an axis.

We can mention the value of theta in degrees for implementing in Tensorflow.

theta: Rotation angle in degrees.

So, By giving the angle in degrees, we have rotated our image 270 degrees.

transformation = tf.keras.preprocessing.image.apply_affine_transform(
img,
theta=270
)
plt.imshow(transformation)

3. Scaling

Scaling is a linear transformation that enlarges or shrinks objects by a scale factor that is the same in all directions. We can specify the values of the sx and sy to enlarge or shrink our images. It is basically zooming in the image or zooming out the image.

We can mention the value of zx and zy for implementing in Tensorflow.

zx: Zoom in x direction.
zy: Zoom in y direction

By giving the values of zx and zy, we get the following result…

transformation = tf.keras.preprocessing.image.apply_affine_transform(
img,
zx=0.5,
zy= 0.5
)
plt.imshow(transformation)

4. Shear

Shear is sometimes also referred to as transvection. A transvection is a function that shifts every point with constant distance in a basis direction(x or y).

We can also mention the value of shear in degrees for implementing in Tensorflow.

shear: Shear angle in degrees.

So, By giving the angle in degrees,

transformation = tf.keras.preprocessing.image.apply_affine_transform(
img,
shear=50
)
plt.imshow(transformation)

So this is some of the basic operations we can perform in Affine Transformation. These operations will be available on every ML Framework.

Code Implementation

Next Part

Credits

  1. https://in.mathworks.com/discovery/affine-transformation.html
  2. http://www.sci.utah.edu/~acoste/uou/Image/project3/ArthurCOSTE_Project3.pdf
  3. Tensorflow

Thank You!

Stay Tuned & Stay Connected with #MLAIT

Follow Us for more tutorials on ML, AI and Cloud and join telegram group of MLAIT

--

--