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 |
*/
|
|
|
27 |
static unsigned grayToBinary(unsigned num, unsigned numBits)
|
|
|
28 |
{
|
|
|
29 |
for (unsigned shift = 1; shift < numBits; shift <<= 1){
|
|
|
30 |
num ^= num >> shift;
|
|
|
31 |
}
|
|
|
32 |
return num;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
/*
|
|
|
36 |
* Function takes the decimal number
|
|
|
37 |
* Function takes the Nth bit (1 to 31)
|
|
|
38 |
* Return the value of Nth bit from decimal
|
|
|
39 |
* Source: http://icfun.blogspot.com/2009/04/get-n-th-bit-value-of-any-integer.html
|
|
|
40 |
*/
|
|
|
41 |
static int get_bit(int decimal, int N){
|
|
|
42 |
|
|
|
43 |
// Shifting the 1 for N-1 bits
|
|
|
44 |
int constant = 1 << (N-1);
|
|
|
45 |
|
|
|
46 |
// If the bit is set, return 1
|
|
|
47 |
if( decimal & constant ){
|
|
|
48 |
return 1;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
// If the bit is not set, return 0
|
|
|
52 |
return 0;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
static inline int powi(int num, unsigned int exponent){
|
|
|
56 |
// NOT EQUIVALENT TO pow()
|
|
|
57 |
if(exponent == 0)
|
|
|
58 |
return 1;
|
|
|
59 |
|
|
|
60 |
float res = num;
|
|
|
61 |
for(unsigned int i=0; i<exponent-1; i++)
|
|
|
62 |
res *= num;
|
|
|
63 |
|
|
|
64 |
return res;
|
|
|
65 |
}
|
|
|
66 |
|
36 |
jakw |
67 |
// Algorithm
|
41 |
jakw |
68 |
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows, CodingDir _dir) : Algorithm(_screenCols, _screenRows, _dir){
|
4 |
jakw |
69 |
|
41 |
jakw |
70 |
// on/off patterns
|
42 |
jakw |
71 |
Nbits = ceilf(log2f((float)screenCols));
|
|
|
72 |
N = 2 + Nbits*2;
|
41 |
jakw |
73 |
|
|
|
74 |
// all on pattern
|
42 |
jakw |
75 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
|
|
76 |
patterns.push_back(allOn);
|
41 |
jakw |
77 |
|
|
|
78 |
// all off pattern
|
42 |
jakw |
79 |
cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
|
|
|
80 |
patterns.push_back(allOff);
|
41 |
jakw |
81 |
|
4 |
jakw |
82 |
|
42 |
jakw |
83 |
// horizontally encoding patterns
|
|
|
84 |
for(unsigned int p=0; p<Nbits; p++){
|
|
|
85 |
cv::Mat pattern(1, screenCols, CV_8UC3);
|
|
|
86 |
cv::Mat patternInv(1, screenCols, CV_8UC3);
|
4 |
jakw |
87 |
|
42 |
jakw |
88 |
for(unsigned int j=0; j<screenCols; j++){
|
4 |
jakw |
89 |
|
42 |
jakw |
90 |
unsigned int jGray = binaryToGray(j);
|
|
|
91 |
// Amplitude of channels
|
|
|
92 |
int bit = get_bit(jGray, Nbits-p);
|
|
|
93 |
pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
|
|
|
94 |
int invBit = bit^1;
|
|
|
95 |
patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
|
4 |
jakw |
96 |
}
|
42 |
jakw |
97 |
patterns.push_back(pattern);
|
|
|
98 |
patterns.push_back(patternInv);
|
4 |
jakw |
99 |
}
|
42 |
jakw |
100 |
|
|
|
101 |
|
4 |
jakw |
102 |
}
|
|
|
103 |
|
36 |
jakw |
104 |
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
|
4 |
jakw |
105 |
return patterns[depth];
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
41 |
jakw |
109 |
bool sortingLarger(cv::Vec4f i,cv::Vec4f j){ return (i[3]<j[3]);}
|
42 |
jakw |
110 |
bool sortingEqual(cv::Vec4f i,cv::Vec4f j){ return (i[3]==j[3]);}
|
41 |
jakw |
111 |
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4f>& edges){
|
4 |
jakw |
112 |
|
41 |
jakw |
113 |
int nCols = scanLine.cols;
|
42 |
jakw |
114 |
const short *data = scanLine.ptr<const short>(0);
|
41 |
jakw |
115 |
|
42 |
jakw |
116 |
short labelLeft;
|
|
|
117 |
short labelRight = data[0];
|
41 |
jakw |
118 |
|
43 |
jakw |
119 |
// collect edges
|
42 |
jakw |
120 |
for(int col=1; col<nCols; col++){
|
41 |
jakw |
121 |
|
42 |
jakw |
122 |
labelLeft = labelRight;
|
|
|
123 |
labelRight = data[col];
|
|
|
124 |
|
41 |
jakw |
125 |
if(labelLeft != -1 && labelRight != -1 && labelLeft != labelRight){
|
43 |
jakw |
126 |
int orderingRelation = (labelLeft << Nbits) + labelRight;
|
|
|
127 |
edges.push_back(cv::Vec4f(col-0.5, labelLeft, labelRight, orderingRelation));
|
41 |
jakw |
128 |
}
|
|
|
129 |
}
|
|
|
130 |
|
42 |
jakw |
131 |
// sort
|
41 |
jakw |
132 |
std::sort(edges.begin(), edges.end(), sortingLarger);
|
42 |
jakw |
133 |
|
|
|
134 |
// remove duplicates
|
|
|
135 |
std::vector<cv::Vec4f>::iterator it;
|
|
|
136 |
it = std::unique(edges.begin(), edges.end(), sortingEqual);
|
|
|
137 |
edges.resize(std::distance(edges.begin(),it));
|
4 |
jakw |
138 |
}
|
|
|
139 |
|
42 |
jakw |
140 |
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 |
141 |
|
41 |
jakw |
142 |
assert(frames0.size() == N);
|
|
|
143 |
assert(frames1.size() == N);
|
4 |
jakw |
144 |
|
42 |
jakw |
145 |
int frameRows = frames0[0].rows;
|
|
|
146 |
int frameCols = frames0[0].cols;
|
|
|
147 |
|
|
|
148 |
// rectifying homographies (rotation+projections)
|
|
|
149 |
cv::Size frameSize(frameCols, frameRows);
|
|
|
150 |
cv::Mat R, T;
|
|
|
151 |
// stereoRectify segfaults unless R is double precision
|
|
|
152 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
|
|
153 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
|
|
154 |
cv::Mat R0, R1, P0, P1, QRect;
|
|
|
155 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
|
|
156 |
|
43 |
jakw |
157 |
std::cout << "R0" << std::endl << R0 << std::endl;
|
|
|
158 |
std::cout << "P0" << std::endl << P0 << std::endl;
|
|
|
159 |
std::cout << "R1" << std::endl << R1 << std::endl;
|
|
|
160 |
std::cout << "P1" << std::endl << P1 << std::endl;
|
|
|
161 |
|
41 |
jakw |
162 |
// interpolation maps
|
|
|
163 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
42 |
jakw |
164 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
|
|
165 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
41 |
jakw |
166 |
|
43 |
jakw |
167 |
// gray-scale and remap
|
|
|
168 |
std::vector<cv::Mat> frames0Rect(N);
|
|
|
169 |
std::vector<cv::Mat> frames1Rect(N);
|
|
|
170 |
for(int i=0; i<N; i++){
|
|
|
171 |
cv::Mat temp;
|
|
|
172 |
cv::cvtColor(frames0[i], temp, CV_RGB2GRAY);
|
|
|
173 |
cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
|
|
|
174 |
cv::cvtColor(frames1[i], temp, CV_RGB2GRAY);
|
|
|
175 |
cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
|
|
|
176 |
}
|
41 |
jakw |
177 |
|
43 |
jakw |
178 |
// color remaps
|
|
|
179 |
cv::Mat color0Rect, color1Rect;
|
|
|
180 |
cv::remap(frames0[0], color0Rect, map0X, map0Y, CV_INTER_CUBIC);
|
|
|
181 |
cv::remap(frames1[0], color1Rect, map0X, map0Y, CV_INTER_CUBIC);
|
41 |
jakw |
182 |
|
43 |
jakw |
183 |
int frameRectRows = frames0Rect[0].rows;
|
|
|
184 |
int frameRectCols = frames0Rect[0].cols;
|
42 |
jakw |
185 |
|
43 |
jakw |
186 |
// occlusion maps
|
|
|
187 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
|
|
188 |
cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
|
|
|
189 |
occlusion0Rect = occlusion0Rect > 50;
|
|
|
190 |
cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
|
|
|
191 |
occlusion1Rect = occlusion1Rect > 50;
|
|
|
192 |
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
|
|
193 |
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
42 |
jakw |
194 |
|
43 |
jakw |
195 |
// decode patterns
|
|
|
196 |
cv::Mat code0Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
|
|
|
197 |
cv::Mat code1Rect(frameRectRows, frameRectCols, CV_16S, cv::Scalar(-1));
|
|
|
198 |
cv::add(code0Rect, 1, code0Rect, occlusion0Rect, CV_16S);
|
|
|
199 |
cv::add(code1Rect, 1, code1Rect, occlusion1Rect, CV_16S);
|
|
|
200 |
|
|
|
201 |
for(int i=0; i<Nbits; i++){
|
|
|
202 |
cv::Mat bit0;
|
|
|
203 |
cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0, cv::noArray(), CV_16S);
|
|
|
204 |
bit0 = bit0 > 0;
|
|
|
205 |
// cvtools::writeMat(bit0, "bit0.mat", "bit0");
|
|
|
206 |
cv::add(code0Rect, bit0/255*powi(2,i), code0Rect, occlusion0Rect, CV_16S);
|
|
|
207 |
// cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
|
|
|
208 |
cv::Mat bit1;
|
|
|
209 |
cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1, cv::noArray(), CV_16S);
|
|
|
210 |
bit1 = bit1 > 0;
|
|
|
211 |
cv::add(code1Rect, bit1/255*powi(2,i), code1Rect, occlusion1Rect, CV_16S);
|
|
|
212 |
}
|
|
|
213 |
|
|
|
214 |
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
|
|
|
215 |
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
|
|
|
216 |
|
41 |
jakw |
217 |
// matching
|
42 |
jakw |
218 |
std::vector<cv::Vec2f> q0Rect, q1Rect;
|
43 |
jakw |
219 |
for(int row=0; row<frameRectRows; row++){
|
41 |
jakw |
220 |
|
|
|
221 |
std::vector<cv::Vec4f> edges0, edges1;
|
|
|
222 |
|
43 |
jakw |
223 |
// sorted, unique edges
|
42 |
jakw |
224 |
getEdgeLabels(code0Rect.row(row), Nbits, edges0);
|
|
|
225 |
getEdgeLabels(code1Rect.row(row), Nbits, edges1);
|
41 |
jakw |
226 |
|
43 |
jakw |
227 |
// subpixel refinement
|
|
|
228 |
// ...
|
|
|
229 |
|
41 |
jakw |
230 |
int i=0, j=0;
|
|
|
231 |
while(i<edges0.size() && j<edges1.size()){
|
|
|
232 |
|
|
|
233 |
if(edges0[i][3] == edges1[j][3]){
|
42 |
jakw |
234 |
q0Rect.push_back(cv::Vec2f(edges0[i][0], row));
|
|
|
235 |
q1Rect.push_back(cv::Vec2f(edges1[j][0], row));
|
41 |
jakw |
236 |
i += 1;
|
|
|
237 |
j += 1;
|
42 |
jakw |
238 |
} else if(edges0[i][3] < edges1[j][3]){
|
41 |
jakw |
239 |
i += 1;
|
42 |
jakw |
240 |
} else if(edges0[i][3] > edges1[j][3]){
|
41 |
jakw |
241 |
j += 1;
|
|
|
242 |
}
|
|
|
243 |
}
|
|
|
244 |
|
|
|
245 |
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
// retrieve color information
|
42 |
jakw |
249 |
int nMatches = q0Rect.size();
|
41 |
jakw |
250 |
color.resize(nMatches);
|
|
|
251 |
for(int i=0; i<nMatches; i++){
|
|
|
252 |
|
42 |
jakw |
253 |
cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
|
|
|
254 |
cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
|
41 |
jakw |
255 |
|
42 |
jakw |
256 |
color[i] = 0.5*(c0 + c1);
|
41 |
jakw |
257 |
}
|
|
|
258 |
|
42 |
jakw |
259 |
// triangulate points
|
|
|
260 |
cv::Mat QMatHomogenous, QMat;
|
|
|
261 |
cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
|
|
|
262 |
cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
|
|
|
263 |
cvtools::matToPoints3f(QMat, Q);
|
4 |
jakw |
264 |
}
|