DevonDahon
DevonDahon

Reputation: 8360

ImageMagick: Filling big holes

How to fill big holes in black and white images with ImageMagick, like in the element at row 6, column 1 ?

enter image description here

=== ADDED ===

Actually, I need to fill inner holes and bays in a mask like the one below. With Gimp, I would fuzzy select the background, invert the selection, grow it by 20 pixels, shrink it by 20 pixels and then Edit > Fill with Foreground color.

enter image description here

Upvotes: 0

Views: 739

Answers (1)

fmw42
fmw42

Reputation: 53164

This seems very similar to your other recent post ImageMagick: Is there an equivalent of Gimp's tool to "shrink" and "grow" a selection?.

For small holes, -morphology will work. But if your holes are larger than the distance between black regions, the black regions will merge.

The best way to do that is to use Imagemagick connected components. This will not bleed between regions. But if your holes are larger than any black regions, those black regions will be merged into white if above the area-threshold.

convert image.png \
-define connected-components:area-threshold=500 \
-define connected-components:mean-color=true \
-connected-components 4 \
result.png


enter image description here

Upvotes: 1

Related Questions