在 ImageMagick 中,有一个连接组件工具,它可以用一点 unix 脚本来做你想做的事情。它仅在字符之间存在白色分隔时才有效。我相信 OpenCV 也有类似的东西。
输入:

让我们从连接组件开始,以显示返回的文本数据:
convert rUNOP.png -alpha off -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-connected-components 4 null: | sed 's/^[ ]*//'
Objects (id: bounding-box centroid area mean-color):
0: 236x139+0+0 118.2,71.3 30849 gray(255)
2: 36x50+27+11 39.7,30.5 630 gray(0)
3: 29x50+90+11 103.3,34.3 580 gray(0)
1: 23x50+155+9 163.3,33.3 502 gray(0)
4: 13x24+160+21 165.0,32.9 243 gray(255)
现在将它与一些脚本结合起来,找到那些黑色的条目,即灰色(0)颜色并提取边界框并使用它来裁剪输入图像。
OLDIFS=$IFS
IFS=$'\n'
arr=(`convert rUNOP.png -alpha off -threshold 50% -type bilevel \
-define connected-components:verbose=true \
-connected-components 4 null: | sed 's/^[ ]*//'`)
num=${#arr[*]}
IFS=$OLDIFS
for ((i=0; i<num; i++)); do
bbox=`echo ${arr[$i]} | cut -d\ -f2`
color=`echo ${arr[$i]} | cut -d\ -f5`
if [ "$color" = "gray(0)" ]; then
convert rUNOP.png -crop $bbox +repage rUNOP_crop_$i.gif
fi
done


