Connected-component labeling with ImageMagick

半城伤御伤魂 提交于 2019-11-30 09:24:39

问题


Is it possible to perform a connected-component labeling on a image with ImageMagick?

Connected-component labeling in Wikipedia


回答1:


Yes, it is now possible with ImageMagick 6.8.9-10 and newer, see here.

So, if we start with this image:

we can get the components labelled and also the bounding boxes, centroids and other statistics for each blob or component like this:

convert input.png                                    \
    -colorspace gray -negate -threshold 10%          \
    -define connected-components:verbose=true        \
    -define connected-components:area-threshold=100  \
    -connected-components 8 -auto-level output.png

Objects (id: bounding-box centroid area mean-color):
  0: 600x600+0+0 296.9,299.6 260033 srgb(0,0,0)
  2: 467x345+70+211 350.1,398.1 53563 srgb(255,255,255)
  1: 422x105+56+81 266.5,133.0 34814 srgb(255,255,255)
  4: 105x90+112+310 164.0,354.5 9450 srgb(255,255,255)
  3: 178x73+393+246 481.5,282.0 2140 srgb(255,255,255)

You can then draw in the bounding box like this:

convert output.png -fill none -stroke red \
  -draw "rectangle 70,211 537,556"        \
  -draw "rectangle 56,81 478,186"         \
  -draw "rectangle 112,310 217,400"       \
  -draw "rectangle 393,246 571,319"       \
  x.png



来源:https://stackoverflow.com/questions/14377463/connected-component-labeling-with-imagemagick

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!