Before reading, please refer to this blog for better understanding.
In this blog, we will discuss how to perform a geometric transformation using OpenCV-Python. In geometric transformation, we move the pixels of an image based on some mathematical formulae. This involves translation, rotation, scaling, and distortion (or undistortion!) of images. This is frequently used as a pre-processing step in many applications where the input is distorted while capturing like document scanning, matching temporal images in remote sensing and many more.
There are two basic steps in geometric transformation
- Spatial Transformation: Calculating the spatial position of pixels in the transformed image.
- Intensity Interpolation: Finding the intensity values at the newly calculated positions.
OpenCV has built-in functions to apply the different geometric transformations to images like translation, rotation, affine transformation, etc. You can find all the functions here: Geometric Transformations of Images
In this blog, we will learn how to change the apparent perspective of an image. This will make the image look more clear and easy to read. Below image summarizes what we want to do. See how easily we can read the words in the corrected image.
For perspective transformation, we need 4 points on the input image and corresponding points on the output image. The points should be selected counterclockwise. From these points, we will calculate the transformation matrix which when applied to the input image yields the corrected image. Let’s see the steps using OpenCV-Python
Steps:
- Load the image
- Convert the image to RGB so as to display via matplotlib
- Select 4 points in the input image (counterclockwise, starting from the top left) by using matplotlib interactive window.
- Specify the corresponding output coordinates.
- Compute the perspective transform M using cv2.getPerspectiveTransform()
- Apply the perspective transformation to the input image using cv2.warpPerspective() to obtain the corrected image.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import cv2 import numpy as np import matplotlib.pyplot as plt # To open matplotlib in interactive mode %matplotlib qt # Load the image img = cv2.imread('D:/downloads/geometric.jpg') # Create a copy of the image img_copy = np.copy(img) # Convert to RGB so as to display via matplotlib # Using Matplotlib we can easily find the coordinates # of the 4 points that is essential for finding the # transformation matrix img_copy = cv2.cvtColor(img_copy,cv2.COLOR_BGR2RGB) plt.imshow(img_copy) |
By running the above code you will get an interactive matplotlib window popup. Now select any four points(better to select corner points) for the inputs. Then specify the corresponding output points.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Specify input and output coordinates that is used # to calculate the transformation matrix input_pts = np.float32([[80,1286],[3890,1253],[3890,122],[450,115]]) output_pts = np.float32([[100,100],[100,3900],[2200,3900],[2200,100]]) # Compute the perspective transform M M = cv2.getPerspectiveTransform(input_pts,output_pts) # Apply the perspective transformation to the image out = cv2.warpPerspective(img,M,(img.shape[1], img.shape[0]),flags=cv2.INTER_LINEAR) # Display the transformed image plt.imshow(out) |
Hope you enjoy reading.
If you have any doubt/suggestion please feel free to ask and I will do my best to help or improve myself. Good-bye until next time.