Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to find all clusters of adjacent white points in a binary image. By term "adjacent" I mean that coordinates of two points differ by no more than 1 (i.e. the points {0,0} and {1,1} are adjacent). How to do this in Mathematica efficiently?

share|improve this question
up vote 13 down vote accepted

In version 7 and 8 you have MorphologicalComponents which can do this. Its default Method (version 8) is "Connected" with 8 point connectivity being the default (you can switch this off with the option CornerNeighbors->False). So, this seems ideally suited to your requirements.

An example from the MorphologicalComponents doc page:

Mathematica graphics

share|improve this answer
serves me right for starting to implement flood fill in mathematica before reading the docs... – acl May 20 '12 at 11:41
@acl ;-) Useful for v6 and earlier. – Sjoerd C. de Vries May 20 '12 at 11:49
@Sjoerd +1. You (and the creators of the Documentation) forgot to mention that the target clusters of points must be white on black background. My original image is negate of this and this is the reason why my first attempts to use MorphologicalComponents failed. – Alexey Popkov May 21 '12 at 7:53
@AlexeyPopkov Just use ColorNegate to invert the image. – Sjoerd C. de Vries May 21 '12 at 8:30

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.