📜  透视变换Python OpenCV

📅  最后修改于: 2022-05-13 01:55:47.520000             🧑  作者: Mango

透视变换Python OpenCV

透视转换中,我们可以更改给定图像或视频的透视,以便更好地了解所需信息。在透视变换中,我们需要通过改变透视来提供图像上要从中收集信息的点。我们还需要提供要在其中显示图像的点。然后,我们从给定的两组点中得到透视变换,并将其与原始图像包裹起来。

我们使用cv2.getPerspectiveTransform然后cv2.warpPerspective

下面是解释透视变换的Python代码:

Python3
# import necessary libraries
 
import cv2
import numpy as np
 
# Turn on Laptop's webcam
cap = cv2.VideoCapture(0)
 
while True:
     
    ret, frame = cap.read()
 
    # Locate points of the documents
    # or object which you want to transform
    pts1 = np.float32([[0, 260], [640, 260],
                       [0, 400], [640, 400]])
    pts2 = np.float32([[0, 0], [400, 0],
                       [0, 640], [400, 640]])
     
    # Apply Perspective Transform Algorithm
    matrix = cv2.getPerspectiveTransform(pts1, pts2)
    result = cv2.warpPerspective(frame, matrix, (500, 600))
     
    # Wrap the transformed image
    cv2.imshow('frame', frame) # Initial Capture
    cv2.imshow('frame1', result) # Transformed Capture
 
    if cv2.waitKey(24) == 27:
        break
 
cap.release()
cv2.destroyAllWindows()


输出: