-
Notifications
You must be signed in to change notification settings - Fork 0
/
cnsslis9smallworldness2.m
101 lines (85 loc) · 3.53 KB
/
cnsslis9smallworldness2.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
% cnsslis9smallworldness computes small-world-ness of a graph generated
% from img (resized to 1/3) similar to a graph generated by cnsslis9 in its
% first phase
% Use: [S_ws,C_ws,L_ws,S_trans,C_trans,L_trans] = cnsslis9smallworldness(img, fw, k, disttype)
% Output:
% S_ws - Cws
% S_trans - transitivity C (no. of triangles)
% C and L are the mean clustering coefficient C and mean shortest path length L of A.
function [S,C,E,Crand_mean,Erand_mean] = cnsslis9smallworldness2(img, fw, k, disttype)
if (nargin < 4) || isempty(disttype)
disttype = 'euclidean'; % distância euclidiana não normalizada
end
if (nargin < 3) || isempty(k)
k = 10; % quantidade de vizinhos mais próximos
end
if (nargin < 2) || isempty(fw)
fw = ones(1,9);
%fw = [1 1 0.5 0.5 0.5 0.5 0.5 0.5 0.5];
end
% tratamento da entrada
k = uint16(k);
rs_img = imresize(img,1/3,'bicubic');
[qtnode,X] = getFeatures(rs_img,fw);
clear rs_img;
% já estamos normalizando de qualquer forma
if strcmp(disttype,'seuclidean')==1
disttype='euclidean';
end
% encontrando k-vizinhos mais próximos
KNN = knnsearch(X,X,'K',k+1,'NSMethod','kdtree','Distance',disttype);
KNN = uint32(KNN);
clear X;
KNN = KNN(:,2:end); % eliminando o elemento como vizinho de si mesmo
A = zeros(qtnode,qtnode,'single');
for i=1:qtnode
A(i,KNN(i,:))=1;
end
clear KNN;
% analysis parameters
%Num_ER_repeats = 100; % to estimate C and L numerically for E-R random graph
%Num_S_repeats = 1000; % to get P-value for S; min P = 0.001 for 1000 samples
Num_ER_repeats = 20;
FLAG_Cws = 1;
FLAG_Ctransitive = 2;
% get its basic properties
n = size(A,1); % number of nodes
%k = sum(A); % degree distribution of undirected network % that would be to undirect graphs (F.B.)
%m = sum(k)/2; % that would be to undirect graphs (F.B.)
m = n*k; % I've changed to sum(k) for directed graphs (F.B.)
%K = mean(k); % mean degree of network % I don't need this (F.B.)
[Erand,Crand] = fb_NullModel_L_C(n,m,Num_ER_repeats,FLAG_Ctransitive);
% Note: if using a different random graph null model, e.g. the
% configuration model, then use this form
% compute small-world-ness using mean value over Monte-Carlo realisations
% NB: some path lengths in L will be INF if the ER network was not fully
% connected: we disregard these here as the dolphin network is fully
% connected.
%Lrand_mean = mean(Lrand(Lrand < inf));
%[S_ws,C_ws,E_ws] = fb_small_world_ness2(A,mean(Erand),mean(Crand),FLAG_Cws); % Using WS clustering coefficient
Erand_mean = mean(Erand);
Crand_mean = mean(Crand);
[S,C,E] = fb_small_world_ness2(A,Erand_mean,Crand_mean,FLAG_Ctransitive); % Using transitive clustering coefficient
end
function [qtnode,X] = getFeatures(img,fw)
% Dimensões da imagem
dim = size(img);
qtnode = dim(1)*dim(2);
X = zeros(qtnode,9);
% primeiro e segundo elementos são linha e coluna normalizadas no intervalo 0:1
X(:,1:2) = [repmat(((1:dim(1))/dim(1))',dim(2),1), reshape(repmat((1:dim(1))/dim(1),dim(2),1),dim(1)*dim(2),1)];
% depois vem os 3 elementos RGB normalizados em 0:1
imgvec = double(squeeze(reshape(img,dim(1)*dim(2),1,3)))/255;
X(:,3:5) = imgvec;
% depois vem os 3 elementos HSV
imghsv = rgb2hsv(double(img)/255);
X(:,6) = squeeze(reshape(imghsv(:,:,3),dim(1)*dim(2),1,1));
% em seguida ExR, ExG, e ExB
exr = 2.*double(img(:,:,1)) - double(img(:,:,2)) - double(img(:,:,3));
exg = 2.*double(img(:,:,2)) - double(img(:,:,1)) - double(img(:,:,3));
exb = 2.*double(img(:,:,3)) - double(img(:,:,1)) - double(img(:,:,2));
imgex = cat(3, exr, exg, exb);
clear exr exg exb;
X(:,7:9) = squeeze(reshape(imgex,dim(1)*dim(2),1,3));
X = zscore(X) .* repmat(fw,qtnode,1);
end