You are on page 1of 19

HCMC University of Technology and Education

Faculty of Electrical & Electronic Engineering

Lecture:
IMAGE PROCESSING
Chapter 6:
Image Segmentation

1
Assoc. Prof. Nguyen Thanh Hai, PhD
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Segmentation of an image to produce regions


with the same characteristics based on brightness,
a group or type, regions
- Convert from color image to gray image
- Find threshold for segmentation (Histogram, global
and Otsu)
- Convert to binary image using threshold
- Segment region by multiplying between binary and
gray image

2
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Example of Image
segmentation

3
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation
- Figure has three regions (0-
100), (100-150), (150-210)
- (100-150) can be threshold
region
- (0-100) can be background
region
- (150-210) can be object
region
Segmentation based on brightness

4
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Segmentation based on brightness


Gray (Brightness) threshold, T based on Histogram of
the input image f(x,y) and the output image g(x, y) is
defined as follows:

This formula produces binary image


 a f ( x, y )  T
g ( x, y )  
b f ( x, y )  T

 a f ( x, y )  T1

g ( x, y )  b T2  f ( x, y )  T1
c f ( x, y )  T
Assoc. Prof. Nguyen Thanh Hai
 2
5
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation
Segmentation based on brightness
close all;clear all;
%%% Segmentation of Original image
Fingureprint image
I=
imread('SegFigureprint.jpg');
%I1=rgb2gray(I);
figure(1),imshow(I)
T=100;
g = im2bw(I,T/255 );
figure(2), imhist(I)
figure(3), imshow(J)

T=120 Histogram 6
Assoc. Prof. Nguyen Thanh Hai
T=100
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Segmentation Using Basic Global Thresholding


1. Select an initial estimate for the global threshold, T.
2. Segment the image using T to produce two groups of
pixels (G1 and G2), in which G1 consists of all pixels with
intensity values greater than T and G2 consists of pixels
with values less than or equal to T.
3. Compute the average intensity values m1 and m2 for the
pixels in regions G1 and G2 respectively.
4. Compute a new threshold value:
T=(1/2)(m1+m2)
5. Repeat steps 2 through 4 until the difference in T in
successive iterations is smaller than a predefined value, T
6. Segment the image based on T
7
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation
Segmentation Using Basic Global Thresholding
close all;clear all;
%%% Segmentation of Fingureprint image
f = imread('SegFigureprint.jpg');
count=0 ;
T=mean2(f) ;
done=false ;
while -done
count=count + 1
g = f > T ;
Tnext =0.5*(mean(f(g)) + mean(f(-g))) ;
done=abs(T-Tnext)<0.5 ;
T=Tnext
end
g = im2bw(f,T/255 ) ;
imshow(f) % .
figure , imhist(f)
figure , imshow(g)
8
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Segmentation Using Basic Global Thresholding

T = 116.5485

Count=2, dt=0.5 9
Assoc. Prof. Nguyen Thanh Hai T = 112.7171
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Segmentation Using Otsu Thresholding


Otsu thresholding

Otsu's thresholding method involves iterating through


all the possible threshold values and calculating a
measure of spread for the pixel levels each side of the
threshold, i.e. the pixels that either fall in foreground or
background.

The aim is to find the threshold value where the sum of


foreground and background spreads is at its minimum.

10
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

For example,
The algorithm will be demonstrated using the simple
6x6 image shown below. The histogram for the image
is shown next to it. To simplify the explanation, only 6
greyscale levels are used.

0 0 1 4 4 5
0 1 3 4 3 4
1 3 4 2 1 3
4 4 3 1 0 0
5 4 2 1 0 0
5 5 4 3 1 0

11
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

The calculations for finding the


foreground and background variances
(the measure of spread) for a single
threshold are now shown. In this case
the threshold value is 3.
n0  n1  n2 8  7  2 Weight W is the probability of the
b    0.4167
n 36 background class.

0  n0  1 n1  2  n2 0  8  1 7  2  2 the background class


b    0.6471
(n0  n1  n2 ) 17 mean

((0   b )  n0 )  ((1   b )  n1 )  ((2   b )  n2 ) the background class variance


Vb2 
(n0  n1  n2 )
((0  0.6471)  8)  ((1  0.6471)  7)  ((2  0.6471)  2)
  0.4637
17
12
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

* Calculate the 'Within-Class Variance'.


* This is simply the sum of the two variances
multiplied by their associated weights.
Weight W is the probability of the background class.
n3  n4  n5 6  9  4
f    0.5278
n 36
3  n4  4  n4  5  n5 3  6  4  9  5  4 the background class
f    3.8947
(n3  n4  n6 ) 19 mean

((3   f )  n3 )  ((4   f )  n4 )  ((5   f )  n5 )


V f2  the background class variance
(n0  n1  n2 )
((3  3.8947)  6)  ((4  3.8947)  9)  ((5  3.8947)  4)
  0.5152
19
VW2  b  Vb2   f  V f2  Sum of the two-classes variances
0.4722  0.4637  0.5278  0.5152  0.4909 13
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

14
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

It can be seen that for the threshold equal to 3, as well


as being used for the example, also has the lowest
sum of weighted variances.
Therefore, this is the final selected threshold. All pixels
with a level less than 3 are background, all those with
a level equal to or greater than 3 are foreground. As
the images in the table show, this threshold works well.

15
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation
Segmentation based on histogram threshold
close all;clear all;clc;

I = imread('SegFigureprint.jpg');
figure(1),imshow(I);
figure(2),imhist(I); % display the Histogram
%%=================================================
n=imhist(I); % Compute the histogram
N=sum(n); % sum the values of all the histogram values
max=0; %initialize maximum to zero
%%=====================================
for i=1:256
P(i)=n(i)/N; %Computing the probability of each intensity
level
end

16
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Segmentation using Otsu thresholding


for T=2:255 % step through all thresholds from 2 to 255
w0=sum(P(1:T)); % Probability of class 1 (separated by
threshold)
w1=sum(P(T+1:256)); %probability of class2 (separated by
threshold)
u0=dot([0:T-1],P(1:T))/w0; % class mean u0
u1=dot([T:255],P(T+1:256))/w1; % class mean u1
sigma=w0*w1*((u1-u0)^2); % compute sigma i.e variance
(between class)
if sigma>max % compare sigma with maximum
max=sigma; % update the value of max i.e max=sigma
threshold=T-1 % desired threshold corresponds to maximum
variance of between class
end
end

17
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

Segmentation Using Basic Global Thresholding

T = 112

18
Assoc. Prof. Nguyen Thanh Hai
HCMC University of Technology and Education
Faculty of Electrical & Electronic Engineering Image Segmentation

The End

19
Assoc. Prof. Nguyen Thanh Hai

You might also like