Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Building a segmentation pipeline

In this module we extend our thresholding widget into a full segmentation pipeline: we add morphological cleaning, label connected components, compute quantitative features (area, centroid), display them on a Points layer, and finally refine the result with a watershed step seeded by manual point annotations.

1. Thresholding + morphology + labels

Let’s build on the previous widget. We add two new parameters — min_hole_size and min_obj_size — and return intermediate layers so you can see each processing step.

04_labels.py

Play with min_hole_size and min_obj_size to clean up the binary mask before labeling. You can toggle visibility (eye icon in the layerlist) of the intermediate layers to see what each step does.

Loading...

2. Quantitative features + Points layer

Let’s compute region properties (area, centroid) for each detected object and display them. We use skimage.measure.regionprops_table and attach the results as layer features, then add a Points layer with the centroids.

05_features_and_points.py

Loading...

The number of layers is starting to be high and knowing exactly which parameter to adjust can be tricky without an overview of all the steps. Let’s enable the grid view to spread out each individual layer into its own viewbox. We can also enable the layer name overlay on each layer, to make it easier to know what’s what.

Loading...

3. Watershed refinement

Sometimes the label boundaries are not perfect — especially when objects touch. We can refine them using a watershed segmentation, seeded by points we place manually.

For this, let’s implement a separate function, since this requires manual intervention and is also too computationally expensive to run continuously.

06_watershed.py

Loading...

Recap

Next up: mouse callbacks for interactive label inspection, and taking our pipeline into 3D!