简体   繁体   English

如何使用opencv纠正立体图像对?

[英]How to rectify a stereo image pair using opencv?

I have set up two cameras and calibrated them separately, after that I calibrated both using stereoCalibrate function. 我已经设置了两个摄像头并分别校准,之后我使用stereoCalibrate功能进行了校准。 Calibration seemed to work fine, as all returned reprojection errors are in the range of 0.4 to 0.5. 校准似乎工作正常,因为所有返回的重投影误差都在0.4到0.5的范围内。 Now i want to calculate a depth map out of the stereo image pair. 现在我想从立体图像对中计算出深度图。 As far as i could follow the tutorials, i first need to rectify my images, grayscale them and pass them to StereoBM (or any other matcher). 至于我可以按照教程,我首先需要纠正我的图像,灰度图像并将它们传递给StereoBM(或任何其他匹配器)。

As far as i understand the code below should rectify and show two live images of the cameras. 据我所知,下面的代码应该纠正并显示相机的两个实时图像。

... Load calibration matrices

//compute rectification
Mat R1, R2, P1, P2, Q;
stereoRectify(cameraMatrix_left, distortionCoefficients_left,
   cameraMatrix_right, distortionCoefficients_right,
   Size(left_camera.get(CAP_PROP_FRAME_WIDTH),
   left_camera.get(CAP_PROP_FRAME_HEIGHT)),
   R, T, R1, R2, P1, P2, Q, 0, 0.0,
   Size(left_camera.get(CAP_PROP_FRAME_WIDTH),
   left_camera.get(CAP_PROP_FRAME_HEIGHT)));

//compute undistortion
Mat rmap[2][2];
initUndistortRectifyMap(cameraMatrix_left, distortionCoefficients_left, R1, P1, Size(left_camera.get(CAP_PROP_FRAME_WIDTH), left_camera.get(CAP_PROP_FRAME_HEIGHT)), CV_16SC2, rmap[0][0], rmap[0][1]);
initUndistortRectifyMap(cameraMatrix_right, distortionCoefficients_right, R2, P2, Size(right_camera.get(CAP_PROP_FRAME_WIDTH), right_camera.get(CAP_PROP_FRAME_HEIGHT)), CV_16SC2, rmap[1][0], rmap[1][1]);

while (true) {
    if (!right_camera.read(capturedFrame_right))
        break;
    if (!left_camera.read(capturedFrame_left))
        break;

    remap(capturedFrame_left, drawFrame_left, rmap[0][0], rmap[0][1], INTER_LINEAR, BORDER_DEFAULT, Scalar());
    remap(capturedFrame_right, drawFrame_right, rmap[1][0], rmap[1][1], INTER_LINEAR, BORDER_DEFAULT, Scalar());

    cvtColor(drawFrame_left, grayScale_left, COLOR_RGB2GRAY);
    cvtColor(drawFrame_right, grayScale_right, COLOR_RGB2GRAY);

    imshow(RIGHT_CAMERA, grayScale_right);
    imshow(LEFT_CAMERA, grayScale_left);
}

I would expect that both images are rectified as shown on the documentation of stereoRectify . 我希望这两个图像都可以修正,如立体声录制的文档所示。

However, they are simply not. 但是,它们根本就不是。 There is a noticable vertical difference between both pictures. 两张图片之间存在明显的垂直差异。 What did i miss? 我错过了什么?

You need to check your calibration is correct. 您需要检查校准是否正确。 Proper calibration matrix are loaded for that camera, it should provide a rectified image with the above code. 为该摄像机加载适当的校准矩阵,它应该提供具有上述代码的校正图像。

There were guidelines to do stereo calibration in the internet. 有指导在互联网上进行立体声校准。 Few of them listed here. 其中很少有人列在这里。

  1. Specific distance of the calibration pattern from the camera shouldn't really matter. 校准图案与相机的特定距离应该不重要。 Calibration must be performed with fixed focus. 必须使用固定焦点进行校准。
  2. Having a checkerboard with more number of squares is beneficial as there are more corner points to extract. 具有更多正方形的棋盘是有益的,因为有更多的角点要提取。 This enables us to get more 3D-2D point correspondences (Size of square shouldn't make a difference). 这使我们能够获得更多的3D-2D点对应关系(正方形的大小不应该有所不同)。
  3. Different points of views and angles needed. 需要不同的观点和角度。 Calibration detects focal length and distortion parameters by least square method, for which different angles of the checkerboard are needed to obtain a better solution. 校准通过最小二乘法检测焦距和失真参数,为此需要不同角度的棋盘以获得更好的解决方案。
  4. Enough wide border around the calibration pattern is required. 校准图案周围需要足够宽的边框。
  5. A large pattern is better for cameras with larger baseline, as stereo overlap is lesser. 对于具有较大基线的相机,大图案更好,因为立体重叠较小。
  6. Some calibration toolboxes require the input of checkerboard dimensions to be either odd number x even number (eg 9x6) or vice versa (eg 8x7). 一些校准工具箱要求棋盘尺寸的输入为奇数x偶数(例如9×6)或反之(例如8×7)。
  7. Preferably the checkerboard should cover at least half of the area of the image. 优选地,棋盘应覆盖图像区域的至少一半。
  8. The calibration pattern must be moved to cover the entire FOV of the camera to obtain better calibration.Skew helps in focal length determination and moving the checkerboard towards the corners helps in determining the distortion co coefficients. 必须移动校准图案以覆盖相机的整个FOV以获得更好的校准.Skew有助于焦距确定并且将棋盘朝向角移动有助于确定失真系数。
  9. Good lighting conditions is of at most importance and is often overlooked. 良好的照明条件至关重要,往往被忽视。
  10. Some sources say circles are easier to localize than corner points and using a circles pattern might lead to better calibration. 一些消息来源称圆圈比角点更容易定位,使用圆形图案可能会带来更好的校准。
  11. Humidity changes might affect normal paper that is used as it absorbs moisture. 湿度变化可能会影响正常纸张,因为它吸收水分。 Thick paper must be used and the calibration pattern must be printed using a Laser printer and should be stuck on a glass backing preferably. 必须使用厚纸,并且必须使用激光打印机打印校准图案,并且最好将其粘在玻璃背衬上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM