为什么cv :: stereoRectify返回大小为(0,0)的ROI

时间:2017-12-19 07:59:10

标签: opencv stereo-3d calibration

我正在学习opencv并测试立体相机。在我的代码中,stereoRectify函数有时会返回其大小为(0,0)的ROI。 我无法解决这个问题。

Mat cameraMatrix[2], distCoeffs[2];
cameraMatrix[0] = initCameraMatrix2D(objectPoints, imagePoints_l, 
  imageSize, 0);
cameraMatrix[1] = initCameraMatrix2D(objectPoints, imagePoints_r, 
  imageSize, 0);

Mat R, T, E, F;
double rms = stereoCalibrate(objectPoints, imagePoints_l, imagePoints_r,
  cameraMatrix[0], distCoeffs[0],
  cameraMatrix[1], distCoeffs[1],
  imageSize, R, T, E, F,
  CALIB_FIX_ASPECT_RATIO +
  CALIB_ZERO_TANGENT_DIST +
  CALIB_USE_INTRINSIC_GUESS +
  CALIB_SAME_FOCAL_LENGTH +
  CALIB_RATIONAL_MODEL +
  CALIB_FIX_K3 + CALIB_FIX_K4 + CALIB_FIX_K5,
  TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 100, 1e-5));

Mat R1, R2, P1, P2, Q;
Rect validRoi[2];

stereoRectify(cameraMatrix[0], distCoeffs[0],
  cameraMatrix[1], distCoeffs[1],
  imageSize, R, T, R1, R2, P1, P2, Q,
  CALIB_ZERO_DISPARITY, 0.5, imageSize, &validRoi[0], &validRoi[1]);

cout << validRoi[0] << validRoi[1] << endl;

在终端输出:

[0 x 0 from (0, 0)][215 x 184 from (77, 19)]

或:

[0 x 0 from (0, 0)][0 x 0 from (0, 0)]

我还输出了相机矩阵和失真矩阵:

[204.3355426705726, 0, 193.3280616575545;
0, 189.8976931576431, 123.307676870377;
0, 0, 1]
[-0.5404320611827982, 0.3292134879059271, 0, 0, 0, 0, 0, 
0.1371856537119246, 0, 0, 0, 0, 0, 0]
[204.3355426705726, 0, 183.756931129648;
0, 189.8976931576431, 131.4500619272754;
0, 0, 1]
[-0.524923482702874, 0.3121043629068334, 0, 0, 0, 0, 0, 
0.1269780773557047, 0, 0, 0, 0, 0, 0]

3 个答案:

答案 0 :(得分:0)

stereoRectify函数可能返回无效的ROI。请发布完整的代码。

之前你做过立体声校准吗? 你是什​​么输入矩阵?摄像机矩阵是立体声校准的输出矩阵吗? 您是否尝试设置自由缩放参数aplha = 0?然后ROI涵盖整个图像。 你能否发布经过纠正的图片?

以下是OpenCV的完整StereoCalibration示例: https://github.com/opencv/opencv/blob/master/samples/cpp/stereo_calib.cpp

和StereoRectify的OpenCV文档: https://docs.opencv.org/2.4/modules/calib3d/doc/camera_calibration_and_3d_reconstruction.html#stereorectify

答案 1 :(得分:0)

首先检查你的立体声标志。看看你真正需要的OpenCV-Documentation标志。你可以在没有任何旗帜的情况下先试试。

您是否从立体声校准中检查了返回的rms值?良好的校准会使rms小于1。

你的图像是否得到了很好的纠正?您可以使用以下代码重新映射并显示它:

// remap
Mat rmap[2][2];
initUndistortRectifyMap(cameraMatrix1, distCoeffs1, R1, P1, imageSize, CV_16SC2, rmap[0][0], rmap[0][1]);
initUndistortRectifyMap(cameraMatrix2, distCoeffs2, R2, P2, imageSize, CV_16SC2, rmap[1][0], rmap[1][1]);
Mat out1, out2;
remap(img1, out1, rmap[0][0], rmap[0][1], CV_INTER_LINEAR);
remap(img2, out2, rmap[1][0], rmap[1][1], CV_INTER_LINEAR);
Mat result;
hconcat(out1, out2, result);

// draw horizontal line
for(int j = 0; j < result.rows; j += 16 ) {
    line(result, Point(0, j), Point(result.cols, j), Scalar(0, 255, 0), 1, 8);
}

// show
imshow("result", result);
waitKey();

答案 2 :(得分:0)

至于现在(31.03.2018,opencv 3.4.1),许多用户报告立体声纠正以返回无效结果:

https://github.com/opencv/opencv/issues/11131