区分文档的图像和另一个图像
Differentiating between image of a document and another image
我试图区分文本文档的图像和非文本文档的图像。我想写一个方法 returns DOCUMENT
如果图像是文档,或者 IMAGE
如果它不是文档图像。
文本文档的示例图片
非文本文档的示例图片
有什么方法可以做到这一点?我是否应该使用霍夫线变换并查看图像中是否有直线水平线?
我正在考虑的方法
- 对图像进行霍夫变换。只考虑水平线。如果有太多(一些预定义的阈值)那么我可以说它是文本文档的图像
- 检测是否有大量白底黑字。然后我可以自信地说,它是文本文档的图像。 (虽然我不确定该怎么做)
使用像http://scikit-image.org/docs/stable/user_guide.html
这样的图像处理模块
您最有可能必须将 pdf 转换为图像才能执行此操作。
逐个像素分析图像,也许对图像使用OCR,如果看到字符,则可能是文档。如果没有字符存在,那么它可能不是文档。你如何精确地做到这一点取决于你。单独使用 OCR 是不够的,但现存的文本是一个需要考虑的属性
这里有一些想法 - 在 ImageMagick 中表达,但很容易适应 OpenCV。 ImageMagick 安装在大多数 Linux 发行版中,并且可用于 OSX 和 Windows,如果您不幸不得不使用它的话。
建议 1 - 初始质量
第一个建议是尝试以 PNG 格式而不是 JPEG 格式获取屏幕截图 - 对于任何严肃的处理来说,这是更可取的。
建议 2 - 裁剪垃圾
其次,由于您周围有大量无关的垃圾,包括您的 PDF 查看器的框架,我建议您在进行任何处理之前将图像的中间部分剪掉,因为这将删除大部分垃圾并且不会产生太大影响检测很可能与页面中间相同的文本行。即:
convert textual.jpg -gravity center -crop 70x70% x.png
建议 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
建议 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
而对于非文本图像:
# 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
建议 5 - 连接组件分析
最后,我会考虑"Connected Component Analysis"或"Blob Analysis"。对于文本图像,您将获得许多水平对齐的小斑点 - 对应于单词或字母 - 取决于原始屏幕抓取的质量。
对于文字图片:
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
输出 - 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)
显示已找到对象的带标签的输出图像 - 每个对象都有一个逐渐变浅的阴影(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
输出 - 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)
带标签的输出图像显示找到的对象 - 每个对象的阴影都逐渐变浅,您可以看到阴影越来越少(只有 57 个):
我试图区分文本文档的图像和非文本文档的图像。我想写一个方法 returns DOCUMENT
如果图像是文档,或者 IMAGE
如果它不是文档图像。
文本文档的示例图片
非文本文档的示例图片
有什么方法可以做到这一点?我是否应该使用霍夫线变换并查看图像中是否有直线水平线?
我正在考虑的方法
- 对图像进行霍夫变换。只考虑水平线。如果有太多(一些预定义的阈值)那么我可以说它是文本文档的图像
- 检测是否有大量白底黑字。然后我可以自信地说,它是文本文档的图像。 (虽然我不确定该怎么做)
使用像http://scikit-image.org/docs/stable/user_guide.html
这样的图像处理模块您最有可能必须将 pdf 转换为图像才能执行此操作。
逐个像素分析图像,也许对图像使用OCR,如果看到字符,则可能是文档。如果没有字符存在,那么它可能不是文档。你如何精确地做到这一点取决于你。单独使用 OCR 是不够的,但现存的文本是一个需要考虑的属性
这里有一些想法 - 在 ImageMagick 中表达,但很容易适应 OpenCV。 ImageMagick 安装在大多数 Linux 发行版中,并且可用于 OSX 和 Windows,如果您不幸不得不使用它的话。
建议 1 - 初始质量
第一个建议是尝试以 PNG 格式而不是 JPEG 格式获取屏幕截图 - 对于任何严肃的处理来说,这是更可取的。
建议 2 - 裁剪垃圾
其次,由于您周围有大量无关的垃圾,包括您的 PDF 查看器的框架,我建议您在进行任何处理之前将图像的中间部分剪掉,因为这将删除大部分垃圾并且不会产生太大影响检测很可能与页面中间相同的文本行。即:
convert textual.jpg -gravity center -crop 70x70% x.png
建议 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
建议 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
而对于非文本图像:
# 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
建议 5 - 连接组件分析
最后,我会考虑"Connected Component Analysis"或"Blob Analysis"。对于文本图像,您将获得许多水平对齐的小斑点 - 对应于单词或字母 - 取决于原始屏幕抓取的质量。
对于文字图片:
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
输出 - 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)
显示已找到对象的带标签的输出图像 - 每个对象都有一个逐渐变浅的阴影(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
输出 - 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)
带标签的输出图像显示找到的对象 - 每个对象的阴影都逐渐变浅,您可以看到阴影越来越少(只有 57 个):