首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >OpenCV中文档的模式抽取层

OpenCV中文档的模式抽取层
EN

Stack Overflow用户
提问于 2011-12-28 11:06:53
回答 1查看 2.2K关注 0票数 2

首先,通过对图像灰度的80%进行阈值化处理,并反演得到的图像,生成图像的二值化。在二值图像中,白色像素表示字符、图形和线条等。模式提取的第一步是定位矩形区域,称为“rect”。rect是由松散连接的白色像素1组成的矩形区域,它包含文档的某个逻辑部分。我们考虑简单的8邻域连通性,并对二值图像进行连通分量(等高线)分析,从而对文本分量进行分割。对于算法的下一部分,我们使用了轮廓的最小边界矩形。然后,利用最左上角的2D点信息,对这些矩形进行上下排序和从左到右的排序。较小的连接模式被丢弃,因为它们可能起源于依赖于图像采集系统的噪声,而对最终布局没有任何贡献。此外,标点符号也被忽略了,使用较小的大小标准,如逗号、句号等。在这个级别上,我们也以avgh (平均高度)作为阈值,根据边界的高度对字体进行分离。两个阈值用于将字体分为三类--小的、正常的和大的。

n.jpg

你能帮我把这个理论翻译成opencv源代码或者给我任何相关的链接吗,我目前正在为我的论文做文档图像分析。

EN

回答 1

Stack Overflow用户

发布于 2012-04-22 18:14:20

我知道这是迟来的答复。但我认为未来的人可以从中得到帮助。

下面是我从上面理解的答案(所有代码都是OpenCV-Pythonv2.4-beta版):

我把这个当作输入图像。这是一个简单的形象,为了理解。

First we generate the binary image of the give image by thresholding it at 80% of its intensity and inverting the resulting image.

代码语言:javascript
代码运行次数:0
运行
复制
import cv2
import numpy as np

img = cv2.imread('doc4.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,0.8*gray.max(),255,1)
contours, hier = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

缩影图像:

We considered simple 8-neighborhood connectivity and performed connected component (contour) analysis of the binary image leading to the segmentation of the textual components.

这是简单的轮廓查找在OpenCV,也称为连接组件标签。,它选择图像中的所有白色斑点(组件)。

代码语言:javascript
代码运行次数:0
运行
复制
contours, hier = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

轮廓:

For next part of algorithm we use the minimum bounding rectangle of contours.

现在我们在每个检测到的轮廓周围找到包围矩形。然后用小区域删除等高线以删除逗号等。参见以下语句:

Smaller connected patterns were discarded based on the assumption that they may have originated due to noise dependent on image acquisition system and does not in any way contribute to the final layout. Also punctuation marks were neglected using smaller size criterion e.g. comma, full-stop etc.

我们还找到了平均身高,avgh

代码语言:javascript
代码运行次数:0
运行
复制
height = 0
num = 0
letters = []
ht = []

for (i,cnt) in enumerate(contours):
    (x,y,w,h) = cv2.boundingRect(cnt)
    if w*h<200:
        cv2.drawContours(thresh2,[cnt],0,(0,0,0),-1)
    else:
        cv2.rectangle(thresh2,(x,y),(x+w,y+h),(0,255,0),1)
        height = height + h
        num = num + 1
        letters.append(cnt)
        ht.append(h)

avgh = height/num

因此,在删除所有逗号等之后,围绕选定的矩形绘制绿色矩形:

At this level we also segregate the fonts based on the height of the bounding rect using avgh (average height) as threshold. Two thresholds are used to classify fonts into three categories - small, normal and large (根据给定的通道方程)。

这里的平均身高是40。因此,如果高度小于26.66 (即40x2/3),则为small,如果是height>60,则为normal。但是在给定的图像中,所有的高度都在(28,58)之间,所以所有的高度都是正常的。所以你看不出区别。

因此,我只是做了一个小的修改,以便于可视化:小的如果height<30,正常的if 3050。

代码语言:javascript
代码运行次数:0
运行
复制
for (cnt,h) in zip(letters,ht):
    print h
    if h<=30:
        cv2.drawContours(thresh2,[cnt],0,(255,0,0),-1)
    elif 30 < h <= 50:
        cv2.drawContours(thresh2,[cnt],0,(0,255,0),-1)
    else:
        cv2.drawContours(thresh2,[cnt],0,(0,0,255),-1)
cv2.imshow('img',thresh2)
cv2.waitKey(0)
cv2.destroyAllWindows()

现在,您得到的结果是将字母分类为小的、正常的、大的:

These rectangles were then sorted top-to-bottom and left-to-right order, using 2D point information of leftmost-topmost corner.

这部分我省略了。它只是排序所有的包围,重新开始,他们的最左边-最顶端的角落。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8655085

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档