你还记得 我之前的帖子 吗?这个答案是基于那个帖子的。
因此,我获得了围绕书籍的边界框的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)
- rbo13rect = cv2.minAreaRect(cnt)
;box = cv2.boxPoints(rect)
;box = np.int0(box)
。四个角落都会在box
里面。更多信息请查看此文档。 - Jeru Lukepts_dst
中 :D - Jeru Luke