Segmenting data
The qim3d library provides a set of methods for data segmentation.
qim3d.segmentation.watershed
Performs watershed segmentation to separate touching objects in a binary volume.
This function converts a binary mask (foreground vs. background) into a labeled instance segmentation (where each object is assigned a unique integer ID). It is particularly useful for splitting fused particles, cells, or blobs that have been merged during thresholding. The algorithm uses a distance transform to identify markers (seeds) at the centers of objects and grows regions outward until they meet the boundaries of the original mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_volume
|
ndarray
|
The 3D binary input mask. Non-zero elements represent the objects to segment. |
required |
min_distance
|
int
|
The minimum distance (in pixels) allowed between distinct object centers (peaks). Increasing this value prevents over-segmentation (splitting single objects), while decreasing it helps separate closely packed objects. Defaults to 5. |
5
|
Returns:
| Name | Type | Description |
|---|---|---|
labeled_vol |
ndarray
|
A 3D integer array with the same shape as the input. Each segmented object is filled with a unique label ID (1, 2, 3...). Background is 0. |
num_labels |
int
|
The total count of unique objects identified. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
import qim3d
vol = qim3d.examples.cement_128x128x128
binary_volume = qim3d.filters.gaussian(vol, sigma = 2)<60
fig1 = qim3d.viz.slices_grid(binary_volume, slice_axis=1, display_figure=True)
labeled_volume, num_labels = qim3d.segmentation.watershed(binary_volume)
cmap = qim3d.viz.colormaps.segmentation(num_labels)
fig2 = qim3d.viz.slices_grid(labeled_volume, slice_axis=1, color_map=cmap, display_figure=True)
Source code in qim3d/segmentation/_common_segmentation_methods.py
qim3d.segmentation.connected_components
Identifies and labels discrete objects (connected components) in a binary volume.
This function groups adjacent non-zero voxels into distinct clusters, assigning a unique integer ID to each detected object. It is a fundamental tool for segmentation analysis, allowing you to count particles, filter noise (by removing small isolated spots), or extract specific regions of interest based on their size.
It returns a specialized ConnectedComponents object that provides convenient methods to:
- Filter objects by size (voxel count).
- Extract specific components or their bounding boxes.
- Analyze the distribution of object sizes via histograms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
The binary input volume (boolean or integer) where non-zero values represent the foreground objects. |
required |
connectivity
|
int
|
Defines which neighbors are considered "connected".
|
1
|
Returns:
| Name | Type | Description |
|---|---|---|
cc |
ConnectedComponents
|
A wrapper object containing the labeled volume. It supports methods like |
Example
Show the largest connected components
import qim3d
vol = qim3d.examples.cement_128x128x128
binary = qim3d.filters.gaussian(vol, sigma=2) < 60
cc = qim3d.segmentation.connected_components(binary)
filtered = cc.filter_by_largest(5)
color_map = qim3d.viz.colormaps.segmentation(len(cc), style='bright')
qim3d.viz.volumetric(filtered, color_map=color_map, constant_opacity=True)
Filter the connected components by size
import qim3d
vol = qim3d.examples.cement_128x128x128
binary = qim3d.filters.gaussian(vol, sigma=2) < 60
cc = qim3d.segmentation.connected_components(binary)
# Show a histogram of the distribution of label sizes
cc.sizes_histogram()
# Based on the histogram, choose a range of sizes
filtered = cc.filter_by_size(min_size=1e2, max_size=2e2)
color_map = qim3d.viz.colormaps.segmentation(len(cc), style='bright')
qim3d.viz.volumetric(filtered, color_map=color_map, constant_opacity=True)