区分文档的图像和另一个图像

Ant*_*ony 5 python opencv classification machine-learning computer-vision

我试图区分文本文档的图像和非文本文档的另一个图像。我想编写一个方法,DOCUMENT如果图像是文档的图像或IMAGE不是文档的图像,则返回该方法。

文本文档的示例图像

在此输入图像描述

非文本文档的示例图像

在此输入图像描述

有什么方法可以做到这一点?我应该使用霍夫线变换并查看图像中是否有水平直线?

我正在考虑的方法

  1. 对图像进行霍夫变换。仅考虑水平线。如果太多(某个预定义的阈值),那么我可以说它是文本文档的图像
  2. 检测白色背景上是否有大量黑色文本。然后我可以有信心地说,这是一个文本文档的图像。(但我不知道该怎么做)

Mar*_*ell 2

以下是一些想法 - 用 ImageMagick 表达,但很容易适应 OpenCV。ImageMagick 已安装在大多数 Linux 发行版中,并且适用于 OSX,如果您不幸不得不使用它,也可用于 Windows。

建议 1 - 初始质量

第一个建议是尝试以 PNG 格式而不是 JPEG 格式捕获屏幕截图 - 这对于任何严肃的处理来说都是更可取的。

建议 2 - 作物垃圾

其次,由于您周围有大量无关的垃圾,包括 PDF 查看器的框架,我建议在进行任何处理之前裁剪图像的中间部分,因为这将删除大部分垃圾,并且对检测文本行没有太大影响它们很可能与页面中间的相同。那是:

convert textual.jpg -gravity center -crop 70x70% x.png
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

建议 3 - 白色百分比

接下来,查看白色像素的百分比,如果是文本,则查找较高的数字,如果是非文本,则查找较低的数字:

# Check percentage white space
convert textual.jpg -gravity center -crop 70x70% -normalize -threshold 90% -format "%[fx:int(mean*100)]\n" info:
90

convert nontextual.jpg -gravity center -crop 70x70% -normalize -threshold 90% -format "%[fx:int(mean*100)]\n" info:
8
Run Code Online (Sandbox Code Playgroud)

建议 4 - 寻找交替的黑白行

接下来,尝试对图像进行波形处理,直到其宽度为 1 像素且高度与原始图像相同,然后对其进行阈值处理。然后计算黑色和白色之间的交替次数 - 很多是文本,很少是非文本:

# Check for alternating black and white horizontal lines
convert textual.jpg -gravity center -crop 70x70%  -threshold 50% -resize 1x! -normalize -threshold 95% -scale 20x! result.png
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

而对于非文本图像:

    # Check for alternating black and white horizontal lines
convert nontextual.jpg -gravity center -crop 70x70%  -threshold 50% -resize 1x! -normalize -threshold 95% -scale 20x! result.png
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

建议 5 - 连接组件分析

最后,我会考虑“连通分量分析”“斑点分析”。使用文本图像,您将获得许多小的、水平对齐的斑点 - 对应于单词或字母 - 这取决于原始屏幕抓取的质量。

对于文本图像:

convert textual.jpg -gravity center -crop 70x70%     \
    -colorspace gray -negate -threshold 10%          \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=0    \
    -connected-components 8 -auto-level output.png
Run Code Online (Sandbox Code Playgroud)

输出 - 1300 个对象

Objects (id: bounding-box centroid area mean-color):
  88: 768x627+0+18 387.5,315.7 436659 srgb(0,0,0)
  0: 768x18+0+0 387.6,9.2 12194 srgb(255,255,255)
  28: 118x7+408+0 466.1,2.8 709 srgb(0,0,0)
  354: 78x16+125+428 164.8,435.3 466 srgb(255,255,255)
  1184: 76x16+146+629 185.1,636.7 417 srgb(255,255,255)
  158: 28x35+358+250 371.5,265.9 411 srgb(255,255,255)
  ...
  ...
  14: 1x1+201+0 201.0,0.0 1 srgb(0,0,0)
  346: 1x1+456+419 456.0,419.0 1 srgb(255,255,255)
  347: 1x1+46+423 46.0,423.0 1 srgb(255,255,255)
  183: 1x1+126+274 126.0,274.0 1 srgb(0,0,0)
Run Code Online (Sandbox Code Playgroud)

带标签的输出图像显示找到的对象 - 每个对象都有逐渐变浅的阴影(1300 种阴影):

在此输入图像描述

而对于非文本图像:

convert nontextual.jpg -gravity center -crop 70x70%     \
    -colorspace gray -negate -threshold 10%          \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=0    \
    -connected-components 8 -auto-level output.png
Run Code Online (Sandbox Code Playgroud)

输出 - 57 个物体

Objects (id: bounding-box centroid area mean-color):
  1: 315x237+0+0 153.6,115.2 68351 srgb(255,255,255)
  22: 56x147+181+42 215.4,119.3 3768 srgb(0,0,0)
  35: 23x10+106+227 117.0,232.0 184 srgb(0,0,0)
  36: 23x10+179+227 189.9,231.9 183 srgb(0,0,0)
  38: 22x10+264+227 274.5,231.9 179 srgb(0,0,0)
  37: 22x10+230+227 240.7,231.9 178 srgb(0,0,0)
  ...
  ...
  24: 1x1+200+50 200.0,50.0 1 srgb(0,0,0)
  25: 1x1+216+57 216.0,57.0 1 srgb(0,0,0)
  26: 1x1+220+61 220.0,61.0 1 srgb(0,0,0)
Run Code Online (Sandbox Code Playgroud)

带标签的输出图像显示找到的对象 - 每个对象都有逐渐变浅的阴影,您可以看到阴影更少(只有 57 个):

在此输入图像描述

  • 感谢您的精彩回答。我真的很喜欢建议3。我会在opencv中尝试一下。对于建议 4,除了手动检查之外,还有其他方法可以检测黑白线交替的数量吗? (3认同)