# How can I use SelectComponents to select for components that have a pixel within a certain RGB range within their convex hull?

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?

-
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

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


...with a lot of components:

Max[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];


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[
Flatten[
0]
(* {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


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[
Flatten[
(1 + components) * ImageData[critMask] ],
0]
(* {1, 399, 451, 523, 527, 634} *)

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


-