Line 150... |
Line 150... |
150 |
assert(frames1.size() == N);
|
150 |
assert(frames1.size() == N);
|
151 |
|
151 |
|
152 |
int frameRows = frames0[0].rows;
|
152 |
int frameRows = frames0[0].rows;
|
153 |
int frameCols = frames0[0].cols;
|
153 |
int frameCols = frames0[0].cols;
|
154 |
|
154 |
|
- |
|
155 |
// Rectifying homographies (rotation+projections)
|
- |
|
156 |
cv::Size frameSize(frameCols, frameRows);
|
- |
|
157 |
cv::Mat R, T;
|
- |
|
158 |
// stereoRectify segfaults unless R is double precision
|
- |
|
159 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
- |
|
160 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
- |
|
161 |
cv::Mat R0, R1, P0, P1, QRect;
|
- |
|
162 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
- |
|
163 |
|
- |
|
164 |
// Interpolation maps (lens distortion and rectification)
|
- |
|
165 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
- |
|
166 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
- |
|
167 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
- |
|
168 |
|
- |
|
169 |
|
155 |
// Gray-scale everything
|
170 |
// Gray-scale and remap
|
156 |
std::vector<cv::Mat> frames0Gray(N);
|
171 |
std::vector<cv::Mat> frames0Rect(N);
|
157 |
std::vector<cv::Mat> frames1Gray(N);
|
172 |
std::vector<cv::Mat> frames1Rect(N);
|
158 |
for(unsigned int i=0; i<N; i++){
|
173 |
for(unsigned int i=0; i<N; i++){
|
- |
|
174 |
cv::Mat temp;
|
159 |
cv::cvtColor(frames0[i], frames0Gray[i], CV_BayerBG2GRAY);
|
175 |
cv::cvtColor(frames0[i], temp, CV_BayerBG2GRAY);
|
- |
|
176 |
cv::remap(temp, frames0Rect[i], map0X, map0Y, CV_INTER_LINEAR);
|
160 |
cv::cvtColor(frames1[i], frames1Gray[i], CV_BayerBG2GRAY);
|
177 |
cv::cvtColor(frames1[i], temp, CV_BayerBG2GRAY);
|
- |
|
178 |
cv::remap(temp, frames1Rect[i], map1X, map1Y, CV_INTER_LINEAR);
|
161 |
}
|
179 |
}
|
162 |
|
180 |
|
163 |
// Decode camera0
|
181 |
// Decode camera0
|
164 |
std::vector<cv::Mat> frames0Primary(frames0Gray.begin()+2, frames0Gray.begin()+2+nStepsPrimary);
|
182 |
std::vector<cv::Mat> frames0Primary(frames0Rect.begin()+2, frames0Rect.begin()+2+nStepsPrimary);
|
165 |
std::vector<cv::Mat> frames0Secondary(frames0Gray.begin()+2+nStepsPrimary, frames0Gray.end()-nStepsTertiary);
|
183 |
std::vector<cv::Mat> frames0Secondary(frames0Rect.begin()+2+nStepsPrimary, frames0Rect.end()-nStepsTertiary);
|
166 |
std::vector<cv::Mat> frames0Tertiary(frames0Gray.end()-nStepsTertiary, frames0Gray.end());
|
184 |
std::vector<cv::Mat> frames0Tertiary(frames0Rect.end()-nStepsTertiary, frames0Rect.end());
|
167 |
|
185 |
|
168 |
std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
|
186 |
std::vector<cv::Mat> F0Primary = getDFTComponents(frames0Primary);
|
169 |
cv::Mat up0Primary;
|
187 |
cv::Mat up0Primary;
|
170 |
cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
|
188 |
cv::phase(F0Primary[2], -F0Primary[3], up0Primary);
|
171 |
std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
|
189 |
std::vector<cv::Mat> F0Secondary = getDFTComponents(frames0Secondary);
|
Line 180... |
Line 198... |
180 |
up0 *= screenCols/(2.0*pi);
|
198 |
up0 *= screenCols/(2.0*pi);
|
181 |
cv::Mat amplitude0;
|
199 |
cv::Mat amplitude0;
|
182 |
cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
|
200 |
cv::magnitude(F0Primary[2], -F0Primary[3], amplitude0);
|
183 |
|
201 |
|
184 |
// Decode camera1
|
202 |
// Decode camera1
|
185 |
std::vector<cv::Mat> frames1Primary(frames1Gray.begin()+2, frames1Gray.begin()+2+nStepsPrimary);
|
203 |
std::vector<cv::Mat> frames1Primary(frames1Rect.begin()+2, frames1Rect.begin()+2+nStepsPrimary);
|
186 |
std::vector<cv::Mat> frames1Secondary(frames1Gray.begin()+2+nStepsPrimary, frames1Gray.end()-nStepsTertiary);
|
204 |
std::vector<cv::Mat> frames1Secondary(frames1Rect.begin()+2+nStepsPrimary, frames1Rect.end()-nStepsTertiary);
|
187 |
std::vector<cv::Mat> frames1Tertiary(frames1Gray.end()-nStepsTertiary, frames1Gray.end());
|
205 |
std::vector<cv::Mat> frames1Tertiary(frames1Rect.end()-nStepsTertiary, frames1Rect.end());
|
188 |
|
206 |
|
189 |
std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
|
207 |
std::vector<cv::Mat> F1Primary = getDFTComponents(frames1Primary);
|
190 |
cv::Mat up1Primary;
|
208 |
cv::Mat up1Primary;
|
191 |
cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
|
209 |
cv::phase(F1Primary[2], -F1Primary[3], up1Primary);
|
192 |
std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
|
210 |
std::vector<cv::Mat> F1Secondary = getDFTComponents(frames1Secondary);
|
Line 208... |
Line 226... |
208 |
//cvtools::writeMat(up0Unwrap, "up0Unwrap.mat", "up0Unwrap");
|
226 |
//cvtools::writeMat(up0Unwrap, "up0Unwrap.mat", "up0Unwrap");
|
209 |
//cvtools::writeMat(up0, "up0.mat", "up0");
|
227 |
//cvtools::writeMat(up0, "up0.mat", "up0");
|
210 |
//cvtools::writeMat(up1, "up1.mat", "up1");
|
228 |
//cvtools::writeMat(up1, "up1.mat", "up1");
|
211 |
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
|
229 |
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
|
212 |
|
230 |
|
213 |
// Rectifying homographies (rotation+projections)
|
- |
|
214 |
cv::Size frameSize(frameCols, frameRows);
|
231 |
//cvtools::writeMat(amplitude0, "amplitude0.mat", "amplitude0");
|
215 |
cv::Mat R, T;
|
- |
|
216 |
// stereoRectify segfaults unless R is double precision
|
- |
|
217 |
cv::Mat(calibration.R1).convertTo(R, CV_64F);
|
- |
|
218 |
cv::Mat(calibration.T1).convertTo(T, CV_64F);
|
232 |
//cvtools::writeMat(amplitude1, "amplitude1.mat", "amplitude1");
|
219 |
cv::Mat R0, R1, P0, P1, QRect;
|
- |
|
220 |
cv::stereoRectify(calibration.K0, calibration.k0, calibration.K1, calibration.k1, frameSize, R, T, R0, R1, P0, P1, QRect, 0);
|
- |
|
221 |
|
233 |
|
222 |
// Interpolation maps (lens distortion and rectification)
|
234 |
// Color debayer and remap
|
223 |
cv::Mat map0X, map0Y, map1X, map1Y;
|
235 |
cv::Mat color0, color1;
|
224 |
cv::initUndistortRectifyMap(calibration.K0, calibration.k0, R0, P0, frameSize, CV_32F, map0X, map0Y);
|
236 |
cv::cvtColor(frames0[0], color0, CV_BayerBG2RGB);
|
225 |
cv::initUndistortRectifyMap(calibration.K1, calibration.k1, R1, P1, frameSize, CV_32F, map1X, map1Y);
|
237 |
cv::cvtColor(frames1[0], color1, CV_BayerBG2RGB);
|
226 |
|
238 |
|
227 |
// Phase remaps
|
- |
|
228 |
cv::Mat up0Rect, up1Rect;
|
- |
|
229 |
cv::remap(up0, up0Rect, map0X, map0Y, CV_INTER_LINEAR);
|
- |
|
230 |
cv::remap(up1, up1Rect, map1X, map1Y, CV_INTER_LINEAR);
|
- |
|
231 |
|
- |
|
232 |
// amplitude remaps
|
- |
|
233 |
cv::Mat amplitude0Rect, amplitude1Rect;
|
- |
|
234 |
cv::remap(amplitude0, amplitude0Rect, map0X, map0Y, CV_INTER_LINEAR);
|
- |
|
235 |
cv::remap(amplitude1, amplitude1Rect, map1X, map1Y, CV_INTER_LINEAR);
|
- |
|
236 |
|
- |
|
237 |
//cvtools::writeMat(amplitude0Rect, "amplitude0Rect.mat", "amplitude0Rect");
|
- |
|
238 |
//cvtools::writeMat(amplitude1Rect, "amplitude1Rect.mat", "amplitude1Rect");
|
- |
|
239 |
|
- |
|
240 |
// color debayer and remap
|
- |
|
241 |
cv::Mat color0Rect, color1Rect;
|
- |
|
242 |
// frames0[0].convertTo(color0Rect, CV_8UC1, 1.0/256.0);
|
- |
|
243 |
cv::cvtColor(frames0[0], color0Rect, CV_BayerBG2RGB);
|
- |
|
244 |
cv::remap(color0Rect, color0Rect, map0X, map0Y, CV_INTER_LINEAR);
|
- |
|
245 |
|
- |
|
246 |
// frames1[0].convertTo(color1Rect, CV_8UC1, 1.0/256.0);
|
- |
|
247 |
cv::cvtColor(frames1[0], color1Rect, CV_BayerBG2RGB);
|
- |
|
248 |
cv::remap(color1Rect, color1Rect, map1X, map1Y, CV_INTER_LINEAR);
|
- |
|
249 |
|
- |
|
250 |
//cvtools::writeMat(frames0Rect[18], "frames0Rect_18.mat", "frames0Rect_18");
|
- |
|
251 |
//cvtools::writeMat(frames0Rect[19], "frames0Rect_19.mat", "frames0Rect_19");
|
- |
|
252 |
|
- |
|
253 |
//cvtools::writeMat(color0Rect, "color0Rect.mat", "color0Rect");
|
239 |
//cvtools::writeMat(color0, "color0.mat", "color0");
|
254 |
//cvtools::writeMat(color1Rect, "color1Rect.mat", "color1Rect");
|
240 |
//cvtools::writeMat(color1, "color1.mat", "color1");
|
255 |
|
- |
|
256 |
// On/off remaps
|
- |
|
257 |
cv::Mat frames0OnRect, frames0OffRect;
|
- |
|
258 |
cv::remap(frames0Gray[0], frames0OnRect, map0X, map0Y, CV_INTER_LINEAR);
|
- |
|
259 |
cv::remap(frames0Gray[1], frames0OffRect, map0X, map0Y, CV_INTER_LINEAR);
|
- |
|
260 |
|
- |
|
261 |
cv::Mat frames1OnRect, frames1OffRect;
|
- |
|
262 |
cv::remap(frames1Gray[0], frames1OnRect, map1X, map1Y, CV_INTER_LINEAR);
|
- |
|
263 |
cv::remap(frames1Gray[1], frames1OffRect, map1X, map1Y, CV_INTER_LINEAR);
|
- |
|
264 |
|
241 |
|
265 |
// Occlusion masks
|
242 |
// Occlusion masks
|
266 |
cv::Mat occlusion0Rect, occlusion1Rect;
|
243 |
cv::Mat occlusion0, occlusion1;
|
267 |
cv::subtract(frames0OnRect, frames0OffRect, occlusion0Rect);
|
244 |
cv::subtract(frames0Rect[0], frames0Rect[1], occlusion0);
|
268 |
occlusion0Rect = (occlusion0Rect > 5) & (occlusion0Rect < 250);
|
245 |
occlusion0 = (occlusion0 > 5) & (occlusion0 < 250);
|
269 |
cv::subtract(frames1OnRect, frames1OffRect, occlusion1Rect);
|
246 |
cv::subtract(frames1Rect[0], frames1Rect[1], occlusion1);
|
270 |
occlusion1Rect = (occlusion1Rect > 5) & (occlusion1Rect < 250);
|
247 |
occlusion1 = (occlusion1 > 5) & (occlusion1 < 250);
|
271 |
|
248 |
|
272 |
// // Threshold on energy at primary frequency
|
249 |
// // Threshold on energy at primary frequency
|
273 |
// occlusion0Rect = occlusion0Rect & (amplitude0Rect > 5.0*nStepsPrimary);
|
250 |
// occlusion0 = occlusion0 & (amplitude0 > 5.0*nStepsPrimary);
|
274 |
// occlusion1Rect = occlusion1Rect & (amplitude1Rect > 5.0*nStepsPrimary);
|
251 |
// occlusion1 = occlusion1 & (amplitude1 > 5.0*nStepsPrimary);
|
275 |
|
252 |
|
276 |
//cvtools::writeMat(occlusion0Rect, "occlusion0Rect.mat", "occlusion0Rect");
|
253 |
//cvtools::writeMat(occlusion0, "occlusion0.mat", "occlusion0");
|
277 |
//cvtools::writeMat(occlusion1Rect, "occlusion1Rect.mat", "occlusion1Rect");
|
254 |
//cvtools::writeMat(occlusion1, "occlusion1.mat", "occlusion1");
|
278 |
|
255 |
|
279 |
// // Erode occlusion masks
|
256 |
// // Erode occlusion masks
|
280 |
// cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
|
257 |
// cv::Mat strel = cv::getStructuringElement(cv::MORPH_ELLIPSE, cv::Size(5,5));
|
281 |
// cv::erode(occlusion0Rect, occlusion0Rect, strel);
|
258 |
// cv::erode(occlusion0, occlusion0, strel);
|
282 |
// cv::erode(occlusion1Rect, occlusion1Rect, strel);
|
259 |
// cv::erode(occlusion1, occlusion1, strel);
|
283 |
|
260 |
|
284 |
// Threshold on gradient of phase
|
261 |
// Threshold on gradient of phase
|
285 |
cv::Mat edges0;
|
262 |
cv::Mat edges0;
|
286 |
cv::Sobel(up0Rect, edges0, -1, 1, 1, 5);
|
263 |
cv::Sobel(up0, edges0, -1, 1, 1, 5);
|
287 |
occlusion0Rect = occlusion0Rect & (abs(edges0) < 150);
|
264 |
occlusion0 = occlusion0 & (abs(edges0) < 150);
|
288 |
cv::Mat edges1;
|
265 |
cv::Mat edges1;
|
289 |
cv::Sobel(up1Rect, edges1, -1, 1, 1, 5);
|
266 |
cv::Sobel(up1, edges1, -1, 1, 1, 5);
|
290 |
occlusion1Rect = occlusion1Rect & (abs(edges1) < 150);
|
267 |
occlusion1 = occlusion1 & (abs(edges1) < 150);
|
291 |
|
268 |
|
292 |
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
|
269 |
//cvtools::writeMat(edges0, "edges0.mat", "edges0");
|
293 |
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
|
270 |
//cvtools::writeMat(edges1, "edges1.mat", "edges1");
|
294 |
|
271 |
|
295 |
// Match phase maps
|
272 |
// Match phase maps
|
296 |
int frameRectRows = map0X.rows;
|
273 |
int frameRectRows = map0X.rows;
|
297 |
int frameRectCols = map0X.cols;
|
274 |
int frameRectCols = map0X.cols;
|
298 |
|
275 |
|
299 |
// camera0 against camera1
|
276 |
// camera0 against camera1
|
300 |
std::vector<cv::Vec2f> q0Rect, q1Rect;
|
277 |
std::vector<cv::Vec2f> q0, q1;
|
301 |
for(int row=0; row<frameRectRows; row++){
|
278 |
for(int row=0; row<frameRectRows; row++){
|
302 |
for(int col=0; col<frameRectCols; col++){
|
279 |
for(int col=0; col<frameRectCols; col++){
|
303 |
|
280 |
|
304 |
if(!occlusion0Rect.at<char>(row,col))
|
281 |
if(!occlusion0.at<char>(row,col))
|
305 |
continue;
|
282 |
continue;
|
306 |
|
283 |
|
307 |
float up0i = up0Rect.at<float>(row,col);
|
284 |
float up0i = up0.at<float>(row,col);
|
308 |
for(int col1=0; col1<up1Rect.cols-1; col1++){
|
285 |
for(int col1=0; col1<up1.cols-1; col1++){
|
309 |
|
286 |
|
310 |
if(!occlusion1Rect.at<char>(row,col1) || !occlusion1Rect.at<char>(row,col1+1))
|
287 |
if(!occlusion1.at<char>(row,col1) || !occlusion1.at<char>(row,col1+1))
|
311 |
continue;
|
288 |
continue;
|
312 |
|
289 |
|
313 |
float up1Left = up1Rect.at<float>(row,col1);
|
290 |
float up1Left = up1.at<float>(row,col1);
|
314 |
float up1Right = up1Rect.at<float>(row,col1+1);
|
291 |
float up1Right = up1.at<float>(row,col1+1);
|
315 |
|
292 |
|
316 |
if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1.0) && (up1Right-up0i < 1.0)){
|
293 |
if((up1Left <= up0i) && (up0i <= up1Right) && (up0i-up1Left < 1.0) && (up1Right-up0i < 1.0)){
|
317 |
|
294 |
|
318 |
float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
|
295 |
float col1i = col1 + (up0i-up1Left)/(up1Right-up1Left);
|
319 |
|
296 |
|
320 |
q0Rect.push_back(cv::Point2f(col, row));
|
297 |
q0.push_back(cv::Point2f(col, row));
|
321 |
q1Rect.push_back(cv::Point2f(col1i, row));
|
298 |
q1.push_back(cv::Point2f(col1i, row));
|
322 |
|
299 |
|
323 |
break;
|
300 |
break;
|
324 |
}
|
301 |
}
|
325 |
}
|
302 |
}
|
326 |
}
|
303 |
}
|
327 |
}
|
304 |
}
|
328 |
|
305 |
|
329 |
// // camera1 against camera0
|
- |
|
330 |
// for(int row=0; row<frameRectRows; row++){
|
- |
|
331 |
// for(int col=0; col<frameRectCols; col++){
|
- |
|
332 |
|
- |
|
333 |
// if(!occlusion1Rect.at<char>(row,col))
|
- |
|
334 |
// continue;
|
- |
|
335 |
|
- |
|
336 |
// float up1i = up1Rect.at<float>(row,col);
|
- |
|
337 |
// for(int col0=0; col0<up0Rect.cols-1; col0++){
|
- |
|
338 |
|
- |
|
339 |
// if(!occlusion0Rect.at<char>(row,col0) || !occlusion0Rect.at<char>(row,col0+1))
|
- |
|
340 |
// continue;
|
- |
|
341 |
|
- |
|
342 |
// float up0Left = up0Rect.at<float>(row,col0);
|
- |
|
343 |
// float up0Right = up0Rect.at<float>(row,col0+1);
|
- |
|
344 |
|
- |
|
345 |
// if((up0Left <= up1i) && (up1i <= up0Right) && (up1i-up0Left < 1) && (up0Right-up1i < 1)){
|
- |
|
346 |
|
- |
|
347 |
// float col0i = col0 + (up1i-up0Left)/(up0Right-up0Left);
|
- |
|
348 |
|
- |
|
349 |
// q1Rect.push_back(cv::Point2f(col, row));
|
- |
|
350 |
// q0Rect.push_back(cv::Point2f(col0i, row));
|
- |
|
351 |
|
- |
|
352 |
// break;
|
- |
|
353 |
// }
|
- |
|
354 |
// }
|
- |
|
355 |
// }
|
- |
|
356 |
// }
|
- |
|
357 |
|
- |
|
358 |
int nMatches = q0Rect.size();
|
306 |
int nMatches = q0.size();
|
359 |
|
307 |
|
360 |
if(nMatches < 1){
|
308 |
if(nMatches < 1){
|
361 |
Q.resize(0);
|
309 |
Q.resize(0);
|
362 |
color.resize(0);
|
310 |
color.resize(0);
|
363 |
|
311 |
|
Line 366... |
Line 314... |
366 |
|
314 |
|
367 |
// Retrieve color information
|
315 |
// Retrieve color information
|
368 |
color.resize(nMatches);
|
316 |
color.resize(nMatches);
|
369 |
for(int i=0; i<nMatches; i++){
|
317 |
for(int i=0; i<nMatches; i++){
|
370 |
|
318 |
|
371 |
cv::Vec3b c0 = color0Rect.at<cv::Vec3b>(q0Rect[i][1], q0Rect[i][0]);
|
319 |
cv::Vec3b c0 = color0.at<cv::Vec3b>(q0[i][1], q0[i][0]);
|
372 |
cv::Vec3b c1 = color1Rect.at<cv::Vec3b>(q1Rect[i][1], q1Rect[i][0]);
|
320 |
cv::Vec3b c1 = color1.at<cv::Vec3b>(q1[i][1], q1[i][0]);
|
373 |
|
321 |
|
374 |
color[i] = 0.5*c0 + 0.5*c1;
|
322 |
color[i] = 0.5*c0 + 0.5*c1;
|
375 |
}
|
323 |
}
|
376 |
|
324 |
|
377 |
// Triangulate points
|
325 |
// Triangulate points
|
378 |
cv::Mat QMatHomogenous, QMat;
|
326 |
cv::Mat QMatHomogenous, QMat;
|
379 |
cv::triangulatePoints(P0, P1, q0Rect, q1Rect, QMatHomogenous);
|
327 |
cv::triangulatePoints(P0, P1, q0, q1, QMatHomogenous);
|
380 |
cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
|
328 |
cvtools::convertMatFromHomogeneous(QMatHomogenous, QMat);
|
381 |
|
329 |
|
382 |
// Undo rectification
|
330 |
// Undo rectification
|
383 |
cv::Mat R0Inv;
|
331 |
cv::Mat R0Inv;
|
384 |
cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
|
332 |
cv::Mat(R0.t()).convertTo(R0Inv, CV_32F);
|