如果你想要的3个组件总是在同一个地方,你可以根据坐标提取:
convert image.png -crop 164x146+27+0 +repage result-0.png
convert image.png -crop 12x146+0+0 +repage result-1.png
convert image.png -crop 30x7+138+151 +repage result-2.png
最后一个是空的!
如果它们不总是在同一个地方,我会查看图像的 alpha/transaprency 层:
convert image.png -alpha extract alpha.png
因为它在白色上显示了您想要的位,所以我会使用 "Connected Component Analysis"
寻找白色斑点
convert image.png -alpha extract \
-define connected-components:verbose=true \
-define connected-components:area-threshold=200 \
-connected-components 4 -normalize result.png
输出
Objects (id: bounding-box centroid area mean-color):
2: 164x146+27+0 108.5,72.5 23944 srgb(255,255,255)
3: 32x161+174+0 196.5,87.0 2670 srgb(2,2,2)
5: 174x15+0+146 79.8,152.8 2370 srgb(1,1,1)
1: 15x146+12+0 19.0,72.5 2190 srgb(2,2,2)
0: 12x146+0+0 5.5,72.5 1752 srgb(255,255,255)
39: 30x7+138+151 152.5,154.0 210 srgb(255,255,255)
这向我们展示了您图像中的所有斑点。回顾 alpha 层,您只需要白色层,并且您需要行中的第二个字段,因为它会告诉您在哪里裁剪该 blob。
这导致我们这样做:
#!/bin/bash
# Edit this according to your input image name
image="image.png"
i=0
convert "$image" -alpha extract \
-define connected-components:verbose=true \
-define connected-components:area-threshold=200 \
-connected-components 4 -normalize result.png |
awk '/255,255,255/{print $2}' |
while read c ; do
convert "$image" -crop "$c" +repage result-$i.png
((i=i+1))
done
希望能做到你想要的。