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 have an array of $M$ sets of morphological components corresponding to the same image, however computed in different ways. For example, we might have:

     M = Array[{} &, 4];

     t1 = 0.1;
     t2 = 0.2;
     t3 = 0.5;

     M[[1]] = WatershedComponents[image,t1];
     M[[2]] = MorphologicalComponents[CrossingDetect[image],t2, Method->"BoundingDisk"];
     M[[3]] = MorphologicalComponents[image,t3, Method->"ConvexHull"];
     M[[4]] = MorphologicalComponents[EdgeDetect[image],t1];

Is there a mechanism in Mathematica v9.0 that would allow me to create a function like:


Which will return a set of morphological components in testComponentList that nowhere intersect / overlap any of the components in the array of component lists $M$? Or, in the opposite manner, perhaps a list of components in testComponentList that intersect / overlap with each at least one component in each list of components in $M$?

share|improve this question
up vote 2 down vote accepted

I've used a different set of components to test with:

image = ExampleData[{"TestImage", "Lena"}]~ImageResize~150;    
M = MorphologicalComponents[#, 0.5] & /@ ColorSeparate[image];    
test = WatershedComponents[image];

Colorize /@ Join[M, {test}]

enter image description here

The intersecting components of test for each part of M are:

 intersections = DeleteCases[Union@Flatten[test Unitize@#], 0] & /@ M;

For the components in test which do not intersect any of the M:

SelectComponents[test, "Label", FreeQ[Union @@ intersections, #] &] // Colorize

enter image description here

For the components in test which intersect with all the M:

SelectComponents[test, "Label", MemberQ[Intersection @@ intersections, #] &] // Colorize

enter image description here

share|improve this answer

Brute force, not optimized:

t1 = 0.1;
t2 = 0.2;
t3 = 0.5;
image = ExampleData[{"TestImage", "Lena"}];

M = {WatershedComponents[image],
   MorphologicalComponents[CrossingDetect[image], t2, Method -> "BoundingDisk"],
   MorphologicalComponents[image, t3, Method -> "ConvexHull"],
   MorphologicalComponents[EdgeDetect[image], t1]};

(* let's define a test components array*)
j = First@ImageDimensions@image;
test = Insert[Array[0 &, {j - 1, j}], Flatten@Table[Sequence@{i, i}, {i, j/2}], IntegerPart[j/2]];
(* intersect all *)
s = ((1 + #) & /@ (- Unitize /@ M));
inter = Times @@ Append[s, test];
(*Get the surviving elements*)
survElem = First /@ (Rest@Tally@Flatten@inter);
(*Let's see if the whole component survived*)
Table[If[Position[test, i] == Position[inter, i], i], {i, survElem}]

(* {2, 3, 4, 5, Null} *)
share|improve this answer

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.