-
Notifications
You must be signed in to change notification settings - Fork 3
/
BinaryImplementation.cpp
170 lines (123 loc) · 4.91 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
163
164
165
166
167
168
169
170
#include "BinaryImplementation.h"
BinaryImplementation::BinaryImplementation(int newNumberColumns): slImplementation(string("BinaryImplementation")), numberColumns(newNumberColumns) {
Black_Value = 0;
//White_Value = 195;
White_Value = 255;
Black_Threshold = -50;
White_Threshold = 50;
}
void BinaryImplementation::preExperimentRun() {
currentNumberColumns = 1;
Size cameraResolution = experiment->getInfrastructure()->getCameraResolution();
int arraySize = cameraResolution.width * cameraResolution.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 numberColumns;
}
double BinaryImplementation::getBinaryCode(int xProjector, int y) {
Size cameraResolution = experiment->getInfrastructure()->getCameraResolution();
// return this->binaryCode[(y * cameraResolution.width) + xProjector];
for (int x = 0; x < cameraResolution.width; x++) {
int binaryXProjector = binaryCode[(y * cameraResolution.width) + x];
if (binaryXProjector == xProjector) {
return (double)x;
}
}
return -1;
}
void BinaryImplementation::postExperimentRun() {
delete[] binaryCode;
}
int BinaryImplementation::getNumberPatterns() {
return (int)(log(numberColumns) / log(2.0));
}
bool BinaryImplementation::hasMoreIterations() {
return experiment->getIterationIndex() < (2 * getNumberPatterns());
}
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 projectorResolution = experiment->getInfrastructure()->getProjectorResolution();
int iterationIndex = experiment->getIterationIndex();
int projectorWidth = (int)projectorResolution.width;
int projectorHeight = (int)projectorResolution.height;
pattern.create(projectorHeight, projectorWidth, CV_8UC3);
if (iterationIndex % 2 == 0) {
currentNumberColumns *= 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 projectorResolution = experiment->getInfrastructure()->getProjectorResolution();
int projectorHeight = (int)projectorResolution.height;
generateBackground(pattern,colour);
double width = projectorResolution.width / (double)currentNumberColumns;
for (int w = width; w < projectorResolution.width; w += (2 * width)) {
rectangle(pattern, Point(w, 0), Point((w + width) - 1, projectorHeight), colour, FILLED);
}
/*
int flooredWidth = abs(floor(width));
if (width == flooredWidth) {
for (int w = width; w < projectorResolution.width; w += (2 * width)) {
rectangle(pattern, Point(w, 0), Point((w + width) - 1, projectorHeight), colour, FILLED);
}
} else {
double previousWidth = projectorResolution.width / (double)(currentNumberColumns - 1);
int nextWidth = previousWidth - flooredWwidth;
}
*/
return pattern;
}
void BinaryImplementation::processCapture(Mat captureMat) {
experiment->storeCapture(captureMat);
Size cameraResolution = experiment->getInfrastructure()->getCameraResolution();
if (experiment->getIterationIndex() % 2 != 0) {
Mat positiveMat = experiment->getCaptureAt(experiment->getNumberCaptures() - 2);
Mat negativeMat = experiment->getLastCapture();
for (int y = 0; y < cameraResolution.height; y++) {
for (int x = 0; x < cameraResolution.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 * cameraResolution.width) + x;
int before = binaryCode[arrayOffset];
if (binaryCode[arrayOffset] != -1) {
binaryCode[arrayOffset] <<= 1;
if(colourDifference == -1) binaryCode[arrayOffset] = -1;
else binaryCode[arrayOffset] += colourDifference;
}
}
}
}
}
double BinaryImplementation::solveCorrespondence(int xProjector, int y) {
static double lastBinaryCode = -1;
if (xProjector == 0) {
lastBinaryCode = -1;
}
double currentBinaryCode = getBinaryCode(xProjector, y);
if (currentBinaryCode != -1 && currentBinaryCode != lastBinaryCode) {
lastBinaryCode = currentBinaryCode;
return currentBinaryCode;
}
return -1;
}