3
$\begingroup$

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?

$\endgroup$

2 Answers 2

5
$\begingroup$

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

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

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.

{m1,m2}

enter image description here

You can find the intersection of the two components by multiplying

ImageMultiply[m1,m2]

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

$\endgroup$
2
  • 1
    $\begingroup$ I think the OP is referring to a Union operation, while you're addressing an Intersection. Not sure, though $\endgroup$ Nov 2, 2013 at 15:51
  • 1
    $\begingroup$ @belisarius -- I've added the union operation. $\endgroup$
    – bill s
    Nov 2, 2013 at 15:55
5
$\begingroup$

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.

$\endgroup$
3
  • $\begingroup$ Out of curiosity, there isn't anything like ComponentJoin[{m1, m2, ...}]? $\endgroup$
    – RVoight
    Nov 2, 2013 at 15:21
  • $\begingroup$ Not that I know of. But you could always add the two images, binarize the result and use MorphologicalComponents to implement that. $\endgroup$ Nov 2, 2013 at 15:50
  • $\begingroup$ Can probably be done with ArrayComponents and ComponentMeasurements[image,"Label",-1] $\endgroup$
    – Sterling
    Feb 24, 2021 at 5:56

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.