-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcDBLThreshold.m
80 lines (69 loc) · 1.8 KB
/
calcDBLThreshold.m
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
function [ Th1, Th2, hb, hbw, hw ] = calcDBLThreshold( ipImg )
% This function calculates 2 threshold values of the input gray scale image
% using the Shannon based entropy method
% It returns 2 output threshold values
max = 0.0;
Th1 = 0;
Th2 = 0;
img_hst = imhist(ipImg);
pb = 0.0;
pbw = 0.0;
pw = 0.0;
nTot = numel(ipImg);
tmp = 0.0 ;
for i = 1 : numel(img_hst)
for j = 1 : numel(img_hst)
pb = 0.0;
pbw = 0.0;
pw = 0.0;
for k = 1 : i
pb = pb + (img_hst(k)/ nTot);
end
for k = i + 1 : j
pbw = pbw + (img_hst(k)/ nTot);
end
for k = j + 1 : numel(img_hst)
pw = pw + (img_hst(k)/ nTot);
end
if(pb == 0 | pw == 0 | pbw == 0)
continue;
end
%hs = 0.0;
hbTmp = 0.0;
hbwTmp = 0.0;
hwTmp = 0.0;
for k = 1 : i
tmp = (img_hst(k) / nTot) / pb;
if(tmp == 0)
continue;
end
hbTmp = hbTmp - tmp * log(tmp);
end
for k = i+1 : j
tmp = (img_hst(k) / nTot) / pbw;
if(tmp == 0)
continue;
end
hbwTmp = hbwTmp - tmp * log(tmp);
end
for k = j+1 : numel(img_hst)
tmp = (img_hst(k) / nTot) / pw;
if(tmp == 0)
continue;
end
hwTmp = hwTmp - tmp * log(tmp);
end
hs = hbTmp + hbwTmp + hwTmp;
if hs > max
max = hs;
Th1 = i - 1;
Th2 = j - 1;
hb = hbTmp;
hbw = hbwTmp;
hw = hwTmp;
end
end
end
%disp('Threshold values are ');
%disp([Th1, Th2]);
end