forked from ehendrikd/slBenchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryImplementation.cpp
162 lines (123 loc) · 4.79 KB
/
BinaryImplementation.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
#include "BinaryImplementation.h"
BinaryImplementation::BinaryImplementation(): slImplementation(string("BinaryImplementation")) {
numberPatterns = 6;
Black_Value = 0;
White_Value = 195;
Black_Threshold = -50;
White_Threshold = 50;
}
void BinaryImplementation::preExperimentRun() {
numberColumns = 1;
Rect croppedArea = experiment->getInfrastructure()->getCroppedArea();
int arraySize = croppedArea.width * croppedArea.height;
binaryCode = new int[arraySize];
for (int index = 0; index < arraySize; index++) {
binaryCode[index] = 0;
}
}
// For binary implementations, the "width" of the pattern
// is the number of columns.
double BinaryImplementation::getPatternWidth() {
return (double) this->getNumberColumns();
}
unsigned int BinaryImplementation::getNumberPatterns() {
return this->numberPatterns;
}
unsigned int BinaryImplementation::getNumberColumns() {
return this->numberColumns;
}
int BinaryImplementation::getBinaryCode(int x, int y) {
Rect croppedArea = experiment->getInfrastructure()->getCroppedArea();
return this->binaryCode[(y * croppedArea.width) + x];
}
void BinaryImplementation::postExperimentRun() {
delete[] binaryCode;
}
bool BinaryImplementation::hasMoreIterations() {
return experiment->getIterationIndex() < getNumberPatterns() * 2;
}
int BinaryImplementation::guessColour(int colourDifference) {
if (colourDifference < Black_Threshold) {
return 1;
} else if (colourDifference > White_Threshold) {
return 0; }
return -1;
}
void BinaryImplementation::generateBackground(Mat &pattern, Scalar &colour) {
Size cameraResolution = experiment->getInfrastructure()->getCameraResolution();
int iterationIndex = experiment->getIterationIndex();
int cameraWidth = (int)cameraResolution.width;
int cameraHeight = (int)cameraResolution.height;
pattern.create(cameraHeight, cameraWidth, CV_8UC3);
if (iterationIndex % 2 == 0) {
numberColumns *= 2;
pattern.setTo(Scalar(White_Value, White_Value, White_Value));
colour = Scalar(Black_Value, Black_Value, Black_Value);
} else {
pattern.setTo(Scalar(Black_Value, Black_Value, Black_Value));
colour = Scalar(White_Value, White_Value, White_Value);
}
}
Mat BinaryImplementation::generatePattern() {
Mat pattern;
Scalar colour;
Size cameraResolution = experiment->getInfrastructure()->getCameraResolution();
int cameraWidth = (int)cameraResolution.width;
int cameraHeight = (int)cameraResolution.height;
generateBackground(pattern,colour);
int width = cameraWidth / numberColumns;
for (int w = width; w < cameraWidth; w += (2 * width)) {
rectangle(pattern, Point(w, 0), Point((w + width) - 1, cameraHeight), colour, FILLED);
}
return pattern;
}
void BinaryImplementation::iterationProcess() {
Rect croppedArea = experiment->getInfrastructure()->getCroppedArea();
if (experiment->getIterationIndex() % 2 != 0) {
Mat positiveMat = experiment->getCaptureAt(experiment->getNumberCaptures() - 2);
Mat negativeMat = experiment->getLastCapture();
for (int y = 0; y < croppedArea.height; y++) {
for (int x = 0; x < croppedArea.width; x++) {
Vec3b positivePixelBGR = positiveMat.at<Vec3b>(y, x);
Vec3b negativePixelBGR = negativeMat.at<Vec3b>(y, x);
int positiveColourTotal = (int)positivePixelBGR[0] + (int)positivePixelBGR[1] + (int)positivePixelBGR[2];
int negativeColourTotal = (int)negativePixelBGR[0] + (int)negativePixelBGR[1] + (int)negativePixelBGR[2];
int colourDifference = guessColour(positiveColourTotal - negativeColourTotal);
int arrayOffset = (y * croppedArea.width) + x;
binaryCode[arrayOffset] <<= 1;
if(colourDifference == -1) binaryCode[arrayOffset] = -1;
else binaryCode[arrayOffset] += colourDifference;
}
}
}
}
/*
void BinaryImplementation::postIterationsProcess() {
Rect croppedArea = experiment->getInfrastructure()->getCroppedArea();
double width = experiment->getInfrastructure()->getCameraResolution().width/getNumberColumns();
for (int y = 0; y < croppedArea.height; y++) {
int lastBinaryCode = -1;
for (int x = 0; x < croppedArea.width; x++) {
int currentBinaryCode = getBinaryCode(x,y);
if (currentBinaryCode != -1 && currentBinaryCode != lastBinaryCode) {
double displacement = getDisplacement((double)currentBinaryCode,x);
slDepthExperimentResult result(currentBinaryCode*width/2, y, displacement * this->getScale());
experiment->storeResult(&result);
lastBinaryCode = currentBinaryCode;
}
}
}
}
*/
double BinaryImplementation::solveCorrespondence(int x, int y) {
static int lastBinaryCode = -1;
if (x == 0) {
lastBinaryCode = -1;
}
int currentBinaryCode = getBinaryCode(x,y);
if (currentBinaryCode != -1 && currentBinaryCode != lastBinaryCode) {
lastBinaryCode = currentBinaryCode;
return (double)currentBinaryCode;
}
return -1;
}