gpt4 book ai didi

python - 无法分割手写字符

转载 作者:行者123 更新时间:2023-12-05 08:48:13 25 4
gpt4 key购买 nike

我正在尝试从图像中提取手写数字和字母表,为此我点击了这个 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书写时能够正确提取数字。

下面是一个例子:

原始图片

Original Image

正确提取字符

Correctly extracting charachters

无法读取的图像。

原始图片

enter image description here

提取不正确

enter image description here

最佳答案

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

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

而且有效。

enter image description here enter image description here

关于python - 无法分割手写字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66312009/

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