-
Notifications
You must be signed in to change notification settings - Fork 2
/
Hyper_Spectral
78 lines (68 loc) · 2.01 KB
/
Hyper_Spectral
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
function index = MyHyperMartinIndex9(A,B,mask)
index = min(MyMartinIndex9Helper(A,B,mask), MyMartinIndex9Helper(B,A,mask));
end
%% Helper method to calculate Martin Index %%
function index = MyMartinIndex9Helper(A, B, mask)
[PixelR,PixelC] = size(A);
M = max(max(A));
N = max(max(B));
A = A.*mask;
%% Initializing matrices %%
% CountA and CountB indicate matrix |A| and |B|
CountA = zeros(1,M);
CountB = zeros(1,N);
Weights2D = zeros(M, N); % Indicates Wji
Weight = zeros(1,M); % Indicates Wj
Intersection = zeros(M,N);
Union = zeros(M,N);
%% Populating Intersection matrix %%
for i=1:PixelR
for j=1:PixelC
Intersection(A(i,j),B(i,j))= Intersection(A(i,j),B(i,j)) +1;
end
end
%% Populating Union matrix %%
for i = 1:PixelR
for j = 1:PixelC
if Intersection(A(i,j),B(i,j)) ~= 0 && Union(A(i,j), B(i,j)) == 0
Union(A(i,j),B(i,j)) = sum(A(:)==A(i,j)) + sum(B(:)==B(i,j)) - Intersection(A(i,j),B(i,j));
end
end
end
%% Populating CountA and CountB matrices %%
for i=1:M
CountA(i) = sum(A(:)==i);
end
for i=1:N
CountB(i) = sum(B(:)==i);
end
%% Calculating Wj %%
for i=1:M
Weight(i) = CountA(i)/sum(CountA);
end
%% Calculating Wji %%
for i=1:M
for j=1:N
if Intersection(i,j)~=0
denom = 0;
for k=1:N
if(Intersection(i,k) ~= 0)
denom = denom + CountB(k);
end
end
Weights2D(i,j) = CountB(j)/denom;
end
end
end
%% Calculating index %%
index = 0;
for i=1:M
innerRes = 0;
for j=1:N
if Intersection(i,j) ~= 0
innerRes = innerRes + (Weights2D(i,j) * Intersection(i,j) / Union(i,j));
end
end
index = index + (1-innerRes) * Weight(i);
end
end