-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslBenchmark.h
646 lines (497 loc) · 17.6 KB
/
slBenchmark.h
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
/*
* File: slBenchmark.h
*
* Copyright 2016 Evan Dekker
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Description:
*
* This file defines classes slBenchmark and slImplementation. The
* slBenchmark class conducts the structured light scanning tests
* and stores various benchmarking information. The slImplementation
* class allows a custom structured light implementation to be
* tested.
*/
#ifndef SLBENCHMARK_H
#define SLBENCHMARK_H
#include <iostream>
#include <iterator>
#include <fstream>
#include <stdlib.h>
#include <ctime>
#include <sys/stat.h>
#include <opencv2/opencv.hpp>
//Physical camera/projector calibration filename/XML names
#define INTRINSIC_NAME "intrinsic"
#define DISTORTION_NAME "distortion"
//Default camera resolution
#define DEFAULT_CAMERA_PROJECTOR_WIDTH 1920
#define DEFAULT_CAMERA_PROJECTOR_HEIGHT 1080
//Default camera/projector FOV
#define DEFAULT_CAMERA_HORIZONTAL_FOV 49.134
#define DEFAULT_CAMERA_VERTICAL_FOV 28.841
#define DEFAULT_PROJECTOR_HORIZONTAL_FOV 20.0
#define DEFAULT_PROJECTOR_VERTICAL_FOV 20.0
//Default camera projector separation
#define DEFAULT_CAMERA_PROJECTOR_SEPARATION 1
//Default camera values
#define DEFAULT_CAMERA_PIPE ""
#define DEFAULT_CAMERA_INDEX 0
//Default projection and capture wait (pause) time in milliseconds
#define DEFAULT_WAIT_TIME 1000
using namespace std;
using namespace cv;
//Debug
//#ifdef DEBUG_BUILD
#define DB(x) cerr << x << endl;
//#else
//#define DB(x)
//#endif
//Fatal error
#define FATAL(x) cerr << "FATAL: " << x << endl; exit(-1);
//Cross platform OS separator
#ifdef _WIN32
#define OS_SEP '\\'
#else
#define OS_SEP '/'
#endif
//Forward declaration
class slExperiment;
//Abstract class that defines a structured light implementation
class slImplementation {
public:
//Create a structured light implementation instance with an identifier
slImplementation(string);
//Clean up
virtual ~slImplementation() {};
//Initialise prior to an experiment running
virtual void preExperimentRun() {};
//Initialise after an experiment has run
virtual void postExperimentRun() {};
//Get the width of the pattern
virtual double getPatternWidth() = 0;
//Get the pattern x offset factor, can account for uneven column widths
virtual double getPatternXOffsetFactor(double);
//Get the identifier
string getIdentifier();
//Check if there are any more pattern generation and capture iterations
virtual bool hasMoreIterations();
//Generate the structure light pattern
virtual Mat generatePattern() = 0;
//Process a capture
virtual void processCapture(Mat) {};
//Process after the interations
virtual void postIterationsProcess();
//Solve the correspondence problem
virtual double solveCorrespondence(int, int) {return 0;}
//A reference to the current experiment
slExperiment *experiment;
protected:
//Set the identifier
void setIdentifier(string);
private:
//Initailise this structured light implementation instance
void initialise();
//A string used to identify this implementation
string identifier;
};
//A device for capturing images
class slCameraDevice {
public:
//Create a camera device
slCameraDevice(
int newResolutionWidth = DEFAULT_CAMERA_PROJECTOR_WIDTH,
int newResolutionHeight = DEFAULT_CAMERA_PROJECTOR_HEIGHT,
double newHorizontalFOV = DEFAULT_CAMERA_HORIZONTAL_FOV,
double newVerticalFOV = DEFAULT_CAMERA_VERTICAL_FOV,
string newCameraPipe = string(DEFAULT_CAMERA_PIPE),
int newCameraIndex = DEFAULT_CAMERA_INDEX
):
resolution(Size(newResolutionWidth, newResolutionHeight)),
horizontalFOV(newHorizontalFOV),
verticalFOV(newVerticalFOV),
cameraPipe(newCameraPipe),
cameraIndex(newCameraIndex)
{};
//The resolution
Size resolution;
//The horizontal FOV (degrees)
double horizontalFOV;
//The vertical FOV (degrees)
double verticalFOV;
//The gstreamer pipe to capture with
string cameraPipe;
//The camera index to capture with
int cameraIndex;
};
//A device for projecting images
class slProjectorDevice {
public:
//Create a projection device
slProjectorDevice(
int newResolutionWidth = DEFAULT_CAMERA_PROJECTOR_WIDTH,
int newResolutionHeight = DEFAULT_CAMERA_PROJECTOR_HEIGHT,
double newHorizontalFOV = DEFAULT_PROJECTOR_HORIZONTAL_FOV,
double newVerticalFOV = DEFAULT_PROJECTOR_VERTICAL_FOV
):
resolution(Size(newResolutionWidth, newResolutionHeight)),
horizontalFOV(newHorizontalFOV),
verticalFOV(newVerticalFOV)
{};
//The resolution
Size resolution;
//The horizontal FOV (degrees)
double horizontalFOV;
//The vertical FOV (degrees)
double verticalFOV;
};
//An infrastructure setup represents a particular capture device, projection device and separation between them
class slInfrastructureSetup {
public:
//Create an infrastructure setup
slInfrastructureSetup(
slCameraDevice newCameraDevice = slCameraDevice(),
slProjectorDevice newProjectorDevice = slProjectorDevice(),
double newCameraProjectorSeparation = DEFAULT_CAMERA_PROJECTOR_SEPARATION
):
cameraDevice(newCameraDevice),
projectorDevice(newProjectorDevice),
cameraProjectorSeparation(newCameraProjectorSeparation)
{};
//The camera device
slCameraDevice cameraDevice;
//The projector device
slProjectorDevice projectorDevice;
//The distance between the camera and projector
double cameraProjectorSeparation;
};
//Abstract infrastruture (projector and camera) class used for the benchmarking
class slInfrastructure {
public:
//Create an infrastructure instance with a benchmark and a name
slInfrastructure(
string newName,
slInfrastructureSetup newInfrastructureSetup = slInfrastructureSetup()
):
name(newName),
infrastructureSetup(newInfrastructureSetup),
experiment(NULL)
{};
//Clean up
virtual ~slInfrastructure() {};
//Initialise the infrastucture
virtual void init();
//Project the structured light implementation pattern and capture it
virtual Mat projectAndCapture(Mat) = 0;
//Return the name of this infrastructure
string getName();
//Get the camera resolution
Size getCameraResolution();
//Get the projector resolution
Size getProjectorResolution();
//Get the horizontal camera FOV angle (degrees)
double getCameraHorizontalFOV();
//Get the vertical camera FOV angle (degrees)
double getCameraVerticalFOV();
//Get the horizontal projector FOV angle (degrees)
double getProjectorHorizontalFOV();
//Get the vertical projector FOV angle (degrees)
double getProjectorVerticalFOV();
//Get the distance between the camera and the projector
double getCameraProjectorSeparation();
//A reference to the current experiment
slExperiment *experiment;
//The intrinsic calibration matrix
Mat intrinsicMat;
//The distortion calibration matrix
Mat distortionMat;
protected:
//The infrastructure setup
slInfrastructureSetup infrastructureSetup;
private:
//Generate a unique identifier for this infrastructure and setup (for saving/reading calibration)
unsigned int getUniqueID();
//The name of this infrastructure
string name;
};
//Virtual infrastructure using open source 3D modelling software Blender (http://www.blender.org)
//NOTE: Ensure the blender executable can be found via the environment PATH variable to use
class slBlenderVirtualInfrastructure : public slInfrastructure {
public:
//Create a blender virtual infrastruture instance
slBlenderVirtualInfrastructure(
slInfrastructureSetup newInfrastructureSetup = slInfrastructureSetup()
):
slInfrastructure(
string("slBlenderVirtualInfrastructure"),
slInfrastructureSetup(
newInfrastructureSetup.cameraDevice,
//Limitation of blender spot spight projector, can only be square, horizontal sizes used
slProjectorDevice(
(int)newInfrastructureSetup.projectorDevice.resolution.width,
(int)newInfrastructureSetup.projectorDevice.resolution.width,
newInfrastructureSetup.projectorDevice.horizontalFOV,
newInfrastructureSetup.projectorDevice.horizontalFOV
),
newInfrastructureSetup.cameraProjectorSeparation
)
),
saveBlenderFile(false),
virtualSceneJSONFilename(string("slVirtualScene.json"))
{};
//Check if saving blender file
bool saveBlenderFile;
//The JSON filename that describes the objects in the virtual scene
string virtualSceneJSONFilename;
//Initialise the infrastucture
void init();
//Project the structured light implementation pattern and capture it
Mat projectAndCapture(Mat);
};
//Physical infrastructure using opencv projection and video capture
class slPhysicalInfrastructure : public slInfrastructure {
public:
//Create a physical infrastruture instance
slPhysicalInfrastructure(slInfrastructureSetup newInfrastructureSetup, int newWaitTime = DEFAULT_WAIT_TIME);
//Clean up
virtual ~slPhysicalInfrastructure();
//Project the structured light implementation pattern and capture it
Mat projectAndCapture(Mat);
private:
//The wait (pause) time in milliseconds between each projection and capture
int waitTime;
//The video capture object
VideoCapture videoCapture;
};
//A simple infrastructure class that reads capture files stored in the system, for example as a result of a previous experiment
class slFileInfrastructure : public slInfrastructure {
public:
//Create a file infrastruture instance
slFileInfrastructure(slInfrastructureSetup newInfrastructureSetup):
slInfrastructure(string("slFileInfrastructure"), newInfrastructureSetup)
{};
//Project the structured light implementation pattern and capture it
Mat projectAndCapture(Mat);
};
//Abstract class that defines a result from a particular experiment
class slExperimentResult {
public:
//Create an experiment result
slExperimentResult() {};
//Clean up
virtual ~slExperimentResult() {};
};
//Abstract class that defines an experiment that tests a given implementation on a given infrastructure
class slExperiment {
public:
//Get the current session path
static string getSessionPath();
//Create an experiment
slExperiment(slInfrastructure *, slImplementation *);
//Clean up
virtual ~slExperiment();
//Get the current experiment path
string getPath();
//Run this experiment
void run();
//Run before all iterations begin
virtual void runPreIterations() {};
//Run before this iteration begins
virtual void runPreIteration() {};
//Run before a pattern is generated
virtual void runPrePatternGeneration() {};
//Run after a pattern is generated
virtual void runPostPatternGeneration() {};
//Run before pattern is projected and captured
virtual void runPreProjectAndCapture() {};
//Run after pattern is projected and captured
virtual void runPostProjectAndCapture() {};
//Run before the implementation processes the capture
virtual void runPreProcessCapture() {};
//Run after the implementation processes the capture
virtual void runPostProcessCapture() {};
//Run after this iteration has completed
virtual void runPostIteration() {};
//Run after all iterations have completed
virtual void runPostIterations() {};
//Run before the implementation processes after all the iterations
virtual void runPreImplementationPostIterationsProcess() {};
//Run after the implementation processes after all the iterations
virtual void runPostImplementationPostIterationsProcess() {};
//Store a result of this experiment
virtual void storeResult(slExperimentResult *) {};
//Get the current infrastructure
slInfrastructure *getInfrastructure();
//Get the current implementation
slImplementation *getImplementation();
//Get the current pattern generation and capture iteration index
int getIterationIndex();
//Store the capture
void storeCapture(Mat);
//Get the capture at an index
Mat getCaptureAt(int);
//Get the last capture
Mat getLastCapture();
//Get the number of captures
int getNumberCaptures();
//Compute the depth from a pair of x coordinates from the projection pattern and the image
virtual double getDisplacement(double,double);
virtual double getDisplacement(double,double,bool);
//Get a meaningful identifier of this experiment
string getIdentifier();
protected:
//The infrastructure used for this experiment
slInfrastructure *infrastructure;
//The implementation being tested in this experiement
slImplementation *implementation;
private:
//The current session path
static string sessionPath;
//The current experiment path
string path;
//The current pattern generation and capture iteration index
int iterationIndex;
//The stored captures
vector<Mat> *captures;
};
//Class that defines a kind of experiment that records depth
class slDepthExperiment : public virtual slExperiment {
public:
//Create a depth experiment
slDepthExperiment(slInfrastructure *, slImplementation *);
//Clean up
virtual ~slDepthExperiment();
//Store a result of this experiment
virtual void storeResult(slExperimentResult *);
//Get the number of depth data values
//int getNumDepthDataValues();
//Check if depth data value has been set
//bool isDepthDataValued(int);
bool isDepthDataValued(int, int);
//Get depth data value
//double getDepthData(int);
double getDepthData(int, int);
private:
//Number of depth data values
//int numDepthDataValues;
//Check if the depth data value has been set
//bool *depthDataValued;
map<int, map<int, bool> > depthDataValued;
//The depth data
//double *depthData;
map<int, map<int, double> > depthData;
};
//Class that defines a depth experiment result with x, y and z coordinates
class slDepthExperimentResult : public slExperimentResult {
public:
//Create a depth experiment result
slDepthExperimentResult(int, int, double);
//The x coordinate
int x;
//The y coordinate
int y;
//The z coordinate
double z;
};
//Class that defines a kind of experiment that records the speed of processing
class slSpeedExperiment : public virtual slExperiment {
public:
//Create a speed experiment
slSpeedExperiment(slInfrastructure *, slImplementation *);
//Run before a pattern is generated
virtual void runPrePatternGeneration();
//Run after a pattern is generated
virtual void runPostPatternGeneration();
//Run before pattern is projected and captured
virtual void runPreProjectAndCapture();
//Run after pattern is projected and captured
virtual void runPostProjectAndCapture();
//Run before the implementation processes this iteration
virtual void runPreIterationProcess();
//Run after the implementation processes this iteration
virtual void runPostIterationProcess();
//Run before the implementation processes after all the iterations
virtual void runPrePostIterationsProcess();
//Run after the implementation processes after all the iterations
virtual void runPostPostIterationsProcess();
//Get the total clock value
clock_t getTotalClock();
private:
//The previous stored clock
clock_t previousClock;
//Total clock taken to run this experiment
long totalClock;
};
//Class that defines a kind of experiment that records the speed of processing and depth
class slSpeedDepthExperiment : public slSpeedExperiment, public slDepthExperiment {
public:
//Create a speed and depth experiment
slSpeedDepthExperiment(slInfrastructure *, slImplementation *);
};
//A metric to measure for a benchmark
class slMetric {
public:
//Create a metric
slMetric() {};
//Clean up
virtual ~slMetric() {};
//Compare an experiment against the reference experiment
virtual void compareExperimentAgainstReference(slExperiment *, slExperiment *) = 0;
};
//Metric that compares the processing speed of experiments
class slSpeedMetric : public slMetric {
public:
//Compare an experiment against the reference experiment
virtual void compareExperimentAgainstReference(slExperiment *, slExperiment *);
};
//Metric that compares the depth accuracy of experiments
class slAccuracyMetric : public slMetric {
public:
//Compare an experiment against the reference experiment
virtual void compareExperimentAgainstReference(slExperiment *, slExperiment *);
};
//Metric that compares the resolution of experiments
class slResolutionMetric : public slMetric {
public:
//Compare an experiment against the reference experiment
virtual void compareExperimentAgainstReference(slExperiment *, slExperiment *);
};
//Abstract structured light benchmarking class that can compare measurable values of experiements
class slBenchmark {
public:
//Create a structured light benchmark given a reference experiment
slBenchmark(slExperiment *);
//Clean up
~slBenchmark();
//Add an metric to this benchmark
void addMetric(slMetric *);
//Add an experiment to this benchmark
void addExperiment(slExperiment *);
//Compare the experiments of this benchmark
void compareExperiments();
protected:
//The reference experiment to compare all other experiments against
slExperiment *referenceExperiment;
//The metrics to benchmark against
vector<slMetric *> *metrics;
//The experiments to benchmark
vector<slExperiment *> *experiments;
};
//Reconstruct the 3D data of a given depth expriment
class sl3DReconstructor {
public:
//Write a XYZ point cloud file for the given depth experiment
static void writeXYZPointCloud(slDepthExperiment *);
};
#endif //SLBENCHMARK_H