Sid*_*esh 10 python opencv text-segmentation handwriting-recognition
我正在尝试使用python和opencv构建一个手写识别系统.字符的识别不是问题,而是分割.我成功了:
但我无法在文档中分割不同的行.我尝试对轮廓进行排序(以避免线分割并仅使用分词),但它不起作用.我使用以下代码来分割手写文档中包含的单词,但它返回不按顺序的单词(它以从左到右的排序方式返回单词):
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)
Run Code Online (Sandbox Code Playgroud)
请注意,我能够将所有单词分段,但它们显示顺序.有没有办法按照从上到下的顺序对这些轮廓进行排序
要么
将图像分割成单独的行,以便使用上面的代码将每一行分割成单词?
Sid*_*esh 11
我通过更改上面的代码获得了所需的细分:
kernel = np.ones((5,5), np.uint8)
Run Code Online (Sandbox Code Playgroud)
我改成了:
kernel = np.ones((5,100), np.uint8)
Run Code Online (Sandbox Code Playgroud)
现在我得到如下输出
这也适用于手写文本图像,其中的线条不是完全水平的:
编辑: 要获取单个字符,请执行以下操作:
使用以下代码调整包含单词的轮廓的大小.
im = cv2.resize(image,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)
Run Code Online (Sandbox Code Playgroud)应用与线分割相同的轮廓检测过程,但内核大小为(5,5),即:
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(im_th, kernel, iterations=1)
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
7128 次 |
| 最近记录: |