gpt4 book ai didi

python - OpenCV 用 Python 拉直图像

转载 作者:太空宇宙 更新时间:2023-11-03 21:21:37 25 4
gpt4 key购买 nike

有什么方法可以使用带有 Python 的 OpenCV 来拉直这张图像吗?我正在使用不同的转换来解决这个问题,但我无法理解。

Image

这是我的代码:

rows, cols, h = img.shape

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

然后我应用仿射变换。

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

我仍然无法得到想要拉直的图像输出。现在挠我的头将近一个小时了。谁能帮帮我?

最佳答案

你还记得吗my previous post ?此答案基于此。

所以我获得了书周围边界框的 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)

enter image description here

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

关于python - OpenCV 用 Python 拉直图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41995916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com