7
$\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$

1 Answer 1

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$
4
  • $\begingroup$ serves me right for starting to implement flood fill in mathematica before reading the docs... $\endgroup$
    – acl
    Commented May 20, 2012 at 11:41
  • $\begingroup$ @acl ;-) Useful for v6 and earlier. $\endgroup$ Commented May 20, 2012 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$ Commented May 21, 2012 at 7:53
  • $\begingroup$ @AlexeyPopkov Just use ColorNegate to invert the image. $\endgroup$ Commented May 21, 2012 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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