123 |
jakw |
1 |
#include "AlgorithmLineShift.h"
|
4 |
jakw |
2 |
#include <cmath>
|
42 |
jakw |
3 |
#include "cvtools.h"
|
4 |
jakw |
4 |
|
124 |
jakw |
5 |
#include <opencv2/imgproc/imgproc.hpp>
|
123 |
jakw |
6 |
|
4 |
jakw |
7 |
#ifndef log2f
|
|
|
8 |
#define log2f(x) (log(x)/log(2.0))
|
|
|
9 |
#endif
|
|
|
10 |
|
125 |
jakw |
11 |
static unsigned int nLineShifts = 20; // number of columns over which each line is shifted
|
4 |
jakw |
12 |
|
|
|
13 |
/*
|
|
|
14 |
* The purpose of this function is to convert an unsigned
|
|
|
15 |
* binary number to reflected binary Gray code.
|
|
|
16 |
*
|
|
|
17 |
* The operator >> is shift right. The operator ^ is exclusive or.
|
|
|
18 |
* Source: http://en.wikipedia.org/wiki/Gray_code
|
|
|
19 |
*/
|
|
|
20 |
static unsigned int binaryToGray(unsigned int num) {
|
|
|
21 |
return (num >> 1) ^ num;
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
/*
|
|
|
25 |
* From Wikipedia: http://en.wikipedia.org/wiki/Gray_code
|
|
|
26 |
* The purpose of this function is to convert a reflected binary
|
|
|
27 |
* Gray code number to a binary number.
|
|
|
28 |
*/
|
45 |
jakw |
29 |
static unsigned int grayToBinary(unsigned int num){
|
|
|
30 |
unsigned int mask;
|
|
|
31 |
for(mask = num >> 1; mask != 0; mask = mask >> 1)
|
|
|
32 |
num = num ^ mask;
|
4 |
jakw |
33 |
return num;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/*
|
45 |
jakw |
37 |
* Return the Nth bit of an unsigned integer number
|
4 |
jakw |
38 |
*/
|
45 |
jakw |
39 |
static bool getBit(int decimal, int N){
|
4 |
jakw |
40 |
|
45 |
jakw |
41 |
return decimal & 1 << (N-1);
|
|
|
42 |
}
|
4 |
jakw |
43 |
|
47 |
jakw |
44 |
/*
|
|
|
45 |
* Return the number of bits set in an integer
|
|
|
46 |
*/
|
|
|
47 |
static int countBits(int n) {
|
|
|
48 |
unsigned int c; // c accumulates the total bits set in v
|
|
|
49 |
for (c = 0; n>0; c++)
|
|
|
50 |
n &= n - 1; // clear the least significant bit set
|
|
|
51 |
return c;
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
/*
|
|
|
55 |
* Return the position of the least significant bit that is set
|
|
|
56 |
*/
|
|
|
57 |
static int leastSignificantBitSet(int x){
|
|
|
58 |
if(x == 0)
|
|
|
59 |
return 0;
|
|
|
60 |
|
|
|
61 |
int val = 1;
|
|
|
62 |
while(x>>=1)
|
|
|
63 |
val++;
|
|
|
64 |
|
|
|
65 |
return val;
|
|
|
66 |
}
|
|
|
67 |
|
45 |
jakw |
68 |
static inline unsigned int powi(int num, unsigned int exponent){
|
|
|
69 |
|
4 |
jakw |
70 |
if(exponent == 0)
|
|
|
71 |
return 1;
|
|
|
72 |
|
|
|
73 |
float res = num;
|
|
|
74 |
for(unsigned int i=0; i<exponent-1; i++)
|
|
|
75 |
res *= num;
|
|
|
76 |
|
|
|
77 |
return res;
|
|
|
78 |
}
|
|
|
79 |
|
45 |
jakw |
80 |
static inline unsigned int twopowi(unsigned int exponent){
|
|
|
81 |
|
|
|
82 |
return 1 << exponent;
|
|
|
83 |
}
|
|
|
84 |
|
36 |
jakw |
85 |
// Algorithm
|
123 |
jakw |
86 |
AlgorithmLineShift::AlgorithmLineShift(unsigned int _screenCols, unsigned int _screenRows) : Algorithm(_screenCols, _screenRows){
|
4 |
jakw |
87 |
|
125 |
jakw |
88 |
int nTotalBits = ceilf(log2f((float)screenCols));
|
41 |
jakw |
89 |
|
125 |
jakw |
90 |
nGrayBits = nTotalBits - floorf(log2f((float)nLineShifts));
|
|
|
91 |
|
123 |
jakw |
92 |
N = 2 + 2*nGrayBits + nLineShifts;
|
|
|
93 |
|
41 |
jakw |
94 |
// all on pattern
|
42 |
jakw |
95 |
cv::Mat allOn(1, screenCols, CV_8UC3, cv::Scalar::all(255));
|
|
|
96 |
patterns.push_back(allOn);
|
41 |
jakw |
97 |
|
|
|
98 |
// all off pattern
|
42 |
jakw |
99 |
cv::Mat allOff(1, screenCols, CV_8UC3, cv::Scalar::all(0));
|
|
|
100 |
patterns.push_back(allOff);
|
41 |
jakw |
101 |
|
4 |
jakw |
102 |
|
123 |
jakw |
103 |
// Gray code patterns
|
|
|
104 |
for(unsigned int p=0; p<nGrayBits; p++){
|
42 |
jakw |
105 |
cv::Mat pattern(1, screenCols, CV_8UC3);
|
|
|
106 |
cv::Mat patternInv(1, screenCols, CV_8UC3);
|
4 |
jakw |
107 |
|
42 |
jakw |
108 |
for(unsigned int j=0; j<screenCols; j++){
|
4 |
jakw |
109 |
|
42 |
jakw |
110 |
unsigned int jGray = binaryToGray(j);
|
|
|
111 |
// Amplitude of channels
|
125 |
jakw |
112 |
int bit = (int)getBit(jGray, nTotalBits-p);
|
42 |
jakw |
113 |
pattern.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*bit,255.0*bit,255.0*bit);
|
|
|
114 |
int invBit = bit^1;
|
|
|
115 |
patternInv.at<cv::Vec3b>(0,j) = cv::Vec3b(255.0*invBit,255.0*invBit,255.0*invBit);
|
4 |
jakw |
116 |
}
|
42 |
jakw |
117 |
patterns.push_back(pattern);
|
|
|
118 |
patterns.push_back(patternInv);
|
4 |
jakw |
119 |
}
|
42 |
jakw |
120 |
|
123 |
jakw |
121 |
// line shifts
|
|
|
122 |
for(unsigned int p=0; p<nLineShifts; p++){
|
124 |
jakw |
123 |
cv::Mat pattern(1, screenCols, CV_8UC3, cv::Scalar(0));
|
42 |
jakw |
124 |
|
123 |
jakw |
125 |
for(unsigned int j=p; j<screenCols; j+= nLineShifts)
|
|
|
126 |
pattern.at<cv::Vec3b>(0, j) = cv::Vec3b(255, 255, 255);
|
4 |
jakw |
127 |
|
123 |
jakw |
128 |
patterns.push_back(pattern);
|
41 |
jakw |
129 |
}
|
|
|
130 |
|
123 |
jakw |
131 |
}
|
42 |
jakw |
132 |
|
123 |
jakw |
133 |
cv::Mat AlgorithmLineShift::getEncodingPattern(unsigned int depth){
|
|
|
134 |
return patterns[depth];
|
4 |
jakw |
135 |
}
|
|
|
136 |
|
98 |
jakw |
137 |
static cv::Vec3b getColorSubpix(const cv::Mat& img, cv::Point2f pt){
|
95 |
jakw |
138 |
assert(!img.empty());
|
|
|
139 |
assert(img.channels() == 3);
|
|
|
140 |
|
|
|
141 |
int x = (int)pt.x;
|
|
|
142 |
int y = (int)pt.y;
|
|
|
143 |
|
|
|
144 |
int x0 = cv::borderInterpolate(x, img.cols, cv::BORDER_REFLECT_101);
|
|
|
145 |
int x1 = cv::borderInterpolate(x+1, img.cols, cv::BORDER_REFLECT_101);
|
|
|
146 |
int y0 = cv::borderInterpolate(y, img.rows, cv::BORDER_REFLECT_101);
|
|
|
147 |
int y1 = cv::borderInterpolate(y+1, img.rows, cv::BORDER_REFLECT_101);
|
|
|
148 |
|
|
|
149 |
float a = pt.x - (float)x;
|
|
|
150 |
float c = pt.y - (float)y;
|
|
|
151 |
|
|
|
152 |
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)
|
|
|
153 |
+ (img.at<cv::Vec3b>(y1, x0)[0] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[0] * a) * c);
|
|
|
154 |
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)
|
|
|
155 |
+ (img.at<cv::Vec3b>(y1, x0)[1] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[1] * a) * c);
|
|
|
156 |
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)
|
|
|
157 |
+ (img.at<cv::Vec3b>(y1, x0)[2] * (1.f - a) + img.at<cv::Vec3b>(y1, x1)[2] * a) * c);
|
|
|
158 |
|
|
|
159 |
return cv::Vec3b(b, g, r);
|
|
|
160 |
}
|
|
|
161 |
|
124 |
jakw |
162 |
void getlineCenters(const cv::Mat& linesScanLine, const cv::Mat& codeScanLine, std::vector<cv::Vec2f>& lineCenters){
|
4 |
jakw |
163 |
|
123 |
jakw |
164 |
int nCols = linesScanLine.cols;
|
|
|
165 |
|
|
|
166 |
// fourth order derivative filtering
|
125 |
jakw |
167 |
cv::Mat der(1, nCols, CV_32F);
|
123 |
jakw |
168 |
for(int i=2; i<nCols-2; i++)
|
125 |
jakw |
169 |
der.at<float>(0, i) = linesScanLine.at<unsigned char>(0, i-2)+linesScanLine.at<unsigned char>(0, i-1)-
|
|
|
170 |
linesScanLine.at<unsigned char>(0, i+1)-linesScanLine.at<unsigned char>(0, i+2);
|
123 |
jakw |
171 |
|
125 |
jakw |
172 |
// cvtools::writeMat(codeScanLine, "codeScanLine.mat", "codeScanLine");
|
|
|
173 |
// cvtools::writeMat(linesScanLine, "linesScanLine.mat", "linesScanLine");
|
|
|
174 |
// cvtools::writeMat(der, "der.mat", "der");
|
|
|
175 |
|
123 |
jakw |
176 |
for(int i=0; i<nCols; i++){
|
126 |
jakw |
177 |
int code = codeScanLine.at<int>(0, i);
|
123 |
jakw |
178 |
|
126 |
jakw |
179 |
if((code != -1) && (der.at<float>(0, i) < 0.0) && (der.at<float>(0, i+1) > 0.0) && ((der.at<float>(0, i+1) - der.at<float>(0, i)) > 20.0)){
|
123 |
jakw |
180 |
|
126 |
jakw |
181 |
lineCenters.push_back(cv::Vec2f(i, code));
|
123 |
jakw |
182 |
// TODO: subpixel interpolation, non-max suppression
|
|
|
183 |
}
|
|
|
184 |
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
void AlgorithmLineShift::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){
|
|
|
190 |
|
41 |
jakw |
191 |
assert(frames0.size() == N);
|
|
|
192 |
assert(frames1.size() == N);
|
4 |
jakw |
193 |
|
42 |
jakw |
194 |
int frameRows = frames0[0].rows;
|
|
|
195 |
int frameCols = frames0[0].cols;
|
|
|
196 |
|
|
|
197 |
// rectifying homographies (rotation+projections)
|
|
|
198 |
cv::Size frameSize(frameCols, frameRows);
|
|
|
199 |
cv::Mat R, T;
|
|
|
200 |
// stereoRectify segfaults unless R is double precision
|
|
|
201 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
|
|
202 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
|
|
203 |
cv::Mat R0, R1, P0, P1, QRect;
|
|
|
204 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
|
|
205 |
|
41 |
jakw |
206 |
// interpolation maps
|
|
|
207 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
42 |
jakw |
208 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
|
|
209 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
41 |
jakw |
210 |
|
43 |
jakw |
211 |
// gray-scale and remap
|
|
|
212 |
std::vector<cv::Mat> frames0Rect(N);
|
|
|
213 |
std::vector<cv::Mat> frames1Rect(N);
|
|
|
214 |
for(int i=0; i<N; i++){
|
|
|
215 |
cv::Mat temp;
|
113 |
jakw |
216 |
cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
|
114 |
jakw |
217 |
cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
|
113 |
jakw |
218 |
cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
|
114 |
jakw |
219 |
cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
|
43 |
jakw |
220 |
}
|
41 |
jakw |
221 |
|
123 |
jakw |
222 |
//cvtools::writeMat(frames0Rect[0], "frames0Rect_0.mat", "frames0Rect_0");
|
|
|
223 |
//cvtools::writeMat(frames0Rect[1], "frames0Rect_1.mat", "frames0Rect_1");
|
|
|
224 |
//cvtools::writeMat(frames0Rect[20], "frames0Rect_20.mat", "frames0Rect_20");
|
|
|
225 |
//cvtools::writeMat(frames0Rect[21], "frames0Rect_21.mat", "frames0Rect_21");
|
120 |
jakw |
226 |
|
113 |
jakw |
227 |
// color debayer and remap
|
43 |
jakw |
228 |
cv::Mat color0Rect, color1Rect;
|
121 |
jakw |
229 |
cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
|
120 |
jakw |
230 |
cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
|
41 |
jakw |
231 |
|
121 |
jakw |
232 |
cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
|
120 |
jakw |
233 |
cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
|
|
|
234 |
|
43 |
jakw |
235 |
int frameRectRows = frames0Rect[0].rows;
|
|
|
236 |
int frameRectCols = frames0Rect[0].cols;
|
42 |
jakw |
237 |
|
47 |
jakw |
238 |
// occlusion masks
|
43 |
jakw |
239 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
|
|
240 |
cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0Rect);
|
121 |
jakw |
241 |
occlusion0Rect = (occlusion0Rect > 20) & (occlusion0Rect < 250);
|
43 |
jakw |
242 |
cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1Rect);
|
121 |
jakw |
243 |
occlusion1Rect = (occlusion1Rect > 20) & (occlusion1Rect < 250);
|
47 |
jakw |
244 |
|
125 |
jakw |
245 |
// cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
|
|
246 |
// cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
123 |
jakw |
247 |
|
47 |
jakw |
248 |
// erode occlusion masks
|
114 |
jakw |
249 |
cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(2,2));
|
47 |
jakw |
250 |
cv::erode(occlusion0Rect, occlusion0Rect, strel);
|
|
|
251 |
cv::erode(occlusion1Rect, occlusion1Rect, strel);
|
|
|
252 |
|
120 |
jakw |
253 |
// correct for texture modulation and ambient
|
|
|
254 |
cv::Mat A0 = frames0Rect[1];
|
|
|
255 |
cv::Mat M0 = frames0Rect[0]-frames0Rect[1];
|
123 |
jakw |
256 |
|
121 |
jakw |
257 |
cv::divide(256.0, M0, M0, CV_32F);
|
120 |
jakw |
258 |
cv::Mat A1 = frames1Rect[1];
|
|
|
259 |
cv::Mat M1 = frames1Rect[0]-frames1Rect[1];
|
121 |
jakw |
260 |
cv::divide(256.0, M1, M1, CV_32F);
|
120 |
jakw |
261 |
|
|
|
262 |
for(int i=2; i<N; i++){
|
121 |
jakw |
263 |
cv::multiply(frames0Rect[i]-A0, M0, frames0Rect[i], 1.0, CV_8UC1);
|
|
|
264 |
cv::multiply(frames1Rect[i]-A1, M1, frames1Rect[i], 1.0, CV_8UC1);
|
120 |
jakw |
265 |
}
|
|
|
266 |
|
123 |
jakw |
267 |
//cvtools::writeMat(frames0Rect[22], "frames0Rect_22.mat", "frames0Rect_22");
|
|
|
268 |
//cvtools::writeMat(frames0Rect[23], "frames0Rect_23.mat", "frames0Rect_23");
|
120 |
jakw |
269 |
|
123 |
jakw |
270 |
// divide into Gray coding frames and line shift frames
|
125 |
jakw |
271 |
std::vector<cv::Mat> frames0GrayCode(frames0Rect.begin()+2, frames0Rect.begin()+2+2*nGrayBits);
|
|
|
272 |
std::vector<cv::Mat> frames0LineShift(frames0Rect.begin()+2+2*nGrayBits, frames0Rect.end());
|
|
|
273 |
std::vector<cv::Mat> frames1GrayCode(frames1Rect.begin()+2, frames1Rect.begin()+2+2*nGrayBits);
|
|
|
274 |
std::vector<cv::Mat> frames1LineShift(frames1Rect.begin()+2+2*nGrayBits, frames1Rect.end());
|
42 |
jakw |
275 |
|
43 |
jakw |
276 |
// decode patterns
|
123 |
jakw |
277 |
cv::Mat code0Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
|
|
278 |
cv::Mat code1Gray(frameRectRows, frameRectCols, CV_32S, cv::Scalar(0));
|
43 |
jakw |
279 |
|
45 |
jakw |
280 |
// into gray code
|
123 |
jakw |
281 |
for(int i=0; i<nGrayBits; i++){
|
120 |
jakw |
282 |
cv::Mat temp, bit0, bit1;
|
|
|
283 |
|
123 |
jakw |
284 |
cv::compare(frames0GrayCode[i*2], frames0GrayCode[i*2+1], temp, cv::CMP_GT);
|
120 |
jakw |
285 |
temp.convertTo(bit0, CV_32S, 1.0/255.0);
|
124 |
jakw |
286 |
cv::add(code0Gray, bit0*twopowi(nGrayBits-i-1), code0Gray, cv::noArray(), CV_32S);
|
120 |
jakw |
287 |
|
123 |
jakw |
288 |
cv::compare(frames1GrayCode[i*2], frames1GrayCode[i*2+1], temp, cv::CMP_GT);
|
120 |
jakw |
289 |
temp.convertTo(bit1, CV_32S, 1.0/255.0);
|
124 |
jakw |
290 |
cv::add(code1Gray, bit1*twopowi(nGrayBits-i-1), code1Gray, cv::noArray(), CV_32S);
|
43 |
jakw |
291 |
}
|
|
|
292 |
|
125 |
jakw |
293 |
// convert to standard binary
|
|
|
294 |
cv::Mat code0Binary(code0Gray.rows, code0Gray.cols, CV_32S, cv::Scalar(-1));
|
|
|
295 |
cv::Mat code1Binary(code1Gray.rows, code1Gray.cols, CV_32S, cv::Scalar(-1));
|
78 |
jakw |
296 |
for(int r=0; r<frameRectRows; r++){
|
|
|
297 |
for(int c=0; c<frameRectCols; c++){
|
125 |
jakw |
298 |
code0Binary.at<int>(r,c) = grayToBinary(code0Gray.at<int>(r,c));
|
|
|
299 |
code1Binary.at<int>(r,c) = grayToBinary(code1Gray.at<int>(r,c));
|
78 |
jakw |
300 |
}
|
|
|
301 |
}
|
|
|
302 |
|
125 |
jakw |
303 |
// set occluded pixels to -1
|
123 |
jakw |
304 |
for(int r=0; r<frameRectRows; r++){
|
|
|
305 |
for(int c=0; c<frameRectCols; c++){
|
125 |
jakw |
306 |
if(occlusion0Rect.at<unsigned char>(r,c) == 0)
|
|
|
307 |
code0Binary.at<int>(r,c) = -1;
|
|
|
308 |
if(occlusion1Rect.at<unsigned char>(r,c) == 0)
|
|
|
309 |
code1Binary.at<int>(r,c) = -1;
|
123 |
jakw |
310 |
}
|
|
|
311 |
}
|
120 |
jakw |
312 |
|
126 |
jakw |
313 |
//cvtools::writeMat(code0Gray, "code0Gray.mat", "code0Gray");
|
|
|
314 |
//cvtools::writeMat(code1Gray, "code1Gray.mat", "code1Gray");
|
|
|
315 |
//cvtools::writeMat(code0Binary, "code0Binary.mat", "code0Binary");
|
|
|
316 |
//cvtools::writeMat(code1Binary, "code1Binary.mat", "code1Binary");
|
123 |
jakw |
317 |
|
41 |
jakw |
318 |
// matching
|
42 |
jakw |
319 |
std::vector<cv::Vec2f> q0Rect, q1Rect;
|
126 |
jakw |
320 |
for(int s=0; s<nLineShifts; s++){
|
41 |
jakw |
321 |
|
126 |
jakw |
322 |
cv::Mat lines0 = frames0LineShift[s];
|
|
|
323 |
cv::Mat lines1 = frames1LineShift[s];
|
41 |
jakw |
324 |
|
126 |
jakw |
325 |
for(int row=0; row<frameRectRows; row++){
|
41 |
jakw |
326 |
|
126 |
jakw |
327 |
// line center data structure containing [x-coordinate (sub-px), region-code]
|
|
|
328 |
std::vector<cv::Vec2f> lineCenters0, lineCenters1;
|
41 |
jakw |
329 |
|
126 |
jakw |
330 |
// sorted, unique line centers
|
|
|
331 |
getlineCenters(lines0.row(row), code0Binary.row(row), lineCenters0);
|
|
|
332 |
getlineCenters(lines1.row(row), code1Binary.row(row), lineCenters1);
|
|
|
333 |
|
|
|
334 |
// if(s==0 && row==1300){
|
|
|
335 |
// std::cout << cv::Mat(lineCenters0) << std::endl;
|
|
|
336 |
// std::cout << cv::Mat(lineCenters1) << std::endl;
|
|
|
337 |
|
|
|
338 |
// cvtools::writeMat(lines0.row(row), "lines0.mat", "lines0");
|
|
|
339 |
// cvtools::writeMat(lines1.row(row), "lines1.mat", "lines1");
|
|
|
340 |
// cvtools::writeMat(code0Binary.row(row), "code0Binary.mat", "code0Binary");
|
|
|
341 |
// cvtools::writeMat(code1Binary.row(row), "code1Binary.mat", "code1Binary");
|
|
|
342 |
// }
|
|
|
343 |
|
|
|
344 |
// match and store
|
|
|
345 |
int i=0, j=0;
|
|
|
346 |
while(i<lineCenters0.size() && j<lineCenters1.size()){
|
|
|
347 |
|
|
|
348 |
if(lineCenters0[i][1] == lineCenters1[j][1]){
|
|
|
349 |
q0Rect.push_back(cv::Point2f(lineCenters0[i][0], row));
|
|
|
350 |
q1Rect.push_back(cv::Point2f(lineCenters1[j][0], row));
|
|
|
351 |
i += 1;
|
|
|
352 |
j += 1;
|
|
|
353 |
} else if(lineCenters0[i][1] < lineCenters1[j][1]){
|
|
|
354 |
i += 1;
|
|
|
355 |
} else if(lineCenters0[i][1] > lineCenters1[j][1]){
|
|
|
356 |
j += 1;
|
|
|
357 |
}
|
41 |
jakw |
358 |
}
|
126 |
jakw |
359 |
|
41 |
jakw |
360 |
}
|
|
|
361 |
}
|
|
|
362 |
|
63 |
jakw |
363 |
int nMatches = q0Rect.size();
|
|
|
364 |
|
|
|
365 |
if(nMatches < 1){
|
|
|
366 |
Q.resize(0);
|
|
|
367 |
color.resize(0);
|
|
|
368 |
|
|
|
369 |
return;
|
|
|
370 |
}
|
|
|
371 |
|
95 |
jakw |
372 |
// retrieve color information (at integer coordinates)
|
41 |
jakw |
373 |
color.resize(nMatches);
|
|
|
374 |
for(int i=0; i<nMatches; i++){
|
|
|
375 |
|
42 |
jakw |
376 |
cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
|
|
|
377 |
cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
|
95 |
jakw |
378 |
// cv::Vec3b c0 = getColorSubpix(color0Rect, q0Rect[i]);
|
|
|
379 |
// cv::Vec3b c1 = getColorSubpix(color1Rect, q0Rect[i]);
|
41 |
jakw |
380 |
|
44 |
jakw |
381 |
color[i] = 0.5*c0 + 0.5*c1;
|
41 |
jakw |
382 |
}
|
|
|
383 |
|
42 |
jakw |
384 |
// triangulate points
|
|
|
385 |
cv::Mat QMatHomogenous, QMat;
|
123 |
jakw |
386 |
|
42 |
jakw |
387 |
cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
|
|
|
388 |
cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
|
44 |
jakw |
389 |
|
95 |
jakw |
390 |
// undo rectifying rotation
|
44 |
jakw |
391 |
cv::Mat R0Inv;
|
|
|
392 |
cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
|
|
|
393 |
QMat = R0Inv*QMat;
|
|
|
394 |
|
42 |
jakw |
395 |
cvtools::matToPoints3f(QMat, Q);
|
44 |
jakw |
396 |
|
4 |
jakw |
397 |
}
|