-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTracking.cpp
370 lines (290 loc) · 11.6 KB
/
Tracking.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*********************************************************************
* Author : Himangshu Saikia
* Email : [email protected]
* Project : Merge Tree Library
*
*********************************************************************
*/
#include "Tracking.h"
#include "Histogram4d.h"
#include <fstream>
#include <sstream>
#include <string>
#include <queue>
namespace mtlib {
void get_overlap_1_with_cumul_2(
const int& a1,
const int& a2,
const std::vector<std::vector<int> >& overlap,
std::vector<std::vector<int> >& overlap_1_with_cumul_2,
const std::map <int, std::vector<int> >& itree2
)
{
if (overlap_1_with_cumul_2[a1][a2] != -1) {
return;
}
int ret = overlap[a1][a2];
if (itree2.find(a2) != itree2.end()) {
for (auto j = 0; j < itree2.find(a2)->second.size(); j++) {
int b2 = itree2.find(a2)->second[j];
get_overlap_1_with_cumul_2(a1, b2, overlap, overlap_1_with_cumul_2, itree2);
ret += overlap_1_with_cumul_2[a1][b2];
}
}
overlap_1_with_cumul_2[a1][a2] = ret;
}
void get_overlap_cumul_1_with_2(
const int& a1,
const int& a2,
const std::vector<std::vector<int> >& overlap,
std::vector<std::vector<int> >& overlap_cumul_1_with_2,
const std::map <int, std::vector<int> >& itree1
)
{
if (overlap_cumul_1_with_2[a1][a2] != -1) {
return;
}
int ret = overlap[a1][a2];
if (itree1.find(a1) != itree1.end()) {
for (auto i = 0; i < itree1.find(a1)->second.size(); i++) {
int b1 = itree1.find(a1)->second[i];
get_overlap_cumul_1_with_2(b1, a2, overlap, overlap_cumul_1_with_2, itree1);
ret += overlap_cumul_1_with_2[b1][a2];
}
}
overlap_cumul_1_with_2[a1][a2] = ret;
}
void getOverlap12(
const int& a1,
const int& a2,
const std::vector<std::vector<int> >& overlap,
std::vector<std::vector<int> >& overlap_1_with_cumul_2,
std::vector<std::vector<int> >& overlap_cumul_1_with_2,
std::vector<std::vector<int> >& cumulativeOverlap,
const std::map <int, std::vector<int> >& itree1,
const std::map <int, std::vector<int> >& itree2
) {
//std::cout << "getOverlap for " << a1 << "," << a2 << "\n";
if (cumulativeOverlap[a1][a2] != -1) {
return;
}
int ret = overlap[a1][a2];
if (itree1.find(a1) != itree1.end()) {
for (auto i = 0; i < itree1.find(a1)->second.size(); i++) {
int b1 = itree1.find(a1)->second[i];
get_overlap_cumul_1_with_2(b1, a2, overlap, overlap_cumul_1_with_2, itree1);
ret += overlap_cumul_1_with_2[b1][a2];
}
}
if (itree2.find(a2) != itree2.end()) {
for (auto j = 0; j < itree2.find(a2)->second.size(); j++) {
int b2 = itree2.find(a2)->second[j];
get_overlap_1_with_cumul_2(a1, b2, overlap, overlap_1_with_cumul_2, itree2);
ret += overlap_1_with_cumul_2[a1][b2];
}
}
if (itree1.find(a1) != itree1.end() && itree2.find(a2) != itree2.end()) {
for (auto i = 0; i < itree1.find(a1)->second.size(); i++) {
for (auto j = 0; j < itree2.find(a2)->second.size(); j++) {
int b1 = itree1.find(a1)->second[i];
int b2 = itree2.find(a2)->second[j];
getOverlap12(b1, b2, overlap, overlap_1_with_cumul_2, overlap_cumul_1_with_2, cumulativeOverlap, itree1, itree2);
ret += cumulativeOverlap[b1][b2];
}
}
}
cumulativeOverlap[a1][a2] = ret;
return;
}
void getCumulVox(
const int& root,
const std::vector<int>& numVoxelsForLS,
std::vector<int>& cumulNumVoxelsForLS,
const std::map <int, std::vector<int> >& itree
) {
//std::cout << "getCumulVox for " << root << "\n";
if (cumulNumVoxelsForLS[root] != -1) {
return;
}
int ret = numVoxelsForLS[root];
if (itree.find(root) != itree.end()) {
for (auto i = 0; i < itree.find(root)->second.size(); i++) {
int child = itree.find(root)->second[i];
getCumulVox(child, numVoxelsForLS, cumulNumVoxelsForLS, itree);
ret += cumulNumVoxelsForLS[child];
}
}
cumulNumVoxelsForLS[root] = ret;
return;
}
void Tracking::findOverlapRecursive(
const std::vector<FeatureIdx>& belongs1,
const std::vector<FeatureIdx>& belongs2,
const std::map <FeatureIdx, std::vector<FeatureIdx> >& invHierarchyTree1,
const std::map <FeatureIdx, std::vector<FeatureIdx> >& invHierarchyTree2,
const FeatureIdx& invTreeRoot1,
const FeatureIdx& invTreeRoot2,
TrackInfo& tri,
clock_t& time_taken
)
{
clock_t start = std::clock();
std::vector<std::vector<int> > overlap(tri.numFeatures1, std::vector<int>(tri.numFeatures2, 0));
std::vector<int> numVoxelsForLS1(tri.numFeatures1, 0); // number of voxels contained in this level set
std::vector<int> numVoxelsForLS2(tri.numFeatures2, 0); // number of voxels contained in this level set
//set1 and set2 are from same domain, hence they must have same number of voxels
for (auto i = 0; i < belongs1.size(); i++) {
overlap[belongs1[i]][belongs2[i]]++;
numVoxelsForLS1[belongs1[i]]++;
numVoxelsForLS2[belongs2[i]]++;
}
//std::cout << "Accumulating done\n";
/* Initially overlap only contains the overlap of two merge tree edges
* We need to extend this overlap to overlap of subtrees
* The overlap of any child with X is added to the overlap of all ancestors of that child with X
* Let us assume (I) --> Intersection function
* If M = M_1 + M_2 + ... + M_n where all M_i are mutually exclusive then
* M (I) A = M_1 (I) A + M_2 (I) A + ... + M_n (I) A
* i.e. the intersection of sums is the sum of the intersections.
*/
std::vector<std::vector<int> > overlap_1_with_cumul_2(tri.numFeatures1, std::vector<int>(tri.numFeatures2, -1));
std::vector<std::vector<int> > overlap_cumul_1_with_2(tri.numFeatures1, std::vector<int>(tri.numFeatures2, -1));
std::vector<std::vector<int> > cumulativeOverlap(tri.numFeatures1, std::vector<int>(tri.numFeatures2, -1));;
std::vector<int> cumulNumVoxelsForLS1(tri.numFeatures1, -1);
std::vector<int> cumulNumVoxelsForLS2(tri.numFeatures2, -1);
for (auto i = 0; i < tri.numFeatures1; i++) {
for (auto j = 0; j < tri.numFeatures2; j++) {
getOverlap12(i, j,
overlap, overlap_1_with_cumul_2, overlap_cumul_1_with_2,
cumulativeOverlap, invHierarchyTree1, invHierarchyTree2);
}
}
getCumulVox(invTreeRoot1, numVoxelsForLS1, cumulNumVoxelsForLS1, invHierarchyTree1);
getCumulVox(invTreeRoot2, numVoxelsForLS2, cumulNumVoxelsForLS2, invHierarchyTree2);
for (auto i = 0; i < tri.numFeatures1; i++) {
for (auto j = 0; j < tri.numFeatures2; j++) {
tri.normOverlap[i][j] = static_cast<double>(cumulativeOverlap[i][j]) / (cumulNumVoxelsForLS1[i] + cumulNumVoxelsForLS2[j] - cumulativeOverlap[i][j]);
tri.normOverlapOld[i][j] = static_cast<double>(std::abs(cumulNumVoxelsForLS1[i] - cumulNumVoxelsForLS2[j]))
/ std::max(cumulNumVoxelsForLS1[i], cumulNumVoxelsForLS2[j]);
}
}
//std::cout << "Overlap Computed.\n";
time_taken = std::clock() - start;
}
void Tracking::findOverlapFlat(
const std::vector<FeatureIdx>& belongs1,
const std::vector<FeatureIdx>& belongs2,
TrackInfo& tri,
clock_t& time_taken
)
{
clock_t start = std::clock();
std::vector<std::vector<int> > overlap(tri.numFeatures1, std::vector<int>(tri.numFeatures2, 0));
std::vector<int> numVoxelsForLS1(tri.numFeatures1, 0); // number of voxels contained in this level set
std::vector<int> numVoxelsForLS2(tri.numFeatures2, 0); // number of voxels contained in this level set
//set1 and set2 are from same domain, hence they must have same number of voxels
for (auto i = 0; i < belongs1.size(); i++) {
overlap[belongs1[i]][belongs2[i]]++;
numVoxelsForLS1[belongs1[i]]++;
numVoxelsForLS2[belongs2[i]]++;
}
for (auto i = 0; i < tri.numFeatures1; i++) {
for (auto j = 0; j < tri.numFeatures2; j++) {
tri.normOverlap[i][j] = static_cast<double>(overlap[i][j]) / (numVoxelsForLS1[i] + numVoxelsForLS2[j] - overlap[i][j]);
tri.normOverlapOld[i][j] = static_cast<double>(std::max(numVoxelsForLS1[i] - overlap[i][j], numVoxelsForLS2[j] - overlap[i][j]))
/ std::max(numVoxelsForLS1[i], numVoxelsForLS2[j]);
}
}
time_taken = std::clock() - start;
}
void Tracking::findHistDiff(
const std::vector < Hist >& hists1,
const std::vector < Hist >& hists2,
const HistDiffType& type,
const int& normFactor,
TrackInfo& tri,
clock_t& time_taken
)
{
// computing histogram diffs
assert(tri.numFeatures1 == hists1.size() && tri.numFeatures2 == hists2.size());
clock_t start = std::clock();
for (auto i = 0; i < hists1.size(); i++) {
for (auto j = 0; j < hists2.size(); j++) {
tri.histogramDist[i][j] = Histogram4d::histDiff(hists1[i], hists2[j], normFactor, type);
tri.volDist[i][j] = ( static_cast<double>(hists2[j].vol - hists1[i].vol) / normFactor);
tri.cmDist[i][j] = Vertex3Df::EucDist(hists2[j].cm, hists1[i].cm);
}
}
time_taken = std::clock() - start;
}
void Tracking::createDAG(const std::vector<std::vector<Hist>>& histsAll, const std::vector<TrackInfo>& tracks, Dag & dag, bool ignoreLastFeature)
{
dag.clear();
int tot_poss_edge = 0;
int withLast = ignoreLastFeature ? -1 : 0;
for (auto t = 0; t < tracks.size(); t++) {
for (auto s1 = 0; s1 < tracks[t].numFeatures1 + withLast; s1++) {
for (auto s2 = 0; s2 < tracks[t].numFeatures2 + withLast; s2++) {
tot_poss_edge++;
auto edgeKey = DagEdge::makeKey(t, s1, s2);
auto nodeKey1 = DagNode::makeKey(t, s1);
auto nodeKey2 = DagNode::makeKey(t + 1, s2);
dag.addEdge(edgeKey, nodeKey1, nodeKey2, 1);
}
}
}
std::vector<double> overlap_dists(dag.numEdges());
std::vector<double> overlap_dists_old(dag.numEdges());
std::vector<double> hist_dists(dag.numEdges());
std::vector<double> vol_dists(dag.numEdges());
std::vector<double> cm_dists(dag.numEdges());
std::vector<double> node_vols(dag.numNodes());
std::vector<double> node_cmX(dag.numNodes());
std::vector<double> node_cmY(dag.numNodes());
std::vector<double> node_cmZ(dag.numNodes());
for (auto t = 0; t < tracks.size(); t++) {
for (auto s1 = 0; s1 < tracks[t].numFeatures1 + withLast; s1++) {
for (auto s2 = 0; s2 < tracks[t].numFeatures2 + withLast; s2++) {
auto edgeKey = DagEdge::makeKey(t, s1, s2);
overlap_dists_old[dag.getEIdx(edgeKey)] = tracks[t].normOverlapOld[s1][s2];
overlap_dists[dag.getEIdx(edgeKey)] = 1.0 - tracks[t].normOverlap[s1][s2];
hist_dists[dag.getEIdx(edgeKey)] = tracks[t].histogramDist[s1][s2];
vol_dists[dag.getEIdx(edgeKey)] = tracks[t].volDist[s1][s2];
cm_dists[dag.getEIdx(edgeKey)] = tracks[t].cmDist[s1][s2];
}
}
}
for (auto t = 0; t < histsAll.size(); t++) {
for (auto i = 0; i < histsAll[t].size() + withLast; i++) {
auto nodeKey = DagNode::makeKey(t, i);
node_vols[dag.getNIdx(nodeKey)] = histsAll[t][i].vol;
node_cmX[dag.getNIdx(nodeKey)] = histsAll[t][i].cm.x;
node_cmY[dag.getNIdx(nodeKey)] = histsAll[t][i].cm.y;
node_cmZ[dag.getNIdx(nodeKey)] = histsAll[t][i].cm.z;
}
}
dag.addEdgesData(OVRLP_DIST, overlap_dists);
dag.addEdgesData(OVRLP_DIST_OLD, overlap_dists_old);
dag.addEdgesData(HIST_DIST, hist_dists);
dag.addEdgesData(VOL_DIST, vol_dists);
dag.addEdgesData(CM_DIST, cm_dists);
dag.addNodesData(VOLUME, node_vols);
dag.addNodesData(POS_CM_X, node_cmX);
dag.addNodesData(POS_CM_Y, node_cmY);
dag.addNodesData(POS_CM_Z, node_cmZ);
std::cout << "Dag Created. Nodes " << dag.numNodes() << ".Edges " << dag.numEdges()
<< " (" << std::setprecision(5) << 100.0 * dag.numEdges() / tot_poss_edge << "% out of " << tot_poss_edge << ")\n";
}
double stdDev(const std::vector<double>& nums) {
double mean = 0;
for (auto i = 0; i < nums.size(); i++) {
mean = (mean * i + nums[i]) / (i + 1);
}
double stdD2 = 0;
for (auto num : nums) {
stdD2 += pow((num - mean), 2);
}
return sqrt(stdD2 / nums.size());
}
}