-
Notifications
You must be signed in to change notification settings - Fork 0
/
rltentrpthreshold2D.m
111 lines (92 loc) · 2.06 KB
/
rltentrpthreshold2D.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
function [KL,KJ,KG]=rltentrpthreshold2D(im)
% RLTENTRPTHRESHOLD2D thresholds gray-scale image using Local Relative Entropy,
% Joint Relative Entropy and Globle Relative Entropy methods
% input is image IM
% output are:
% KL : local relative entropy threshold
% KJ : joint relative entropy threshold
% KG : global relative entropy threshold
% Programmed By Yingzi (Eliza) Du on 03/08/2004
mn=min(min(im))+1;
mx=max(max(im))+1;
his2 = hist2D(im);
Nm=zeros(1,256);
k=0;
ml=100000;
mj=100000;
mg=-100000;
his=hist(im);
for i=mn:mx
if his(i)>0
k=k+1;
end;
Nm(i)=k;
end;
for i=mn:mx-1;
Hge=0;
Hle=0;
Hje=0;
Pa=sum(sum(his2(mn:i,mn:i)));
Pb=sum(sum(his2(mn:i,i+1:mx)));
Pc=sum(sum(his2(i+1:mx,i+1:mx)));
Pd=sum(sum(his2(i+1:mx,mn:i)));
qa=0;
qb=0;
qc=0;
qd=0;
%this part to make sure there is no 0 for the information calculation
%calculate the new local, joint and relative entgopy
Hr=0;
hisa=his2(mn:i,mn:i);
hisb=his2(mn:i,i:mx);
hisc=his2(i+1:mx,i+1:mx);
hisd=his2(i+1:mx,mn:i);
area1=Nm(i);
area2=Nm(mx)-area1;
if (Pa>0)
qa=Pa/area1/area1;
Hle=-Pa.*log(qa./(Pa+Pc));
hisa=(hisa==0)+hisa;
Hle=Hle+sum(sum(hisa.*log(hisa)));
Hge=Pa*log(qa);
end;
if (Pb>0)
qb=Pb/area2/area1;
Hje=-Pb*log(qb/(Pb+Pd));
hisb=(hisb==0)+hisb;
Hje=Hje+sum(sum(hisb.*log(hisb)));
Hge=Hge+Pb*log(qb);
end;
if (Pc>0 )
qc=Pc/area2/area2;
Hle=Hle-Pc*log(qc/(Pa+Pc));
hisc=(hisc==0)+hisc;
Hle=Hle+sum(sum(hisc.*log(hisc)));
Hge=Hge+Pc*log(qc);
end;
if (Pd>0)
qd=Pd/area1/area2;
Hje=Hje+Pd*log(qd/(Pb+Pd));
hisd=(hisd==0)+hisd;
Hje=Hje+sum(sum(hisd.*log(hisd)));
Hge=Hge+Pd*log(qd);
end;
if Pa+Pc>0
Hle=Hle./(Pa+Pc)-log(Pa+Pc);
if Hle<ml
ml=Hle;
KL=i-1;
end;
end;
if (Pb+Pd>0)
Hje=Hje./(Pb+Pd)-log(Pb+Pd);
if(Hje<mj )
mj=Hje;
KJ=i-1;
end;
end;
if Hge>mg
mg=Hge;
KG=i-1;
end;
end;