如何将轮廓放入图像编号中? (文字识别)

How to put the contours in a image numbers? (OCR)

我已经研究 CV 几个月了,但我 运行 在我的第二个项目中遇到了一个问题,我需要从数字序列中去除噪音,以便应用 ocr。我设法清理了它,但数字丢失了一些内部像素。

查看初始图像和当前最终图像。

Initial

Final

使用的代码:

blur = cv2.GaussianBlur(img, (15, 15), 2)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
lower_gray = np.array([1, 1, 1])
upper_gray = np.array([102, 102, 102])
mask = cv2.inRange(hsv, lower_gray, upper_gray)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
opened_mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
masked_img = cv2.bitwise_and(img, img, mask=opened_mask)
coloured = masked_img.copy()
coloured[mask == 0] = (255, 255, 255)

gray = cv2.cvtColor(coloured, cv2.COLOR_BGR2GRAY)

des = cv2.bitwise_not(gray)

contour, hier = cv2.findContours(des, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contour:
    cv2.drawContours(des, [cnt], 0, 255, -1)

#des is the final image

有没有更好的方法来清理 OCR 的背景,或者关闭字符中丢失的像素?

很容易通过形态学膨胀或闭合得到干净的背景,通过高斯滤波得到平滑的轮廓,然后二值化。但是我没有找到分隔字符的方法。

我设法解决了,我没有使用你说的方法,但这是一个好方法,我担心它会导致字符膨胀,不利于OCR阅读。

This is my final result:

for mrz in mrz_list:
    try:
        thresh = cv2.threshold(mrz, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]

        dist = cv2.distanceTransform(thresh, cv2.DIST_L2, 5)
        dist = cv2.normalize(dist, dist, 0, 1.0, cv2.NORM_MINMAX)
        dist = (dist * 255).astype("uint8")

        thresh = cv2.threshold(dist, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

        kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
        opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)

        cnts = cv2.findContours(opening.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        cnts = imutils.grab_contours(cnts)
        chars = []
        for c in cnts:
            (x, y, w, h) = cv2.boundingRect(c)
            if w >= 20 and h >= 25:
                chars.append(c)

        chars = np.vstack([chars[i] for i in range(0, len(chars))])
        hull = cv2.convexHull(chars)

        mask = np.zeros(mrz.shape[:2], dtype="uint8")
        cv2.drawContours(mask, [hull], -1, 255, -1)
        mask = cv2.dilate(mask, None, iterations=2)

        final = cv2.bitwise_and(opening, opening, mask=mask)`

谢谢大家