How to Apply Affine Transformation on an Image in OpenCV Python?
Affine transformation is a type of image manipulation technique that involves rotation, scaling, and translation. This technique is widely used in various computer vision applications, such as object detection and image registration.
In this article, we will learn how to apply affine transformation on an image using OpenCV Python. We will specifically look at how to rotate and scale an image using affine transformation.
Setting Up the Environment
Before we get started with the code, we need to set up our environment. We will need OpenCV and NumPy packages installed in our Python environment. You can install these packages using pip by running the following commands in your terminal:
pip install opencv-python
pip install numpy
Loading the Image
First, let’s load an image that we will use for our transformation. We will be using the cv2.imread()
function from OpenCV to load the image. This function takes the image path as an argument and returns a NumPy array representing the image.
import cv2
import numpy as np
# Load the image
img = cv2.imread('path/to/image.jpg')
# Show the original image
cv2.imshow('Original Image', img)
cv2.waitKey(0)
In the above code, we have loaded an image and displayed it using the cv2.imshow()
function. The cv2.waitKey()
function is used to wait for a key press before closing the image window.
Affine Transformation
Affine transformation involves three operations: rotation, scaling, and translation. Let’s look at how we can perform these operations using OpenCV Python.
Rotation
To rotate an image using affine transformation, we need to define a rotation matrix and apply it to the image using the cv2.warpAffine()
function.
# Define the rotation angle (in degrees)
angle = 30
# Get the image shape
height, width, _ = img.shape
# Define the rotation matrix
M = cv2.getRotationMatrix2D((width/2, height/2), angle, 1)
# Apply the rotation to the image
rotated_img = cv2.warpAffine(img, M, (width, height))
# Show the rotated image
cv2.imshow('Rotated Image', rotated_img)
cv2.waitKey(0)
In the above code, we have defined a rotation angle of 30 degrees and calculated the rotation matrix using the cv2.getRotationMatrix2D()
function. We then applied the rotation matrix to the image using the cv2.warpAffine()
function.
Scaling
To scale an image using affine transformation, we need to define a scaling matrix and apply it to the image using the cv2.warpAffine()
function.
# Define the scale factor
scale = 0.5
# Define the scaling matrix
M = np.array([[scale, 0, 0],
[0, scale, 0]])
# Apply the scaling to the image
scaled_img = cv2.warpAffine(img, M, (width, height))
# Show the scaled image
cv2.imshow('Scaled Image', scaled_img)
cv2.waitKey(0)
In the above code, we have defined a scale factor of 0.5 and calculated the scaling matrix using NumPy. We then applied the scaling matrix to the image using the cv2.warpAffine()
function.
Conclusion
In this article, we have learned how to apply affine transformation on an image using OpenCV Python. We have specifically looked at how to rotate and scale an image using affine transformation. Affine transformation is a powerful technique that can be used to manipulate images in various ways. With the knowledge of affine transformation, you can easily perform complex image manipulation tasks in your computer vision applications.