Skip to content

Commit a7f8975

Browse files
author
Julien Ugon
committed
Moved the displacement calculations into the parent class rather than
reimplementing it every time in the children classes. * Changed the Z-scale from being #defined to a class attribute, so that it can be changed * Added class attributes for the pattern and the image widths, with getters and setters. By default they correspond to the dimensions of the projected and captured images, but in many implementations (binary, phase shift), the pattern width is the number of columns. * Simplified the Phase Shift Method displacement calculations.
1 parent 8c826cd commit a7f8975

12 files changed

+188
-85
lines changed

BinaryImplementation.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ void BinaryImplementation::preExperimentRun() {
1717
}
1818
}
1919

20+
// For binary implementations, the "width" of the pattern
21+
// is the number of columns.
22+
double BinaryImplementation::getPatternWidth() {
23+
return (double) this->getNumberColumns();
24+
}
25+
26+
unsigned int BinaryImplementation::getNumberColumns() {
27+
return this->numberColumns;
28+
}
29+
2030
void BinaryImplementation::postExperimentRun() {
2131
delete[] binaryCode;
2232
}
@@ -96,10 +106,8 @@ void BinaryImplementation::postIterationsProcess() {
96106

97107
if (currentBinaryCode != -1 && currentBinaryCode != lastBinaryCode) {
98108

99-
double displacement =
100-
((double)x / (double)croppedArea.width) - ((double)currentBinaryCode / (double)numberColumns);
101-
102-
slDepthExperimentResult result(x, y, displacement * BINARY_Z_SCALE);
109+
double displacement = getDisplacement(currentBinaryCode,x);
110+
slDepthExperimentResult result(x, y, displacement * this->getScale());
103111
experiment->storeResult(&result);
104112

105113
lastBinaryCode = currentBinaryCode;

BinaryImplementation.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#define BINARY_WHITE_THRESHOLD 50
1212
#define BINARY_BLACK_THRESHOLD -50
1313

14-
#define BINARY_Z_SCALE 10000
15-
1614
using namespace cv;
1715

1816
class BinaryImplementation : public slImplementation {
@@ -21,13 +19,15 @@ class BinaryImplementation : public slImplementation {
2119
virtual ~BinaryImplementation() {};
2220
void preExperimentRun();
2321
void postExperimentRun();
22+
virtual double getPatternWidth();
2423
bool hasMoreIterations();
2524
virtual Mat generatePattern();
2625
virtual void iterationProcess();
2726
virtual void postIterationsProcess();
27+
unsigned int getNumberColumns();
2828

2929
protected:
30-
int numberColumns;
30+
unsigned int numberColumns;
3131
int *binaryCode;
3232
};
3333

DeBruijnImplementation.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,44 @@
11
#include "DeBruijnImplementation.h"
22

3-
DeBruijnImplementation::DeBruijnImplementation(): slImplementation(string("DeBruijnImplementation")) {
3+
DeBruijnImplementation::DeBruijnImplementation(): slImplementation(string("DeBruijnImplementation")),numberEdges(124) {
44
}
55

6+
DeBruijnImplementation::DeBruijnImplementation(unsigned int numEdges): slImplementation(string("DeBruijnImplementation")),numberEdges(numEdges) {
7+
}
68
void DeBruijnImplementation::preExperimentRun() {
79
slInfrastructure *infrastructure = experiment->getInfrastructure();
810

9-
transitions = new Vec3s[DEBRUIJN_NUM_EDGES];
11+
transitions = new Vec3s[getNumberEdges()];
1012
}
1113

1214
void DeBruijnImplementation::postExperimentRun() {
1315
delete[] transitions;
1416
}
1517

18+
// For these implementations, the "width" of the pattern
19+
// is the number of columns.
20+
double DeBruijnImplementation::getPatternWidth() {
21+
return this->getNumberColumns();
22+
}
1623
bool DeBruijnImplementation::hasMoreIterations() {
1724
return experiment->getIterationIndex() < 1;
1825
}
1926

27+
unsigned int DeBruijnImplementation::getNumberColumns() {
28+
return this->numberEdges+1;
29+
}
30+
31+
unsigned int DeBruijnImplementation::getNumberEdges() {
32+
return this->numberEdges;
33+
}
34+
2035
Mat DeBruijnImplementation::generatePattern() {
2136
Size cameraResolution = experiment->getInfrastructure()->getCameraResolution();
2237

2338
int screenWidth = (int)cameraResolution.width;
2439
int screenHeight = (int)cameraResolution.height;
2540

26-
float columnWidth = (float)screenWidth / (float)(DEBRUIJN_NUM_EDGES + 1);
41+
float columnWidth = (float)screenWidth / getNumberColumns();
2742

2843
int size = DEBRUIJN_K * DEBRUIJN_N;
2944
vector<int> a(size), sequence;
@@ -36,7 +51,7 @@ Mat DeBruijnImplementation::generatePattern() {
3651
float columnX = 0;
3752
int pj = 1;
3853

39-
for (int columnIndex = 0; columnIndex < (DEBRUIJN_NUM_EDGES + 1); columnIndex++) {
54+
for (int columnIndex = 0; columnIndex < getNumberColumns(); columnIndex++) {
4055
int dj = sequence[columnIndex] + 1;
4156

4257
int pjInit = pj;
@@ -75,7 +90,7 @@ void DeBruijnImplementation::postIterationsProcess() {
7590
Rect croppedArea = infrastructure->getCroppedArea();
7691
Mat captureMat = experiment->getLastCapture();
7792

78-
float columnWidth = (float)infrastructure->getCameraResolution().width / (float)(DEBRUIJN_NUM_EDGES + 1);
93+
float columnWidth = (float)infrastructure->getCameraResolution().width / (float)getNumberColumns();
7994

8095
for (int y = 0; y < croppedArea.height; y++) {
8196
int prevR = 0;
@@ -118,24 +133,24 @@ void DeBruijnImplementation::postIterationsProcess() {
118133
S = (pairScore**) malloc(edgeIndex * sizeof(*S));
119134

120135
for (int i = 0; i< edgeIndex; i++) {
121-
S[i] = (pairScore*)malloc(DEBRUIJN_NUM_EDGES * sizeof(pairScore));
136+
S[i] = (pairScore*)malloc(getNumberEdges() * sizeof(pairScore));
122137

123-
for (int j = 0;j < DEBRUIJN_NUM_EDGES; j++) {
138+
for (int j = 0;j < getNumberEdges(); j++) {
124139
S[i][j].score = 0;
125140
S[i][j].numberItems = 0;
126141
S[i][j].caseSigma = 0;
127142
}
128143
}
129144

130-
sigma(edgeIndex - 1, DEBRUIJN_NUM_EDGES - 1, transitions, edges, S);
145+
sigma(edgeIndex - 1, getNumberEdges() - 1, transitions, edges, S);
131146

132-
nCorrespondences = S[edgeIndex - 1][DEBRUIJN_NUM_EDGES - 1].numberItems;
147+
nCorrespondences = S[edgeIndex - 1][getNumberEdges() - 1].numberItems;
133148

134149
// DB("Number of correspondences for y=" << y << ": " << nCorrespondences)
135150

136151
int (*correspondences)[2] = new int[nCorrespondences][2];
137152

138-
populateCorrespondences(edgeIndex - 1, DEBRUIJN_NUM_EDGES - 1, correspondences, S);
153+
populateCorrespondences(edgeIndex - 1, getNumberEdges() - 1, correspondences, S);
139154

140155
for (int i = 0; i < edgeIndex; i++) {
141156
free(S[i]);
@@ -146,12 +161,11 @@ void DeBruijnImplementation::postIterationsProcess() {
146161
int newX = correspondences[i][0];
147162
int x = correspondence[newX];
148163

149-
int xPos = (correspondences[i][1] + 1) * columnWidth;
164+
int xPos = (correspondences[i][1] + 1);
150165

151-
double displacement =
152-
((double)xPos / (double)infrastructure->getCameraResolution().width) - ((double)x / (double)croppedArea.width);
166+
double displacement = getDisplacement(xPos,x);
153167

154-
slDepthExperimentResult result(x, y, displacement * DEBRUIJN_Z_SCALE);
168+
slDepthExperimentResult result(x, y, displacement * this->getScale());
155169
experiment->storeResult(&result);
156170
}
157171

DeBruijnImplementation.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
#include "slBenchmark.h"
55

6-
#define DEBRUIJN_Z_SCALE 10000
7-
86
#define DEBRUIJN_K 5
97
#define DEBRUIJN_N 3
108

@@ -26,13 +24,18 @@ typedef struct {
2624
class DeBruijnImplementation : public slImplementation {
2725
public:
2826
DeBruijnImplementation();
27+
// Add a constructor to allow for a different number of edges
28+
DeBruijnImplementation(unsigned int);
2929
virtual ~DeBruijnImplementation() {};
3030
void preExperimentRun();
3131
void postExperimentRun();
32+
virtual double getPatternWidth();
3233
bool hasMoreIterations();
3334
virtual Mat generatePattern();
3435
void calculateCroppedCapture();
3536
virtual void postIterationsProcess();
37+
unsigned int getNumberColumns();
38+
unsigned int getNumberEdges();
3639

3740
protected:
3841
void db(int, int, int, int, vector<int> &, vector<int> &);
@@ -42,6 +45,8 @@ class DeBruijnImplementation : public slImplementation {
4245
double sigma(int, int, Vec3s *, Vec3s *, pairScore **);
4346
void populateCorrespondences(int, int, int[][2], pairScore **);
4447
Vec3s *transitions;
48+
private:
49+
double numberEdges;
4550
};
4651

4752
#endif //DEBRUIJN_IMPLEMENTATION_H

GrayCodedBinaryImplementation.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,16 +88,15 @@ void GrayCodedBinaryImplementation::postIterationsProcess() {
8888
int lastBinaryCode = -1;
8989

9090
for (int x = 0; x < croppedArea.width; x++) {
91-
double xCamera = (double)x / (double)croppedArea.width;
9291
int currentBinaryCode = binaryCode[(y * croppedArea.width) + x];
9392

9493
if (currentBinaryCode != -1 && currentBinaryCode != lastBinaryCode) {
9594

96-
double xPattern = ((double)convertGrayCodeToInteger(currentBinaryCode, numberColumns, BINARY_NUM_PATTERNS) / (double)numberColumns);
97-
double displacement = xCamera - xPattern;
98-
double z = displacement * BINARY_Z_SCALE;
95+
double xPattern = (double)convertGrayCodeToInteger(currentBinaryCode, numberColumns, BINARY_NUM_PATTERNS);
96+
double displacement = getDisplacement(xPattern,x);
97+
double z = displacement * this->getScale();
9998

100-
slDepthExperimentResult result(x, y, displacement * BINARY_Z_SCALE);
99+
slDepthExperimentResult result(x, y, z);
101100
experiment->storeResult(&result);
102101

103102
lastBinaryCode = currentBinaryCode;

GroundTruthImplementation.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,8 @@ void GroundTruthImplementation::postIterationsProcess() {
7272

7373
//if (currentBinaryCode != -1 && currentBinaryCode != lastBinaryCode) {
7474
if (currentOriginalColumn != -1) {
75-
76-
double displacement =
77-
((double)x / (double)croppedArea.width) - ((double)currentOriginalColumn / (double)experiment->getInfrastructure()->getCameraResolution().width);
78-
79-
slDepthExperimentResult result(x, y, displacement * GROUND_TRUTH_Z_SCALE);
75+
double displacement = getDisplacement(currentOriginalColumn,x);
76+
slDepthExperimentResult result(x, y, displacement * this->getScale());
8077
experiment->storeResult(&result);
8178

8279
//lastBinaryCode = currentBinaryCode;

GroundTruthImplementation.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
#define GROUND_TRUTH_WHITE_THRESHOLD 50
1010

11-
#define GROUND_TRUTH_Z_SCALE 10000
12-
1311
using namespace cv;
1412

1513
class GroundTruthImplementation : public slImplementation {

PSMImplementation.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#include "PSMImplementation.h"
22

3-
PSMImplementation::PSMImplementation(): slImplementation(string("PSMImplementation")) {
3+
PSMImplementation::PSMImplementation(): slImplementation(string("PSMImplementation")),numberColumns(32) {
44
}
55

6+
PSMImplementation::PSMImplementation(unsigned int nCol): slImplementation(string("PSMImplementation")),numberColumns(nCol) {
7+
}
68
void PSMImplementation::preExperimentRun() {
79
pixelsToProcess = new priority_queue<WrappedPixel, vector<WrappedPixel>, CompareWrappedPixel>();
810

@@ -31,6 +33,16 @@ bool PSMImplementation::hasMoreIterations() {
3133
return experiment->getIterationIndex() < 3;
3234
}
3335

36+
unsigned int PSMImplementation::getNumberColumns() {
37+
return this->numberColumns;
38+
}
39+
40+
// For Phase shift methods we use the number of columns
41+
// to determine the "size" of the pattern...
42+
double PSMImplementation::getPatternWidth() {
43+
return (double) getNumberColumns();
44+
}
45+
3446
Mat PSMImplementation::generatePattern() {
3547
Size cameraResolution = experiment->getInfrastructure()->getCameraResolution();
3648

@@ -39,13 +51,13 @@ Mat PSMImplementation::generatePattern() {
3951
int screenWidth = (int)cameraResolution.width;
4052
int screenHeight = (int)cameraResolution.height;
4153

42-
int columnWidth = screenWidth / PSM_NUM_COLS;
54+
int columnWidth = screenWidth / getNumberColumns();
4355

4456
float offset = -1.6;
4557

4658
Mat pattern(screenHeight, screenWidth, CV_8UC3);
4759

48-
for (int column = 0; column < PSM_NUM_COLS; column++) {
60+
for (int column = 0; column < getNumberColumns(); column++) {
4961
int columnX = (column * columnWidth);
5062

5163
for (int x = columnX; x < (columnX + columnWidth); x++) {
@@ -239,16 +251,12 @@ void PSMImplementation::makeDepth() {
239251

240252
for (int y = 0; y < croppedArea.height; y += PSM_RENDER_DETAIL) {
241253
for (int x = 0; x < croppedArea.width; x += PSM_RENDER_DETAIL) {
242-
// planephase is 0 at the right border of the image, 1/2 in the middle and 1 at the
243-
// left border of the image.
244-
double planephase = 0.5f - ((x - (croppedArea.width / 2.0f)) / croppedArea.width);
245254
int arrayOffset = (y * croppedArea.width) + x;
246255

247256
if (mask[arrayOffset] == 0) {
248-
// xScaled should give us the position of the point in the projected picture (scaled to [0,1]).
249-
double xScaled = phase[arrayOffset] / PSM_NUM_COLS + 0.5f;
250-
double displacement = xScaled - planephase;
251-
double z = displacement * PSM_Z_SCALE;
257+
double xPos = getNumberColumns()/2 - phase[arrayOffset];
258+
double displacement = getDisplacement(xPos,x);
259+
double z = displacement * this->getScale();
252260
slDepthExperimentResult result(x, y, z);
253261
experiment->storeResult(&result);
254262
}

PSMImplementation.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
#define PSM_NOISE_THRESHOLD 0.1
88
#define PSM_TWO_PI 6.2831853
99
#define PSM_TWO_PI_ON_3 PSM_TWO_PI / 3.0
10-
#define PSM_NUM_COLS 32
11-
12-
#define PSM_Z_SCALE 10000
13-
//#define PSM_Z_SCALE 100
14-
//#define PSM_Z_SKEW 24
15-
//#define PSM_Z_SKEW 6
1610

1711
#define PSM_RENDER_DETAIL 7
1812

@@ -38,12 +32,16 @@ struct CompareWrappedPixel {
3832
class PSMImplementation : public slImplementation {
3933
public:
4034
PSMImplementation();
35+
// Constructor to set the number of columns to something else that 32...
36+
PSMImplementation(unsigned int);
4137
virtual ~PSMImplementation() {};
4238
void preExperimentRun();
4339
void postExperimentRun();
4440
bool hasMoreIterations();
41+
virtual double getPatternWidth();
4542
virtual Mat generatePattern();
4643
virtual void postIterationsProcess();
44+
unsigned int getNumberColumns();
4745

4846
private:
4947
float diff(float, float);
@@ -56,6 +54,8 @@ class PSMImplementation : public slImplementation {
5654
void phaseUnwrap();
5755
void makeDepth();
5856

57+
unsigned int numberColumns;
58+
5959
priority_queue<WrappedPixel, vector<WrappedPixel>, CompareWrappedPixel> *pixelsToProcess;
6060

6161
float *phase;

0 commit comments

Comments
 (0)