BBSysDyn
BBSysDyn

Reputation: 4601

Finding the Largest Connected Blob Starting from Selected Point

I am trying to extract a region from an image that is already marked with a certain color. In the picture below

enter image description here

I would like to extract only the pixels which belong to the sidewalk, that is, all pixels that belong to the black blob that is connected to the mid-lower part of the image. There are black dots outside that blob which I am not interested in. So if I could get roughly the region shown below

enter image description here

it would be perfect. Does anyone know of some common algorithms that can do this? Morphology? Region growing using a kind of flooding algorithm?

Thanks,

Upvotes: 0

Views: 108

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207395

You can do that quite easily with a flood fill. If I use ImageMagick to demonstrate at the command line because it is installed on most Linux distros and is available for macOS and Windows.

So, bearing in mind that the pixel you identified as your seed is at around 440,520 in the image you supplied that includes the axes, we can floodfill all pixels that match that colour and touch the seed with cyan using:

convert scene.png -fill cyan -draw 'color 440,520 floodfill' result.png

enter image description here

Or, we can make a mask by changing the non-cyan pixels to white and the cyan pixels to black:

convert scene.png -fill cyan -draw 'color 440,520 floodfill' -fill white +opaque cyan -fill black -opaque cyan z.png

enter image description here

There are a thousand other things you can simply do from the command line to take this further... fill small holes in the mask, make a transparency layer from the mask - just ask more questions if you need a hand.


If you want to close the holes in your image, you probably want to use morphological functions. I am away from any computers with ImageMagick for a week so I can only tell you in general terms. Start with the pure black and white (no grey) picture above and try:

convert image.png -morphology open disk:3 result.jpg

Try replacing the word open above with close, erode or dilate. Experiment with disk, disk:3 disk:7 and so on.

Upvotes: 1

Related Questions