({{, (RefLink[SelectComponents, paclet:ref/SelectComponents])[{image,lmat},crit]
selects components of image indicated by the label matrix lmat that satisfy crit, replacing other parts with black.}, {, (RefLink[SelectComponents, paclet:ref/SelectComponents])[image,crit]
selects connected components of image.}, {, (RefLink[SelectComponents, paclet:ref/SelectComponents])[…,"prop",n]
computes the property "prop" and selects the first n in sorted order.}, {, (RefLink[SelectComponents, paclet:ref/SelectComponents])[…,"prop",n,p]
sorts computed properties using the ordering function p.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[SelectComponents, paclet:ref/SelectComponents]) can be used to select image components with specific desired characteristics. Selection may be performed on components' location, shapes, and intensity properties.

Component selection is also used to filter undesired image parts, including small, large, border touching, or specifically shaped components.

(RefLink[SelectComponents, paclet:ref/SelectComponents]) works with 2D or 3D images and label matrices.

A label matrix lmat is an array of non-negative integers, in which each integer represents a component, and 0 represents the background.

If the label matrix lmat is not given, it is computed using the connectivity of nonzero pixels in image.

(RefLink[SelectComponents, paclet:ref/SelectComponents]) selects components based on the given criteria crit. Components that are not selected are replaced with 0 (black) in both image and lmat.

Possible forms of crit include:

When a pure function f is used, component properties can be accessed using (Subscript[#prop, 1]), (Subscript[#prop, 2]), …. A component is selected if f returns (RefLink[True, paclet:ref/True]).

For the complete list of possible properties, see the reference page for (RefLink[ComponentMeasurements, paclet:ref/ComponentMeasurements]).

(RefLink[SelectComponents, paclet:ref/SelectComponents])[{image,lmat},…] returns a modified version of {image,lmat} where unselected components are replaced with 0.

(RefLink[SelectComponents, paclet:ref/SelectComponents])[lmat,…] can also be used to select components from a label matrix lmat.

(RefLink[SelectComponents, paclet:ref/SelectComponents])[…,"prop",n] computes "prop" for all components and returns the first n, sorted using (RefLink[Sort, paclet:ref/Sort]).

(RefLink[SelectComponents, paclet:ref/SelectComponents])[…,{"(Subscript[prop, 1])","(Subscript[prop, 2])",…},n] computes and sorts multiple properties.

(RefLink[SelectComponents, paclet:ref/SelectComponents])[…,"prop",-n] returns the last n components.

(RefLink[SelectComponents, paclet:ref/SelectComponents]) takes a (RefLink[CornerNeighbors, paclet:ref/CornerNeighbors]) option. The default setting is (RefLink[CornerNeighbors, paclet:ref/CornerNeighbors])->(RefLink[True, paclet:ref/True]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (16)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Select the outermost components in an image:

SelectComponents[,#EnclosingComponentCount==0&]

Select three smallest components:

SelectComponents[,"Count",3]

Select 3D image components:

SelectComponents[, #Count>500&]

OpenersExampleSection1(Spacer1)Scope  (7)

Use a single property to select components:

SelectComponents[,#Elongation>.3&]

Use multiple properties:

SelectComponents[,#Elongation>.3&&#AdjacentBorderCount==0&]

Select large components from a binary image:

SelectComponents[,Large]

Select small components:

SelectComponents[,Small]

Select the six smallest components:

SelectComponents[,"Count",6]

Select the six largest components:

SelectComponents[,"Count",-6]

Select components of a label matrix:

SelectComponents[ (({{1, 1, 1, 0}, {1, 1, 1, 0}, {1, 1, 1, 0}, {0, 2, 2, 2}})),#Count> 5&]//MatrixForm

OpenersExampleSection2(Spacer1)Options  (1)

OpenersExampleSubsection0(Spacer1)CornerNeighbors  (1)

By default, (RefLink[SelectComponents, paclet:ref/SelectComponents]) considers diagonally adjacent corners to be connected:

SelectComponents[,Large]

Use (RefLink[CornerNeighbors, paclet:ref/CornerNeighbors])->(RefLink[False, paclet:ref/False]) to assume otherwise:

SelectComponents[,Large,CornerNeighbors->False]

OpenersExampleSection3(Spacer1)Applications  (5)

Select all the border components:

i=;

SelectComponents[i,#AdjacentBorderCount>0&]

Select all interior components:

SelectComponents[i,#AdjacentBorderCount==0&]

Select the four roundest objects:

SelectComponents[,"FilledCircularity",-4]

Find objects smaller than 500 pixels that have holes and do not touch an image border:

m=MorphologicalComponents[Binarize[,{0,.7}]];

SelectComponents[m,#Count<500&&#AdjacentBorderCount==0&&#Holes>0&]//Colorize

Extract segments belonging to numbers in an image of a calendar:

m = MorphologicalComponents[BottomHatTransform[,8],.1];
SelectComponents[m,#Width>5&]//Colorize

Find the river in a satellite image by selecting the longest component:

c=ImageForestingComponents[];
SelectComponents[c,"Length",-1]//Image

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ComponentMeasurements, paclet:ref/ComponentMeasurements])  ▪  (RefLink[DeleteSmallComponents, paclet:ref/DeleteSmallComponents])  ▪  (RefLink[DeleteBorderComponents, paclet:ref/DeleteBorderComponents])  ▪  (RefLink[Select, paclet:ref/Select])  ▪  (RefLink[MorphologicalComponents, paclet:ref/MorphologicalComponents])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Segmentation Analysis, paclet:guide/SegmentationAnalysis])}, {▪(RefLinkPlain[Mathematical Morphology, paclet:guide/MathematicalMorphology])}, {▪(RefLinkPlain[Image Computation for Microscopy, paclet:guide/ImageComputationForMicroscopy])}})}})