【问题标题】:OpenCV Straighten an Image with PythonOpenCV 用 Python 拉直图像
【发布时间】:2017-02-02 06:54:05
【问题描述】:

有什么方法可以使用 OpenCV 和 Python 拉直这个图像?我正在使用不同的转换来解决它,但我无法得到它。

这是我的代码:

rows, cols, h = img.shape

M = np.float32([[1, 0, 100], [0, 1, 50]])

然后我应用仿射变换。

dst = cv2.warpAffine(roi, M, (cols, rows))

我仍然无法得到想要的图像输出。抓挠我的头快一个小时了。谁能帮帮我?

【问题讨论】:

  • 只有知道旋转角度或者知道矩形的边界点才有可能。
  • 边界点是什么意思?
  • 限定书籍边界的粉红色框
  • @ZdaR 我会根据粉色框调整它吗?

标签: python-2.7 opencv image-processing image-rotation affinetransform


【解决方案1】:

你还记得我之前的帖子吗?这个答案就是基于此。

所以我得到了书本边界框的 4 个角点,并将其输入到单应性函数中。

代码:

#---- 4 corner points of the bounding box
pts_src = np.array([[17.0,0.0], [77.0,5.0], [0.0, 552.0],[53.0, 552.0]])

#---- 4 corner points of the black image you want to impose it on
pts_dst = np.array([[0.0,0.0],[77.0, 0.0],[ 0.0,552.0],[77.0, 552.0]])

#---- forming the black image of specific size
im_dst = np.zeros((552, 77, 3), np.uint8)

#---- Framing the homography matrix
h, status = cv2.findHomography(pts_src, pts_dst)

#---- transforming the image bound in the rectangle to straighten
im_out = cv2.warpPerspective(im, h, (im_dst.shape[1],im_dst.shape[0]))
cv2.imwrite("im_out.jpg", im_out)

因为您在书本周围有轮廓边界框;您必须将这 4 个点输入数组 pts_src

【讨论】:

  • 啊,好吧...从来没想过我可以在这种情况下应用它。多谢,伙计。一个问题,你是怎么得到这个值的? pts_src = np.array([[17.0,0.0], [77.0,5.0], [0.0, 552.0],[53.0, 552.0]])pts_dst = np.array([[0.0,0.0],[77.0, 0.0],[ 0.0,552.0],[77.0, 552.0]])im_dst = np.zeros((552, 77, 3), np.uint8)
  • 我在边界框的角上手动绘制了圆圈。您所要做的就是从您获得的轮廓中获取这些点
  • 如何从轮廓中获取点?因为当我打印它时,它会返回一个集合。我不知道从哪里开始。希望你能给我建议。非常感谢。
  • 您是如何找到并绘制轮廓的?找到轮廓后,为轮廓应用以下代码:rect = cv2.minAreaRect(cnt); box = cv2.boxPoints(rect); box = np.int0(box)。四个角将在box 内。请参阅THIS DOC 了解更多信息
  • 嗨@JeruLuke。我会在cv2.findHomography()box 吗?这是我迄今为止在link 中的进展
猜你喜欢
  • 2012-07-22
  • 1970-01-01
  • 2021-10-25
  • 1970-01-01
  • 2017-08-27
  • 2019-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多