Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用OpenCV提取手写文本_Python_Opencv - Fatal编程技术网

Python 用OpenCV提取手写文本

Python 用OpenCV提取手写文本,python,opencv,Python,Opencv,我对OpenCV Python非常陌生,这里我真的需要一些帮助 所以我在这里要做的是从下图中提取这些单词 文字和形状都是手绘的,所以它们并不完美。我在下面做了一些编码 首先,我对图像进行灰度处理 img_final = cv2.imread(file_name) img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 然后我使用THRESH\u INV来显示内容 ret, new_img = cv2.threshold(image_final, 100

我对OpenCV Python非常陌生,这里我真的需要一些帮助

所以我在这里要做的是从下图中提取这些单词

文字和形状都是手绘的,所以它们并不完美。我在下面做了一些编码

首先,我对图像进行灰度处理

img_final = cv2.imread(file_name)
img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
然后我使用THRESH\u INV来显示内容

ret, new_img = cv2.threshold(image_final, 100 , 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3 , 3)) 
dilated = cv2.dilate(new_img,kernel,iterations = 3)
之后,我对内容进行了扩展

ret, new_img = cv2.threshold(image_final, 100 , 255, cv2.THRESH_BINARY_INV)
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3 , 3)) 
dilated = cv2.dilate(new_img,kernel,iterations = 3)
我放大图像是因为我可以将文本识别为一个簇

然后,我在轮廓周围应用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)
contours,hierarchy=cv2.查找轮廓(扩展,cv2.外部翻新,cv2.链约无)#获取轮廓
索引=0
对于等高线中的等高线:
#获取矩形边界轮廓
[x,y,w,h]=cv2.boundingRect(轮廓)
#不要绘制非文本的小误报
如果w<10或h<10:
持续
#在原始图像上围绕轮廓绘制矩形
cv2.矩形(img,(x,y),(x+w,y+h),(255,0255),2)
这就是我在那之后得到的

我只能检测到其中一个文本。我尝试过许多其他方法,但这是我得到的最隐秘的结果,它不符合要求

我之所以要识别文本,是因为我可以通过放置一个边框“boundingRect()”来获取此图像中每个文本的X和Y坐标


请帮帮我。非常感谢

您可以使用这样一个事实,即字母的连接部分比图表其余部分的大笔划小得多

我在代码中使用了opencv3连接的组件,但是您可以使用FindContentours做同样的事情

守则:

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)
导入cv2
将numpy作为np导入
#Params
最大面积=150
米纳雷=10
#读取图像
I=cv2.imread('I.jpg')
#变灰
Igray=cv2.CVT颜色(I,cv2.COLOR\U RGB2GRAY)
#门槛
ret,Ithresh=cv2.阈值(Igray,0255,cv2.阈值二元值阈值+cv2.阈值大津)
#只保留小部件,但不要太小
组件=cv2。已连接组件状态(Ithresh)
标签=组件[1]
labelStats=comp[2]
labelAreas=labelStats[:,4]
对于范围(1,组件[0],1]中的平面图:
如果labelAreas[compLabel]>maxArea或labelAreas[compLabel]0]=1
#扩张
se=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(25,25))
IdilateText=cv2.morphologyEx(labels.astype(np.uint8),cv2.MORPH\u deplate,se)
#再次查找连接的组件
comp=cv2.连接的组件swithstats(IdilateText)
#在文本周围画一个矩形
标签=组件[1]
labelStats=comp[2]
#labelAreas=labelStats[:,4]
对于范围(1,组件[0],1]中的平面图:
cv2.矩形(I,(labelStats[Clambel,0],labelStats[Clambel,1]),(labelStats[Clambel,0]+labelStats[Clambel,2],labelStats[Clambel,1]+labelStats[Clambel,3]),(0,0255),2)

你不能使用tesseract对图像进行OCR吗?@Kells1986原因是我需要知道这些单词的坐标,以便其他用途。用boundingRect()环绕已识别的单词,我将能够在这张图片中获得单词的X和Y坐标,这太神奇了,兄弟!!让我仔细阅读您的代码并理解它。connectedComponentsWithStats()的含义是什么?我到处找了找这个词的意思,但是解释太难理解了。看这个:非常感谢。这样好多了。嗯,你介意看看这个帖子吗,我也需要一些指导和提示