-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathegs.m
177 lines (177 loc) · 6.77 KB
/
egs.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This code is an unoptimized version with square complexity. But it is still quite fast. We will release the optimized version with linear complexity once we have done the preparation.
% Author: Shijie Lin
% Email: [email protected]
% License: MIT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clc;clear;close all;
format longG
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
timescale = 1e6;
image_height = 260;
image_width = 346;
start_path = "..\EAD"; % your path to the EAD
spliter = ";"; % for win
% spliter = ":" % for linux
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gt_path = fullfile(start_path, "gt_focus_points.txt");
fileID_score = fopen('result_egs_detail.txt','w'); % resutlt txt
allSubFolders = genpath(start_path);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, spliter);
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames);
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
% Get MAT files.
filePattern = sprintf('%s/*.mat', thisFolder);
baseFileNames = dir(filePattern);
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
thisFolder_sep=regexp(thisFolder,filesep,'split');
sequence_name = thisFolder_sep{end};
% Go through all those mat files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
load(fullFileName);
% load the focus position
focus_position_file = fullfile(thisFolder, "focus_position.txt");
fileID = fopen(focus_position_file);
focus_position_ts_list = textscan(fileID, '%f %f');
fclose(fileID);
focus_position_list = focus_position_ts_list{2};
focus_timestamp_list = focus_position_ts_list{1}./ timescale;
% load event data
y_o = double(aedat.data.polarity.y);
x_o = double(aedat.data.polarity.x);
pol_o = double(aedat.data.polarity.polarity);
focus_o = double(aedat.data.polarity.focusPosition);
pol_o(pol_o==0) = -1;
t_o = double(aedat.data.polarity.timeStamp) ./ timescale;
t_o = t_o - t_o(1);
% load Groundtruth
gt_focus_file = gt_path;
gt_focus_fileID = fopen(gt_focus_file);
gt_focus = textscan(gt_focus_fileID, '%s %f');
gt_focus_seq_name_list = gt_focus{1};
find_return = strfind(gt_focus_seq_name_list, sequence_name);
index_gt_foc = 0;
for i=1:length(find_return)
if find_return{i} == 1
index_gt_foc = i;
end
end
gt_focus_pos_list = gt_focus{2};
gt_focus_pos = gt_focus_pos_list(index_gt_foc);
fclose(gt_focus_fileID);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
optimal_focus_time = realmax;
optimal_focus_pos = realmax;
prev_optimal_focus_time = 0;
uptaded_focus_moving_step = realmax;
stopping_threshold = 0.001;
eventstart_time = t_o(1);
eventend_time = t_o(end);
golden_search_range = t_o(end) - t_o(1);
while golden_search_range > stopping_threshold
% -------------------------------------------- %
% load data in a smaller subset
% -------------------------------------------- %
x = x_o; y = y_o; pol = pol_o; t = t_o; focus = focus_o;
idx = (t>=eventstart_time)&(t<=eventend_time);
y(idx~=1)=[];
x(idx~=1)=[];
pol(idx~=1)=[];
t(idx~=1)=[];
focus(idx~=1)=[];
% -------------------------------------------- %
% computer ER to determine next best interval
% -------------------------------------------- %
event_rate = 0;
event_rate_sec = 0;
event_rate_list = [];
event_rate_ts_list = [];
time_range = t(end) - t(1);
golden_search_range = time_range;
delta_time = time_range * 0.618;
event_size = length(t);
fst_start_time = t(1);
second_start_time = t(1) + time_range * 0.381;
CAPTURE = true;
CAPTURE_SEC = true;
EV_FLAG = true;
EV_FLAG_SEC = true;
corres_focus_pos_list = [];
for i = 1:event_size
ev_t = t(i);
ev_x = x(i) + 1;
ev_y = y(i) + 1;
ev_p = pol(i);
ev_focus = focus(i);
% -------------------------------------------- %
% the first interval
% -------------------------------------------- %
if ev_t > fst_start_time && ev_t <= (fst_start_time + delta_time) && EV_FLAG
event_rate = event_rate + 1;
elseif ev_t > fst_start_time + delta_time && EV_FLAG
event_rate = event_rate / delta_time;
event_rate_list = [event_rate_list, event_rate];
event_rate_ts_list = [event_rate_ts_list, ev_t];
event_rate = 0;
fst_start_time = ev_t;
EV_FLAG = false;
end
if ev_t >= (fst_start_time + delta_time/2) && CAPTURE
CAPTURE = false;
corres_focus_pos_list(end+1) = ev_focus;
end
% -------------------------------------------- %
% the second interval
% -------------------------------------------- %
if ev_t > second_start_time && ev_t <= (second_start_time + delta_time) && EV_FLAG_SEC
event_rate_sec = event_rate_sec + 1;
elseif ev_t > second_start_time + delta_time && EV_FLAG_SEC
event_rate_sec = event_rate_sec / delta_time;
event_rate_list = [event_rate_list, event_rate_sec];
event_rate_ts_list = [event_rate_ts_list, ev_t];
event_rate_sec = 0;
second_start_time = ev_t;
EV_FLAG_SEC = false;
end
if ev_t >= (second_start_time + delta_time/2) && CAPTURE_SEC
CAPTURE_SEC = false;
corres_focus_pos_list(end+1) = ev_focus;
end
end
% find the optimal focus point
[max_ev_rate, max_ev_rate_index] = max(event_rate_list);
optimal_focus_time = event_rate_ts_list(max_ev_rate_index);
optimal_focus_pos = corres_focus_pos_list(max_ev_rate_index);
uptaded_focus_moving_step = abs(optimal_focus_time - prev_optimal_focus_time);
prev_optimal_focus_time = optimal_focus_time;
% give next best event interval
eventstart_time = optimal_focus_time - delta_time;
eventend_time = optimal_focus_time;
end
error_cal = (optimal_focus_pos - gt_focus_pos);
% print the error and step size
fprintf(' Processing mat file %s\n', fullFileName);
disp(["optimal_f_pos:", optimal_focus_pos, "steps:", uptaded_focus_moving_step , "e_cal:", error_cal])
% record results in the text file
fprintf(fileID_score,'%s & %.1f\n',sequence_name, error_cal);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
clearvars aedat
end
end
end
fclose(fileID_score);