【问题标题】:Perspective transform using Opencv使用 Opencv 进行透视变换
【发布时间】:2018-12-27 05:34:08
【问题描述】:

我正在尝试使用 Python 和 Open CV 实现透视转换。虽然通过选择图像上的 4 个点来完成变换,但输出图像高度模糊。即使我不使用鼠标事件来选择 4 个点(而不是硬编码),图像质量仍然模糊。这是我的编程尝试:

`def draw_circle(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img, (x, y), 5, (255, 0, 0), -1)
        p = (x, y)
        l.append(p)
        print(l)

cv2.namedWindow('image', cv2.WINDOW_NORMAL)
img = cv2.imread('Path  to my input image')
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.resizeWindow('image', 600, 600)

cv2.setMouseCallback('image', draw_circle)

while 1:
    cv2.imshow('image', img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

rows, cols, channels = img.shape
pts1 = np.float32(l)
pts2 = np.float32([[0, 0], [200, 0], [200, 100], [0, 100]])

M = cv2.getPerspectiveTransform(pts1, pts2)
dst = cv2.warpPerspective(img, M, (200, 100), cv2.INTER_LINEAR)

h1 = math.sqrt((abs(pts1[1][0] - pts1[0][0])) ** 2 + (abs(pts1[1][1] - pts1[0][1])) ** 2)
h2 = math.sqrt((abs(pts1[3][0] - pts1[2][0])) ** 2 + (abs(pts1[3][1] - pts1[2][1])) ** 2)
v1 = math.sqrt((abs(pts1[3][0] - pts1[0][0])) ** 2 + (abs(pts1[3][1] - pts1[0][1])) ** 2)
v2 = math.sqrt((abs(pts1[2][0] - pts1[1][0])) ** 2 + (abs(pts1[2][1] - pts1[1][1])) ** 2)
max_h = int(max(h1, h2))
max_v = int(max(v1, v2))


dst = cv2.resize(dst, (max_h, max_v))
plt.subplot(121), plt.imshow(img), plt.title('Input')
plt.subplot(122), plt.imshow(dst), plt.title('Output')
plt.show()`

这是我的输入图像:这是带有选择性饮料的冰箱图像

这是我的输出图像:这是透视变换后的输出图像

【问题讨论】:

标签: python opencv


【解决方案1】:

在你的代码中替换这一行

pts2 = np.float32([[0, 0], [200, 0], [200, 100], [0, 100]])

到这个(可能要切换v/h顺序,我不懂python语法):

pts2 = np.float32([[0, 0], [max_h,0], [max_h,max_v], [0,max_v]])

通过将 max_h/max_v 计算移至转换计算之前。然后删除调整大小的代码。

此时您第一次(隐式)调整为 100x200 的临时图像,如果您之后将其调整为更大的图像,将会非常模糊。

【讨论】:

  • 这个解决方案很有帮助!如果我想删除鼠标单击事件,有哪些可能的选项可以做到这一点?我可以完全自动化吗?
  • 感谢@Micka 的回答
  • 您需要自动检测和分割所需的对象。这是一项非常艰巨的任务,目前还没有通用的工作解决方案,并且可能需要大量的开放式工作。人们正在对图像中的对象进行这些点击和选择/裁剪,以训练机器学习检测器/分类器。如果有可能使这项任务自动化,他们就会有一个工作检测器/分类器;)
  • 感谢分享这个@Micka.Kudos!!
猜你喜欢
  • 2012-03-08
  • 1970-01-01
  • 2016-11-27
  • 2011-08-26
  • 2011-03-10
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 1970-01-01
相关资源
最近更新 更多