你的方法是正确的.指定角点的坐标时会出现问题.我不知道你是如何计算它们的,但你已经交换了你的X轴和Y轴.这反映在应用于最终图像的变换中.我发现角点是:
ptsTrap = [[[ 99. 51.]]
[[ 64. 251.]]
[[ 234. 251.]]
[[ 199. 51.]]]
ptsRect = [[[ 102. 49.]]
[[ 100. 249.]]
[[ 200. 250.]]
[[ 200. 50.]]]
从这些点中查找透视变换会得到正确的结果:
作为参考,这是我使用的代码:
import cv2
import numpy as np
def find_corners(image):
im = cv2.Canny(image,100,200)
cnt = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0]
cnt = cv2.approxPolyDP(cnt[0],5,True)
return cnt.astype(np.float32)
def main(argv):
trap = cv2.imread('trap.png',cv2.IMREAD_GRAYSCALE)
rect = cv2.imread('rect.png',cv2.IMREAD_GRAYSCALE)
ptsTrap = find_corners(trap)
ptsRect = find_corners(rect)
T = cv2.getPerspectiveTransform(ptsTrap,ptsRect)
warp = cv2.warpPerspective(trap,rect.shape[:2])
cv2.imshow('',warp)
cv2.imwrite('warp.png',warp)
cv2.waitKey()
cv2.destroyAllWindows()