How to extract the foreground of an image using OpenCV Python?

OpenCV's GrabCut algorithm is a powerful method for foreground extraction from images. The cv2.grabCut() function uses iterative graph cuts to separate foreground objects from the background based on a user-defined rectangle.

Algorithm Overview

GrabCut works by modeling foreground and background pixels using Gaussian Mixture Models (GMM). You define a rectangle around the object of interest, and the algorithm iteratively refines the segmentation ?

Step-by-Step Process

  • Import required libraries OpenCV and NumPy

  • Read the input image using cv2.imread()

  • Initialize variables: mask, bgdModel, and fgdModel

  • Define rectangle coordinates (x, y, width, height) that enclose the foreground object

  • Apply cv2.grabCut() with mode cv2.GC_INIT_WITH_RECT

  • Create final mask and extract the foreground

Syntax

cv2.grabCut(img, mask, rect, bgdModel, fgdModel, iterCount, mode)

Example

Here's a complete example that extracts the foreground from an image ?

import numpy as np
import cv2

# Create a sample image for demonstration
img = np.ones((400, 600, 3), dtype=np.uint8) * 255
cv2.rectangle(img, (200, 100), (400, 300), (0, 128, 255), -1)  # Orange rectangle
cv2.circle(img, (300, 200), 50, (255, 0, 0), -1)  # Blue circle

# Initialize mask and models
mask = np.zeros(img.shape[:2], np.uint8)
bgdModel = np.zeros((1, 65), np.float64)
fgdModel = np.zeros((1, 65), np.float64)

# Define rectangle around the foreground object
rect = (150, 50, 300, 250)  # (x, y, width, height)

# Apply GrabCut algorithm
cv2.grabCut(img, mask, rect, bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT)

# Create final mask (0 for background, 1 for foreground)
mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')

# Extract foreground
foreground = img * mask2[:, :, np.newaxis]

# Display results
cv2.imshow('Original Image', img)
cv2.imshow('Foreground Extracted', foreground)
cv2.waitKey(0)
cv2.destroyAllWindows()

Key Parameters

Parameter Description Typical Value
iterCount Number of iterations 5-20
rect Rectangle coordinates (x,y,w,h) Encloses object
mode Initialization mode cv2.GC_INIT_WITH_RECT

Tips for Better Results

  • Ensure the rectangle tightly encloses the foreground object

  • Use higher iteration counts (10-20) for complex images

  • For better accuracy, you can manually mark some pixels as definite foreground/background

Conclusion

GrabCut is an effective semi-automatic segmentation tool that requires minimal user input. The key to success lies in accurate rectangle placement and appropriate iteration count for your specific image.

Updated on: 2026-03-26T22:58:10+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements