-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathModalityGridData.hpp
696 lines (564 loc) · 17 KB
/
ModalityGridData.hpp
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
//
// ModalityGridData.h
// segmenthreetion
//
// Created by Albert Clapés on 01/03/14.
//
//
#ifndef __segmenthreetion__ModalityGridData__
#define __segmenthreetion__ModalityGridData__
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "GridMat.h"
using namespace std;
class ModalityGridData
{
public:
ModalityGridData()
: m_ModalityName(""), m_hp(0), m_wp(0)
{
m_MinVal = std::numeric_limits<double>::max();
m_MaxVal = std::numeric_limits<double>::min();
}
ModalityGridData(ModalityGridData& other, cv::Mat logicals)
{
m_ModalityName = other.m_ModalityName;
m_hp = other.m_hp;
m_wp = other.m_wp;
m_MinVal = other.m_MinVal;
m_MaxVal = other.m_MaxVal;
for (int k = 0; k < other.getTags().size(); k++)
{
unsigned char logical = (logicals.rows > 1) ? logicals.at<int>(k,0) : logicals.at<int>(0,k);
if (logical)
{
if (!other.isMock())
{
addGridFrame(other.getGridFrame(k));
addGridMask(other.getGridMask(k));
}
addGridMaskOffset(other.getGridMaskOffset(k));
addGridFrameID(other.getGridFrameID(k));
addFramePath(other.getFramePath(k));
addFrameFilename(other.getFrameFilename(k));
addMaskFilename(other.getMaskFilename(k));
addFrameResolution(other.getFrameResolution(k));
addGridBoundingRect(other.getGridBoundingRect(k));
addTag(other.getTag(k));
addValidnesses(other.getValidnesses(k));
for (int i = 0; i < m_hp; i++) for (int j = 0; j < m_wp; j++)
{
addDescriptor(other.getDescriptor(i, j, k), i, j);
addDescriptorMirrored(other.getDescriptorMirrored(i, j, k), i, j);
}
addElementPartition(other.getElementPartition(k));
}
}
}
void clear()
{
m_GFrames.clear();
m_GMasks.clear();
m_MasksOffsets.clear();
m_FrameIDs.clear();
m_FramesPaths.clear();
m_FramesFilenames.clear();
m_FramesResolutions.clear();
m_GBoundingRects.clear();
m_Tags.clear();
m_Descriptors.release();
m_DescriptorsMirrored.release();
m_Validnesses.release();
m_ValidnessesMirrored.release();
m_Partitions.clear();
}
ModalityGridData(const ModalityGridData& other)
{
*this = other;
}
ModalityGridData& operator=(const ModalityGridData& other)
{
if (this != &other)
{
m_hp = other.m_hp;
m_wp = other.m_wp;
m_ModalityName = other.m_ModalityName;
m_GFrames = other.m_GFrames;
m_GMasks = other.m_GMasks;
m_MasksOffsets = other.m_MasksOffsets;
m_FrameIDs = other.m_FrameIDs;
m_FramesPaths = other.m_FramesPaths;
m_FramesFilenames = other.m_FramesFilenames;
m_FramesResolutions = other.m_FramesResolutions;
m_GBoundingRects = other.m_GBoundingRects;
m_Tags = other.m_Tags;
m_Descriptors = other.m_Descriptors;
m_DescriptorsMirrored = other.m_DescriptorsMirrored;
m_Validnesses = other.m_Validnesses;
m_ValidnessesMirrored = other.m_ValidnessesMirrored;
m_MinVal = other.m_MinVal;
m_MaxVal = other.m_MaxVal;
m_Partitions = other.m_Partitions;
}
return *this;
}
// Getters
GridMat getGridFrame(int k)
{
return m_GFrames[k];
}
GridMat getGridMask(int k)
{
return m_GMasks[k];
}
unsigned char getGridMaskOffset(int k)
{
return m_MasksOffsets[k];
}
int getGridFrameID(int k)
{
return m_FrameIDs[k];
}
string getFramePath(int k)
{
return m_FramesPaths[k];
}
string getFrameFilename(int k)
{
return m_FramesFilenames[k];
}
string getMaskFilename(int k)
{
return m_MasksFilenames[k];
}
cv::Point2d getFrameResolution(int k)
{
return m_FramesResolutions[k];
}
cv::Rect getGridBoundingRect(int k)
{
return m_GBoundingRects[k];
}
int getTag(int k)
{
return m_Tags[k];
}
cv::Mat getValidnesses(int k)
{
cv::Mat validness (m_hp, m_wp, cv::DataType<unsigned char>::type);
for (int i = 0; i < m_hp; i++) for (int j = 0; j < m_wp; j++)
validness.at<unsigned char>(i,j) = m_Validnesses.at<unsigned char>(i,j,k,0);
return validness;
}
GridMat getDescriptor(int k)
{
GridMat gDescriptors (m_hp, m_wp);
for (int i = 0; i < m_hp; i++) for (int j = 0; j < m_wp; j++)
{
gDescriptors.assign(m_Descriptors.at(i,j).row(k), i, j);
}
return gDescriptors;
}
GridMat getDescriptorMirrored(int k)
{
GridMat gDescriptors (m_hp, m_wp);
for (int i = 0; i < m_hp; i++) for (int j = 0; j < m_wp; j++)
{
gDescriptors.assign(m_DescriptorsMirrored.at(i,j).row(k), i, j);
}
return gDescriptors;
}
cv::Mat& getDescriptors(unsigned int i, unsigned int j)
{
return m_Descriptors.at(i,j);
}
cv::Mat& getDescriptorsMirrored(unsigned int i, unsigned int j)
{
return m_DescriptorsMirrored.at(i,j);
}
cv::Mat getDescriptor(unsigned int i, unsigned int j, int k)
{
return m_Descriptors.at(i,j).row(k);
}
cv::Mat getDescriptorMirrored(unsigned int i, unsigned int j, int k)
{
return m_DescriptorsMirrored.at(i,j).row(k);
}
// GridMat getValidDescriptors()
// {
// return GridMat(m_Descriptors, m_Validnesses);
// }
int getElementPartition(int k)
{
return m_Partitions[k];
}
int getNumOfScenes()
{
return m_ScenesPaths.size();
}
vector<GridMat>& getGridsFrames()
{
return m_GFrames;
}
vector<GridMat>& getGridsMasks()
{
return m_GMasks;
}
vector<int>& getSceneIDs()
{
return m_SceneIDs;
}
vector<int>& getFrameIDs()
{
return m_FrameIDs;
}
vector<string>& getFramesPaths()
{
return m_FramesPaths;
}
vector<string>& getFramesFilenames()
{
return m_FramesFilenames;
}
vector<string>& getMasksFilenames()
{
return m_MasksFilenames;
}
vector<cv::Point2d>& getFramesResolutions()
{
return m_FramesResolutions;
}
vector<cv::Rect>& getGridsBoundingRects()
{
return m_GBoundingRects;
}
vector<int>& getTags()
{
return m_Tags;
}
GridMat& getValidnesses()
{
return m_Validnesses;
}
GridMat& getValidnessesMirrored()
{
return m_ValidnessesMirrored;
}
cv::Mat& getValidnesses(unsigned int i, unsigned int j)
{
return m_Validnesses.at(i,j);
}
cv::Mat& getValidnessesMirrored(unsigned int i, unsigned int j)
{
return m_ValidnessesMirrored.at(i,j);
}
GridMat& getDescriptors()
{
return m_Descriptors;
}
GridMat& getDescriptorsMirrored()
{
return m_DescriptorsMirrored;
}
cv::Mat getSceneIDsMat()
{
return cv::Mat(m_SceneIDs.size(), 1, cv::DataType<int>::type, m_SceneIDs.data());
}
cv::Mat getFrameIDsMat()
{
return cv::Mat(m_FrameIDs.size(), 1, cv::DataType<int>::type, m_FrameIDs.data());
}
cv::Mat getTagsMat()
{
return cv::Mat(m_Tags.size(), 1, cv::DataType<int>::type, m_Tags.data());
}
GridMat getValidTags()
{
GridMat gValidTags (m_hp, m_wp);
for (int i = 0; i < m_hp; i++) for (int j = 0; j < m_wp; j++)
{
for (int k = 0; k < getValidnesses(i,j).rows; k++)
{
if (m_Validnesses.at<unsigned char>(i,j,k,0))
gValidTags.at(i,j).push_back(m_Tags[k]);
}
}
return gValidTags;
}
cv::Mat getPartitions()
{
return cv::Mat(m_Partitions.size(), 1, cv::DataType<int>::type, m_Partitions.data());
}
int getHp()
{
return m_hp;
}
int getWp()
{
return m_wp;
}
bool isMock()
{
return m_GFrames.size() == 0 && m_GMasks.size() == 0 && m_Tags.size() > 0;
}
bool isDescribed()
{
return !m_Descriptors.isEmpty();
}
// Setters
void setHp(int hp)
{
m_hp = hp;
}
void setWp(int wp)
{
m_wp = wp;
}
void setMinVal(double value)
{
m_MinVal = value;
}
void setMaxVal(double value)
{
m_MaxVal = value;
}
double getMinVal()
{
return m_MinVal;
}
double getMaxVal()
{
return m_MaxVal;
}
void setModality(string name)
{
m_ModalityName = name;
}
string getModality()
{
return m_ModalityName;
}
void setGridsFrames(vector<GridMat> gframes)
{
m_GFrames = gframes;
}
void setGridsMasks(vector<GridMat> gmasks)
{
m_GMasks = gmasks;
}
void setGridMasksOffsets(vector<unsigned char> gmasksoffsets)
{
m_MasksOffsets = gmasksoffsets;
}
void setGridsFrameIDs(vector<int> gframeids)
{
m_FrameIDs = gframeids;
}
void setFramesPaths(vector<string> paths)
{
m_FramesPaths = paths;
}
void setFramesFilenames(vector<string> filenames)
{
m_FramesFilenames = filenames;
}
void setMasksFilenames(vector<string> filenames)
{
m_MasksFilenames = filenames;
}
void setFramesResolutions(vector<cv::Point2d> resolutions)
{
m_FramesResolutions = resolutions;
}
void setGridsBoundingRects(vector<cv::Rect> gboundingrects)
{
m_GBoundingRects = gboundingrects;
}
void setTags(vector<int> tags)
{
m_Tags = tags;
}
void setValidnesses(GridMat validnesses)
{
m_Validnesses = validnesses;
}
void setDescriptors(GridMat descriptors, GridMat& validDescriptors, GridMat& validnesses)
{
for (int i = 0; i < m_hp; i++) for (int j = 0; j < m_wp; j++)
{
for (int k = 0; k < validnesses.at(i,j).rows; k++)
{
cv::Mat g = descriptors.at(i,j);
cv::Mat d = g.row(k);
unsigned char bValidMask = validnesses.at<unsigned char>(i,j,k,0);
unsigned char bValidDescriptor = cv::checkRange(d);
if (!bValidMask || !bValidDescriptor)
validnesses.at<unsigned char>(i,j,k,0) = 0;
validDescriptors.at(i,j).push_back(d);
}
}
}
void setDescriptors(cv::Mat descriptors, unsigned int i, unsigned int j)
{
m_Descriptors.at(i,j) = descriptors;
}
void setValidnesses(int k, cv::Mat validness)
{
for (int i = 0; i < m_hp; i++) for (int j = 0; j < m_wp; j++)
{
m_Validnesses.at<unsigned char>(i,j,k,0) = validness.at<unsigned char>(i,j);
}
}
// void setValidness(bool validness, unsigned int i, unsigned int j, unsigned int k, GridMat& gvalidnesses)
// {
// gvalidnesses.at<unsigned char>(i,j,k,0) = validness ? 255 : 0;
// }
void addGridFrame(GridMat gframe)
{
m_GFrames.push_back(gframe);
}
void addGridMask(GridMat gmask)
{
m_GMasks.push_back(gmask);
}
void addGridMaskOffset(unsigned char offset)
{
m_MasksOffsets.push_back(offset);
}
void addSceneID(int id)
{
m_SceneIDs.push_back(id);
}
void addScenePath(string scenePath)
{
m_ScenesPaths.push_back(scenePath);
}
void addGridFrameID(int id)
{
m_FrameIDs.push_back(id);
}
void addFramePath(string path)
{
m_FramesPaths.push_back(path);
}
void addFrameFilename(string filename)
{
m_FramesFilenames.push_back(filename);
}
void addMaskFilename(string filename)
{
m_MasksFilenames.push_back(filename);
}
void addFrameResolution(int x, int y)
{
m_FramesResolutions.push_back(cv::Point2d(x,y));
}
void addFrameResolution(cv::Point2d res)
{
m_FramesResolutions.push_back(res);
}
void addGridBoundingRect(cv::Rect gboundingrect)
{
m_GBoundingRects.push_back(gboundingrect);
}
void addTag(int tag)
{
m_Tags.push_back(tag);
}
void addValidnesses(cv::Mat validnesses)
{
GridMat g(validnesses, m_hp, m_wp);
GridMat gm = g.flip(1);
m_Validnesses.vconcat(g);
m_ValidnessesMirrored.vconcat(gm);
}
void addElementPartition(int fold)
{
m_Partitions.push_back(fold);
}
void addDescriptors(GridMat descriptors)
{
GridMat g = descriptors;
for (int i = 0; i < descriptors.crows(); i++) for (int j = 0; j < descriptors.ccols(); j++)
{
unsigned char v = cv::checkRange(descriptors.at(i,j)) ? 255 : 0;
m_Validnesses.at<unsigned char>(i, j, m_Descriptors.at(i,j).rows, 0) = v;
m_Descriptors.at(i,j).push_back(descriptors.at(i,j));
}
}
void addDescriptorsMirrored(GridMat descriptors)
{
for (int i = 0; i < descriptors.crows(); i++) for (int j = 0; j < descriptors.ccols(); j++)
{
m_ValidnessesMirrored.at<unsigned char>(i,j,m_DescriptorsMirrored.at(i,j).rows,0) = cv::checkRange(descriptors.at(i,j)) ? 255 : 0;
m_DescriptorsMirrored.at(i,j).push_back(descriptors.at(i,j));
}
}
void addDescriptor(cv::Mat descriptor, unsigned int i, unsigned int j)
{
m_Validnesses.at<unsigned char>(i,j,m_Descriptors.at(i,j).rows,0) = cv::checkRange(descriptor) ? 255 : 0;
m_Descriptors.at(i,j).push_back(descriptor);
}
void addDescriptorMirrored(cv::Mat descriptor, unsigned int i, unsigned int j)
{
m_ValidnessesMirrored.at<unsigned char>(i,j,m_Descriptors.at(i,j).rows,0) = cv::checkRange(descriptor) ? 255 : 0;
m_DescriptorsMirrored.at(i,j).push_back(descriptor);
}
void saveDescription(string sequencePath, string filename)
{
m_Descriptors.save(sequencePath + "Description/" + filename);
m_DescriptorsMirrored.save(sequencePath + "Description/Mirrored" + filename);
}
void saveDescription(vector<string> sequencesPaths, string filename)
{
for (int i = 0; i < sequencesPaths.size(); i++)
{
saveDescription(sequencesPaths[i], filename);
}
}
void loadDescription(string sequencePath, string filename)
{
GridMat descriptors, descriptorsMirrored;
descriptors.load(sequencePath + "Description/" + filename);
descriptors.load(sequencePath + "Description/Mirrored" + filename);
setDescriptors(descriptors, m_Descriptors, m_Validnesses);
setDescriptors(descriptorsMirrored, m_DescriptorsMirrored, m_ValidnessesMirrored);
}
void loadDescription(vector<string> sequencesPaths, string filename)
{
GridMat descriptors, descriptorsMirrored;
for (int i = 0; i < sequencesPaths.size(); i++)
{
GridMat aux, auxMirrored;
aux.load(sequencesPaths[i] + "Description/" + filename);
auxMirrored.load(sequencesPaths[i] + "Description/Mirrored" + filename);
descriptors.vconcat(aux);
descriptorsMirrored.vconcat(auxMirrored);
}
setDescriptors(descriptors, m_Descriptors, m_Validnesses);
setDescriptors(descriptorsMirrored, m_DescriptorsMirrored, m_ValidnessesMirrored);
}
private:
int m_hp, m_wp;
string m_ModalityName;
vector<GridMat> m_GFrames;
vector<GridMat> m_GMasks;
vector<unsigned char> m_MasksOffsets;
vector<int> m_FrameIDs;
vector<int> m_SceneIDs;
vector<string> m_ScenesPaths;
vector<string> m_FramesPaths;
vector<string> m_FramesFilenames;
vector<string> m_MasksFilenames;
vector<cv::Point2d> m_FramesResolutions;
vector<cv::Rect> m_GBoundingRects;
vector<int> m_Tags;
vector<int> m_Partitions;
GridMat m_Validnesses, m_ValidnessesMirrored; // whether cells in the grids are valid to be described
GridMat m_Descriptors, m_DescriptorsMirrored;
double m_MinVal, m_MaxVal;
};
#endif /* defined(__segmenthreetion__ModalityGridData__) */