SelectComponents
✖
SelectComponents
selects components of image indicated by the label matrix lmat that satisfy crit, replacing other parts with black.
computes the property "prop" and selects the first n in sorted order.
Details and Options


- 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.
- 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.
- 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:
-
Large components bigger than a globally determined threshold Small components smaller than a globally determined threshold f components that satisfy f - When a pure function f is used, component properties can be accessed using #prop1, #prop2, …. A component is selected if f returns True.
- For the complete list of possible properties, see the reference page for ComponentMeasurements.
- SelectComponents[{image,lmat},…] returns a modified version of {image,lmat} where unselected components are replaced with 0.
- SelectComponents[lmat,…] can also be used to select components from a label matrix lmat.
- SelectComponents[…,"prop",n] computes "prop" for all components and returns the first n, sorted using Sort.
- SelectComponents[…,{"prop1","prop2",…},n] computes and sorts multiple properties.
- SelectComponents[…,"prop",-n] returns the last n components.
- SelectComponents takes a CornerNeighbors option. The default setting is CornerNeighbors->True.
Examples
open allclose allBasic Examples (3)Summary of the most common use cases
Scope (7)Survey of the scope of standard use cases
Use a single property to select components:

https://wolfram.com/xid/0dqtl9sfuhf-hzyjni


https://wolfram.com/xid/0dqtl9sfuhf-bu968j

Select large components from a binary image:

https://wolfram.com/xid/0dqtl9sfuhf-ef9njm


https://wolfram.com/xid/0dqtl9sfuhf-pgkyn1

Select the six smallest components:

https://wolfram.com/xid/0dqtl9sfuhf-osq8qd

Select the six largest components:

https://wolfram.com/xid/0dqtl9sfuhf-c82tpf

Select components of a label matrix:

https://wolfram.com/xid/0dqtl9sfuhf-739xv0

Options (1)Common values & functionality for each option
CornerNeighbors (1)
By default, SelectComponents considers diagonally adjacent corners to be connected:

https://wolfram.com/xid/0dqtl9sfuhf-ys0ns

Use CornerNeighborsFalse to assume otherwise:

https://wolfram.com/xid/0dqtl9sfuhf-636qtk

Applications (5)Sample problems that can be solved with this function
Select all the border components:

https://wolfram.com/xid/0dqtl9sfuhf-7i90cj

https://wolfram.com/xid/0dqtl9sfuhf-j49zv9

Select all interior components:

https://wolfram.com/xid/0dqtl9sfuhf-54a7cr

Select the four roundest objects:

https://wolfram.com/xid/0dqtl9sfuhf-cucx8n

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

https://wolfram.com/xid/0dqtl9sfuhf-l3ii5

https://wolfram.com/xid/0dqtl9sfuhf-9lw2l

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

https://wolfram.com/xid/0dqtl9sfuhf-y5y67

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

https://wolfram.com/xid/0dqtl9sfuhf-ev5h3c

Wolfram Research (2010), SelectComponents, Wolfram Language function, https://reference.wolfram.com/language/ref/SelectComponents.html (updated 2016).
Text
Wolfram Research (2010), SelectComponents, Wolfram Language function, https://reference.wolfram.com/language/ref/SelectComponents.html (updated 2016).
Wolfram Research (2010), SelectComponents, Wolfram Language function, https://reference.wolfram.com/language/ref/SelectComponents.html (updated 2016).
CMS
Wolfram Language. 2010. "SelectComponents." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/SelectComponents.html.
Wolfram Language. 2010. "SelectComponents." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2016. https://reference.wolfram.com/language/ref/SelectComponents.html.
APA
Wolfram Language. (2010). SelectComponents. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SelectComponents.html
Wolfram Language. (2010). SelectComponents. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SelectComponents.html
BibTeX
@misc{reference.wolfram_2025_selectcomponents, author="Wolfram Research", title="{SelectComponents}", year="2016", howpublished="\url{https://reference.wolfram.com/language/ref/SelectComponents.html}", note=[Accessed: 24-March-2025
]}
BibLaTeX
@online{reference.wolfram_2025_selectcomponents, organization={Wolfram Research}, title={SelectComponents}, year={2016}, url={https://reference.wolfram.com/language/ref/SelectComponents.html}, note=[Accessed: 24-March-2025
]}