博客
关于我
双目测距 实践记录
阅读量:205 次
发布时间:2019-02-28

本文共 4881 字,大约阅读时间需要 16 分钟。

双目相机标定与深度计算方法

前言

双目相机标定是一项重要的前沿技术,广泛应用于机器人、自动驾驶和3D重建等领域。该方法通过同时捕捉左右两摄像头的图像,利用几何和光学知识,计算出相机的内外参数,并最终生成深度图。本文将详细介绍双目相机标定的实现过程及深度计算方法。

原理介绍

双目相机标定基于几何学和光学学的原理。通过捕捉左右两摄像头的图像,计算出两摄像头的内外参数,进而求解两个相机的相对位置和方向。这种方法通常采用标定板作为校准目标,通过识别标定板上的角点,计算出相机的内参数(如焦距、中心点)和畸变参数。

实现教程

一、左右摄像头同时拍照并保存于本地

  • 固定好左右相机,使用棋盘标定图进行拍摄。建议左右相机各拍摄15张图像,总图像数为30-40张。
  • 编写相应的程序,例如使用C++或Python,调用摄像头并保存图像。以下是一个示例代码:
  • #include 
    #include
    #include
    #include
    using namespace std;
    using namespace cv;
    // 单次拍摄并保存图像
    void captureImages() {
    Mat frame, frame1;
    bool stop = false;
    while (!stop) {
    cap.read(frame);
    cap1.read(frame1);
    imshow("camera0", frame);
    imshow("camera1", frame1);
    int delay = 30;
    if (delay >= 0 && waitKey(delay) >= 0) {
    waitKey(0);
    }
    imwrite("left1.jpg", frame1);
    imwrite("right1.jpg", frame);
    }
    cap.release();
    cap1.release();
    }

    二、单目标定

    单目标定是双目标定的基础步骤。通过单目标定计算出每个相机的内参数。以下是一个单目标定代码示例:

    #include "opencv2/core/core.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/calib3d/calib3d.hpp"
    #include "opencv2/highgui/highgui.hpp"
    using namespace std;
    using namespace cv;
    // 单目相机标定
    void guessCameraParam() {
    // 相机内参数
    intrinsic.create(3, 3, CV_64FC1);
    distortion_coeff.create(5, 1, CV_64FC1);
    // 示例内参数值(需根据实际相机测量得出)
    intrinsic.at
    (0, 0) = 256.8093262; // fx
    intrinsic.at
    (0, 2) = 160.2826538; // cx
    intrinsic.at
    (1, 1) = 254.7511139; // fy
    intrinsic.at
    (1, 2) = 127.6264572; // cy
    // 示例畸变参数(需根据实际相机测量得出)
    distortion_coeff.at
    (0, 0) = -0.193740; // k1
    distortion_coeff.at
    (1, 0) = -0.378588; // k2 distortion_coeff.at
    (2, 0) = 0.028980; // p1 distortion_coeff.at
    (3, 0) = 0.008136; // p2 }

    三、双目标定

    双目标定是基于单目标定的基础上,结合左右两个相机的数据,计算出两相机的相对位置和外参数。以下是一个双目标定代码示例:

    #include "opencv2/core/core.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/calib3d/calib3d.hpp"
    #include "opencv2/highgui/highgui.hpp"
    using namespace std;
    using namespace cv;
    // 双目相机标定
    void stereoCalibrate() {
    // 输入参数
    vector
    objPoints; // 标定板实际坐标
    vector
    leftPoints; // 左相机角点坐标
    vector
    rightPoints; // 右相机角点坐标
    // 输出参数
    Mat cameraMatrixL, cameraMatrixR; // 相机内参数矩阵
    Mat distCoeffL, distCoeffR; // 相机畸变参数
    vector
    rvecs, tvecs; // 旋转向量和平移向量
    // 示例参数值(需根据实际测量得出)
    cameraMatrixL.create(3, 3, CV_64FC1);
    cameraMatrixR.create(3, 3, CV_64FC1);
    distCoeffL.create(5, 1, CV_64FC1);
    distCoeffR.create(5, 1, CV_64FC1);
    // 示例内参数值(需根据实际相机测量得出)
    cameraMatrixL.at
    (0, 0) = 462.279595; // fx
    cameraMatrixL.at
    (1, 1) = 460.220741; // fy cameraMatrixL.at
    (0, 2) = 312.781587; // cx cameraMatrixL.at
    (1, 2) = 208.225803; // cy // 示例畸变参数(需根据实际相机测量得出) distCoeffL.at
    (0, 0) = -0.054929; // k1 distCoeffL.at
    (1, 0) = 0.224509; // k2 distCoeffL.at
    (2, 0) = 0.000386; // p1 distCoeffL.at
    (3, 0) = 0.001799; // p2 distCoeffL.at
    (4, 0) = -0.302288; // k3 // 示例右相机参数 cameraMatrixR.at
    (0, 0) = 463.923124; // fx cameraMatrixR.at
    (1, 1) = 462.203276; // fy cameraMatrixR.at
    (0, 2) = 322.783959; // cx cameraMatrixR.at
    (1, 2) = 256.100655; // cy // 示例畸变参数(需根据实际相机测量得出) distCoeffR.at
    (0, 0) = -0.049056; // k1 distCoeffR.at
    (1, 0) = 0.229945; // k2 distCoeffR.at
    (2, 0) = 0.001745; // p1 distCoeffR.at
    (3, 0) = -0.001862; // p2 distCoeffR.at
    (4, 0) = -0.321533; // k3 // 标定板实际坐标(需根据实际标定板得出) for (int i = 0; i < 15; i++) { Point3f point(i * 25, i * 25, 0); objPoints.push_back(point); } }

    四、深度计算

    通过双目标定得到的外参数和深度函数,可以计算出深度图。以下是一个深度计算代码示例:

    import numpy as np
    import cv2
    # 假设已经完成双目标定,得到了Q矩阵
    Q = np.array([
    [1, 0, 0, 0],
    [0, 1, 0, 0],
    [0, 0, 1, 0]
    ], dtype=np.float32)
    # 读取左右两摄像头图像
    left_img = cv2.imread("left.jpg")
    right_img = cv2.imread("right.jpg")
    # remap图像到校正后的图像
    rectified_left = cv2.remap(left_img, camera_configs.left_map1, camera_configs.left_map2, cv2.INTER_LINEAR)
    rectified_right = cv2.remap(right_img, camera_configs.right_map1, camera_configs.right_map2, cv2.INTER_LINEAR)
    # 将图像转换为灰度格式
    gray_left = cv2.cvtColor(rectified_left, cv2.COLOR_BGR2GRAY)
    gray_right = cv2.cvtColor(rectified_right, cv2.COLOR_BGR2GRAY)
    # 调节trackbar参数
    num = cv2.getTrackbarPos("num", "depth")
    blockSize = cv2.getTrackbarPos("blockSize", "depth")
    # 计算深度图
    stereo = cv2.StereoBM_create(numDisparities=16 * num, blockSize=blockSize)
    disparity = stereo.compute(gray_left, gray_right)
    # 调整深度图
    disp = cv2.normalize(disparity, disparity, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U)
    # 生成深度图像
    depth = cv2.reprojectImageTo3D(disparity.astype(np.float32) / 16., Q)
    # 显示结果
    cv2.imshow("depth", disparity)
    cv2.waitKey(1)
    # 释放资源
    camera1.release()
    camera2.release()
    cv2.destroyAllWindows()

    总结

    通过以上步骤,可以实现双目相机标定并生成深度图。虽然精度有限,但在实际应用中已经足够使用。

    转载地址:http://agpi.baihongyu.com/

    你可能感兴趣的文章
    Mysql 学习总结(88)—— Mysql 官方为什么不推荐用雪花 id 和 uuid 做 MySQL 主键
    查看>>
    Mysql 学习总结(89)—— Mysql 库表容量统计
    查看>>
    mysql 实现主从复制/主从同步
    查看>>
    mysql 审核_审核MySQL数据库上的登录
    查看>>
    mysql 导入 sql 文件时 ERROR 1046 (3D000) no database selected 错误的解决
    查看>>
    mysql 导入导出大文件
    查看>>
    MySQL 导出数据
    查看>>
    mysql 将null转代为0
    查看>>
    mysql 常用
    查看>>
    MySQL 常用列类型
    查看>>
    mysql 常用命令
    查看>>
    Mysql 常见ALTER TABLE操作
    查看>>
    MySQL 常见的 9 种优化方法
    查看>>
    MySQL 常见的开放性问题
    查看>>
    Mysql 常见错误
    查看>>
    mysql 常见问题
    查看>>
    MYSQL 幻读(Phantom Problem)不可重复读
    查看>>
    mysql 往字段后面加字符串
    查看>>
    mysql 快照读 幻读_innodb当前读 与 快照读 and rr级别是否真正避免了幻读
    查看>>
    MySQL 快速创建千万级测试数据
    查看>>