基于OpenCV库实现双目测量系统外参标定过程。通过分析双目测量系统左右相机拍摄的棋盘格标定板图像,包括角点检测、立体标定、立体校正和畸变校正的步骤,获取左右相机的相对位置关系和姿态。
a.检测每张图像中的棋盘格角点,并进行亚像素级精确化;根据左右相机的角点坐标、三维坐标、内参矩阵和畸变系数,计算旋转矩阵R和平移向量T,利用最小二乘法最小化重投影误差;根据标定结果对图像进行畸变校正。
b.输出为双目测量系统的标定结果,包括旋转矩阵、平移向量、投影矩阵和重投影矩阵,并通过imshow展示校正后的图像和视差图,以评估标定质量。
// 单目内参标定后进行双目标定
#include<opencv2/calib3d/calib3d.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/opencv.hpp>
#include<iostream>
#include <opencv2/core/utils/logger.hpp> //隐藏日志using namespace std;
using namespace cv;int n = 1;
int m = 1;int i = 1;
int j = 1;const int imagewidth = 2048;
const int imagehight = 2048;//图像的长宽
const int boardwidth = 11;
const int boardhight = 8;//图片上棋盘格标定板的内角点个数(行、列的角点数)
const int framenumber = 13;//标定图片数
const int squaresize = 50;//棋盘格标定板单个方格的大小
const cv::Size imagesize = cv::Size(imagewidth, imagehight);
const cv::Size boardsize = cv::Size(boardwidth, boardhight);vector<cv::Point3f> objectpoint;
vector<vector<cv::Point3f>> objpoint;vector<cv::Point2f> cornerL;
vector<cv::Point2f> cornerR;vector<vector<cv::Point2f>> imagepointL;
vector<vector<cv::Point2f>> imagepointR;Mat cameraMatrixL = (Mat_<double>(3, 3) << 2946.368631790921, 0, 992.3628859436697,0, 2947.324578115115, 1041.333927112967,0, 0, 1);
Mat distcoeffL = (Mat_<double>(5, 1) << -0.09314094645325596, 0.212990743663531, 0.001099206303953861, 0.0003299289802971191, 2.911155272910228);
Mat cameraMatrixR = (Mat_<double>(3, 3) << 2935.765597816497, 0, 997.8971330325958,0, 2938.133144049969, 1001.295460381107,0, 0, 1);
//获得的畸变参数
Mat distcoeffR = (Mat_<double>(5, 1) << -0.07849722399107049, -0.05592478850461548, -0.0009348860866305323, 8.96474606278152e-06, 4.375424240166232);cv::Mat R, T, E, F;
cv::Mat R1, R2, P1, P2, Q;cv::Mat maplx, maply, maprx, mapry;cv::Mat imageL, grayimageL;
cv::Mat imageR, grayimageR;cv::Rect validROIL, validROIR;void worldpoint()
{for (int i = 0; i < boardhight; i++){for (int j = 0; j < boardwidth; j++){objectpoint.push_back(cv::Point3f(i * squaresize, j * squaresize, 0.0f));}}for (int w = 1; w <= framenumber; w++){objpoint.push_back(objectpoint);}
}void outputparam()
{cv::FileStorage fs("intrinsics.yml", cv::FileStorage::WRITE);fs << "cameraMatrixL" << cameraMatrixL << "cameraDistcoeffL" << distcoeffL << "cameraMatrixR" << cameraMatrixR << "cameraDistcoeffR" << distcoeffR;fs.release();std::cout << "cameraMatrixL=:" << cameraMatrixL << endl << "cameraDistcoeffL=:" << distcoeffL << endl << "cameraMatrixR=:" << cameraMatrixR << endl << "cameraDistcoeffR=:" << distcoeffR << endl;fs.open("extrinsics.yml", cv::FileStorage::WRITE);fs << "R" << R << "T" << T << "Rl" << R1 << "Rr" << R2 << "Pl" << P1 << "Pr" << P2 << "Q" << Q << "F" << F << "E" << E;std::cout << "R=" << R << endl << "T=" << T << endl << "Rl=" << R1 << endl << "Rr=" << R2 << endl << "Pl=" << P1 << endl << "Pr=" << P2 << endl << "Q=" << Q << endl << "F=" << F << endl << "E=" << E << endl;fs.release();}int main()
{cv::utils::logging::setLogLevel(utils::logging::LOG_LEVEL_SILENT);//不再输出日志//或//utils::logging::setLogLevel(utils::logging::LOG_LEVEL_ERROR);//只输出错误日志//读取图片int current = 1;while (current <= framenumber){char frameL[50];sprintf_s(frameL, 50, "external/l%d.bmp", n++);imageL = cv::imread(frameL);cv::cvtColor(imageL, grayimageL, cv::ColorConversionCodes::COLOR_BGR2GRAY);char frameR[50];sprintf_s(frameR, 50, "external/r%d.bmp", m++);imageR = cv::imread(frameR);cv::cvtColor(imageR, grayimageR, cv::ColorConversionCodes::COLOR_BGR2GRAY);bool foundL, foundR;foundL = cv::findChessboardCorners(imageL, boardsize, cornerL);foundR = cv::findChessboardCorners(imageR, boardsize, cornerR);if (foundL == true && foundR == true){cv::cornerSubPix(grayimageL, cornerL, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::MAX_ITER | cv::TermCriteria::EPS, 30, 1e-6));cv::cornerSubPix(grayimageR, cornerR, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::MAX_ITER | cv::TermCriteria::EPS, 30, 1e-6));cv::drawChessboardCorners(imageL, boardsize, cornerL, foundL);//cv::imshow("L", imageL);namedWindow("L", WINDOW_NORMAL);imshow("L", imageL);cv::waitKey(10);cv::drawChessboardCorners(imageR, boardsize, cornerR, foundR);//cv::imshow("R", imageR);namedWindow("R", WINDOW_NORMAL);imshow("R", imageR);cv::waitKey(10);imagepointL.push_back(cornerL);imagepointR.push_back(cornerR);cout << "The image " << current << " is good" << endl;}else{std::cout << "The image is bad please try again" << endl;}current++;}worldpoint();cout << "开始标定" << endl;double err = cv::stereoCalibrate(objpoint, imagepointL, imagepointR, cameraMatrixL, distcoeffL, cameraMatrixR, distcoeffR, imagesize, R, T, E, F,CALIB_USE_INTRINSIC_GUESS,cv::TermCriteria(cv::TermCriteria::COUNT + cv::TermCriteria::EPS, 30, 1e-6));cout << "The err = " << err << endl;cv::stereoRectify(cameraMatrixL, distcoeffL, cameraMatrixR, distcoeffR, imagesize, R, T, R1, R2, P1, P2, Q, cv::CALIB_ZERO_DISPARITY, -1, imagesize, &validROIL, &validROIR);cv::initUndistortRectifyMap(cameraMatrixL, distcoeffL, R1, P1, imagesize, CV_32FC1, maplx, maply);cv::initUndistortRectifyMap(cameraMatrixR, distcoeffR, R2, P2, imagesize, CV_32FC1, maprx, mapry);outputparam();cv::Mat canvas;double sf;int w, h;sf = 600. / MAX(imagesize.width, imagesize.height);w = cvRound(imagesize.width * sf);h = cvRound(imagesize.height * sf);canvas.create(h, w * 2, CV_8UC3);int currents = 1;while (currents <= framenumber){char frameL[50];sprintf_s(frameL, 50, "external/l%d.bmp", i++);imageL = cv::imread(frameL);char frameR[50];sprintf_s(frameR, 50, "external/r%d.bmp", j++);imageR = cv::imread(frameR);cv::Mat rectifyImageL2, rectifyImageR2;cv::remap(imageL, rectifyImageL2, maplx, maply, cv::InterpolationFlags::INTER_LINEAR);cv::remap(imageR, rectifyImageR2, maprx, mapry, cv::InterpolationFlags::INTER_LINEAR);cv::Mat canvasPart = canvas(cv::Rect(w * 0, 0, w, h));resize(rectifyImageL2, canvasPart, canvasPart.size(), 0, 0, cv::INTER_AREA);cv::Rect vroiL(cvRound(validROIL.x * sf), cvRound(validROIL.y * sf),cvRound(validROIL.width * sf), cvRound(validROIL.height * sf));cv::rectangle(canvasPart, vroiL, cv::Scalar(0, 0, 255), 3, 8);canvasPart = canvas(cv::Rect(w, 0, w, h));resize(rectifyImageR2, canvasPart, canvasPart.size(), 0, 0, cv::INTER_LINEAR);cv::Rect vroiR(cvRound(validROIR.x * sf), cvRound(validROIR.y * sf),cvRound(validROIR.width * sf), cvRound(validROIR.height * sf));cv::rectangle(canvasPart, vroiR, cv::Scalar(0, 255, 0), 3, 8);for (int i = 0; i < canvas.rows; i += 16)line(canvas, cv::Point(0, i), cv::Point(canvas.cols, i), cv::Scalar(0, 255, 0), 1, 8);cv::imshow("rectified", canvas);if (cv::waitKey() > 0){currents++;}}return 0;
}
代码运行结果如下: