Affine Transformation in PythonWhat 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 OpenCVOpenCV 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:
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 methodThis 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:
It can also be written as: cv2.warpAffine( ) methodIt is used to evaluate and implement the remapping routines. Syntax of cv2.warpAffine( ) method where:
Let's implement the affine transformation and understand its different methods to handle different transformations. To use getAffineTransform( ) and warpAffine( ) methodIt is used to translate the image by shifting it from one position to another. Code: Output: 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 imageThe 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:
The parameter rotation takes three values, which specify how to rotate the image:
Below is the implementation to rotate the image in affine transformation. Code: Output: 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 imageScaling 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:
Let's understand the resizing of the image by implementing it in Python: Code: Output: 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 ImagePerspective 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:
Implementing the perspective transformation in Python: Code: Output: We used the cv2.warpPerspective( ) and cv2.getPerspectiveTransform( ) functions, which transformed the image and changed its perspective. Next TopicAi-virtual-assistant-using-python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India