Page 1 of 1

Identifying small clusters of colour

Posted: 2012-03-04T07:35:15-07:00
by ajg
Hi All,

I have a PNG composed very simply of either transparent or black:

Code: Select all

76040: (  0,  0,  0,  0) #00000000 none
10360: (  0,  0,  0,255) #000000 black
There a random clusters of pixels I want to identify, and alter the colour of, without affecting the rest of the image. Over the CLI how can I identify these clusters for modification? For example, how would you do this by their approximate size?

Adam

Re: Identifying small clusters of colour

Posted: 2012-03-04T12:11:34-07:00
by fmw42
I don't think IM can find connected regions specifically. It can find all pixels of a given color within a fuzz distance. Then you can change those pixels. But IM will not know if they are connected or not.

If you have a binary image that has lots of small pixels that you want to remove, then you can use -morphology open or close depending upon whether black or white. Morphology may let you find certain shaped regions, but that would be custom kernels. Anthony may have more to say on this as he is the morphology expert.

see
http://www.imagemagick.org/Usage/color_basics/#replace
http://www.imagemagick.org/Usage/morphology/

If you have large regions that you want to separate or deal with, see my script separate at the link below. It may not be what you want, but may give you ideas.

Perhaps you could post a link to an example image, so we have a better idea what you want to do.

Re: Identifying small clusters of colour

Posted: 2012-03-04T18:26:11-07:00
by anthony
The script fred mentions is actually called "separate"... As in separate the parts of an image.
http://www.fmwconcepts.com/imagemagick/ ... /index.php

It is similar to another script in IM examples script area
http://www.imagemagick.org/Usage/scripts/segment_image
See discussions
http://imagemagick.org/discourse-server ... =1&t=11705
http://imagemagick.org/discourse-server ... =1&t=18283
http://imagemagick.org/discourse-server ... =1&t=18683

This is actually a special morphology operation called 'labeling' (silly name really) but which has not been implemented.
I have a FAST 2-pass algorithm for implementing this, which will use a morphology kernel neighbourhood to decide connectivity (typically diamond or square).

It is also closely related to FloodFill (diamond conectivity) for single points and which the above scripts currently use, and a multi-point but slower "Conditional Dilation". See Conditional Dilation example, which was only added a couple of weeks or so ago...
http://www.imagemagick.org/Usage/morpho ... onditional
Note that while conditional dilation can use different colors from different seed points, It only works if the results do not interact with each other. That is the colors remain separated. :-(

Re: Identifying small clusters of colour

Posted: 2012-03-08T10:58:29-07:00
by ajg
Great tips guys! Only just had a chance to check the links Anthony; the new Conditional Dilation is timely, and very useful thank you :)