API:cv2.warpPerspective(img,M,dsize,....)
M是变换矩阵,M是一个3*3的变换矩阵
获取变换矩阵的方法:
API:cv2.getPersectiveTransform(src,dst)(至少需要四个点)
import cv2
import numpy as np
img = cv2.imread('d:\\download\\2.jpg')
img1 = cv2.resize(img, (600, 400)) # (宽,高),dsize是根据(X,Y),而img.shape输出的是(y,x)
img2 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
h, w = img2.shape
src = np.float32([[200, 400], [600, 300], [400, 100], [300, 200]])
dst = np.float32([[300, 600], [500, 400], [200, 200], [400, 100]])
M = cv2.getPerspectiveTransform(src, dst)
img3 = cv2.warpPerspective(img2, M, (600, 400))
img4 = np.hstack([img3, img2])
cv2.imshow('img4', img4)
cv2.waitKey(0)
运行结果