41 |
jakw |
1 |
#include "AlgorithmGrayCode.h"
|
4 |
jakw |
2 |
#include <cmath>
|
42 |
jakw |
3 |
#include "cvtools.h"
|
4 |
jakw |
4 |
|
|
|
5 |
#ifndef log2f
|
|
|
6 |
#define log2f(x) (log(x)/log(2.0))
|
|
|
7 |
#endif
|
|
|
8 |
|
41 |
jakw |
9 |
//using namespace std;
|
4 |
jakw |
10 |
|
|
|
11 |
/*
|
|
|
12 |
* The purpose of this function is to convert an unsigned
|
|
|
13 |
* binary number to reflected binary Gray code.
|
|
|
14 |
*
|
|
|
15 |
* The operator >> is shift right. The operator ^ is exclusive or.
|
|
|
16 |
* Source: http://en.wikipedia.org/wiki/Gray_code
|
|
|
17 |
*/
|
|
|
18 |
static unsigned int binaryToGray(unsigned int num) {
|
|
|
19 |
return (num >> 1) ^ num;
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
/*
|
|
|
23 |
* From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
|
|
|
24 |
* The purpose of this function is to convert a reflected binary
|
|
|
25 |
* Gray code number to a binary number.
|
|
|
26 |
*/
|
45 |
jakw |
27 |
static unsigned int grayToBinary(unsigned int num){
|
|
|
28 |
unsigned int mask;
|
|
|
29 |
for(mask = num >> 1; mask != 0; mask = mask >> 1)
|
|
|
30 |
num = num ^ mask;
|
4 |
jakw |
31 |
return num;
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
/*
|
45 |
jakw |
35 |
* Return the Nth bit of an unsigned integer number
|
4 |
jakw |
36 |
*/
|
45 |
jakw |
37 |
static bool getBit(int decimal, int N){
|
4 |
jakw |
38 |
|
45 |
jakw |
39 |
return decimal & 1 << (N-1);
|
|
|
40 |
}
|
4 |
jakw |
41 |
|
45 |
jakw |
42 |
//static int get_bit(int decimal, int N){
|
4 |
jakw |
43 |
|
45 |
jakw |
44 |
// // Shifting the 1 for N-1 bits
|
|
|
45 |
// int constant = 1 << (N-1);
|
4 |
jakw |
46 |
|
45 |
jakw |
47 |
// // If the bit is set, return 1
|
|
|
48 |
// if( decimal & constant )
|
|
|
49 |
// return 1;
|
|
|
50 |
// else
|
|
|
51 |
// return 0;
|
|
|
52 |
//}
|
|
|
53 |
|
|
|
54 |
static inline unsigned int powi(int num, unsigned int exponent){
|
|
|
55 |
|
4 |
jakw |
56 |
if(exponent == 0)
|
|
|
57 |
return 1;
|
|
|
58 |
|
|
|
59 |
float res = num;
|
|
|
60 |
for(unsigned int i=0; i<exponent-1; i++)
|
|
|
61 |
res *= num;
|
|
|
62 |
|
|
|
63 |
return res;
|
|
|
64 |
}
|
|
|
65 |
|
45 |
jakw |
66 |
static inline unsigned int twopowi(unsigned int exponent){
|
|
|
67 |
|
|
|
68 |
return 1 << exponent;
|
|
|
69 |
}
|
|
|
70 |
|
36 |
jakw |
71 |
// Algorithm
|
41 |
jakw |
72 |
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
|
4 |
jakw |
73 |
|
41 |
jakw |
74 |
// on/off patterns
|
42 |
jakw |
75 |
Nbits = ceilf(log2f((float)screenCols));
|
|
|
76 |
N = 2 + Nbits*2;
|
41 |
jakw |
77 |
|
|
|
78 |
// all on pattern
|
42 |
jakw |
79 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
|
|
80 |
patterns.push_back(allOn);
|
41 |
jakw |
81 |
|
|
|
82 |
// all off pattern
|
42 |
jakw |
83 |
cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
|
|
|
84 |
patterns.push_back(allOff);
|
41 |
jakw |
85 |
|
4 |
jakw |
86 |
|
42 |
jakw |
87 |
// horizontally encoding patterns
|
|
|
88 |
for(unsigned int p=0; p<Nbits; p++){
|
|
|
89 |
cv::Mat pattern(1, screenCols, CV_8UC3);
|
|
|
90 |
cv::Mat patternInv(1, screenCols, CV_8UC3);
|
4 |
jakw |
91 |
|
42 |
jakw |
92 |
for(unsigned int j=0; j<screenCols; j++){
|
4 |
jakw |
93 |
|
42 |
jakw |
94 |
unsigned int jGray = binaryToGray(j);
|
|
|
95 |
// Amplitude of channels
|
45 |
jakw |
96 |
int bit = (int)getBit(jGray, Nbits-p);
|
42 |
jakw |
97 |
pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
|
|
|
98 |
int invBit = bit^1;
|
|
|
99 |
patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
|
4 |
jakw |
100 |
}
|
42 |
jakw |
101 |
patterns.push_back(pattern);
|
|
|
102 |
patterns.push_back(patternInv);
|
4 |
jakw |
103 |
}
|
42 |
jakw |
104 |
|
|
|
105 |
|
4 |
jakw |
106 |
}
|
|
|
107 |
|
36 |
jakw |
108 |
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
|
4 |
jakw |
109 |
return patterns[depth];
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
|
41 |
jakw |
113 |
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
|
42 |
jakw |
114 |
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
|
41 |
jakw |
115 |
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
|
4 |
jakw |
116 |
|
41 |
jakw |
117 |
int nCols = scanLine.cols;
|
45 |
jakw |
118 |
const int *data = scanLine.ptr<const int>(0);
|
41 |
jakw |
119 |
|
45 |
jakw |
120 |
int labelLeft;
|
|
|
121 |
int labelRight = data[0];
|
41 |
jakw |
122 |
|
43 |
jakw |
123 |
// collect edges
|
42 |
jakw |
124 |
for(int col=1; col<nCols; col++){
|
41 |
jakw |
125 |
|
42 |
jakw |
126 |
labelLeft = labelRight;
|
|
|
127 |
labelRight = data[col];
|
|
|
128 |
|
41 |
jakw |
129 |
if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
|
43 |
jakw |
130 |
int orderingRelation = (labelLeft << Nbits) + labelRight;
|
|
|
131 |
edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
|
41 |
jakw |
132 |
}
|
|
|
133 |
}
|
|
|
134 |
|
42 |
jakw |
135 |
// sort
|
41 |
jakw |
136 |
std::sort(edges.begin(), edges.end(), sortingLarger);
|
42 |
jakw |
137 |
|
|
|
138 |
// remove duplicates
|
|
|
139 |
std::vector<cv::Vec4f>::iterator it;
|
|
|
140 |
it = std::unique(edges.begin(), edges.end(), sortingEqual);
|
|
|
141 |
edges.resize(std::distance(edges.begin(),it));
|
4 |
jakw |
142 |
}
|
|
|
143 |
|
42 |
jakw |
144 |
void AlgorithmGrayCode::get3DPoints(SMCalibrationParameters calibration, const std::vector<cv::Mat>& frames0, const std::vector<cv::Mat>& frames1, std::vector<cv::Point3f>& Q, std::vector<cv::Vec3b>& color){
|
4 |
jakw |
145 |
|
41 |
jakw |
146 |
assert(frames0.size() == N);
|
|
|
147 |
assert(frames1.size() == N);
|
4 |
jakw |
148 |
|
45 |
jakw |
149 |
for(int i=0; i<1920; i++){
|
|
|
150 |
std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
|
42 |
jakw |
154 |
int frameRows = frames0[0].rows;
|
|
|
155 |
int frameCols = frames0[0].cols;
|
|
|
156 |
|
|
|
157 |
// rectifying homographies (rotation+projections)
|
|
|
158 |
cv::Size frameSize(frameCols, frameRows);
|
|
|
159 |
cv::Mat R, T;
|
|
|
160 |
// stereoRectify segfaults unless R is double precision
|
|
|
161 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
|
|
162 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
|
|
163 |
cv::Mat R0, R1, P0, P1, QRect;
|
|
|
164 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
|
|
165 |
|
43 |
jakw |
166 |
std::cout << "R0" << std::endl << R0 << std::endl;
|
|
|
167 |
std::cout << "P0" << std::endl << P0 << std::endl;
|
|
|
168 |
std::cout << "R1" << std::endl << R1 << std::endl;
|
|
|
169 |
std::cout << "P1" << std::endl << P1 << std::endl;
|
|
|
170 |
|
41 |
jakw |
171 |
// interpolation maps
|
|
|
172 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
42 |
jakw |
173 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
|
|
174 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
41 |
jakw |
175 |
|
43 |
jakw |
176 |
// gray-scale and remap
|
|
|
177 |
std::vector<cv::Mat> frames0Rect(N);
|
|
|
178 |
std::vector<cv::Mat> frames1Rect(N);
|
|
|
179 |
for(int i=0; i<N; i++){
|
|
|
180 |
cv::Mat temp;
|
|
|
181 |
cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
|
|
|
182 |
cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
|
|
|
183 |
cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
|
|
|
184 |
cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
|
|
|
185 |
}
|
41 |
jakw |
186 |
|
43 |
jakw |
187 |
// color remaps
|
|
|
188 |
cv::Mat color0Rect, color1Rect;
|
|
|
189 |
cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
|
44 |
jakw |
190 |
cv::remap(frames1[0], color1Rect, map1X, map1Y, CV_INTER_CUBIC);
|
41 |
jakw |
191 |
|
43 |
jakw |
192 |
int frameRectRows = frames0Rect[0].rows;
|
|
|
193 |
int frameRectCols = frames0Rect[0].cols;
|
42 |
jakw |
194 |
|
43 |
jakw |
195 |
// occlusion maps
|
|
|
196 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
|
|
197 |
cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
|
|
|
198 |
occlusion0Rect = occlusion0Rect > 50;
|
|
|
199 |
cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
|
|
|
200 |
occlusion1Rect = occlusion1Rect > 50;
|
|
|
201 |
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
|
|
202 |
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
42 |
jakw |
203 |
|
43 |
jakw |
204 |
// decode patterns
|
45 |
jakw |
205 |
cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
|
|
|
206 |
cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(-1));
|
|
|
207 |
cv::add(code0Rect, 1, code0Rect, occlusion0Rect, CV_32S);
|
|
|
208 |
cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_32S);
|
43 |
jakw |
209 |
|
45 |
jakw |
210 |
// into gray code
|
43 |
jakw |
211 |
for(int i=0; i<Nbits; i++){
|
|
|
212 |
cv::Mat bit0;
|
45 |
jakw |
213 |
cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0);
|
43 |
jakw |
214 |
bit0 = bit0 > 0;
|
45 |
jakw |
215 |
bit0.convertTo(bit0, CV_32S, 1.0/255.0);
|
|
|
216 |
// cvtools::writeMat(bit0, "bit0.mat", "bit0");
|
|
|
217 |
cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, occlusion0Rect, CV_32S);
|
43 |
jakw |
218 |
cv::Mat bit1;
|
45 |
jakw |
219 |
cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1);
|
43 |
jakw |
220 |
bit1 = bit1 > 0;
|
45 |
jakw |
221 |
bit1.convertTo(bit1, CV_32S, 1.0/255.0);
|
|
|
222 |
cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, occlusion1Rect, CV_32S);
|
43 |
jakw |
223 |
}
|
|
|
224 |
|
|
|
225 |
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
|
|
|
226 |
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
|
|
|
227 |
|
45 |
jakw |
228 |
// // convert to standard binary
|
|
|
229 |
// for(int r=0; r<frameRectRows; r++){
|
|
|
230 |
// for(int c=0; c<frameRectCols; c++){
|
|
|
231 |
// if(code0Rect.at<int>(r,c) != -1)
|
|
|
232 |
// code0Rect.at<int>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
|
|
|
233 |
// if(code1Rect.at<int>(r,c) != -1)
|
|
|
234 |
// code1Rect.at<int>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
|
|
|
235 |
// }
|
|
|
236 |
// }
|
|
|
237 |
|
|
|
238 |
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
|
|
|
239 |
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
|
|
|
240 |
|
41 |
jakw |
241 |
// matching
|
42 |
jakw |
242 |
std::vector<cv::Vec2f> q0Rect, q1Rect;
|
43 |
jakw |
243 |
for(int row=0; row<frameRectRows; row++){
|
41 |
jakw |
244 |
|
|
|
245 |
std::vector<cv::Vec4f> edges0, edges1;
|
|
|
246 |
|
43 |
jakw |
247 |
// sorted, unique edges
|
42 |
jakw |
248 |
getEdgeLabels(code0Rect.row(row), Nbits, edges0);
|
|
|
249 |
getEdgeLabels(code1Rect.row(row), Nbits, edges1);
|
41 |
jakw |
250 |
|
43 |
jakw |
251 |
// subpixel refinement
|
|
|
252 |
// ...
|
|
|
253 |
|
41 |
jakw |
254 |
int i=0, j=0;
|
|
|
255 |
while(i<edges0.size() && j<edges1.size()){
|
|
|
256 |
|
|
|
257 |
if(edges0[i][3] == edges1[j][3]){
|
42 |
jakw |
258 |
q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
|
|
|
259 |
q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
|
41 |
jakw |
260 |
i += 1;
|
|
|
261 |
j += 1;
|
42 |
jakw |
262 |
} else if(edges0[i][3] < edges1[j][3]){
|
41 |
jakw |
263 |
i += 1;
|
42 |
jakw |
264 |
} else if(edges0[i][3] > edges1[j][3]){
|
41 |
jakw |
265 |
j += 1;
|
|
|
266 |
}
|
|
|
267 |
}
|
|
|
268 |
|
|
|
269 |
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
// retrieve color information
|
42 |
jakw |
273 |
int nMatches = q0Rect.size();
|
41 |
jakw |
274 |
color.resize(nMatches);
|
|
|
275 |
for(int i=0; i<nMatches; i++){
|
|
|
276 |
|
42 |
jakw |
277 |
cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
|
|
|
278 |
cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
|
41 |
jakw |
279 |
|
44 |
jakw |
280 |
color[i] = 0.5*c0 + 0.5*c1;
|
41 |
jakw |
281 |
}
|
|
|
282 |
|
42 |
jakw |
283 |
// triangulate points
|
|
|
284 |
cv::Mat QMatHomogenous, QMat;
|
44 |
jakw |
285 |
// cv::Mat C0 = P0.clone();
|
|
|
286 |
// cv::Mat C1 = P1.clone();
|
|
|
287 |
// C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
|
|
|
288 |
// C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
|
42 |
jakw |
289 |
cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
|
|
|
290 |
cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
|
44 |
jakw |
291 |
|
|
|
292 |
// undo rectification
|
|
|
293 |
cv::Mat R0Inv;
|
|
|
294 |
cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
|
|
|
295 |
QMat = R0Inv*QMat;
|
|
|
296 |
|
42 |
jakw |
297 |
cvtools::matToPoints3f(QMat, Q);
|
44 |
jakw |
298 |
|
4 |
jakw |
299 |
}
|