Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

Let's say I use SelectComponents to select morphological components in an image according to some criterion, like "Elongation". Then let's say, I pull out a different set of morphological components using another criterion like "Area".

m1 = SelectComponents[testImage, "Elongation", # == 1 &];
m2 = SelectComponents[testImage, "Area", # > 42 &];

How can I properly merge m1 and m2 into a single set of non-intersecting morphological components?

share|improve this question
up vote 5 down vote accepted

You can combine the outputs of SelectComponents in a straightforward way. Let's take a test image from the docs:


enter image description here

and select two conditions on the components:

m1 = SelectComponents[c, "Elongation", # > 0.5 &];
m2 = SelectComponents[c, "Area", # < 1000 &];

These m1 and m2 are binary images with 1's where the criterion is fulfilled and 0's where it fails.


enter image description here

You can find the intersection of the two components by multiplying


enter image description here

You can find the union of the two selected components by adding (for binary images, ImageAdd is essentially the logical OR of the two images)

ImageAdd[m1, m2]

enter image description here

share|improve this answer
I think the OP is referring to a Union operation, while you're addressing an Intersection. Not sure, though – Dr. belisarius Nov 2 '13 at 15:51
@belisarius -- I've added the union operation. – bill s Nov 2 '13 at 15:55

From the Mathematica documentation:

Selecting components based on multiple properties:

SelectComponents[image, {"Count", "AdjacentBorderCount"}, #1 > 100 && #2 == 0 &]

So instead of calling SelectComponents twice and somehow merge the output, you can just call it once with any logical combination of your criteria.

share|improve this answer
Out of curiosity, there isn't anything like ComponentJoin[{m1, m2, ...}]? – RVoight Nov 2 '13 at 15:21
Not that I know of. But you could always add the two images, binarize the result and use MorphologicalComponents to implement that. – nikie Nov 2 '13 at 15:50

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.