简体   繁体   English

使用 OpenCV 提取手写文本的形状

[英]Extracting hand writing text out in shape with OpenCV

I am very new to OpenCV Python and I really need some help here.我对 OpenCV Python 非常陌生,我真的需要一些帮助。

So what I am trying to do here is to extract out these words in the image below.所以我在这里要做的是提取下图中的这些词。

手绘图像

The words and shapes are all hand drawn, so they are not perfect.文字和形状都是手绘的,所以并不完美。 I have did some coding below.我在下面做了一些编码。

First of all, I grayscale the image首先,我对图像进行灰度处理

img_final = cv2.imread(file_name)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

Then I use THRESH_INV to show the content然后我使用 THRESH_INV 来显示内容

ret, new_img = cv2.threshold(image_final, 100 , 255, cv2.THRESH_BINARY_INV)

After which, I dilate the content之后,我扩大了内容

kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3 , 3)) 
dilated = cv2.dilate(new_img,kernel,iterations = 3)

I dilate the image is because I can identify text as one cluster我扩大图像是因为我可以将文本识别为一个集群

After that, I apply boundingRect around the contour and draw around the rectangle之后,我在轮廓周围应用 boundingRect 并在矩形周围绘制

contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) # get contours
index = 0
for contour in contours:

    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

    #Don't plot small false positives that aren't text
    if w < 10 or h < 10:
        continue

    # draw rectangle around contour on original image
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,255),2)

This is what I got after that.这是我后来得到的。

结果图像

I am only able to detect one of the text.我只能检测到其中一个文本。 I have tried many other methods but this is the closet results I have got and it does not fulfill the requirement.我尝试了许多其他方法,但这是我得到的壁橱结果,它不满足要求。

The reason for me to identify the text is so that I can get the X and Y coordinate of each of the text in this image by putting a bounding Rectangle "boundingRect()".我识别文本的原因是,我可以通过放置一个边界矩形“boundingRect()”来获得该图像中每个文本的 X 和 Y 坐标。

Please help me out.请帮帮我。 Thank you so much非常感谢

You can use the fact that the connected component of the letters are much smaller than the large strokes of the rest of the diagram.您可以使用这样一个事实,即字母的连接组件比图表其余部分的大笔画小得多。

I used opencv3 connected components in the code but you can do the same things using findContours.我在代码中使用了 opencv3 连接组件,但您可以使用 findContours 执行相同的操作。

The code:编码:

import cv2
import numpy as np

# Params
maxArea = 150
minArea = 10

# Read image
I = cv2.imread('i.jpg')

# Convert to gray
Igray = cv2.cvtColor(I,cv2.COLOR_RGB2GRAY)

# Threshold
ret, Ithresh = cv2.threshold(Igray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)

# Keep only small components but not to small
comp = cv2.connectedComponentsWithStats(Ithresh)

labels = comp[1]
labelStats = comp[2]
labelAreas = labelStats[:,4]

for compLabel in range(1,comp[0],1):

    if labelAreas[compLabel] > maxArea or labelAreas[compLabel] < minArea:
        labels[labels==compLabel] = 0

labels[labels>0] =  1

# Do dilation
se = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25))
IdilateText = cv2.morphologyEx(labels.astype(np.uint8),cv2.MORPH_DILATE,se)

# Find connected component again
comp = cv2.connectedComponentsWithStats(IdilateText)

# Draw a rectangle around the text
labels = comp[1]
labelStats = comp[2]
#labelAreas = labelStats[:,4]

for compLabel in range(1,comp[0],1):

    cv2.rectangle(I,(labelStats[compLabel,0],labelStats[compLabel,1]),(labelStats[compLabel,0]+labelStats[compLabel,2],labelStats[compLabel,1]+labelStats[compLabel,3]),(0,0,255),2)

在此处输入图片说明

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

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