从盒装表单字段图像中提取手写字符

Extract handwritten characters from a boxed form field image

我正在尝试从字段框中提取手写字符

我想要的输出是删除了方框的字符段。到目前为止,我已经尝试定义轮廓并按区域过滤,但没有产生任何好的结果。

# Reading image and binarization
im = cv2.imread('test.png')

char_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
char_bw = cv2.adaptiveThreshold(char_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 75, 10)

# Applying erosion and dilation
kernel = np.ones((5,5), np.uint8) 
img_erosion = cv2.erode(char_bw, kernel, iterations=1)
img_dilation = cv2.dilate(img_erosion, kernel, iterations=1)  

# Find Canny edges 
edged = cv2.Canny(img_dilation, 100, 200) 

# Finding Contours 
edged_copy = edged.copy()
im2, cnts, hierarchy = cv2.findContours(edged_copy, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
print("Number of Contours found = " + str(len(cnts))) 

# Draw all contours 
cv2.drawContours(im, cnts, -1, (0, 255, 0), 3)

# Filter using area and save
for no, c in enumerate(cnts):
    area = cv2.contourArea(c)
    if area > 100:
        contour = c
        (x, y, w, h) = cv2.boundingRect(contour)
        img = im[y:y+h, x:x+w]
        cv2.imwrite(f'./cnts/cnt-{no}.png', img_dilation)

这是一个简单的方法:

  1. 得到二值图像。我们加载图像,使用imutils.resize()放大,转换为灰度,并进行Otsu的阈值处理得到二值图像

  2. 去除水平线。我们创建一个水平核然后进行形态学开运算并使用cv2.drawContours

    [去除水平线=50=]
  3. 去除垂直线。我们创建一个垂直内核然后执行形态学开运算并使用cv2.drawContours

    [去除垂直线=50=]

这是每个步骤的可视化:

二进制图像

检测到lines/boxes删除绿色突出显示

结果

代码

import cv2
import numpy as np
import imutils

# Load image, enlarge, convert to grayscale, Otsu's threshold
image = cv2.imread('1.png')
image = imutils.resize(image, width=500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Remove horizontal
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25,1))
detect_horizontal = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, horizontal_kernel, iterations=2)
cnts = cv2.findContours(detect_horizontal, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (255,255,255), 5)

# Remove vertical
vertical_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,25))
detect_vertical = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, vertical_kernel, iterations=2)
cnts = cv2.findContours(detect_vertical, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    cv2.drawContours(image, [c], -1, (255,255,255), 5)

cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()