You are on page 1of 22

Đstanbul Teknik Üniversitesi

Elektrik Elektronik Fakültesi


Telekomünikasyon Mühendisliği
Bölümü

TEL 432 E

DIGITAL IMAGE

PROCESSING

-Homework 4-

Batuhan Osmanoglu
040010250
Segmentation Algorithms

Introduction

Five segmentation methods are employed on 3 images such as: face, hand-written
text and sky. Original gray level images are given below.

Face: Text: Sky:

Histograms of these images are;


The first segmentation method is Mean Gray Level. Then, we will continue with,
Two Peaks, Edge Pixels, Iterative Selection and Percentage of Black Pixels.

Mean Gray Level

Mean Gray Level Algorithm is simply applied by summing up all the pixel values
in the image and then taking the mean of it to obtain the threshold. MatLab code and
output images are given below.

Image Face Image Text Image Sky Image


Threshold Value 71 189 126

Outputs
MatLab Code

%TEL 432E HW 4
%MEAN GRAY LEVEL
%Segmentation

imface = imread('face.jpg');
imtext = imread('text.jpg');
imsky = imread('sky.jpg');

%FACE
%calculate mean grey level
[rmax cmax]=size(imface);
T=0;
T=sum(sum(imface));
T=T/(rmax*cmax)
imface(find(imface<T))=0;
imface(find(imface>=T))=1;

%Text
%calculate mean grey level
[rmax cmax]=size(imtext);
T=0;
T=sum(sum(imtext));
T=T/(rmax*cmax)
imtext(find(imtext<T))=0;
imtext(find(imtext>=T))=1;

%Sky
%calculate mean grey level
[rmax cmax]=size(imsky);
T=0;
T=sum(sum(imsky));
T=T/(rmax*cmax)
imsky(find(imsky<T))=0;
imsky(find(imsky>=T))=1;

figure(1);colormap('gray'); imagesc(imface);
figure(2);colormap('gray'); imagesc(imtext);
figure(3);colormap('gray'); imagesc(imsky);
Two Peaks

“Method of Two Peaks” is employed by finding two local maximum points in the
histogram and defining a threshold separating them.

Image Face Image Text Image Sky Image


Threshold Value 55 129 153

Outputs
MatLab Code

%TEL 432E HW 4
%Two Peaks
%Segmentation

imface = imread('face.jpg');
imtext = imread('text.jpg');
imsky = imread('sky.jpg');

