-
Notifications
You must be signed in to change notification settings - Fork 25
/
matlab_mod.m
149 lines (119 loc) · 4.94 KB
/
matlab_mod.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
% Hacking a simple filter for deep learning results from Zetao
close all;
clc;
clear;
% Matching parameters
flen = 5; % Filter length % Equivalent to DEE
dt = 3; % Vibration threshold % Equivalent to EPSILON
st = 0.75; % Slope matching tolerance plus minus of the desired slope of 1 or -1. %equivalent to SIGMA
% st is the parameter we sweep to generate the precision recall curve
diff_matrix = h5read('conf_mat_smush_full_overfeat_10.h5','/dataset');
precision_all = [];
recall_all = [];
d = diff_matrix(1:4789,4790:9575); % diff_matrix is the confusion matrix loaded from '10.mat'(see the command above). the whole eynsham datasets (including training/reference and testing data) contains 9575 frames, out of which the first 4789 frames are for training/reference, and 4790~9575 are
% for testing, the diff_matrix is a
% 9575* 9575 matrix (compare each image
% to any other image in the data).
% therefore, we cut
% diff_matrix(1:4789,4790:9575),
% considering only the confusion matrix
% constructed from matching testing
% images to training images.
[a b] = min(d); % the min score is the best match
inlier_fraction = 5/6; % Percentage of top matches used in the vibration calculation, allows the occasional outlier
p = zeros(1, length(b));
cy = zeros(1,length(b)-flen+round(flen/2));
% Go through all frames
for i = 1:length(b) - flen
% for i = 1:100
% for i = 3326:3326
% s = b(i);
maxdiff = 0;
% Index of match
ci = i + round(flen / 2);
% Analyze vibrations
vibrations = abs(diff(b(i:i+flen - 1)));
[sort_vib_val sort_vib_indices] = sort(vibrations);
maxdiff = max(sort_vib_val(1:round(inlier_fraction * flen)));
% maxdiff = max(abs(diff(b(i:i+flen - 1))));
% for j = 0:flen - 2
% maxdiff = max(abs(b(i + j + 1) - b(i + j)));
% end
% Linear regression
pt = polyfit(0:flen - 1, b(i:i + flen - 1), 1);
p(ci) = pt(1);
% cx(ci) = ci;
% cy(ci) = b(i) + p(ci) * 0.5 * flen;
if maxdiff <= dt && (abs(p(ci) - 1) < st || abs(p(ci) - -1) < st) %Forward and reverse matching
% if (maxdiff <= dt)
% if maxdiff <= dt & (abs(p(ci) - 1) < st) %Forward matching only
% q(ci) = b(i);
cy(ci) = pt(2) + pt(1) * 0.5 * flen;
% plot([cx(ci) - flen / 2 cx(ci) + flen / 2], [cy(ci) - p(ci) * flen / 2 cy(ci) + p(ci) * flen / 2], 'b-');
% check previous points and if they are not confident, add them
% for inner_idx = 1:round(flen/2)
% check_idx = ci-inner_idx;
% if(cy(check_idx) == 0) % not confident
% cy(check_idx) = b(check_idx);
% end
% end
%
% for inner_idx = 1:(flen - round(flen/2)-1)
% check_idx = ci+inner_idx;
% if(check_idx<=4783)
% cy(check_idx) = b(check_idx);
% end
% end
%
% end
else
if(cy(ci)==0)
cy(ci) = 0;
end
end
end
% check the ground truth.
load('GroundTruth_Eynsham_40meters');
start_first = 1;
end_first = 4789;
len_first = end_first-start_first+1;
start_second = 4790;
end_second = 9575;
len_second = end_second-start_second+1;
half_matrix = 4785;
ground_matrix = zeros(len_second,len_first); % each row represents the matching result of one image in the second traverse
for ground_idx = start_second:end_second
value_ground = ground_truth(ground_idx,:);
value_fit = find(value_ground == 1);
value_fit2 = value_fit(find(value_fit<end_first));% only store those in first round
value_fit3 = value_fit2 - start_first + 1; % '16' here is the consistent shift between the ground truth
value_fit4 = value_fit3(find(value_fit3>0));
matris_idx = ground_idx - start_second +1;
ground_matrix(matris_idx,value_fit4) = 1;
end
tp_num = 0;
tp_value = [];
fp_num = 0;
fp_value = [];
for truth_idx = 1:length(cy)
ground_row = ground_truth(truth_idx+end_first,:);
ground_row_idx = find(ground_row == 1);
if(cy(truth_idx)~=0) % means we consider it to be a confident match
truth_va = cy(truth_idx);
truth_va2 = round(truth_va);
if(any(ground_row_idx == round(truth_va2)))
tp_num = tp_num + 1;
tp_value = [tp_value;truth_idx];
else
fp_num = fp_num + 1;
fp_value = [fp_value;truth_idx];
truth_x = ones(1,length(ground_row_idx))*truth_idx;
% plot(truth_x,ground_row_idx,'ws','LineWidth',1);
end
else
end
end
precision = tp_num/(tp_num+fp_num)
recall = tp_num/length(b) % 0.8571 is currently the highest rate.
precision_all = [precision_all;precision];
recall_all = [recall_all;recall];