Grab Cut Algorithm
This function block separates foreground objects from the background using an interactive segmentation approach. It offers three operation modes so you can provide different types of hints (automatic detection, a rectangular ROI, or small foreground/background masks) to guide the extraction.
π₯ Inputs (sockets)
Image Any Provide the source color image to segment.
ROI (optional, as input socket in ROI mode) Provide a rectangle to indicate the rough object location (format: [(x1,y1),(x2,y2)]).
Foreground (optional, as input socket in Mask mode) Provide a grayscale mask hinting probable foreground pixels.
Background (optional, as input socket in Mask mode) Provide a grayscale mask hinting probable background pixels.
π€ Outputs (sockets)
Image Any Segmented image where the background is removed or masked out β the foreground object remains.
πΉοΈ Controls
Grabcut Method Select between available extraction modes:
AutomaticThe block attempts to locate the main object automatically and extract it.ROIUse when you can provide a rectangular region around the object.MaskUse when you can supply small foreground and background mask hints.
π¨ Features
Multiple operation modes lets you pick the best approach for your image and workflow.
Accepts both rough rectangles and fine foreground/background hints for flexible segmentation.
Produces a clean cutout suitable for further processing or export.
Works on color images (RGB/BGR) and returns a masked color result.
βοΈ Running mechanism (high level)
The block reads the selected
Grabcut Methodand expects the corresponding sockets to be connected when required.In
Automaticmode it finds an initial foreground region automatically and refines it.In
ROImode it uses the provided rectangle as the initial guess for extraction.In
Maskmode it uses the provided small foreground/background masks to guide the extraction.The result is a segmented image with background removed or suppressed and is provided through the
Image Anyoutput socket.
π Usage Instructions
Connect an image-producing block to the
Image Anyinput.Choose
Grabcut Method:For quick automated results pick
Automatic.For controlled extraction supply a rectangle to
ROIand chooseROI.For the best accuracy in challenging scenes provide small foreground and background masks and choose
Mask.
When using
ROI, supply a rectangle in the format[(x1,y1),(x2,y2)]from an ROI selection block.When using
Mask, supply small grayscale masks marking some foreground and background pixels.Read the segmented output from the
Image Anyoutput socket and continue processing or save/export.
π‘ Tips and Tricks
If the subject is easy to isolate by color, add a color prefilter before this block. Useful blocks for that:
HSV Filter,RGB Mask, orImage Threshold. These can help create helpfulForeground/Backgroundmasks forMaskmode.Use
Image ROI Selectto draw and provide a precise rectangle when usingROImode.For noisy images try smoothing first with
BlurorDenoisingto improve contour detection inAutomaticmode.Combine with
Morphological Transformationsafter segmentation to clean small holes or speckles in the mask.If you need to visualize or save results, attach
Show Imageto preview andImage WriteorImage Loggerto persist outputs.For complex backgrounds try running
Background SubtractororBackground Removal (RMBG-1.4)/Background Removal (BiRefNet)earlier in the flow to create a better initial mask for the Grab Cut block.
π οΈ Troubleshooting
If the result misses parts of the object:
Try
ROImode with a slightly larger rectangle.Or provide a small
Foregroundmask marking a few object pixels.
If too much background remains:
Provide
Backgroundmask hints marking background regions.Try
Morphological Transformationsto remove residual background after segmentation.
If no object is detected in
Automaticmode:Preprocess with
Contrast OptimizationorAuto Contrastto increase separation between object and background.Use
ROImode instead if the automatic guess fails.
Ensure any mask inputs are provided as grayscale images and that ROI coordinates are within image bounds.
This block is ideal when you need a reliable foreground extraction step before detection, measurement, or visualization.
Last updated
Was this helpful?