您可以在命令行中使用 ImageMagick 或在 Python、Perl、PHP 或 C/C++ 中执行此操作。
首先,提取 alpha 通道:
convert input.png -alpha extract alpha.png
但我要做的是形态学,我想要黑底白字,所以把它倒过来:
convert input.png -alpha extract -negate alpha.png
现在运行一个“连接组件”分析来找到白色的斑点:
convert input.png -alpha extract -negate -threshold 50% \
-define connected-components:verbose=true \
-define connected-components:area-threshold=100 \
-connected-components 8 -auto-level null:
输出
Objects (id: bounding-box centroid area mean-color):
0: 600x376+0+0 249.7,205.3 129723 srgb(0,0,0)
2: 203x186+70+20 170.8,112.6 27425 srgb(255,255,255)
1: 218x105+337+13 445.5,65.0 22890 srgb(255,255,255)
4: 218x105+337+251 445.5,303.0 22890 srgb(255,255,255)
3: 218x104+337+132 445.5,183.5 22672 srgb(255,255,255)
他们就在那里。忽略第一行,因为它是黑色的并且对应于整个图像。现在,查看第二行,您可以看到块为 203x186,偏移量为 +70+20。质心也在那里。让我把那个 blob 用红色框起来:
convert input.png -stroke red -fill none -draw "rectangle 70,20 272,205" z.png