我编写了下面的代码来拆分手写字母,在某些情况下,无法正确拆分:
import cv2
import numpy as np
import imutils
from google.colab.patches import cv2_imshow
image = cv2.imread("/content/IMG_3789.JPG")
image = imutils.resize(image, height = 500)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.medianBlur(gray, 5)
thresh = cv2.adaptiveThreshold(blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV,11,8)
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dilate = cv2.dilate(thresh, kernel, iterations=10)
cnts = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area > 500:
x,y,w,h = cv2.boundingRect(c)
ROI = image[y:y+h, x:x+w]
cv2_imshow(ROI)
break
img_gray = cv2.cvtColor(ROI, cv2.COLOR_BGR2GRAY)
img_gauss = cv2.GaussianBlur(img_gray, (3,3), 0)
kernel = np.ones((4,4), np.uint8)
erode = cv2.erode(img_gauss, kernel, iterations=1)
th3 = cv2.adaptiveThreshold(erode,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV,51,10)
im_th2, ctrs, hier = cv2.findContours(th3.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
rects.sort()
x, y, w, h = rects[0]
cv2.rectangle(ROI, (x, y), (x+w, y+h), (0, 255, 0), 3)
cv2_imshow(ROI)
第一个字符有两个字母:
第五个也是:
是否可以正确识别?
最佳答案
前两个字母似乎属于未连接的 blob(除非您的预处理使它们接触)。所以 split 应该不是问题。
对于最后两个字母,仅使用“哑”预处理函数没有真正的解决方案。宽度不是一个可靠的标准,即使您检测到两个字符,您也不知道确切的分割位置。
您必须设计标准来判断 blob 的哪些部分类似于字符(这非常困难),或者执行部分识别,可能有多个假设并保持最有可能。
手写分割极具挑战性。
关于python - 我无法使用 OpenCV 和 Python 拆分所有手写字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58191269/