Connected-component labeling with ImageMagick

后端 未结 1 595
无人共我
无人共我 2020-12-21 07:15

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

Connected-component labeling in Wikipedia

相关标签:
1条回答
  • 2020-12-21 07:54

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

    So, if we start with this image:

    enter image description here

    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)
    

    enter image description here

    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
    

    enter image description here

    0 讨论(0)
提交回复
热议问题