如何在OPENCV中对手写的帐号和分类代码进行数字分割?

时间:2019-03-14 11:55:30

标签: opencv image-segmentation digits handwriting-recognition

是否有一种简单的方法可以对文件进行数字分割,如下面的屏幕截图所示?

我想使用OpenCV来完成它,因为它是我用来执行其余处理的库,但是欢迎其他建议。

纸张形式: PAPER FORM

文本框: Text box

2 个答案:

答案 0 :(得分:0)

简单的OpenCV等高线方法在这里不起作用,因为在某种类型的框模板中都存在数字,因此您需要先检测出框blog

答案 1 :(得分:0)

采用这段代码并适应您的问题。您的情况并不难:

import cv2
import numpy as np

# import image
image = cv2.imread('C:\\Users\\PC\\Desktop\\roi.png')

# grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)

# binary
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('threshold', thresh)

# dilation
kernel = np.ones((10, 1), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)

# find contours
# cv2.findCountours() function changed from OpenCV3 to OpenCV4: now it have only two parameters instead of 3
cv2MajorVersion = cv2.__version__.split(".")[0]
# check for contours on thresh
if int(cv2MajorVersion) >= 4:
    ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
else:
    im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y + h, x:x + w]

    # show ROI
    # cv2.imshow('segment no:'+str(i),roi)
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)

    if w > 15 and h > 15:
        cv2.imwrite('C:\\Users\\PC\\Desktop\\output\\{}.png'.format(i), roi)

cv2.imshow('marked areas', image)
cv2.waitKey(0)

来源:https://lucians.dev/extract-roi-from-image-with-python-and-opencv