我已经尝试过下面的代码,但是它仅适用于这样写为该图像的名称,我一次得到的结果是单个字母。
import cv2
import numpy as np
img = cv2.imread('data1/NAME.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
kernel = np.ones((3, 3), np.uint8)
imgMorph = cv2.erode(thresh1, kernel, iterations = 1)
contours, hierarchy = cv2.findContours(imgMorph,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
i=1
for cnt in contours:
x,y,w,h = cv2.boundingRect(cnt)
if w>10 and w<100 and h>10 and h<100:
#save individual images
cv2.imwrite("data1/NAME_{}.png".format((i)),thresh1[y:y+h,x:x+w])
i=i+1
cv2.imshow('BindingBox',imgMorph)
cv2.waitKey(0)
cv2.destroyAllWindows()
当公用线与其他字母一样粗时,您不能使用形态学操作来分离触摸或重叠的字母。
您无法分割字母,但可以使用机器学习等高级OCR技术识别它们。
阅读此http://www.how-ocr-works.com/OCR/word-character-segmentation.html
这不像阈值化和检测斑点那样简单。您需要训练像Tesseract这样的OCR引擎来检测手写字符。