Updated April 18, 2023
Introduction to OpenCV warpPerspective()
Sometimes the images or videos captured may not be aligned for us to view enough information from the images or videos, in such cases, it is necessary to align such images or videos to obtain better insights from the images or videos and in order to be able to change the perspective of the images or videos to obtain more useful information from the images or videos, we make use of a function called getPerspectiveTransform() function and the changed perspective of the images or videos must be fit to the original images or videos and this can be done using a function called warpPerspective() function in OpenCV.
Syntax to define warpPerspective() function in OpenCV is as follows:
warpPerspective(source_image, result_image, outputimage_size)
where source_image is the image whose perspective is to be changed using the getPerspectiveTransform() function,
result_image is the image returned by using getPerspectiveTransform() on the source_image and
outputimage_size is the size of the output image that fits the size of the original image using the warpPerspective() function.
How warpPerspective() function works in OpenCV?
Working of warpPerspective() function in OpenCV is as follows:
- The perspectives of the images or videos can be aligned to obtain better insights of the images or videos extracting useful information.
- We make use of a function called getPerspectiveTransform() function in OpenCV to change the perspective of the images or videos.
- The getPerspectiveTransform() function returns an image that gives more insights into the required information from the given image or video.
- The size of the returned image by using the getPerspectiveTransform() function must be fit to the size of the original image or video.
- We make use of a function called warpPerspective() function to fit the size of the resulting image by using the getPerspectiveTransform() function to the size of the original image or video.
- The warpPerspective() function returns an image or video whose size is the same as the size of the original image or video.
Examples
Let us discuss examples of OpenCV warpPerspective().
Example #1
OpenCV program in python to demonstrate warpPerspective() function to read the given image and align the given image and then fit the size of the aligned image to the size of the original image using warpPerspective() function:
#importing the module cv2 and numpy
import cv2
import numpy as np
#reading the image whose perspective is to be changed
imageread = 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
points1 = np.float32([[0, 100], [700, 260], [0, 700], [700, 400]])
points2 = np.float32([[0, 200], [600, 0], [0, 700], [1000, 700]])
#applying getPerspectiveTransform() function to transform the perspective of the given source image to the corresponding points in the destination image
resultimage = cv2.getPerspectiveTransform(points1, points2)
#applying warpPerspective() function to fit the size of the resulting image from getPerspectiveTransform() function to the size of source image
finalimage = cv2.warpPerspective(imageread, resultimage, (500, 600))
#displaying the original image and the transformed image as the output on the screen
cv2.imshow('Source_image', imageread)
cv2.imshow('Destination_image', finalimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, the modules cv2 and NumPy are imported. Then we are reading the image whose perspective is to be aligned, using the imread() function. Then we are making use of the getPerspectiveTranform() function to change the perspective of the source image. Then we are passing the resulting image from the getPerspectiveTransform() function to the warp perspective() function to fit the size of the resulting image from the getPerspectiveTransform() function to the size of the original image.
Example #2
OpenCV program in python to demonstrate warpPerspective() function to read the given image and align the given image and then fit the size of the aligned image to the size of the original image using warpPerspective() function:
#importing the module cv2 and numpy
import cv2
import numpy as np
#reading the image whose perspective is to be changed
imageread = cv2.imread('C:/Users/admin/Desktop/car.jpg')
#specifying the points in the source image which is to be transformed to the corresponding points in the destination image
points1 = np.float32([[0, 100], [700, 260], [0, 700], [700, 400]])
points2 = np.float32([[0, 200], [600, 0], [0, 700], [1000, 700]])
#applying getPerspectiveTransform() function to transform the perspective of the given source image to the corresponding points in the destination image
resultimage = cv2.getPerspectiveTransform(points1, points2)
#applying warpPerspective() function to fit the size of the resulting image from getPerspectiveTransform() function to the size of source image
finalimage = cv2.warpPerspective(imageread, resultimage, (500, 600))
#displaying the original image and the transformed image as the output on the screen
cv2.imshow('Source_image', imageread)
cv2.imshow('Destination_image', finalimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, the modules cv2 and NumPy are imported. Then we are reading the image whose perspective is to be aligned, using the imread() function. Then we are making use of the getPerspectiveTranform() function to change the perspective of the source image. Then we are passing the resulting image from the getPerspectiveTransform() function to the warp perspective() function to fit the size of the resulting image from the getPerspectiveTransform() function to the size of the original image.
Example #3
OpenCV program in python to demonstrate warpPerspective() function to read the given image and align the given image and then fit the size of the aligned image to the size of the original image using warpPerspective() function:
#importing the module cv2 and NumPy
import cv2
import NumPy as np
#reading the image whose perspective is to be changed
imageread = cv2.imread('C:/Users/admin/Desktop/car.jpg')
#specifying the points in the source image which is to be transformed to the corresponding points in the destination image
points1 = np.float32([[0, 100], [700, 260], [0, 700], [700, 400]])
points2 = np.float32([[0, 200], [600, 0], [0, 700], [1000, 700]])
#applying getPerspectiveTransform() function to transform the perspective of the given source image to the corresponding points in the destination image
resultimage = cv2.getPerspectiveTransform(points1, points2)
#applying warpPerspective() function to fit the size of the resulting image from getPerspectiveTransform() function to the size of source image
finalimage = cv2.warpPerspective(imageread, resultimage, (500, 600))
#displaying the original image and the transformed image as the output on the screen
cv2.imshow('Source_image', imageread)
cv2.imshow('Destination_image', finalimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
The output of the given program is shown in the snapshot below:
In the above program, the modules cv2 and NumPy are imported. Then we are reading the image whose perspective is to be aligned, using the imread() function. Then we are making use of the getPerspectiveTranform() function to change the perspective of the source image. Then we are passing the resulting image from the getPerspectiveTransform() function to the warpperspective() function to fit the size of the resulting image from getPerspectiveTransform() function to the size of the original image.
Conclusion
In this article, we have learned the concept of warpPerspective() function in OpenCV with corresponding programming examples.
Recommended Articles
We hope that this EDUCBA information on “OpenCV warpPerspective()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.