Updated April 19, 2023
Introduction to OpenCV perspectivetransform
In order to change the perspective of a given image or video or in order to align the given image or video as required in OpenCV, we make use of a function called PerspectiveTransform() in OpenCV and by making use of PerspectiveTranform() function, it is possible to obtain more insights on the required information from the image or video and the points on the image whose perspective is to be transformed as required is given to the PerspectiveTranform() function and that corresponding part of the image is transformed to the size of the original image using another function called warpPerspective() function.
The syntax to define PerspectiveTransform() function and warpPerspective() function in OpenCV is as follows:
cv2.PerspectiveTransform(source_coordinates, destination_coordinates)
where source_coordinates are the points on the source image whose perspective is to be changed and
destination_coordinates are the points corresponding to the points on the source image, in the destination image
cv2.warpPerspective(source_image, destination_image, destination_imagesize)
where source_image is the original image whose perspective is to be transformed,
destination_image is the image whose perspective is transformed as per the size destination_imagesize and
destination_imagesize is the size of the destination image.
Working of PerspectiveTransform() function in OpenCV :
Let us see the working of this function mentioned below:
- Sometimes we have the required images or videos but the necessary information is not properly visible. In such cases, we may have to change the alignment of the images or videos or change the perspective of the images or videos to obtain better insights into the required information from the image or video.
- Then we make use of a function called PerspectiveTransform() function in OpenCV.
- The PerspectiveTransform() function takes the coordinate points on the source image which is to be transformed as required and the coordinate points on the destination image that corresponds to the points on the source image as the input parameters.
- The PerspectiveTransform() function returns the matrix of the transformed perspective as the output.
- Then the original source image and the resulting matrix from the PerspectiveTransform() function along with the size of the required output image is passed to the warpPerspective() function to obtain the transformed image.
- The aligned or transformed image of the source image as per the required size is returned by the warpPerspective() function.
Examples of OpenCV perspectivetransform
Here are the following examples mention below:
Example #1
OpenCV program in python to demonstrate PerspectiveTrasnform() function using which we are going to transform the perspective of a given image to obtain more insights and use warpPerspective() function to display it as the output image as per the required size:
Code:
#importing the module cv2 and numpy
import cv2
import numpy as np
while True:
#reading the image which is to be transformed
imagergb = cv2.imread('C:/Users/admin/Desktop/plane.jpg')
#specifying the points in the source image which is to be transformed to the corresponding points in the destination image
srcpts = np.float32([[0, 100], [700, 260], [0, 700], [700, 400]])
destpts = np.float32([[0, 200], [600, 0], [0, 700], [1000, 700]])
#applying PerspectiveTransform() function to transform the perspective of the given source image to the corresponding points in the destination image
resmatrix = cv2.getPerspectiveTransform(srcpts, destpts)
#applying warpPerspective() function to display the transformed image
resultimage = cv2.warpPerspective(imagergb, resmatrix, (500, 600))
#displaying the original image and the transformed image as the output on the screen
cv2.imshow('frame', imagergb)
cv2.imshow('frame1', resultimage)
if cv2.waitKey(24) == 27:
break
The output of the above program is shown in the snapshot below:
In the above program, we are importing the cv2 and numpy modules. Then we are reading the image whose perspective is to be transformed, using the imread() function. Then we are specifying the points in the source image which is to be transformed to the corresponding points in the destination image using PerspectiveTransform() function. Then we are passing the resultant matrix from the PerspectiveTransform() function along with the original image and expected size of the output image to the warpPerspective() function to obtain the transformed image as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate PerspectiveTrasnform() function using which we are going to transform the perspective of a given image to obtain more insights and use warpPerspective() function to display it as the output image as per the required size:
Code:
#importing the module cv2 and numpy
import cv2
import numpy as np
while True:
#reading the image which is to be transformed
imagergb = cv2.imread('C:/Users/admin/Desktop/educba.jpg')
#specifying the points in the source image which is to be transformed to the corresponding points in the destination image
srcpts = np.float32([[0, 100], [700, 260], [0, 700], [700, 400]])
destpts = np.float32([[0, 200], [600, 0], [0, 700], [1000, 700]])
#applying PerspectiveTransform() function to transform the perspective of the given source image to the corresponding points in the destination image
resmatrix = cv2.getPerspectiveTransform(srcpts, destpts)
#applying warpPerspective() function to display the transformed image
resultimage = cv2.warpPerspective(imagergb, resmatrix, (500, 600))
#displaying the original image and the transformed image as the output on the screen
cv2.imshow('frame', imagergb)
cv2.imshow('frame1', resultimage)
if cv2.waitKey(24) == 27:
break
The output of the above program is shown in the snapshot below:
In the above program, we are importing the cv2 and numpy modules. Then we are reading the image whose perspective is to be transformed, using imread() function. Then we are specifying the points in the source image which is to be transformed to the corresponding points in the destination image using PerspectiveTransform() function. Then we are passing the resultant matrix from the PerspectiveTransform() function along with the original image and expected size of the output image to the warpPerspective() function to obtain the transformed image as the output on the screen. The output is shown in the snapshot above.
Conclusion
In this article, we have learned the concept of PerspectiveTransform() function through definition, syntax, and working of PerspectiveTransform() function with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “OpenCV perspectivetransform” was beneficial to you. You can view EDUCBA’s recommended articles for more information.