我有一些想法,我可能会进一步发展其中一些,也可能不会!
1.使用颜色
该图看起来像是您从某个包(包括 Ghostscript)生成的,而不是从纸上扫描它,所以我认为您可以控制它的生成。如果是这样,最简单和最干净的选择可能是在 Postscript 中插入一个命令来更改所有文本的颜色,或者所有线条和圆圈的颜色,然后您可以只使用颜色来提取文本。
2。使用过滤器
您可以使用较长的水平探测元素和中值来确定水平线,并使用较长的垂直探测元素来移除垂直线。显然,你可以摆弄长度等,但这看起来像这样:
convert drawing.png \
\( -clone 0 -threshold 50% -negate -statistic median 200x1 \) \
-compose lighten -composite \
\( -clone 0 -threshold 50% -negate -statistic median 1x200 \) \
-composite result.png
我尝试使用 200 作为长度:
以500为长度:
3.使用连通分量分析或 Blob 分析
我们的想法是找到图像中的所有 blob,然后删除那些大于您希望保留的字母大小的 blob。我提取了您图像的一部分来使用这种方法:
convert extract.png -colorspace gray -negate -threshold 50% \
-define connected-components:verbose=true \
-connected-components 8 -auto-level output.png
输出
Objects (id: bounding-box centroid area mean-color):
2: 943x660+77+0 553.0,296.5 536272 srgb(0,0,0)
0: 73x660+0+0 36.0,329.3 48150 srgb(0,0,0)
10: 279x176+376+484 507.5,582.9 42374 srgb(0,0,0)
8: 167x99+488+413 574.9,458.8 8939 srgb(0,0,0)
5: 291x253+370+407 517.6,486.0 8121 srgb(255,255,255)
7: 166x83+397+413 477.3,450.4 7479 srgb(0,0,0)
9: 77x90+578+436 628.7,491.1 3511 srgb(0,0,0)
6: 81x67+376+413 403.5,438.0 3197 srgb(0,0,0)
1: 4x660+73+0 74.5,329.5 2640 srgb(255,255,255)
3: 221x154+124+328 213.8,440.1 2225 srgb(255,255,255)
4: 198x154+686+378 798.3,488.4 2133 srgb(255,255,255)
11: 38x59+136+559 154.5,588.1 1094 srgb(255,255,255)
12: 37x59+790+559 808.0,588.0 955 srgb(255,255,255)
13: 37x59+837+559 855.0,588.0 955 srgb(255,255,255)
15: 37x58+230+560 248.6,588.2 888 srgb(255,255,255)
16: 37x58+742+560 760.6,588.2 888 srgb(255,255,255)
14: 39x58+180+560 201.5,587.8 862 srgb(255,255,255) <--- Let's look at this one
19: 23x45+844+566 855.0,588.0 848 srgb(0,0,0)
18: 23x45+797+566 808.0,588.0 848 srgb(0,0,0)
20: 24x22+143+589 154.5,599.5 420 srgb(0,0,0)
17: 18x16+146+566 154.5,573.6 227 srgb(0,0,0)
21: 8x11+114+606 117.5,611.0 72 srgb(255,255,255)
22: 8x11+720+606 723.5,611.0 72 srgb(255,255,255)
23: 2x20+0+628 0.3,637.5 30 srgb(255,255,255)
这些字段的标题在输出的开头,但基本上看的是 blob 14:
14: 39x58+180+560 201.5,587.8 862 srgb(255,255,255)
它宽 39 像素,高 58 像素,位于左上角偏移 180,560 处,它是白色 (255,255,255),由于我对图像进行了否定,因此它在原始图像中是黑色的,因此它对应于文本中一个字母的大小(50x70 左右)。
仅作为解释(实际处理不需要),让我们将其作为矩形绘制到提取物上:
convert extract.png -fill red -draw "rectangle 180,560 219,617" aBlob.png
请注意,我们有图像偏移加上宽度和高度,而下面的-draw rectangle 命令需要左上角和右下角,因此我们需要将宽度和高度添加到偏移中以获得右下角。
好的,现在我们可以制作所有字母的蒙版了!
convert extract.png -colorspace gray -negate -threshold 50% -define connected-components:verbose=true -connected-components 8 -auto-level output.png | awk -F"[ x+]" '/255,255,255/ && $4<=50 && $5<=80{printf "fill white rectangle %d,%d %d,%d\n",$6,$7,$6+$4,$7+$5}' > draw.txt
输出(在文件draw.txt)
fill white rectangle 136,559 174,618
fill white rectangle 790,559 827,618
fill white rectangle 837,559 874,618
fill white rectangle 230,560 267,618
fill white rectangle 742,560 779,618
fill white rectangle 180,560 219,618
fill white rectangle 114,606 122,617
fill white rectangle 720,606 728,617
fill white rectangle 0,628 2,648
下面是如何将所有受保护的 blob 放入掩码中:
convert -size 1020x660 xc:black -draw @draw.txt mask.png
这导致了这个掩码:
然后我们可以将遮罩应用到图像上:
convert extract.png mask.png -compose copyopacity -composite result.png