gpt4 book ai didi

python - 从盒装表单域图像中提取手写字符

转载 作者:行者123 更新时间:2023-12-02 16:08:52 26 4
gpt4 key购买 nike

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

enter image description here

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

# 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)

最佳答案

这是一个简单的方法:

  • 获取二值图像。 我们加载图像,使用 imutils.resize() 放大,转换为灰度,并进行大津阈值处理,得到二值图像
  • 删除水平线。 我们创建一个水平内核,然后使用 cv2.drawContours 执行形态学打开并删除水平线。
  • 去除垂直线。 我们创建一个垂直内核,然后使用 cv2.drawContours 执行形态学打开并删除垂直线。


  • 这是每个步骤的可视化:

    二进制图像



    检测到的行/框以删除以绿色突出显示



    结果



    代码
    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()

    关于python - 从盒装表单域图像中提取手写字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59494048/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com