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 |
|
47 |
jakw |
42 |
/*
|
|
|
43 |
* Return the number of bits set in an integer
|
|
|
44 |
*/
|
|
|
45 |
static int countBits(int n) {
|
|
|
46 |
unsigned int c; // c accumulates the total bits set in v
|
|
|
47 |
for (c = 0; n>0; c++)
|
|
|
48 |
n &= n - 1; // clear the least significant bit set
|
|
|
49 |
return c;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/*
|
|
|
53 |
* Return the position of the least significant bit that is set
|
|
|
54 |
*/
|
|
|
55 |
static int leastSignificantBitSet(int x){
|
|
|
56 |
if(x == 0)
|
|
|
57 |
return 0;
|
|
|
58 |
|
|
|
59 |
int val = 1;
|
|
|
60 |
while(x>>=1)
|
|
|
61 |
val++;
|
|
|
62 |
|
|
|
63 |
return val;
|
|
|
64 |
}
|
|
|
65 |
|
45 |
jakw |
66 |
//static int get_bit(int decimal, int N){
|
4 |
jakw |
67 |
|
45 |
jakw |
68 |
// // Shifting the 1 for N-1 bits
|
|
|
69 |
// int constant = 1 << (N-1);
|
4 |
jakw |
70 |
|
45 |
jakw |
71 |
// // If the bit is set, return 1
|
|
|
72 |
// if( decimal & constant )
|
|
|
73 |
// return 1;
|
|
|
74 |
// else
|
|
|
75 |
// return 0;
|
|
|
76 |
//}
|
|
|
77 |
|
|
|
78 |
static inline unsigned int powi(int num, unsigned int exponent){
|
|
|
79 |
|
4 |
jakw |
80 |
if(exponent == 0)
|
|
|
81 |
return 1;
|
|
|
82 |
|
|
|
83 |
float res = num;
|
|
|
84 |
for(unsigned int i=0; i<exponent-1; i++)
|
|
|
85 |
res *= num;
|
|
|
86 |
|
|
|
87 |
return res;
|
|
|
88 |
}
|
|
|
89 |
|
45 |
jakw |
90 |
static inline unsigned int twopowi(unsigned int exponent){
|
|
|
91 |
|
|
|
92 |
return 1 << exponent;
|
|
|
93 |
}
|
|
|
94 |
|
36 |
jakw |
95 |
// Algorithm
|
70 |
jakw |
96 |
AlgorithmGrayCode::AlgorithmGrayCode(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
|
4 |
jakw |
97 |
|
42 |
jakw |
98 |
Nbits = ceilf(log2f((float)screenCols));
|
|
|
99 |
N = 2 + Nbits*2;
|
41 |
jakw |
100 |
|
|
|
101 |
// all on pattern
|
42 |
jakw |
102 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
|
|
103 |
patterns.push_back(allOn);
|
41 |
jakw |
104 |
|
|
|
105 |
// all off pattern
|
42 |
jakw |
106 |
cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
|
|
|
107 |
patterns.push_back(allOff);
|
41 |
jakw |
108 |
|
4 |
jakw |
109 |
|
42 |
jakw |
110 |
// horizontally encoding patterns
|
|
|
111 |
for(unsigned int p=0; p<Nbits; p++){
|
|
|
112 |
cv::Mat pattern(1, screenCols, CV_8UC3);
|
|
|
113 |
cv::Mat patternInv(1, screenCols, CV_8UC3);
|
4 |
jakw |
114 |
|
42 |
jakw |
115 |
for(unsigned int j=0; j<screenCols; j++){
|
4 |
jakw |
116 |
|
42 |
jakw |
117 |
unsigned int jGray = binaryToGray(j);
|
|
|
118 |
// Amplitude of channels
|
45 |
jakw |
119 |
int bit = (int)getBit(jGray, Nbits-p);
|
42 |
jakw |
120 |
pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
|
|
|
121 |
int invBit = bit^1;
|
|
|
122 |
patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
|
4 |
jakw |
123 |
}
|
42 |
jakw |
124 |
patterns.push_back(pattern);
|
|
|
125 |
patterns.push_back(patternInv);
|
4 |
jakw |
126 |
}
|
42 |
jakw |
127 |
|
|
|
128 |
|
4 |
jakw |
129 |
}
|
|
|
130 |
|
36 |
jakw |
131 |
cv::Mat AlgorithmGrayCode::getEncodingPattern(unsigned int depth){
|
4 |
jakw |
132 |
return patterns[depth];
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
|
47 |
jakw |
136 |
bool sortingLarger(cv::Vec4i i,cv::Vec4i j){ return (i[3]<j[3]);}
|
|
|
137 |
bool sortingEqual(cv::Vec4i i,cv::Vec4i j){ return (i[3]==j[3]);}
|
|
|
138 |
void getEdgeLabels(const cv::Mat& scanLine, int Nbits, std::vector<cv::Vec4i>& edges){
|
4 |
jakw |
139 |
|
41 |
jakw |
140 |
int nCols = scanLine.cols;
|
45 |
jakw |
141 |
const int *data = scanLine.ptr<const int>(0);
|
41 |
jakw |
142 |
|
45 |
jakw |
143 |
int labelLeft;
|
|
|
144 |
int labelRight = data[0];
|
41 |
jakw |
145 |
|
43 |
jakw |
146 |
// collect edges
|
42 |
jakw |
147 |
for(int col=1; col<nCols; col++){
|
41 |
jakw |
148 |
|
42 |
jakw |
149 |
labelLeft = labelRight;
|
|
|
150 |
labelRight = data[col];
|
|
|
151 |
|
95 |
jakw |
152 |
// labels need to be non-background, and differ in exactly one bit
|
47 |
jakw |
153 |
if(labelLeft != -1 && labelRight != -1 && countBits(labelLeft^labelRight) == 1){
|
43 |
jakw |
154 |
int orderingRelation = (labelLeft << Nbits) + labelRight;
|
47 |
jakw |
155 |
// store left label column
|
|
|
156 |
edges.push_back(cv::Vec4i(col-1, labelLeft, labelRight, orderingRelation));
|
41 |
jakw |
157 |
}
|
|
|
158 |
}
|
|
|
159 |
|
42 |
jakw |
160 |
// sort
|
41 |
jakw |
161 |
std::sort(edges.begin(), edges.end(), sortingLarger);
|
42 |
jakw |
162 |
|
|
|
163 |
// remove duplicates
|
47 |
jakw |
164 |
std::vector<cv::Vec4i>::iterator it;
|
42 |
jakw |
165 |
it = std::unique(edges.begin(), edges.end(), sortingEqual);
|
|
|
166 |
edges.resize(std::distance(edges.begin(),it));
|
4 |
jakw |
167 |
}
|
|
|
168 |
|
98 |
jakw |
169 |
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
|
95 |
jakw |
170 |
assert(!img.empty());
|
|
|
171 |
assert(img.channels() == 3);
|
|
|
172 |
|
|
|
173 |
int x = (int)pt.x;
|
|
|
174 |
int y = (int)pt.y;
|
|
|
175 |
|
|
|
176 |
int x0 = cv::borderInterpolate(x, img.cols, cv::BORDER_REFLECT_101);
|
|
|
177 |
int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
|
|
|
178 |
int y0 = cv::borderInterpolate(y, img.rows, cv::BORDER_REFLECT_101);
|
|
|
179 |
int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
|
|
|
180 |
|
|
|
181 |
float a = pt.x - (float)x;
|
|
|
182 |
float c = pt.y - (float)y;
|
|
|
183 |
|
|
|
184 |
uchar b = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[0] * a) * (1.f - c)
|
|
|
185 |
+ (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
|
|
|
186 |
uchar g = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[1] * a) * (1.f - c)
|
|
|
187 |
+ (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
|
|
|
188 |
uchar r = (uchar)cvRound((img.at<cv::Vec3b>(y0, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y0, x1)[2] * a) * (1.f - c)
|
|
|
189 |
+ (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
|
|
|
190 |
|
|
|
191 |
return cv::Vec3b(b, g, r);
|
|
|
192 |
}
|
|
|
193 |
|
42 |
jakw |
194 |
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 |
195 |
|
41 |
jakw |
196 |
assert(frames0.size() == N);
|
|
|
197 |
assert(frames1.size() == N);
|
4 |
jakw |
198 |
|
47 |
jakw |
199 |
// for(int i=0; i<1920; i++){
|
|
|
200 |
// std::cout << i << " " << binaryToGray(i) << " " << grayToBinary(binaryToGray(i)) << std::endl;
|
|
|
201 |
// }
|
45 |
jakw |
202 |
|
42 |
jakw |
203 |
int frameRows = frames0[0].rows;
|
|
|
204 |
int frameCols = frames0[0].cols;
|
|
|
205 |
|
|
|
206 |
// rectifying homographies (rotation+projections)
|
|
|
207 |
cv::Size frameSize(frameCols, frameRows);
|
|
|
208 |
cv::Mat R, T;
|
|
|
209 |
// stereoRectify segfaults unless R is double precision
|
|
|
210 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
|
|
211 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
|
|
212 |
cv::Mat R0, R1, P0, P1, QRect;
|
|
|
213 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
|
|
214 |
|
47 |
jakw |
215 |
// std::cout << "R0" << std::endl << R0 << std::endl;
|
|
|
216 |
// std::cout << "P0" << std::endl << P0 << std::endl;
|
|
|
217 |
// std::cout << "R1" << std::endl << R1 << std::endl;
|
|
|
218 |
// std::cout << "P1" << std::endl << P1 << std::endl;
|
43 |
jakw |
219 |
|
41 |
jakw |
220 |
// interpolation maps
|
|
|
221 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
42 |
jakw |
222 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
|
|
223 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
41 |
jakw |
224 |
|
43 |
jakw |
225 |
// gray-scale and remap
|
|
|
226 |
std::vector<cv::Mat> frames0Rect(N);
|
|
|
227 |
std::vector<cv::Mat> frames1Rect(N);
|
|
|
228 |
for(int i=0; i<N; i++){
|
|
|
229 |
cv::Mat temp;
|
113 |
jakw |
230 |
cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
|
47 |
jakw |
231 |
cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_CUBIC);
|
113 |
jakw |
232 |
cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
|
47 |
jakw |
233 |
cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_CUBIC);
|
43 |
jakw |
234 |
}
|
41 |
jakw |
235 |
|
78 |
jakw |
236 |
// cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
|
|
|
237 |
// cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
|
68 |
jakw |
238 |
|
78 |
jakw |
239 |
// cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
|
|
|
240 |
// cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
|
|
|
241 |
|
68 |
jakw |
242 |
// cv::imwrite("frames0[0].png", frames0[0]);
|
|
|
243 |
// cv::imwrite("frames0Rect[0].png", frames0Rect[0]);
|
|
|
244 |
|
|
|
245 |
// cv::imwrite("frames1[0].png", frames1[0]);
|
|
|
246 |
// cv::imwrite("frames1Rect[0].png", frames1Rect[0]);
|
|
|
247 |
|
113 |
jakw |
248 |
// color debayer and remap
|
|
|
249 |
cv::Mat temp;
|
43 |
jakw |
250 |
cv::Mat color0Rect, color1Rect;
|
113 |
jakw |
251 |
cv::cvtColor(frames0[0], temp, CV_BayerBG2RGB);
|
|
|
252 |
cv::remap(temp, color0Rect, map0X, map0Y, CV_INTER_CUBIC);
|
|
|
253 |
cv::cvtColor(frames1[0], temp, CV_BayerBG2RGB);
|
|
|
254 |
cv::remap(temp, color1Rect, map1X, map1Y, CV_INTER_CUBIC);
|
41 |
jakw |
255 |
|
43 |
jakw |
256 |
int frameRectRows = frames0Rect[0].rows;
|
|
|
257 |
int frameRectCols = frames0Rect[0].cols;
|
42 |
jakw |
258 |
|
47 |
jakw |
259 |
// occlusion masks
|
43 |
jakw |
260 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
|
|
261 |
cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
|
91 |
jakw |
262 |
occlusion0Rect = occlusion0Rect > 25;
|
43 |
jakw |
263 |
cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
|
91 |
jakw |
264 |
occlusion1Rect = occlusion1Rect > 25;
|
47 |
jakw |
265 |
|
|
|
266 |
// erode occlusion masks
|
|
|
267 |
cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(3,3));
|
|
|
268 |
cv::erode(occlusion0Rect, occlusion0Rect, strel);
|
|
|
269 |
cv::erode(occlusion1Rect, occlusion1Rect, strel);
|
|
|
270 |
|
43 |
jakw |
271 |
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
|
|
272 |
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
42 |
jakw |
273 |
|
43 |
jakw |
274 |
// decode patterns
|
78 |
jakw |
275 |
cv::Mat code0Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
|
|
276 |
cv::Mat code1Rect(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
43 |
jakw |
277 |
|
45 |
jakw |
278 |
// into gray code
|
43 |
jakw |
279 |
for(int i=0; i<Nbits; i++){
|
|
|
280 |
cv::Mat bit0;
|
45 |
jakw |
281 |
cv::subtract(frames0Rect[i*2+2], frames0Rect[i*2+3], bit0);
|
43 |
jakw |
282 |
bit0 = bit0 > 0;
|
45 |
jakw |
283 |
bit0.convertTo(bit0, CV_32S, 1.0/255.0);
|
|
|
284 |
// cvtools::writeMat(bit0, "bit0.mat", "bit0");
|
78 |
jakw |
285 |
cv::add(code0Rect, bit0*twopowi(Nbits-i-1), code0Rect, cv::Mat(), CV_32S);
|
43 |
jakw |
286 |
cv::Mat bit1;
|
45 |
jakw |
287 |
cv::subtract(frames1Rect[i*2+2], frames1Rect[i*2+3], bit1);
|
43 |
jakw |
288 |
bit1 = bit1 > 0;
|
45 |
jakw |
289 |
bit1.convertTo(bit1, CV_32S, 1.0/255.0);
|
78 |
jakw |
290 |
cv::add(code1Rect, bit1*twopowi(Nbits-i-1), code1Rect, cv::Mat(), CV_32S);
|
43 |
jakw |
291 |
}
|
|
|
292 |
|
|
|
293 |
//cvtools::writeMat(code0Rect, "code0Rect.mat", "code0Rect");
|
|
|
294 |
//cvtools::writeMat(code1Rect, "code1Rect.mat", "code1Rect");
|
|
|
295 |
|
78 |
jakw |
296 |
|
45 |
jakw |
297 |
// // convert to standard binary
|
78 |
jakw |
298 |
// cv::Mat code0Binary(code0Rect.rows, code0Rect.cols, CV_32F);
|
|
|
299 |
// cv::Mat code1Binary(code1Rect.rows, code1Rect.cols, CV_32F);
|
45 |
jakw |
300 |
// for(int r=0; r<frameRectRows; r++){
|
|
|
301 |
// for(int c=0; c<frameRectCols; c++){
|
|
|
302 |
// if(code0Rect.at<int>(r,c) != -1)
|
78 |
jakw |
303 |
// code0Binary.at<float>(r,c) = grayToBinary(code0Rect.at<int>(r,c));
|
45 |
jakw |
304 |
// if(code1Rect.at<int>(r,c) != -1)
|
78 |
jakw |
305 |
// code1Binary.at<float>(r,c) = grayToBinary(code1Rect.at<int>(r,c));
|
45 |
jakw |
306 |
// }
|
|
|
307 |
// }
|
|
|
308 |
|
78 |
jakw |
309 |
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
|
|
|
310 |
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
|
45 |
jakw |
311 |
|
78 |
jakw |
312 |
// // threshold on vertical discontinuities (due to imperfect rectification)
|
|
|
313 |
// cv::Mat edges0;
|
|
|
314 |
// cv::Sobel(code0Binary, edges0, -1, 0, 1, 5);
|
|
|
315 |
// occlusion0Rect = occlusion0Rect & (abs(edges0) < 50);
|
|
|
316 |
|
|
|
317 |
// cv::Mat edges1;
|
|
|
318 |
// cv::Sobel(code1Binary, edges1, -1, 0, 1, 5);
|
|
|
319 |
// occlusion1Rect = occlusion1Rect & (abs(edges1) < 50);
|
|
|
320 |
|
|
|
321 |
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
|
|
|
322 |
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
|
|
|
323 |
|
|
|
324 |
// set occluded pixels to -1
|
|
|
325 |
for(int r=0; r<frameRectRows; r++){
|
|
|
326 |
for(int c=0; c<frameRectCols; c++){
|
|
|
327 |
if(occlusion0Rect.at<char>(r,c) == 0)
|
|
|
328 |
code0Rect.at<float>(r,c) = -1;
|
|
|
329 |
if(occlusion1Rect.at<char>(r,c) == 0)
|
|
|
330 |
code1Rect.at<float>(r,c) = -1;
|
|
|
331 |
}
|
|
|
332 |
}
|
|
|
333 |
|
41 |
jakw |
334 |
// matching
|
42 |
jakw |
335 |
std::vector<cv::Vec2f> q0Rect, q1Rect;
|
43 |
jakw |
336 |
for(int row=0; row<frameRectRows; row++){
|
41 |
jakw |
337 |
|
95 |
jakw |
338 |
// edge data structure containing [floor(column), labelLeft, labelRight, orderingRelation]
|
47 |
jakw |
339 |
std::vector<cv::Vec4i> edges0, edges1;
|
41 |
jakw |
340 |
|
43 |
jakw |
341 |
// sorted, unique edges
|
42 |
jakw |
342 |
getEdgeLabels(code0Rect.row(row), Nbits, edges0);
|
|
|
343 |
getEdgeLabels(code1Rect.row(row), Nbits, edges1);
|
41 |
jakw |
344 |
|
47 |
jakw |
345 |
// match edges
|
|
|
346 |
std::vector<cv::Vec4i> matchedEdges0, matchedEdges1;
|
41 |
jakw |
347 |
int i=0, j=0;
|
|
|
348 |
while(i<edges0.size() && j<edges1.size()){
|
|
|
349 |
|
|
|
350 |
if(edges0[i][3] == edges1[j][3]){
|
47 |
jakw |
351 |
matchedEdges0.push_back(edges0[i]);
|
|
|
352 |
matchedEdges1.push_back(edges1[j]);
|
41 |
jakw |
353 |
i += 1;
|
|
|
354 |
j += 1;
|
42 |
jakw |
355 |
} else if(edges0[i][3] < edges1[j][3]){
|
41 |
jakw |
356 |
i += 1;
|
42 |
jakw |
357 |
} else if(edges0[i][3] > edges1[j][3]){
|
41 |
jakw |
358 |
j += 1;
|
|
|
359 |
}
|
|
|
360 |
}
|
|
|
361 |
|
47 |
jakw |
362 |
// crude subpixel refinement
|
|
|
363 |
// finds the intersection of linear interpolants in the positive/negative pattern
|
|
|
364 |
for(int i=0; i<matchedEdges0.size(); i++){
|
41 |
jakw |
365 |
|
47 |
jakw |
366 |
int level = Nbits - leastSignificantBitSet(matchedEdges0[i][1]^matchedEdges0[i][2]);
|
|
|
367 |
|
|
|
368 |
// refine for camera 0
|
|
|
369 |
float c0 = matchedEdges0[i][0];
|
|
|
370 |
float c1 = c0+1;
|
|
|
371 |
|
|
|
372 |
float pos0 = frames0Rect[2*level+2].at<char>(row, c0);
|
|
|
373 |
float pos1 = frames0Rect[2*level+2].at<char>(row, c1);
|
|
|
374 |
float neg0 = frames0Rect[2*level+3].at<char>(row, c0);
|
|
|
375 |
float neg1 = frames0Rect[2*level+3].at<char>(row, c1);
|
|
|
376 |
|
|
|
377 |
float col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
|
|
|
378 |
q0Rect.push_back(cv::Point2f(col, row));
|
|
|
379 |
|
|
|
380 |
// refine for camera 1
|
|
|
381 |
c0 = matchedEdges1[i][0];
|
|
|
382 |
c1 = c0+1;
|
|
|
383 |
|
|
|
384 |
pos0 = frames1Rect[2*level+2].at<char>(row, c0);
|
|
|
385 |
pos1 = frames1Rect[2*level+2].at<char>(row, c1);
|
|
|
386 |
neg0 = frames1Rect[2*level+3].at<char>(row, c0);
|
|
|
387 |
neg1 = frames1Rect[2*level+3].at<char>(row, c1);
|
|
|
388 |
|
|
|
389 |
col = c0 + (pos0 - neg0)/(neg1 - neg0 - pos1 + pos0);
|
|
|
390 |
q1Rect.push_back(cv::Point2f(col, row));
|
|
|
391 |
|
|
|
392 |
}
|
|
|
393 |
|
41 |
jakw |
394 |
}
|
|
|
395 |
|
63 |
jakw |
396 |
int nMatches = q0Rect.size();
|
|
|
397 |
|
|
|
398 |
if(nMatches < 1){
|
|
|
399 |
Q.resize(0);
|
|
|
400 |
color.resize(0);
|
|
|
401 |
|
|
|
402 |
return;
|
|
|
403 |
}
|
|
|
404 |
|
95 |
jakw |
405 |
// retrieve color information (at integer coordinates)
|
41 |
jakw |
406 |
color.resize(nMatches);
|
|
|
407 |
for(int i=0; i<nMatches; i++){
|
|
|
408 |
|
42 |
jakw |
409 |
cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
|
|
|
410 |
cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
|
95 |
jakw |
411 |
// cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
|
|
|
412 |
// cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
|
41 |
jakw |
413 |
|
44 |
jakw |
414 |
color[i] = 0.5*c0 + 0.5*c1;
|
41 |
jakw |
415 |
}
|
|
|
416 |
|
42 |
jakw |
417 |
// triangulate points
|
|
|
418 |
cv::Mat QMatHomogenous, QMat;
|
44 |
jakw |
419 |
// cv::Mat C0 = P0.clone();
|
|
|
420 |
// cv::Mat C1 = P1.clone();
|
|
|
421 |
// C0.colRange(0, 3) = C0.colRange(0, 3)*R0;
|
|
|
422 |
// C1.colRange(0, 3) = C1.colRange(0, 3)*R1.t();
|
42 |
jakw |
423 |
cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
|
|
|
424 |
cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
|
44 |
jakw |
425 |
|
95 |
jakw |
426 |
// undo rectifying rotation
|
44 |
jakw |
427 |
cv::Mat R0Inv;
|
|
|
428 |
cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
|
|
|
429 |
QMat = R0Inv*QMat;
|
|
|
430 |
|
42 |
jakw |
431 |
cvtools::matToPoints3f(QMat, Q);
|
44 |
jakw |
432 |
|
4 |
jakw |
433 |
}
|