python - 无法分割手写字符

我正在尝试从图像中提取手写数字和字母表,为此我点击了这个 stackoverflow 链接。对于大多数使用标记书写字母的图像,它工作正常,但是当我使用使用 Pen 写入数据的图像时,它会失败得很惨。需要一些帮助来解决这个问题。

下面是我的代码:

import cv2
import imutils
from imutils import contours

# Load image, grayscale, Otsu's threshold
image = cv2.imread('xxx/pic_crop_7.png')
image = imutils.resize(image, width=350)
img=image.copy()

# Remove border
kernel_vertical = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
temp1 = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel_vertical)
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
temp2 = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, horizontal_kernel)
temp3 = cv2.add(temp1, temp2)
result = cv2.add(temp3, image)

# Convert to grayscale and Otsu's threshold
gray = cv2.cvtColor(result, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray,(5,5),0)
_,thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)
# thresh=cv2.dilate(thresh,None,iterations=1)

# Find contours and filter using contour area
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[0]

MIN_AREA=45

digit_contours = []
for c in cnts:
    if cv2.contourArea(c)>MIN_AREA:
        x,y,w,h = cv2.boundingRect(c)
        cv2.rectangle(img, (x, y), (x + w, y + h), (36,255,12), 2)
        digit_contours.append(c)
#         cv2.imwrite("C:/Samples/Dataset/ocr/segmented" + str(i) + ".png", image[y:y+h,x:x+w])


sorted_digit_contours = contours.sort_contours(digit_contours, method='left-to-right')[0]
contour_number = 0
for c in sorted_digit_contours:
    x,y,w,h = cv2.boundingRect(c)
    ROI = image[y:y+h, x:x+w]
    cv2.imwrite('xxx/segment_{}.png'.format(contour_number), ROI)
    contour_number += 1
    
    
cv2.imshow('thresh', thresh)
cv2.imshow('img', img)
cv2.waitKey()

使用marker书写时能够正确提取数字。

下面是一个例子:

原始图片

正确提取字符

无法读取的图像。

原始图片

提取不正确

最佳答案

在这种情况下,您只需调整您的参数即可。 因为你的手写字背景没有竖线,所以我决定删掉。

# Remove border
horizontal_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
temp2 = 255 - cv2.morphologyEx(image, cv2.MORPH_CLOSE, horizontal_kernel)
result = cv2.add(temp2, image)

而且有效。

https://stackoverflow.com/questions/66312009/

相关文章:

c++ - CMake 找不到生成的 Visual Studio 15 2017 实例,但可以与 V

common-lisp - 拆分格式字符串(格式 t ...)

reactjs - 如何防止 Nivo 刻度轴文本(条形图)中的文本截断

debugging - 调试 : Couldn't match expected type ‘GHC

regex - 计算 Perl 中字符串中非空白字符的数量

c - 如何在不使用数组的情况下验证输入格式,例如输入 "OIL2932"

database - Mongo错误QueryExceededMemoryLimitNoDiskUs

c++ - 如何在 constexpr if-else 链中导致静态错误?

java - 使用 LinkedHashMap 的字符串中的第一个唯一字符

angular - PrimeNG 报错 ConfirmationService has not b