我有一张收据图片,我使用matplotlib绘制了它,如果你看到这张图片,你会发现其中的文本并不是水平的。如何进行去斜和修复?
from skimage import io
import cv2
# x1, y1, x2, y2, x3, y3, x4, y4
bbox_coords = [[20, 68], [336, 68], [336, 100], [20, 100]]
image = io.imread('https://i.ibb.co/3WCsVBc/test.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray, cmap='Greys_r')
# for plotting bounding box uncomment the two lines below
#rect = Polygon(bbox_coords, fill=False, linewidth=1, edgecolor='r')
#ax.add_patch(rect)
plt.show()
print(gray.shape)
(847, 486)
我认为如果我们想要去除偏斜,首先需要找到边缘,因此我尝试使用Canny算法找到边缘,然后获取如下的轮廓。
from skimage import filters, feature, measure
def edge_detector(image):
image = filters.gaussian(image, 2, mode='reflect')
edges = feature.canny(image)
contours = measure.find_contours(edges, 0.8)
return edges, contours
fig, ax = plt.subplots(figsize=(20, 20))
ax.imshow(gray, cmap='Greys_r');
gray_image, contours = edge_detector(gray)
for n, contour in enumerate(contours):
ax.plot(contour[:, 1], contour[:, 0], linewidth=2)
从上面的代码中得到的边缘是每个文本的边缘,但这不是我需要的。我需要得到收据的边缘,对吗?
另外,我需要一种方法来获取去斜图像后的新边界框坐标(即将图像变直)?
如果有人已经解决了类似的问题,请帮帮我?谢谢。