这是我得到的收据图像,我使用 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)
我从上面的代码中得到的边缘是每个文本的边缘,但这不是我需要的。我需要获取收据边缘,对吗?
另外,我需要一种方法来在消除图像倾斜(即拉直图像)后获取新的边界框坐标?
如果有人解决过类似的问题,请帮助我?谢谢。