我知道我迟到了几年:-)但是现在您可以很容易地使用ImageMagick完成这类操作,直接在命令行执行,而无需编译任何内容,因为它内置了连接组件分析的组件:
有一种方法可以做到这一点:#!/bin/bash
image="$1"
draw=$(convert $image \
-threshold 50% \
-define connected-components:verbose=true \
-define connected-components:area-threshold=10 \
-connected-components 8 \
-auto-level objects.png | \
awk 'BEGIN{command=""}
/\+0\+0/||/id:/{next}
{
geom=$2
gsub(/x/," ",geom)
gsub(/+/," ",geom)
split(geom,a," ")
d=sprintf("-draw \x27rectangle %d,%d %d,%d\x27 ",a[3],a[4],a[3]+a[1],a[4]+a[2])
command = command d
#printf "%d,%d %d,%d\n",a[3],a[4],a[3]+a[1],a[4]+a[2]
}
END{print command}')
eval convert "$image" -fill none -strokewidth 2 -stroke red $draw result.png
结果如下:
首先,我把你的形象设定在50%的阈值,这样里面只有纯正的黑人和白人,没有色调的渐变。然后我告诉ImageMagick输出它找到的边界框的详细信息,并且我对总面积小于10像素的对象不感兴趣。然后,我允许像素是8连接的,即到它们的对角线邻域(东北、东南、西北、西南)以及它们的左右和上下邻域。最后,我使用awk解析边界框输出,以在边界框周围绘制红线。
我用awk解析的初始命令的输出如下所示:Objects (id: bounding-box centroid area mean-color):
0: 539x53+0+0 263.7,24.3 20030 srgba(255,255,255,1)
11: 51x38+308+14 333.1,30.2 869 srgba(0,0,0,1)
13: 35x39+445+14 461.7,32.8 670 srgba(0,0,0,1)
12: 35x39+365+14 381.7,32.8 670 srgba(0,0,0,1)
2: 30x52+48+0 60.4,27.0 634 srgba(0,0,0,1)
1: 41x52+1+0 20.9,16.6 600 srgba(0,0,0,1)
8: 30x39+174+14 188.3,33.1 595 srgba(0,0,0,1)
7: 30x39+102+14 116.3,33.1 595 srgba(0,0,0,1)
9: 30x39+230+14 244.3,33.1 595 srgba(0,0,0,1)
10: 35x39+265+14 282.2,33.0 594 srgba(0,0,0,1)
16: 33x37+484+15 500.2,33.0 520 srgba(0,0,0,1)
17: 22x28+272+19 282.3,32.8 503 srgba(255,255,255,1)
5: 18x51+424+2 432.5,27.9 389 srgba(0,0,0,1)
6: 18x51+520+2 528.5,27.9 389 srgba(0,0,0,1)
15: 6x37+160+15 162.5,33.0 222 srgba(0,0,0,1)
14: 6x37+88+15 90.5,33.0 222 srgba(0,0,0,1)
18: 22x11+372+19 382.6,24.9 187 srgba(255,255,255,1)
19: 22x11+452+19 462.6,24.9 187 srgba(255,255,255,1)
3: 6x8+88+0 90.5,3.5 48 srgba(0,0,0,1)
4: 6x8+160+0 162.5,3.5 48 srgba(0,0,0,1)
而awk将其转换为convert http://imgur.com/AVW7A.png -fill none -strokewidth 2 -stroke red \
-draw 'rectangle 308,14 359,52' \
-draw 'rectangle 445,14 480,53' \
-draw 'rectangle 365,14 400,53' \
-draw 'rectangle 48,0 78,52' \
-draw 'rectangle 1,0 42,52' \
-draw 'rectangle 174,14 204,53' \
-draw 'rectangle 102,14 132,53' \
-draw 'rectangle 230,14 260,53' \
-draw 'rectangle 265,14 300,53' \
-draw 'rectangle 484,15 517,52' \
-draw 'rectangle 272,19 294,47' \
-draw 'rectangle 424,2 442,53' \
-draw 'rectangle 520,2 538,53' \
-draw 'rectangle 160,15 166,52' \
-draw 'rectangle 88,15 94,52' \
-draw 'rectangle 372,19 394,30' \
-draw 'rectangle 452,19 474,30' \
-draw 'rectangle 88,0 94,8' \
-draw 'rectangle 160,0 166,8' result.png