What Is Graph-Theoretic Segmentation?
It is a way of dividing an image into regions by thinking of the image as a graph.
↓
Pixels become graph nodes
↓
Neighboring pixels are connected
↓
Compare their colors
↓
Group similar pixels
↓
Segments
Graph-theoretic image segmentation represents an image as a graph and groups similar connected pixels into regions.
Pixels Become Nodes
Imagine every pixel is a point in a graph.
│ │
Pixel C ─── Pixel D
A pixel in the image.
A connection between neighboring pixels.
If neighboring pixels look similar, they probably belong to the same region.
What does a graph node represent?
Similar Pixels Stay Together
The edge between two neighboring pixels can represent how different their colors are.
Blue pixel ─ large difference ─ Red pixel
Pixels are similar → keep together.
Pixels are different → separate them.
Similar → join. Different → separate.
Install the Correct OpenCV Package
Graph segmentation is inside OpenCV's extra ximgproc module.
pip install opencv-contrib-python numpy
Use opencv-contrib-python because ximgproc is an extra OpenCV module.
Read an image
import cv2
image = cv2.imread("image.jpg")
Choose an image with clear regions such as sky, grass, road, buildings, or colorful objects.
Create a Graph Segmentor
OpenCV already implements the graph-based segmentation algorithm for us.
segmentor = cv2.ximgproc.segmentation.createGraphSegmentation(
sigma=0.5,
k=300,
min_size=100
)
How much the image is smoothed first.
Controls how easily regions are joined.
Very tiny regions are merged.
Just start with 0.5, 300, 100.
Call processImage()
Give the image to the graph segmentation algorithm.
labels = segmentor.processImage(image)
labels contains a segment number for every pixel.
0 0 0 1 1
2 2 2 1 1
- All pixels with label
0belong to one segment. - All pixels with label
1belong to another segment. - All pixels with label
2belong to another segment.
processImage() returns segment IDs, not a colorful final image.
Give Each Segment One Color
To make the result easy to see, fill each segment with its average color.
import numpy as np
result = np.zeros_like(image)
for label in np.unique(labels):
mask = labels == label
average_color = image[mask].mean(axis=0)
result[mask] = average_color
↓
Find all its pixels
↓
Find their average color
↓
Paint the whole segment
↓
Repeat
This makes the segmented regions easy to see.
Complete Graph Segmentation Practical
This is the main program students need to understand.
import cv2
import numpy as np
# Read image
image = cv2.imread("image.jpg")
# Create graph segmentation algorithm
segmentor = cv2.ximgproc.segmentation.createGraphSegmentation(
sigma=0.5,
k=300,
min_size=100
)
# Segment the image
labels = segmentor.processImage(image)
# Empty output image
result = np.zeros_like(image)
# Give each segment its average color
for label in np.unique(labels):
mask = labels == label
average_color = image[mask].mean(axis=0)
result[mask] = average_color
# Show result
cv2.imshow("Original", image)
cv2.imshow("Segmented", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
↓
Create GraphSegmentation
↓
processImage()
↓
Get labels
↓
Color each segment
↓
Display
Try only these experiments
- Run with
k=300. - Change
kto500. - See if regions become larger or merge more.
- Change
min_sizefrom100to200.
Understand sigma, k and min_size
You only need a basic understanding of these three values.
| Parameter | Simple meaning |
|---|---|
sigma |
Smooths the image before segmentation. |
k |
Controls how easily regions are merged. Larger values generally give larger regions. |
min_size |
Minimum segment size. Tiny segments are merged. |
The best values depend on the image. Just understand what each one controls.
What You Need to Remember
These short answers are enough for a basic college exam or viva.
cv2.ximgproc.segmentation.createGraphSegmentation().Completion checklist
- I know what graph-theoretic segmentation means.
- I know that pixels are nodes.
- I know that edges connect neighboring pixels.
- I understand similar → join, different → separate.
- I can create GraphSegmentation in OpenCV.
- I know what processImage() returns.
- I understand sigma, k and min_size.
- I can run the complete practical.
Codelab complete! 🎉
You can now explain and demonstrate segmentation by graph-theoretic clustering using OpenCV.