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

In image segmentation it is a common problem that objects appear clustered and are therefore undistinguishable from each other when using simple algorithms. Based on this post I obtained efficient ways to tackle the problem based on fast 3D distance transformation (DistanceTransform3D) and the usage of the Mathematica built-in function MaxDetect. For details please have a look at the answer of UDB in the post. Consider two simple functions to generate a test Image3D containing sphere-like objects:

generateRandomObject3D[dim_, min_, max_] := Block[
   {rdDim, rd, v, s1, s2},
   rdDim = {RandomInteger[{min, max}], RandomInteger[{min, max}], 
     RandomInteger[{min, max}]};
   rd = DiskMatrix[rdDim];
   v = dim - 2 # - 1 & /@ rdDim;
   s1 = # - RandomInteger[{1, #}] & /@ v; s2 = v - s1;
   ArrayPad[rd, MapThread[List, {s1, s2}]]

generateRandomImage3D[dim_, min_, max_, objNum_] := Block[
    Plus @@ Table[generateRandomObject3D[dim, min, max], {objNum}]]

objects=generateRandomImage3D[64, 5, 5, 15];

bin comps

Now we can identify the locations of the potentially clustered objects using a 3D distance transformation followed by MaxDetect.

AbsoluteTiming[MaxDetect[ImageAdjust@DistanceTransform3D[rand], 0.1]]

{1.339987, enter image description here}

As you can see this operation already takes more than one second. If I check for the runtime of the individual steps, DistanceTransform3D needs about 1/4 of the time.

Is there a faster way of doing this? The images I need to apply the algorithm to are much larger. Therefore, efficiency and runtime plays an important role in this case.

share|improve this question

Your Answer


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

Browse other questions tagged or ask your own question.