OpenCV 使用 Python 拉直图像
OpenCV Straighten an Image with Python
有什么方法可以使用带有 Python 的 OpenCV 来拉直这张图像吗?我正在使用不同的转换来解决这个问题,但我无法理解。
这是我的代码:
rows, cols, h = img.shape
M = np.float32([[1, 0, 100], [0, 1, 50]])
然后我应用仿射变换。
dst = cv2.warpAffine(roi, M, (cols, rows))
我仍然无法得到想要拉直的图像输出。现在挠我的头将近一个小时了。任何人都可以帮助我吗?
还记得我之前的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)
因为书周围有轮廓边界框;你必须将这 4 个点输入数组 pts_src
.
有什么方法可以使用带有 Python 的 OpenCV 来拉直这张图像吗?我正在使用不同的转换来解决这个问题,但我无法理解。
这是我的代码:
rows, cols, h = img.shape
M = np.float32([[1, 0, 100], [0, 1, 50]])
然后我应用仿射变换。
dst = cv2.warpAffine(roi, M, (cols, rows))
我仍然无法得到想要拉直的图像输出。现在挠我的头将近一个小时了。任何人都可以帮助我吗?
还记得我之前的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)
因为书周围有轮廓边界框;你必须将这 4 个点输入数组 pts_src
.