Watershed Algorithm
This function block performs region-based segmentation using supplied foreground and background masks to separate touching or overlapping objects. It returns a visual overlay with boundaries and a labeled segmentation map for downstream processing.
π₯ Inputs
Image Any The original color or grayscale image to segment.
Fore Ground A binary/grayscale image indicating sure-foreground regions (objects).
Back Ground A binary/grayscale image indicating sure-background regions.
π€ Outputs
Image Any The original image with detected region boundaries overlaid (visual result).
Shed Image A labeled image (grayscale) representing segmented regions / markers.
πΉοΈ Controls
This function block has no adjustable widgets. Provide accurate Fore Ground and Back Ground inputs to control the segmentation result.
βοΈ Running mechanism
The block uses the provided
Fore GroundandBack Groundmasks to determine which pixels are definitely object and which are definitely background.It identifies the unknown region between these masks and assigns intermediate markers.
A segmentation pass separates regions, draws the detected boundaries on a copy of the input image, and produces a labeled
Shed Imagewhere each segmented region has a distinct marker value.The overlaid image highlights boundaries for quick visual inspection while the labeled image is suitable for measurements or counting.
π― Features
Works well for separating touching objects when clear foreground/background masks are provided.
Produces both a visual overlay for inspection and a labeled output useful for further processing (measurements, counting, extraction).
Accepts color or grayscale input images.
π Usage instructions
Prepare a clear
Fore Groundmask covering the interior of objects and a complementaryBack Groundmask for the background.Connect the original image to
Image Anyand the two masks to their respective inputs.Run the block to obtain the segmented overlay and the labeled
Shed Image.Use the labeled image for downstream tasks such as object counting or extracting individual regions.
π‘ Tips and Tricks
Use
Image Threshold,Image Adaptive Threshold, orHSV Filterto create initial binary masks from the input image.Clean up small holes and noise in masks with
Morphological TransformationsorDenoisingbefore feeding them into this block.For difficult backgrounds, try
Grab Cut AlgorithmorBackground Subtractorto generate better foreground/background candidates.Restrict processing to a region of interest with
Image ROIorImage ROI Selectto speed up segmentation and avoid irrelevant areas.Preview masks and results with
Show Imageand save useful frames usingImage LoggerorImage Write.After segmentation, use
Find Contour,Minimum Rectangle, orMeasure Object Distanceto extract region properties and measurements from the labeledShed Image.
π οΈ Troubleshooting
Poor segmentation or merged objects: check that
Fore Groundcovers only object interiors andBack Groundcovers only background; add morphological opening/closing to refine masks.No boundaries visible: verify that the input images and masks are valid image types (not empty) and that masks are binary or clearly contrasted.
Over-segmentation (too many small regions): smooth or simplify masks using
Blur,Denoising, orMorphological Transformationsbefore segmentation.Under-segmentation (objects not separated): increase the accuracy of foreground markers (use stronger object markers or manual ROI selection) to help separate touching objects.
Last updated
Was this helpful?