UXDE dot Net
Advertisement

Optimum Global Thresholding Using Otsu’s Method Matlab Source Code

By -

 

Steps

  1. Compute the normalized histogram of the input image. Denote the components of the histogram by pi, i=0, 1, …, L-1.
  2. Compute the cumulative sums, P1(k), for k = 0,1, …, L-1.
  3. Compute the cumulative means, m(k), for k = 0,1, …, L-1.
  4. Compute the global intensity mean, mG.
  5. Compute the between-class variance, for k = 0,1, …, L-1.
  6. Obtain the Otsu’s threshold, k*.

 

Equations For Each Step

 

Step 1

p(i)=n(i)/Size(m*n)

Step 2

k is a threshold value, C1[0, k], C2[k +1, L -1]

P1(k) = Summition [ p(i))  ]  Limits: [ i=0:k]

P2(k) = Summition [ p(i)) ] Limits: [ i=k+1:L-1 ] 

Step 3

m1(k) =1/p1(k)  * Summition [ i * p(i) ] Limits[i=0:k]

m2(k) =1/p2(k)  * Summition [ i * p(i) ] Limits[i=L-1:k+1]

Step 4

Pm + Pm = mG(Global mean value)

Step 5

( Partial )^2 = P1(m1-mG) +P2 (m2-mG)^2

Step 6 

k* = max (  (partial)^2(k) ) Limits [k =0:L-1 ]

a=imread(‘cameraman.tif‘);
[r c]=size(a);
L = 256;
k = 80;

p=zeros(1,L);

%—–Step 1———-Computing Histogram-————————–
for level=1:1:L
for i=1:1:r
for j=1:1:c
if(a(i,j)==level)
p(level)=p(level)+1;
end
end
p(level) = p(level);
end
end
p1 = zeros(1,L);
p2 = zeros(1,L);
m1 = zeros(1,L);
m2 = zeros(1,L);
var = zeros(1,L);
temp=0;
temp2 = 0;
temp3 = 0;
temp4 = 0;
mg = 0;
%—–Step 2—————-p1 calculation-————————–
for thresh=1:k
for ite= 1:thresh
temp=temp+p(ite);
end
p1(thresh)=temp;
temp=0;
end

%—–Step 2————–p2 calculation—————————–
for thresh2 = k+1: L
for i = thresh2:L

temp2 = temp2 + p(i);
end
p2(thresh2)=temp2;
temp2=0;
end

%——-Step 3————-m1 calculation—————————-
for thresh3 =1:k
for i =1:thresh3
temp3 = temp3 + i* p(i);
end
m1(thresh3) = temp3/p1(thresh3);
end

%——-Step 3———-m2 calculation———————————–
for thresh4 =k+1:L
for i =thresh4:L
temp4 = temp4 + i* p(i);
end
m2(thresh4) = temp4/p2(thresh4);
end

%——–Step 4———Global mean value calculation ———————

mg = p1(k)*m1(k)+p2(k)*m2(k);

%———-Step 5—–Between Class Variance ——————————
for v =1:L
var(v) = ( p1(v)*(m1(v)-mg)^2 )+( p2(v)*(m2(v)-mg)^2 );
end

%—–Step 6——–finidng max var(k*)—————————–

[maxNum IndexofMaxNum] = max(var)
output=a;
for i=1:r
for j=1:c
if output(i,j) >IndexofMaxNum
output(i,j)=255;
else
output(i,j)=0;
end
end
end

subplot(1,3,1)
imshow(a);
title(‘Orignal Image’);
subplot(1,3,2)
stem(p);
title(‘Histogram‘);
subplot(1,3,3)
title(‘Output Image‘);
imshow(output);

 Input  + Output 

image segmentation

Have a Look at This I Also Calculated Manually 

 

Global Thresholding

Source Code

Like , share this post on Facebook , Twitter by using left moving Social Sharing block and follow us on Facebook Page , Twitter and do comment and enter your email. After you have done you will automatically get source code in your mail. Note : This will be auto generated mail you can’t get until you follow above method.

 

 

Farheen Bibi

You can find Farheen on , and .

7 Comments to Optimum Global Thresholding Using Otsu’s Method Matlab Source Code

    • Adnan Jilani

      Everything is well explained in the related post, you can implement easily, unfortunately source files are deleted

Leave a Reply