Skip to main content

Lesson 1 of 12

What is Image Segmentation?

Image segmentation divides an image into meaningful regions. Instead of working with the whole image, you isolate specific areas — objects, backgrounds, or regions of interest.

8 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

What is Image Segmentation?

Image segmentation is the process of partitioning an image into multiple segments (regions) where each segment contains pixels that share similar properties.

Types of segmentation:

  1. 1Semantic segmentation: label every pixel with a class (car, road, sky, person)
  2. 2Instance segmentation: separate individual object instances of the same class
  3. 3Binary segmentation: split image into foreground (object) and background

In this module we focus on practical binary and colour-based segmentation using classical OpenCV techniques — the foundation before deep-learning segmentation.

Diagram
Loading diagram…

Machine Learning follows a structured pipeline from data to deployment

Segmentation Pipeline

Segmentation Pipeline
import cv2
import numpy as np

# Standard segmentation pipeline:

image = cv2.imread("photo.jpg")

# Step 1: Pre-process
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)

# Step 2: Segment (threshold → binary mask)
_, mask = cv2.threshold(blurred, 0, 255,
                        cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Step 3: Clean mask
kernel = np.ones((5, 5), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)

# Step 4: Extract objects using mask
foreground = cv2.bitwise_and(image, image, mask=mask)

# Step 5: Analyse extracted regions
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print(f"Segmented {len(contours)} regions")

cv2.imwrite("segmented.jpg", foreground)

Finished this lesson?

0 of 12 in Module 6
Sign in to save progress

Topics in This Module

0/12 done