Affine Transformation in Python

What is Affine Transformation?

Affine Transformation is a process of geometric transformation in which the original image is transformed such that the output image will remain parallel. This conserves the collinearity, parallelism of the lines, and the ratio of the distance between two points. The affine transformation is represented by the mapping of the matrix (x / -> Mx + b), in which M is the linear matrix and b is an offset vector. The affine transformation includes the transformation like translation, rotation, scaling, shifting, etc.

We will use OpenCV for performing Affine Transformation in Python.

About OpenCV

OpenCV is an open library that is used for machine learning, computer vision, and image processing. It is used to recognize and identify objects, faces, and many other things like handwritten images using the processing of images and videos. Affine transformation is a type of transformation which is used to fix the geometric imperfections. The affine transformation is represented as a 2 x 3 matrix.

There are a few transformations in affine transformations:

  • Rotating the image using cv2.rotate
  • Scaling an image using cv2.resize
  • Translating using cv2.getPerspectiveTransform
  • Perspective transformation using cv2.warpAffin

Before implementation, we need to download the opencv-python library using the pip command:

Now, we will import the library:

Here, we will understand the different methods and functions of affine transformation.

cv2.getPerspectiveTransform method

This function is used to evaluate the affine transformation using the three pairs of corresponding points. It forms a 2 x 3 matrix of the affine transform.

Syntax of getPerspectiveTransform( ) method

where:

  • source = the coordinates of the source image
  • destination = the coordinates of the destination image

It can also be written as:

cv2.warpAffine( ) method

It is used to evaluate and implement the remapping routines.

Syntax of cv2.warpAffine( ) method

where:

  • source = the source image
  • matrix = transformation matrix
  • imgsize = size of the output image
  • destination = the output image having the same size and type as the source image
  • flag = it determines the interpolation methods
  • mode = it defines the pixel interpolation method
  • value = it gives the value of the constant border

Let's implement the affine transformation and understand its different methods to handle different transformations.

To use getAffineTransform( ) and warpAffine( ) method

It is used to translate the image by shifting it from one position to another.

Code:

Output:

Affine Transformation in Python

This code shows the usage of the cv2.getAffineTransform( ), and the cv2.warpAffine( ) functions to implement the affine transformation to an image. We first imported the required libraries and then read an image. Then, using the getAffineTransform( ) and warpAffine( ) functions, we transformed our image. As an output, it gives both the input image and the transformed output image.

Now, we will implement multiple transformations, including scaling, rotation, etc., using the affine transformation.

Rotation of the image

The rotation of the image in affine transformation refers to the movement of the image in circulation motion. It rotates the image in the clockwise and anti-clockwise direction. To rotate an image, the cv2.rotate( ) function is used. It rotates the image in multiples of 90 degrees.

Syntax of cv2.rotate( ) function

where:

  • img = the source image
  • rotation = the type of rotation

The parameter rotation takes three values, which specify how to rotate the image:

  1. ROTATE_90_CLOCKWISE: It rotates the image by 90 degrees in the clockwise direction.
  2. ROTATE_90_COUNTERCLOCKWISE: It rotates the image in the counter-clockwise direction by 90 degrees, or we can say it rotates the image by 270 degrees in the clockwise direction.
  3. ROTATE_180: It rotates the image by 180 degrees in the clockwise direction.

Below is the implementation to rotate the image in affine transformation.

Code:

Output:

Affine Transformation in Python

Here, we imported all the necessary libraries and read the image using the imread( ) function. Then, using the rotate( ) function, we have rotated the image in every rotation using its code.

Scaling the image

Scaling refers to resizing the image. It results in changing the pixel information. When we reduce the size of the image, we need to resample the pixels, and when we increase the size of the image, we reconstruct the image. We can scale the image using the interpolation methods. It is implemented using the cv2.resize( ) function.

Syntax of resize( ) function

where:

  • src = the input image
  • dst = the output image
  • dsize = it is the output image size
  • fx = the scaling factor of the horizontal axis
  • fy = the scaling factor of the vertical axis
  • interpolation = defines the interpolation method

Let's understand the resizing of the image by implementing it in Python:

Code:

Output:

Affine Transformation in Python

In this, we have imported the libraries and used the resize( ) function to scale the image. We once reduced the size of the image and then enlarged the image.

Perspective Transformation of Image

Perspective transformation is a process by which we can change the perspective of the image for better analysis. It forms a 3 x 3 matrix by which we can change the perspective by changing the coordinates. To evaluate the perspective transformation, we can use the cv2.warpPerspective( ) method.

Syntax of warpPerspective( ) function

where:

  • src = the source image
  • mat = transformation matrix
  • dsize = size of the output image
  • dst = the output image having the same size and type as the source image
  • flag = it determines the interpolation methods
  • mode = it defines the pixel interpolation method
  • value = it gives the value of the constant border

Implementing the perspective transformation in Python:

Code:

Output:

Affine Transformation in Python

We used the cv2.warpPerspective( ) and cv2.getPerspectiveTransform( ) functions, which transformed the image and changed its perspective.