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:

  • Automatic The block attempts to locate the main object automatically and extract it.

  • ROI Use when you can provide a rectangular region around the object.

  • Mask Use 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 Method and expects the corresponding sockets to be connected when required.

  • In Automatic mode it finds an initial foreground region automatically and refines it.

  • In ROI mode it uses the provided rectangle as the initial guess for extraction.

  • In Mask mode 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 Any output socket.

πŸ“ Usage Instructions

  1. Connect an image-producing block to the Image Any input.

  2. Choose Grabcut Method:

    • For quick automated results pick Automatic.

    • For controlled extraction supply a rectangle to ROI and choose ROI.

    • For the best accuracy in challenging scenes provide small foreground and background masks and choose Mask.

  3. When using ROI, supply a rectangle in the format [(x1,y1),(x2,y2)] from an ROI selection block.

  4. When using Mask, supply small grayscale masks marking some foreground and background pixels.

  5. Read the segmented output from the Image Any output 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, or Image Threshold. These can help create helpful Foreground/Background masks for Mask mode.

  • Use Image ROI Select to draw and provide a precise rectangle when using ROI mode.

  • For noisy images try smoothing first with Blur or Denoising to improve contour detection in Automatic mode.

  • Combine with Morphological Transformations after segmentation to clean small holes or speckles in the mask.

  • If you need to visualize or save results, attach Show Image to preview and Image Write or Image Logger to persist outputs.

  • For complex backgrounds try running Background Subtractor or Background 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 ROI mode with a slightly larger rectangle.

    • Or provide a small Foreground mask marking a few object pixels.

  • If too much background remains:

    • Provide Background mask hints marking background regions.

    • Try Morphological Transformations to remove residual background after segmentation.

  • If no object is detected in Automatic mode:

    • Preprocess with Contrast Optimization or Auto Contrast to increase separation between object and background.

    • Use ROI mode 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?