AI Course
Lesson 87: Image Segmentation
Image segmentation is a computer vision technique that divides an image into meaningful regions. Instead of detecting objects with boxes, segmentation works at the pixel level and tells exactly which pixels belong to which object or region.
This makes segmentation more precise than object detection and extremely useful for tasks that require fine-grained understanding of images.
Real-World Connection
Image segmentation is widely used in medical imaging to identify tumors, in satellite imagery to analyze land use, in autonomous driving to separate roads, vehicles, and pedestrians, and in photo editing apps to remove backgrounds accurately.
Any application that needs pixel-level accuracy depends on segmentation.
What Is Image Segmentation?
Image segmentation assigns a label to every pixel in an image so that pixels with the same label belong to the same object or region.
- Works at pixel level
- Produces masks instead of boxes
- Provides high precision
Types of Image Segmentation
There are three main types of segmentation:
- Semantic Segmentation: Labels each pixel by class (road, car, person)
- Instance Segmentation: Separates individual objects of the same class
- Panoptic Segmentation: Combines semantic and instance segmentation
How Image Segmentation Works
Segmentation models use convolutional neural networks to extract features and then predict class labels for each pixel.
- Image passes through convolution layers
- Features are learned at different scales
- Final output is a segmentation mask
Simple Image Segmentation Example
Below is a simple example using OpenCV to perform basic threshold-based segmentation.
import cv2
image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
_, segmented = cv2.threshold(
image, 127, 255, cv2.THRESH_BINARY
)
cv2.imshow("Original", image)
cv2.imshow("Segmented", segmented)
cv2.waitKey(0)
cv2.destroyAllWindows()
What This Code Is Doing
The image is converted to grayscale and then segmented using a threshold. Pixels above the threshold become white, and pixels below become black.
Although simple, this demonstrates the idea of separating regions based on pixel values.
Understanding the Output
The output image contains two regions: foreground and background. This is a basic form of segmentation.
Advanced AI models produce multi-class segmentation masks instead of binary results.
Popular Segmentation Models
- U-Net: Common in medical imaging
- Mask R-CNN: Instance segmentation
- DeepLab: Semantic segmentation
Why Image Segmentation Matters in AI
Segmentation gives machines a detailed understanding of visual scenes. It allows precise measurements, accurate object boundaries, and better decision-making.
Many advanced vision systems rely on segmentation for safety-critical tasks.
Practice Questions
Practice 1: Image segmentation works at which level?
Practice 2: What type of segmentation labels each pixel by class?
Practice 3: What is the output of a segmentation model called?
Quick Quiz
Quiz 1: Which task labels each pixel in an image?
Quiz 2: Which model is popular in medical image segmentation?
Quiz 3: Segmentation provides accuracy at which level?
Coming up next: Face Recognition — identifying and verifying human faces using AI.