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 calculate a set of morphological components:

Components = MorphologicalComponents[Binarize[image,0.1]];

We can now use SelectComponents to filter for components satisfying some metric, for example:

ChosenComponents = SelectComponents[Components, "Elongation", # <= 0.5 &];

However, is there a way to use SelectComponents to select for components that have a pixel within a certain RGB range in their convex hulls? For example, could we select for only the components that have a pixel with a "red" component of 0.2?

share|improve this question
If you could post a link to an example image, someone with enough rep could upload it to the site and include it in your question. You might get a better answer with an image that represents your actual problem. – Michael E2 Aug 5 '13 at 14:46
up vote 1 down vote accepted

The idea is to multiply the component numbers in components by 1 or 0 according as the corresponding pixel does or does not meet your criterion. The nonzero elements left over indicate a component that contains a pixel satisfying the criterion.

I couldn't find a good image to use, but I found one that will illustrate the process.

image = ExampleData[{"TestImage", "Sailboat"}];

To me it seems rather complicated...

(components = MorphologicalComponents[image, 0.4]) // Colorize

Mathematica graphics

...with a lot of components:

(* 685 *)

Here I had to make up a criterion that suited the image being used.

criterion = Function[rgb, rgb[[1]] > 0.7 && rgb[[2]] < 0.5 && rgb[[3]] < 0.5];
critMask = Binarize[image, criterion]

Mathematica graphics

In the product of the image data arrays, the nonzero entries are the components to be selected, so we delete the zeros and remove duplicate entries.

selected = DeleteDuplicates @ DeleteCases[
      components * ImageData[critMask] ],
(* {398, 450, 522, 526, 633} *)

You can use this list to further process components. For instance, below we replace all the undesired component numbers with 0 and Colorize the result.

components /. 
  Except[Alternatives @@ selected, _Integer] -> 0 // Colorize

Mathematica graphics

One issue is that some of the critMask overlaps component 0 in this image. This may not be a problem in the actual images you have. If, however, you wish to include component 0 in the selection process, just add 1 to the components.

selected = DeleteDuplicates @ DeleteCases[
     (1 + components) * ImageData[critMask] ],
(* {1, 399, 451, 523, 527, 634} *)

1 + components /. Except[Alternatives @@ selected, _Integer] -> 0 // Colorize

Mathematica graphics

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.