预处理扫描不良的手写数字

Preprocessing poorly scanned handwritten digits

我有几千个 PDF 文件,其中包含来自数字化纸质表格的黑白图像(1 位)。我正在尝试对某些字段进行 OCR,但有时字迹太模糊:

我刚刚了解了形态变换。他们真的很酷!!!我觉得我在滥用它们(就像我在学习 Perl 时对正则表达式所做的那样)。

我只对日期 07-06-2017 感兴趣:

im = cv2.blur(im, (5, 5))
plt.imshow(im, 'gray')

ret, thresh = cv2.threshold(im, 250, 255, 0)
plt.imshow(~thresh, 'gray')

填写此表格的人似乎对网格有些无视,所以我试图摆脱它。我可以用这个变换来隔离水平线:

horizontal = cv2.morphologyEx(
    ~thresh, 
    cv2.MORPH_OPEN, 
    cv2.getStructuringElement(cv2.MORPH_RECT, (100, 1)),
)
plt.imshow(horizontal, 'gray')

我也能得到竖线:

plt.imshow(horizontal ^ ~thresh, 'gray')

ret, thresh2 = cv2.threshold(roi, 127, 255, 0)
vertical = cv2.morphologyEx(
    ~thresh2, 
    cv2.MORPH_OPEN, 
    cv2.getStructuringElement(cv2.MORPH_RECT, (2, 15)), 
    iterations=2
)
vertical = cv2.morphologyEx(
    ~vertical, 
    cv2.MORPH_ERODE, 
    cv2.getStructuringElement(cv2.MORPH_RECT, (9, 9))
)
horizontal = cv2.morphologyEx(
    ~horizontal, 
    cv2.MORPH_ERODE, 
    cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
)
plt.imshow(vertical & horizontal, 'gray')

现在我可以去掉网格了:

plt.imshow(horizontal & vertical & ~thresh, 'gray')

我得到的最好的是这个,但是 4 仍然分成 2 个部分:

plt.imshow(cv2.morphologyEx(im2, cv2.MORPH_CLOSE, 
    cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))), 'gray')

可能此时最好使用 cv2.findContours 和一些启发式方法来定位每个数字,但我想知道:

  1. 我应该放弃并要求所有文件重新灰度扫描吗?
  2. 是否有更好的方法来隔离和定位微弱的数字?
  3. 你知道任何形态变换来连接像“4”这样的案例吗?

[更新]

Is rescanning the documents too demanding? If it is no great trouble I believe it is better to get higher quality inputs than training and trying to refine your model to withstand noisy and atypical data

一些背景信息: 我在巴西的 public 机构工作,是一个无名小卒。 ICR 解决方案的价格从 6 位数开始,因此没有人相信一个人可以编写 ICR 解决方案 in-house。我天真到相信我能证明他们是错的。这些 PDF 文档位于 FTP 服务器(大约 100K 个文件)中,并且被扫描只是为了摆脱死树版本。也许我可以获得原始表格并自己再次扫描,但我必须寻求一些官方支持 - 因为这是 public 部门,我想尽可能地将这个项目保持在地下。我现在拥有的是 50% 的错误率,但如果这种方法是死胡同,那么尝试改进它就没有意义了。

也许 Active contour model 之类的? 例如,我找到了这个库:https://github.com/pmneila/morphsnakes

拿下你最后的“4”号:

经过一些快速调整(没有真正理解参数,所以可能会得到更好的结果)我得到了这个:

使用以下代码(我也侵入了 morphsnakes.py 一点以保存图像):

import morphsnakes

import numpy as np
from scipy.misc import imread
from matplotlib import pyplot as ppl

def circle_levelset(shape, center, sqradius, scalerow=1.0):
    """Build a binary function with a circle as the 0.5-levelset."""
    grid = np.mgrid[list(map(slice, shape))].T - center
    phi = sqradius - np.sqrt(np.sum((grid.T)**2, 0))
    u = np.float_(phi > 0)
    return u

#img = imread("testimages/mama07ORI.bmp")[...,0]/255.0
img = imread("four.png")[...,0]/255.0

# g(I)
gI = morphsnakes.gborders(img, alpha=900, sigma=3.5)

# Morphological GAC. Initialization of the level-set.
mgac = morphsnakes.MorphGAC(gI, smoothing=1, threshold=0.29, balloon=-1)
mgac.levelset = circle_levelset(img.shape, (39, 39), 39)

# Visual evolution.
ppl.figure()
morphsnakes.evolve_visual(mgac, num_iters=50, background=img)