%FACE
histog=hist(double(imface),256);
histogram=sum(histog');
figure(2);plot(histogram,'r');title('face-histogram');
locmax=find(histogram==max(histogram));
T=zeros(1,256);
for hi=1:256
T(hi)=histogram(hi)*(hi-locmax)^2;
end
locmaxx=find(T==max(T));
tresh=(locmax+locmaxx)/2
imface(find(imface<tresh))=0;
imface(find(imface>=tresh))=1;

%Text
histog=hist(double(imtext),256);
histogram=sum(histog');
figure(4);plot(histogram,'r');title('text-histogram');
locmax=find(histogram==max(histogram));
T=zeros(1,256);
for hi=1:256
T(hi)=histogram(hi)*(hi-locmax)^2;
end
locmaxx=find(T==max(T));
tresh=(locmax+locmaxx)/2
imtext(find(imtext<tresh))=0;
imtext(find(imtext>=tresh))=1;

%Sky
histog=hist(double(imsky),256);
histogram=sum(histog');
figure(6);plot(histogram,'r');title('sky-histogram');
locmax=find(histogram==max(histogram));
T=zeros(1,256);
for hi=1:256
T(hi)=histogram(hi)*(hi-locmax)^2;
end
locmaxx=find(T==max(T));
tresh=(locmax+locmaxx)/2
imsky(find(imsky<tresh))=0;
imsky(find(imsky>=tresh))=1;

figure(1);colormap('gray'); imagesc(imface);title('face
image');

figure(3);colormap('gray'); imagesc(imtext);title('text
image');

figure(5);colormap('gray'); imagesc(imsky);title('sky
image');

Edge Pixels

Laplacian is calculated for each pixel and then histogram of pixels with large
laplacians is created. Using this new histogram a threshold can be detected using any of
the previous methods.

Image Face Image Text Image Sky Image


Threshold Value 54 109 82

Outputs
MatLab Code

%TEL 432E HW 4
%EDGE PIXELS
%Segmentation

imface = imread('face.jpg');
imtext = imread('text.jpg');
imsky = imread('sky.jpg');

%FACE
%calculate laplacian
[rmax cmax]=size(imface);
L=zeros(size(imface));
for r=2:rmax-1
for c=2:cmax-1
L(r,c)=double(imface(r-
1,c))+double(imface(r+1,c))+double(imface(r,c-
1))+double(imface(r,c+1))-4*double(imface(r,c));
end
end
ef=rmax*cmax*0.85;%number of eightyfive percent of pixels
LL=zeros(size(L));
%new image with only eighty five percent and above
ki=0;
while ki<ef
loc=find(L==max(max(L)));
LL(loc)=imface(loc);
L(loc)=0; %do not take this pixel into account again
ki=ki+max(size(loc));
end

%use LL to find Treshold


T=sum(sum(LL));
T=T/(ef)
imface(find(imface<T))=0;
imface(find(imface>=T))=1;

%Text
%calculate laplacian
[rmax cmax]=size(imtext);
L=zeros(size(imtext));
for r=2:rmax-1
for c=2:cmax-1
L(r,c)=double(imtext(r-
1,c))+double(imtext(r+1,c))+double(imtext(r,c-
1))+double(imtext(r,c+1))-4*double(imtext(r,c));
end
end
ef=rmax*cmax*0.85;%number of eightyfive percent of pixels
LL=zeros(size(L));
%new image with only eighty five percent and above
ki=0;
while ki<ef
loc=find(L==max(max(L)));
LL(loc)=imtext(loc);
L(loc)=0; %do not take this pixel into account again
ki=ki+max(size(loc));
end

%use LL to find Treshold


T=sum(sum(LL));
T=T/(ef)
imtext(find(imtext<T))=0;
imtext(find(imtext>=T))=1;

%SKY
%calculate laplacian
[rmax cmax]=size(imsky);
L=zeros(size(imsky));
for r=2:rmax-1
for c=2:cmax-1
L(r,c)=double(imsky(r-
1,c))+double(imsky(r+1,c))+double(imsky(r,c-
1))+double(imsky(r,c+1))-4*double(imsky(r,c));
end
end
ef=rmax*cmax*0.85;%number of eightyfive percent of pixels
LL=zeros(size(L));
%new image with only eighty five percent and above
ki=0;
while ki<ef
loc=find(L==max(max(L)));
LL(loc)=imsky(loc);
L(loc)=0; %do not take this pixel into account again
ki=ki+max(size(loc));
end

%use LL to find Treshold


T=sum(sum(LL));
T=T/(ef)
imsky(find(imsky<T))=0;
imsky(find(imsky>=T))=1;

figure(1);colormap('gray'); imagesc(imface);
figure(2);colormap('gray'); imagesc(imtext);
figure(3);colormap('gray'); imagesc(imsky);

Iterative Threshold Method

In this method a threshold is iteratively calculated and refined by consecutive


passes through the image.

Image Face Image Text Image Sky Image


Threshold Value 76 152 135

Outputs
MatLab Code

%TEL 432E HW 4
%Iterative Selection
%Segmentation

clear all; clc;

imface = imread('face.jpg');
imtext = imread('text.jpg');
imsky = imread('sky.jpg');

tresholds=zeros(1,3);
%FACE
%calculate mean grey level
[rmax cmax]=size(imface);
T=0;
T=sum(sum(imface));
T=T/(rmax*cmax)
Tnew =-1;
while round(T)~=round(Tnew) %not equal
if Tnew~=-1
T=Tnew;
end
Tlow=
sum(sum(imface(find(imface<T))))/max(size(find(imface<T)))
Thigh=
sum(sum(imface(find(imface>=T))))/max(size(find(imface>=T)))
Tnew=(Tlow+Thigh)/2
end

imface(find(imface<T))=0;
imface(find(imface>=T))=1;
figure(1);colormap('gray'); imagesc(imface);
tresholds(1,1)=T;
%Text
%calculate mean grey level
[rmax cmax]=size(imtext);
T=0;
T=sum(sum(imtext));
T=T/(rmax*cmax)
Tnew =-1;
while round(T)~=round(Tnew) %not equal
if Tnew~=-1
T=Tnew;
end
Tlow=
sum(sum(imtext(find(imtext<T))))/max(size(find(imtext<T)))
Thigh=
sum(sum(imtext(find(imtext>=T))))/max(size(find(imtext>=T)))
Tnew=(Tlow+Thigh)/2
end

imtext(find(imtext<T))=0;
imtext(find(imtext>=T))=1;

figure(2);colormap('gray'); imagesc(imtext);
tresholds(1,2)=T;
%Sky
%calculate mean grey level
[rmax cmax]=size(imsky);
T=0;
T=sum(sum(imsky));
T=T/(rmax*cmax)
Tnew =-1;
while round(T)~=round(Tnew) %not equal
if Tnew~=-1
T=Tnew;
end
Tlow=
sum(sum(imsky(find(imsky<T))))/max(size(find(imsky<T)))
Thigh=
sum(sum(imsky(find(imsky>=T))))/max(size(find(imsky>=T)))
Tnew=(Tlow+Thigh)/2
end

imsky(find(imsky<T))=0;
imsky(find(imsky>=T))=1;

figure(3);colormap('gray'); imagesc(imsky);
tresholds(1,3)=T;
thresholds

Percentage of Black Pixels

Assuming that percentage of black pixels is a constant for some types of images,
lower pixel values up to the number of assumed pixels are segmented as background or
black.

Image Face Image Text Image Sky Image


Threshold 94 178 111
Percentage of 60 20 50
Black Pixels
Outputs
MatLab Code

%TEL 432E HW 4
%Percentage of Black Pixels
%Segmentation

clear all; clc;

imface = imread('face.jpg');
imtext = imread('text.jpg');
imsky = imread('sky.jpg');
tresholds=zeros(2,3);
%FACE
%Assumption of black pixel percentage
p=60;%percentage of Black pixels
[rmax cmax]=size(imface);
pxn=rmax*cmax; %number of pixels
T=0;
while max(size(find(imface==0))) < p*pxn/100
T=T+1;
imface(find(imface==T))=0;
end
imface(find(imface>T))=1;
tresholds(1,1)=T;
tresholds(2,1)=p;
figure(1);colormap('gray'); imagesc(imface);

%Text
%Assumption of black pixel percentage
p=20;%percentage of Black pixels
[rmax cmax]=size(imtext);
pxn=rmax*cmax; %number of pixels
T=0;
while max(size(find(imtext==0))) < p*pxn/100
T=T+1;
imtext(find(imtext==T))=0;
end
imtext(find(imtext>T))=1;
tresholds(1,2)=T;
tresholds(2,2)=p;

figure(2);colormap('gray'); imagesc(imtext);

%Sky
%Assumption of black pixel percentage
p=50;%percentage of Black pixels
[rmax cmax]=size(imsky);
pxn=rmax*cmax; %number of pixels
T=0;
while max(size(find(imsky==0))) < p*pxn/100
T=T+1;
imsky(find(imsky==T))=0;
end
imsky(find(imsky>T))=1;
tresholds(1,3)=T;
tresholds(2,3)=p;

figure(3);colormap('gray'); imagesc(imsky);
thresholds
Gray Level Histogram

Gray Level Histogram method is based on minimizing the ratio of between classes
variance to total variance of gray level pixels.

Total gray level variance is the variance of two separate Gaussian functions,
representing the background and object pixels. In case there are two pixel groups with
Gaussian distribution, it is not a great deal to calculate the overall variance of the whole
image, denoted by σt2.

Moreover, it is possible to calculate the variance of the object and the background
pixels separately, for any predefined threshold. This is denoted by σw2 and called within
class variance. This term, within class variance is a measure of standard deviation of
background and object pixels. As a matter of fact, if it is possible to perfectly separate
object and background pixels than we will have two separate Gaussian functions with two
exact within class variance. Even though it is an important measure in ANOVA (Analysis
of Variance), we are not going to deal with this term in Gray Level Histogram Method.

Variations of the mean values for each class from the mean of whole image are
named as between classes’ variance. This is a value that we want to minimize. The name
implies that it is the between classes’ variance. We would like to have no relation
between two classes to be able to make a perfect distinction. Therefore, we are going to
pick up the best threshold value, which minimizes the ratio of σb2/σt2 where σb2 represents
between classes’ variance.

Overall variance σt2 is going to be calculated from the image, using histogram
values. On the other hand, it is not that easy to calculate between classes’ variance. An
expression to calculate σb2 is;

σ b2 = ω 0ω i ( µ 0 µ i ) 2

In this formula µ0 and µi represents the mean values of overall and pixels up to
gray level i. Other terms, w0 and wi are calculated using the expression below:

t
ω 0 = ∑ pi ⇔ ω i = 1 − ω 0
i =0
And;

µi µ − µt t
µ0 = ⇔ µi = T ⇔ µ t = ∑ i × pi
ω0 1 − ω0 i =0

Where pi is the probability of gray level i and µt is the mean of the whole image.

Given these expressions one can calculate each σb2/ σt2 ratio for any t. Value t that
gives the minimum result for that ratio is to be selected as the optimal threshold.
Sources:

1. Advanced Methods in Grey Level Segmentation.


2. Gray Levels and Histogram , December 2004, Ruye Wang
http://fourier.eng.hmc.edu/e161/lectures/digital_image/node9.html
3. Analysis of Variance between groups, December 2004,
http://www.physics.csbsju.edu/stats/anova.html