简体   繁体   English

将手写文字分割成几行

[英]Segmenting handwritten text into lines

I'm attempting to split the handwritten text from a dataset of NIST forms into separate lines. 我正在尝试将NIST表单数据集中的手写文本分成几行。 Here is a link to the dataset: https://www.nist.gov/srd/nist-special-database-19 这是数据集的链接: https : //www.nist.gov/srd/nist-special-database-19

Example Image 范例图片 范例表格

The code I'm using is based off of a similar question on stackoverflow but it doesn't quite work due to some the characters touching. 我正在使用的代码基于关于stackoverflow的类似问题,但由于某些字符接触而无法正常工作。 Here is the code: 这是代码:

import cv2
import numpy as np
#import image
image = cv2.imread('form1.jpg')
#cv2.imshow('orig',image)
#cv2.waitKey(0)

#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.waitKey(0)

#binary
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
cv2.imshow('second',thresh)
cv2.waitKey(0)

#dilation
kernel = np.ones((5,100), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated',img_dilation)
cv2.waitKey(0)

#find contours
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, 
cv2.CHAIN_APPROX_SIMPLE)

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y+h, x:x+w]

    # show ROI
   cv2.imshow('segment no:'+str(i),roi)
   cv2.rectangle(image,(x,y),( x + w, y + h ),(90,0,255),2)
   cv2.waitKey(0)

cv2.imshow('marked areas',image)
cv2.waitKey(0)        

How can I get it to split the lines properly even when some of the characters are overlapping? 即使某些字符重叠,如何获得正确的分割线?

I am working on similar problem, and the sample is of a quite good quality. 我正在研究类似的问题,并且样本质量很好。

From the code that is given I can see that you use Contour Detection. 从给出的代码中,我可以看到您使用了轮廓检测。 You may want to play with aspect ratio restriction on detected contours in order to omit connected components. 您可能希望对检测到的轮廓使用宽高比限制,以便省略连接的组件。 Here you can find insights for your project as well as description on how to make aspects ratio restriction. 在这里,您可以找到您的项目的见解以及有关如何进行宽高比限制的说明。

If you still want to keep them, then you will need to perform post processing. 如果仍要保留它们,则需要执行后处理。 Either it will involve morphology alternation for those elements, or application of some sort of Machine/Deep Learning. 要么涉及这些元素的形态变化,要么涉及某种机器/深度学习的应用。

That is the most complicated part and it might have many different solutions. 那是最复杂的部分,可能有许多不同的解决方案。 For my project I used Convolutional Neural Network with Keras in order to train the model to classify letters, and put outliers in separate class. 对于我的项目,我使用带Keras的卷积神经网络来训练模型对字母进行分类,并将离群值放在单独的类中。 As you might have guessed I have added couple more classes with training data (thousands of training examples will be needed) to the existing dataset of MNIST-like letters. 正如您可能已经猜到的那样,我在现有的类似MNIST的字母数据集中添加了更多的类和训练数据(将需要成千上万个训练示例)。

Good luck! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM