cv2.warpPerspective严重扭曲了图像

时间:2019-06-10 15:19:13

标签: python-2.7 computer-vision

我正在尝试拉伸图像,使其具有真实尺寸比例。我知道一些内部点的比例,并且正在使用这些点扭曲图像的透视图,但是似乎没有任何作用。

可以在此处获取图像:https://drive.google.com/file/d/1LUnnzKHDcMw3GnQb7UCyjjs_LtPI8o_6/view?usp=sharing

我已尝试在此问题的许多不同版本上使用cv2.warpPerspective,并且它们都工作正常,但是这次不起作用。

import numpy as np
import cv2
image = cv2.imread("/Desktop/20190203071840.jpg", 1)
#bridge
image[750:760, 520:530] = [0, 255,0]
#river bend
image[900:910, 3200:3210] = [0, 0,255]
#railing
image[1650:1660, 530:540] = [255, 0,0]
#mid river
image[1640:1650, 3250:3260] = [255, 255,255]

ih, iw, _ = image.shape


#four corners of the image
#pts = np.array([[0,0], [iw,0],[0,ih],[iw,ih]], np.float32)

#bridge, river bend, railing, river/tree
pts = np.array([[750,520], [900, 3200], [1650,530], [1640,3250]],     np.float32)

#real size mapping calculated with geometry
#top left, top rigt, bottom left, bottom right
dst = np.array([[0,0],[575,300],[645,235],[636,445]], np.float32)

#find transformation matrix 
M = cv2.getPerspectiveTransform(pts, dst)

#create a background for our real-sized image 
black = np.zeros((ih , iw, 3), np.uint8)
cv2.imwrite('black.jpg',black)
bh, bw, _ = black.shape

image = cv2.warpPerspective(image, M, (black.shape[1], black.shape[0]))
#resize and show image
image = cv2.resize(image, None, fx= .3, fy=.3, interpolation =     cv2.INTER_LINEAR)
cv2.imshow("Original with Bounding Box and Ellipse", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

我希望从pts中的四个点创建的矩形会延伸到由dst中四个点创建的梯形形状。相反,图像变得严重失真。

我该如何解决这种失真?

0 个答案:

没有答案
相关问题