validation.m
2.95 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
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
112
clear all;
disp('***************** NEW RUN *********************');
X = 128;
Y = 128;
Z = 128;
D = 10;
t0=1;
r1=10;
r2=10;
t=2100;
itr=8;
vote=10;
std = [5 5];
gt_filename = '0-gt.vol';
% out_filename = sprintf('128-128-128/0-nissl-std%d.%d-t0%d-r%d.%d-t%d-out%d.%d.vol',std(1), std(2),t0,r1,r2,t,itr,vote);
out_filename = sprintf('D:/build/ivote3-bld/shared2D-v8/out%d.vol',t);
% txt_filename = sprintf('128-128-128/0-validation-nissl-std%d.%d-r%d.%d-t%d-out%d.%d-D%d.txt',std(1), std(2),r1,r2,t,itr,vote,D);
txt_filename = sprintf('D:/build/ivote3-bld/shared2D-v8/t%d-D%d.txt',t,D);
spec = sprintf('Nissl-std%d.%d-r%d.%d-t%d-out%d.%d',std(1), std(2),r1,r2,t,itr,vote);
fid0 = fopen(gt_filename);
gt = fread(fid0,[X Y*Z], 'single');
fclose(fid0);
gt = reshape(gt, [X Y Z]);
[gtx, gty, gtz] = ind2sub(size(gt),find(gt));
ref = [gtx gty gtz];
fid1 = fopen(out_filename);
out = fread(fid1,[X Y*Z], 'single');
fclose(fid1);
out = reshape(out, [X Y Z]);
[cx, cy, cz] = ind2sub(size(out),find(out));
cent = [cx cy cz];
% 1-find the nearest detected cell for each element in the ground truth.
[idx, dist] = knnsearch(cent,ref);
% 2- find the same detected cells for different elements in the ground truth and assign that to which gt's element with the lowest distance.
ind_eq_idx = zeros(numel(idx), 10);
t=0;
for i=1:numel(idx)
s_ind = sum(ind_eq_idx==i);
if s_ind==0
s = sum(idx==idx(i));
if s>1
t = t+1;
id_ref(t,1) = i;
id_ref(t,2) = s;
ind1 = find(idx==idx(i));
ind_eq_idx(i, 1:numel(ind1)) = ind1;
end
end
end
b = idx;
u_eq_idx = unique(ind_eq_idx(ind_eq_idx>0));
b(u_eq_idx)=0;
oseg=0;
for k=1:size(id_ref,1)
k1 = id_ref(k,1);
k2 = id_ref(k,2);
l = ind_eq_idx(k1,1:k2);
[~, local_id_min] = min(dist(l));
b(l(local_id_min)) = idx(k1);
u_eq_idx (u_eq_idx == l(local_id_min))=0;
ly = l (l~=l(local_id_min));
distl = dist(ly);
sl = sum(distl<D);
if sl>0
oseg = oseg+1;
end
end
%
u_idx1 = u_eq_idx (u_eq_idx>0);
ref1 = ref(u_idx1,:);
ub = unique(b(b>0));
mm=0;
for jj=1:size(cent,1)
z = sum(ub(:)==jj);
if z==0
mm = mm+1;
cent_id1(mm) = jj;
end
end
cent1 = cent(cent_id1,:);
[idx1, dist1] = knnsearch(cent1,ref1);
min1= min(dist1(:));
if min1 < D
miss1 = sum(dist1(:)<D);
end
%
b_ind = find(b==0);
b_dist = dist;
b_dist(b_ind)=-1;
TP = sum(b_dist>=0) - sum(b_dist>=D);
FN = size(ref, 1) - TP;
FP = size(cent,1) - TP;
Precision = TP/(TP + FP);
Recall = TP/(TP+FN);
Accuracy= TP/(TP+FP+FN);
fid_text = fopen(txt_filename, 'w');
fprintf(fid_text, '********--------\tValidation Results for %s\t--------********\n\n',spec);
fprintf(fid_text, 'number of detected cells by GROUND_TRUTH = %d\n', size(ref,1));
fprintf(fid_text, 'number of detected cells by IVOTE3 = %d\n', size(cent,1));
fprintf(fid_text, 'number of OVER-SEGMENTED cells = %d\n', oseg);
fprintf(fid_text, 'TP = %f,\t\tFP = %f,\t\tFN = %f\n', TP,FP,FN);
fprintf(fid_text, 'Precision = %f\n', Precision);
fprintf(fid_text, 'Accuracy = %f\n', Accuracy);
fprintf(fid_text, 'Recall = %f\n', Recall);
fclose(fid_text);