imagemagick - 返回每个连续对象的边界框

问题描述 投票:0回答:1

如何使用 imagemagick 返回图像中识别的每个对象的边界框坐标?

imagemagick
1个回答
0
投票

您可以在 Imagemagick 中使用连接组件处理来做到这一点。以下是 Imagemagick 6 的 Unix 语法

读取输入。然后阈值并添加一些填充。然后应用形态学来关闭一些应该连接的断开区域(尤其是头部的骨头)并剃掉填充物。然后应用连通分量处理。

convert objects.jpg \
-negate -threshold 15% -bordercolor black -border 10 \
-morphology close disk:8 -type bilevel -shave 10x10 \
-define connected-components:exclude-header=true \
-define connected-components:mean-color=true \
-define connected-components:area-threshold=100 \
-define connected-components:verbose=true \
-connected-components 8 null: | grep "gray(255)" | awk '{ print $2 }'

结果列出到终端窗口:

268x567+408+35
212x379+185+48
560x98+2+652
75x434+36+5
128x131+26+460
128x130+231+498

list="268x567+408+35
212x379+185+48
560x98+2+652
75x434+36+5
128x131+26+460
128x130+231+498"
convert objects.jpg tmp.png
for item in $list; do
ww=`echo "$item" | tr "x" "+" | cut -d+ -f1`
hh=`echo "$item" | tr "x" "+" | cut -d+ -f2`
xo=`echo "$item" | tr "x" "+" | cut -d+ -f3`
yo=`echo "$item" | tr "x" "+" | cut -d+ -f4`
x1=$xo
y1=$yo
x2=$((x1+ww))
y2=$((y1+hh))
convert tmp.png -fill none -stroke red \
-draw "rectangle $x1,$y1 $x2,$y2" -alpha off tmp.png
done
convert tmp.png objects_bboxes.jpg
rm -f tmp.png

© www.soinside.com 2019 - 2024. All rights reserved.