In this era, data is of great importance. Many companies spend their fortune on collecting suitable data. As only a handful of open source datasets are available. This can be targeted by artificially creating new data. When it comes to image data, we can perform different transformations. This process of artificially generating new data is called data augmentation.
The combination of linear transformations is called an affine transformation. By linear transformation, we mean that lines will be mapped to new lines preserving their parallelism, and pixels will be mapped to new pixels without disrupting the distance ratio. Affine transformation is also used in satellite image processing, data augmentation for images, and so on.
These transformations are performed by different matrices multiplication with a matrix
Note: A combination of these transformations is also an affine transformation.
As mentioned earlier, matrix multiplication and addition play a big role in affine transformation. We first take a point
Now, if we want to transform this point
When we scale an image, we shrink or expand it. The
Here
Now we'll use tf.keras.preprocessing.image.apply_affine_transform
to apply the transformations. We can find the details about the parameters in the official documentation.
Let's look at the code below:
image = cv2.imread("Detective.png")image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)transformation = tf.keras.preprocessing.image.apply_affine_transform(image, # input imagezx=0.5,zy=0.5,row_axis=0,col_axis=1,channel_axis=2)
BGR
, which we need to convert into RGB
. zx
and zy
as the scaling parameters.The output of scaling is as follows:
Translation means to pick up the image and place it in a new dimension. The kernel for translation is as follows:
Here
Let's look at the code below:
transformation = tf.keras.preprocessing.image.apply_affine_transform(image,tx=-400,ty=400,row_axis=0,col_axis=1,channel_axis=2)
tx
and ty
as the translation parameters.The output of the translation transform is as follows:
In this transformation, the image is slanted in the x or y direction.
The kernel for horizontal shear is:
And the kernel for vertical shear is:
Here
Let's look at the code below:
transformation = tf.keras.preprocessing.image.apply_affine_transform(image,shear=30,row_axis=0,col_axis=1,channel_axis=2)
tf.keras.preprocessing.image.apply_affine_transform
, we provide an angle for shearing.The output of the shearing transform is as follows:
In rotation, we rotate the image in
Let's look at the code below:
transformation = tf.keras.preprocessing.image.apply_affine_transform(image,theta=90,row_axis=0,col_axis=1,channel_axis=2)
theta
angle.The output of the rotation transform is as follows: