6
$\begingroup$

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?

$\endgroup$
13
$\begingroup$

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

$\endgroup$
  • $\begingroup$ serves me right for starting to implement flood fill in mathematica before reading the docs... $\endgroup$ – acl May 20 '12 at 11:41
  • $\begingroup$ @acl ;-) Useful for v6 and earlier. $\endgroup$ – Sjoerd C. de Vries May 20 '12 at 11:49
  • 1
    $\begingroup$ @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. $\endgroup$ – Alexey Popkov May 21 '12 at 7:53
  • $\begingroup$ @AlexeyPopkov Just use ColorNegate to invert the image. $\endgroup$ – Sjoerd C. de Vries May 21 '12 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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