OpenCV透视损失图像

2024-06-25 06:11:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用OpenCV将透视图转换为平面图像。在例子中,我发现,它工作得很完美,当我使用简单的角度-它的作品。(example 1

但在我的例子中,当我使用不同的角度标记图像的地方,我需要转换,它丢失扭曲的图像。(example 2

如何解决它,并得到图像无论如何-不重要的是我用什么角度?你知道吗

points = []
for i in range(0, 4):
    point = cv2.selectROI('Select speed', frame)
    points.append((point[0], point[1]))

widthA = np.sqrt(((points[0][0] - points[1][0]) ** 2) + ((points[0][1] - points[1][1]) ** 2))
heightA = np.sqrt(((points[1][0] - points[2][0]) ** 2) + ((points[1][1] - points[2][1]) ** 2))
widthB = np.sqrt(((points[2][0] - points[3][0]) ** 2) + ((points[2][1] - points[3][1]) ** 2))
heightB= np.sqrt(((points[3][0] - points[0][0]) ** 2) + ((points[3][1] - points[0][1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
maxHeight = max(int(heightA), int(heightB))
dst_def = np.array([[0, 0],
                   [maxWidth-1, 0],
                   [maxWidth-1, maxHeight-1],
                   [0, maxHeight-1]], dtype='float32')
points = np.array(points, dtype = "float32")
m = cv2.getPerspectiveTransform(order_points(points), dst_def)
warped = cv2.warpPerspective(frame, m, (maxWidth, maxHeight))
cv2.imshow('Image', warped)
cv2.waitKey(0)

Tags: 图像examplenpsqrtcv2framepoints例子