# How can I merge multiple sets of morphological components (perhaps selected using different metrics)?

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?

-

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

c=Import["http://i.stack.imgur.com/gSXIj.png"]


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.

{m1,m2}


You can find the intersection of the two components by multiplying

ImageMultiply[m1,m2]


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]


-
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.

-
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