Digital Image
Processing
Image Segmentation
Dr. Fawad Nasim
fawad.nasim@superior.edu.pk
Contents
• What is Segmentation?
• What is thresholding?
• Simple thresholding
• Adaptive thresholding
Image segmentation
Image segmentation is a computer vision technique that partitions a digital image
into discrete groups of pixels—image segments—to inform object detection and
related tasks.
Each segment:
Looks uniform
Belongs to a single object.
Have some uniform attributes.
All the pixels related to it are connected.
Detection of Discontinuities
Point Detection
Line Detection
Edge Detection
Detection of Discontinuities
Matrix notation
w = [w1 w2 ... wn] maske
Z = [ z1 z2 … zn] tilhørende billed pixels
R(x,y) = [w] [Z]’
Convolution with 3x3 mask
R w1 z1 w2 z 2 ..... w9 z9
i 9 i n
wi zi or general wi zi
i 1 i 1
Point detection
R T
T is a non-zero threshold
Segmentation Algorithms
Segmentation algorithms are based on one of
two basic properties of intensity values
discontinuity and similarity.
First category is to partition an image based on
abrupt changes in intensity, such as edges in
an image.
Second category are based on partitioning an
image into regions that are similar according to
a predefined criteria. Thresholding, Histogram
Thresholding approach falls under this
category.
Thresholding
Thresholding is usually the first
step in any segmentation approach
We have talked about simple
single value thresholding already
Single value thresholding can be
1 if f ( x, y ) asT follows:
given mathematically
g ( x, y )
0 if f ( x, y ) T
Thresholding
3 5 7 3 4 2 1
2 4 9 10 22 9 3
3 5 12 11 15 10 3
5 6 11 9 17 19 1
2 3 11 12 18 16 2
3 6 8 10 18 9 5
4 6 7 8 3 3 1
Thresholding
3 5 7 3 4 2 1
2 4 9 10 22 9 3
3 5 12 11 15 10 3
5 6 11 9 17 19 1
2 3 11 12 18 16 2
3 6 8 10 18 9 5
4 6 7 8 3 3 1
Thresholding
3 5 7 3 4 2 1
2 4 9 10 22 9 3
3 5 12 11 15 10 3
5 6 11 9 17 19 1
2 3 11 12 18 16 2
3 6 8 10 18 9 5
4 6 7 8 3 3 1
Thresholding
3 5 7 3 4 2 1
2 4 9 10 22 9 3
3 5 12 11 15 10 3
5 6 11 9 17 19 1
2 3 11 12 18 16 2
3 6 8 10 18 9 5
4 6 7 8 3 3 1
Example
A 4x4 grayscale image with the following pixel intensities:
Example
Step 1: Calculate the Average Intensity
•Sum of all pixel intensities: 1045
•Total number of pixels: 16
•Average intensity (T): 1045 / 16 ≈ 65.31
Step 2: Initial Thresholding
•Set the initial threshold to 65.
•Compare each pixel value to the threshold:
• If the pixel value is greater than 65,
assign it to the foreground (e.g., 1).
• If the pixel value is less than or equal
to 65, assign it to the background (e.g.,
0).
Example
Step 3: Update Threshold
•Calculate the average intensity of the
foreground and background pixels:
• Average intensity of foreground pixels:
(104 + 113 + 126) / 3 ≈ 111
• Average intensity of background
pixels: (52 + 55 + ... + 64) / 13 ≈ 58.54
•New threshold: (111 + 58.54) / 2 ≈ 84.77
Second Iteration:
•Reclassify pixels based on the new threshold of 84.77.
Note: This is a simplified example. In practice, iterative thresholding can be
continued until the threshold converges or a maximum number of iterations is
reached. Additionally, more sophisticated techniques like Otsu's method can
be used to automatically determine the optimal threshold value.
Thresholding Example
Imagine a poker playing robot that
needs to visually interpret the
cards in its hand
Original Image Thresholded Image
But Be Careful
Ifyou get the threshold wrong the
results can be disastrous
Threshold Too Low Threshold Too High
Basic Global Thresholding
Based on the histogram of an image
Partition the image histogram using a
single global threshold
The success of this technique very
strongly depends on how well the
histogram can be partitioned
Basic Global Thresholding Algorithm
The basic global threshold, T, is calculated
as follows:
1. Select an initial estimate for T (typically the
average grey level in the image)
2. Segment the image using T to produce two
groups of pixels: G1 consisting of pixels with
grey levels >T and G2 consisting pixels with
grey levels ≤ T
3. Compute the average grey levels of pixels in
G1 to give μ1 and G2 to give μ2
Basic Global Thresholding Algorithm
4. Compute a new threshold value:
1 2
T
2
5. Repeat steps 2 – 4 until the difference in
T in successive iterations is less than a
predefined limit T∞
This algorithm works very well for
finding thresholds when the histogram
is suitable
Thresholding Example 1
Thresholding Example 2
Problems With Single Value
Thresholding
Single value thresholding only
works for bimodal histograms
Images with other kinds of
histograms need more than a
single threshold
Problems With Single Value
• Let’s say weThresholding
want to (cont…)
isolate the contents
of the bottles
• Think about what the
histogram for this
image would look like
• What would happen if we used a single
threshold value?
Single Value Thresholding and
Illumination
Uneven illumination can really upset a
single valued thresholding scheme
Basic Adaptive Thresholding
An approach to handling situations in
which single value thresholding will
not work is to divide an image into
sub images and threshold these
individually
Since the threshold for each pixel
depends on its location within an
image this technique is said to
adaptive
Basic Adaptive Thresholding Example
Theimage below shows an example of
using adaptive thresholding with the
image shown previously
As can be seen success is mixed
But, we can further subdivide the
troublesome sub images for more
success
Basic Adaptive Thresholding Example
(cont…)
These images show the
troublesome parts of the
previous problem further
subdivided
After this sub division
successful thresholding
can be
achieved
Segmentation Based on Clustering