Geometric Transformation OpenCV Python
In this article, you will learn about the geometric transformation of images using Python OpenCV.
Starting from the existing position, direction, or shape, geometric transformations are expected to give an entity the required position, direction, or shape. Geometric transformations are also called spatial transformations or 2D transformations. This includes translation, rotation, scaling, and non-linear warping of images. It is used to make some changes to any given geometric shape.
Python OpenCV provides two transformation functions with which we can perform all kinds of transformations - cv.warpAffine() and cv.warpPerspective(). The cv2.warpAffine() method takes a 2x3 transformation matrix as input, while cv2.warpPerspective() method takes a 3x3 transformation matrix as input. Here, we have mentioned some mostly used geometric transformation using Python OpenCV.
OpenCV Image Translation
Translation is the shifting of an object's location without changing anything in it. Hence, the shape, size, and orientation remain the same. First, we have taken a numpy array of type np.float32 and passed it into the function cv2.warpAffine(). The matrix_trans represents a 2 X 3 transformation matrix. Here is an example of image transformation.
import cv2
import numpy as np
img = cv2.imread('nature.jpg')
rows, cols, ch = img.shape
matrix_trans = np.float32([[1, 0, -100], [0, 1, -30]])
translated_img = cv2.warpAffine(img, matrix_trans, (cols, rows))
cv2.imshow("Translated image", translated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Here is the output -
OpenCV Image Scaling
Image scaling refers to the resizing of images. This is mostly used in many image processing as well as machine learning applications. It helps in reducing or zooming the number of pixels in an input image. The Python OpenCV library cv2 provides a cv2.resize() function to resize an image.
import cv2
import numpy as np
img = cv2.imread('nature.jpg')
scaled_img = cv2.resize(img, None, fx=0.6, fy=0.6)
cv2.imshow("Scaled image",scaled_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV Image Rotation
In Python OpenCV, we can rotate an image at different angles, like - 90, 180, 270 degrees. For this, first we have to calculate the affine matrix that does the affine transformation and then warp the input image with the affine matrix.
import cv2
import numpy as np
img = cv2.imread('nature.jpg')
rows, cols, ch = img.shape
matrix_rotated = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 0.6)
rotated_img = cv2.warpAffine(img, matrix_rotated, (cols, rows))
cv2.imshow("Rotated image",rotated_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV Affine transformations
In affine transformation, all parallel lines in the original image will still be parallel in the output image. To find the transformation matrix, we need three points from the input image and their corresponding locations in the output image. Then cv2.getAffineTransform() will create a 2x3 matrix which is to be passed to cv2.warpAffine().
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('nature.jpg')
rows,cols,ch = img.shape
pt1 = np.float32([[40,40],[200,40],[40,200]])
pt2 = np.float32([[10,100],[200,50],[100,250]])
matrix_aff = cv2.getAffineTransform(pt1,pt2)
dst = cv2.warpAffine(img,matrix_aff,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
OpenCV Perspective Transformation
For perspective transformation, you need a 3x3 transformation matrix. Straight lines will remain straight even after the transformation. To find this transformation matrix, you need 4 points on the input image and corresponding points on the output image. Among these 4 points, 3 of them should not be collinear. Then the transformation matrix can be found by the function cv2.getPerspectiveTransform(). Then apply cv2.warpPerspective() with this 3x3 transformation matrix.
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('nature.jpg')
rows,cols,ch = img.shape
pt1 = np.float32([[50,65],[370,52],[30,387],[390,390]])
pt2 = np.float32([[0,0],[310,0],[0,310],[310,310]])
matrix_aff = cv2.getPerspectiveTransform(pt1,pt2)
dst = cv2.warpPerspective(img,matrix_aff,(cols,rows))
plt.subplot(121),plt.imshow(img),plt.title('Input')
plt.subplot(122),plt.imshow(dst),plt.title('Output')
plt.show()
Reference links - https://docs.opencv.org/3.4/d4/d61/tutorial_warp_affine.html
Related Articles
How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples
Python OpenCV Gaussian Blur Filtering
Python program to map two lists into a dictionary
Install NLTK for Python on Windows 64 bit
Find the stop words in nltk Python