[c28c68]: / Code / ostu.m

Download this file

44 lines (37 with data), 1.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
function ostu_img=ostu(B);
%Image contains 2 classes of pixels, foreground and background pixels.
%Calculate optimal threshold such that inter-class variance is maximum
B = imresize(B, [256, 256]);
[r,c] = size(B);
% Calculation of the normalized histogram
n = 256;
h = hist(B(:), n);
h = h/(length(B(:))+1);
% Calculation of the cumulated histogram and the mean values
cumSum = cumsum(h);
mu = zeros(n, 1);
mu(1) = h(1);
for i=2:n
mu(i) = mu(i-1) + i*h(i);
end
% Initialisation of the values used for the threshold calculation
w0 = cumSum(1);
w1 = 1-w0;
mu0 = mu(1)/w0;
mu1 = (mu(end)-mu(1))/w1;
max = w0*w1*(mu1-mu0)*(mu1-mu0);
lev = 1;
for i = 2:n
w0 = cumSum(i);
w1 = 1-w0;
mu0 = mu(i)/w0;
mu1 = (mu(end)-mu(i))/w1;
s = w0*w1*(mu1-mu0)*(mu1-mu0);
if (s > max)
max = s;
lev = i;
end
end
% Normalisation of the threshold
lev = lev/n;
ostu_img = imbinarize(uint8(B),lev);