opencv-裁剪手写线(线段) [英] opencv - cropping handwritten lines (line segmentation)

查看:181
本文介绍了opencv-裁剪手写线(线段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python和opencv构建手写识别系统. 字符的识别不是问题,而是分段. 我已经成功了:

I'm trying to build a handwriting recognition system using python and opencv. The recognition of the characters is not the problem but the segmentation. I have successfully :

  • 将一个单词分割成单个字符
  • 按所需顺序将单句分段成单词.
  • segmented a word into single characters
  • segmented a single sentence into words in the required order.

但是我无法在文档中分割不同的行.我尝试对轮廓进行排序(以避免线段分割,仅使用字词分割),但没有奏效. 我已经使用以下代码对手写文档中包含的单词进行了细分,但是它以乱序方式返回单词(它以从左到右的排序方式返回单词):

But I couldn't segment different lines in the document. I tried sorting the contours (to avoid line segmentation and use only word segmentation) but it didnt work. I have used the following code to segment words contained in a handwritten document , but it returns the words out-of-order(it returns words in left-to-right sorted manner) :

import cv2
import numpy as np
#import image
image = cv2.imread('input.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,5), 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)

请注意,我可以在此处将所有单词分段,但它们显示的顺序不对.有没有办法按从上到下的顺序对这些轮廓进行排序

Please note that i am able to segment all the words here but they appear out order.Is there any way to sort these contours in order of top to bottom

OR

将图像分割成单独的行,以便可以使用上述代码将每行分割成单词?

推荐答案

通过更改上面的代码,我得到了所需的分段:

I got the required segmentation by making a change to the above code on the line:

kernel = np.ones((5,5), np.uint8)

我将其更改为:

kernel = np.ones((5,100), np.uint8)

现在,我得到如下输出 这也适用于手写文本图像,这些文本的线条不是完全水平的:

Now i get the outputs as following This also works with handwritten text images with lines that are not perfectly horizontal:

要使单词中的各个字符不起作用,请执行以下操作:

EDIT : For getting individual characters out of a word, do the following :

  1. 使用以下代码调整包含单词的轮廓的大小.

  1. Resize the contour containing the word using the code as follows.

im = cv2.resize(image,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)

  • 应用与线段分割相同的轮廓检测过程,但是内核大小为(5,5),即:

  • Apply same contour detection process as in line segmentation, but with a kernel of size (5,5), i.e :

    kernel = np.ones((5,5), np.uint8)
    img_dilation = cv2.dilate(im_th, kernel, iterations=1)
    

  • 这篇关于opencv-裁剪手写线(线段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