目录
学习 opencv 自带示例 stereo_calib(立体相机标定)
准备
首先,需要编译 opencv 自带示例,可以参考之前的博客记录:
cmake+windows 编译 opencv 自带示例.
调试示例代码
目标工程
确定输入参数类型
/// 参数:
/// @ w:棋盘格(圆形)标定板的长边角点数目,对于 12x9 的棋盘格,w = 11
/// @ h:棋盘格(圆形)标定板的短边角点数目,对于 12x9 的棋盘格,h = 8
/// @ s:棋盘格方格尺寸,单位 cm
/// @ nr:no rectified: 如果加上这个参数,就不会显示矫正后的图像
/// @ help:命令行执行帮助提示
/// @ @input:保存标定图像集路径的 .xml 文件
cv::CommandLineParser parser(argc, argv, "{w|9|}{h|6|}{s|1.0|}{nr||}{help||}{@input|stereo_calib.xml|}");
if (parser.has("help"))
return print_help();
showRectified = !parser.has("nr");
imagelistfn = samples::findFile(parser.get<string>("@input"));
boardSize.width = parser.get<int>("w");
boardSize.height = parser.get<int>("h");
float squareSize = parser.get<float>("s");
调试模式(debug)输入参数
右键此项目,点击属性选项,进入项目的属性页面,在调试 --> 命令参数
栏中输入参数,我的标定板尺寸为 12x9 (角点尺寸 11x8), 1.5cm
。
标定图像集路径 .xml
文件地址(如上图)&格式(如下)。
stereo_calib.xml(部分文件)
<?xml version="1.0"?>
<opencv_storage>
<imagelist>
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\left_1.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\right_1.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\left_2.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\right_2.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\left_3.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\right_3.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\left_4.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\right_4.png"
...
...
...
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\left_48.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\right_48.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\left_49.png"
"D:\M_MindVisionSDK_Clone\common_stereo_0702\common\build\calibration\image_pair\right_49.png"
</imagelist>
</opencv_storage>
F5 调试
图像的分辨率为1280x1024
,一共 49 对(98张)图片,调试过程执行时间大约在 20+ 分钟左右(i5-9400F)。
标定开始(47对图像符合要求(找到角点)):
程序执行完毕(输出项目)
stereo_calib.cpp
执行完毕后会在命令行中显示输出,如果在程序执行前没有输入nr
参数,此时就会显示每一对矫正后标定图像(包含裁剪区域,水平线);在stereo_calib.cpp
源文件所在目录,也会生成内参(intrinsic.yml)与外参(extrinsic.yml) 文件。
命令行输出
矫正图像
内外参程序默认存储地址
内参 intrinsic.yml
%YAML:1.0
---
M1: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 1.3241615532180620e+03, 0., 6.3819839672280261e+02, 0.,
1.3248893276176391e+03, 5.1955679264247931e+02, 0., 0., 1. ]
D1: !!opencv-matrix
rows: 1
cols: 14
dt: d
data: [ -1.1636352792431695e-01, 2.8974617123762275e-01, 0., 0., 0.,
0., 0., 1.2908722457060287e-01, 0., 0., 0., 0., 0., 0. ]
M2: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 1.3241615532180620e+03, 0., 6.3481565514416604e+02, 0.,
1.3248893276176391e+03, 5.1084131542728164e+02, 0., 0., 1. ]
D2: !!opencv-matrix
rows: 1
cols: 14
dt: d
data: [ -1.0253422568346042e-01, 1.8838231393096699e-01, 0., 0., 0.,
0., 0., -3.7932170237531426e-02, 0., 0., 0., 0., 0., 0. ]
外参 extrinsic.yml
%YAML:1.0
---
R: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 9.9999962469823134e-01, -8.6625827227159650e-04,
-1.4142211807947691e-05, 8.6626849570826839e-04,
9.9999935124684680e-01, 7.3965179573864872e-04,
1.3501473146517586e-05, -7.3966376909856987e-04,
9.9999972635757206e-01 ]
T: !!opencv-matrix
rows: 3
cols: 1
dt: d
data: [ -7.9332505690605970e+00, -1.7537722400808173e-02,
-5.2447786996653978e-01 ]
R1: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 9.9782178211702999e-01, 1.2926754401776212e-03,
6.5954682343262627e-02, -1.3199419700206951e-03,
9.9999906048566101e-01, 3.6983914196800176e-04,
-6.5954142295917270e-02, -4.5608990507940109e-04,
9.9782255090572503e-01 ]
R2: !!opencv-matrix
rows: 3
cols: 3
dt: d
data: [ 9.9781935509686992e-01, 2.2058396748603540e-03,
6.5967180214069804e-02, -2.1862041633003604e-03,
9.9999754186115819e-01, -3.6984185448794930e-04,
-6.5967833869417944e-02, 2.2481763670781029e-04,
9.9782172473423680e-01 ]
P1: !!opencv-matrix
rows: 3
cols: 4
dt: d
data: [ 1.2570131053880084e+03, 0., 5.2801861572265625e+02, 0., 0.,
1.2570131053880084e+03, 5.1521139144897461e+02, 0., 0., 0., 1.,
0. ]
P2: !!opencv-matrix
rows: 3
cols: 4
dt: d
data: [ 1.2570131053880084e+03, 0., 5.2801861572265625e+02,
-9.9939932841530499e+03, 0., 1.2570131053880084e+03,
5.1521139144897461e+02, 0., 0., 0., 1., 0. ]
Q: !!opencv-matrix
rows: 4
cols: 4
dt: d
data: [ 1., 0., 0., -5.2801861572265625e+02, 0., 1., 0.,
-5.1521139144897461e+02, 0., 0., 0., 1.2570131053880084e+03, 0.,
0., 1.2577686112529091e-01, 0. ]
内外参数据说明
内参 intrinsic.yml
M1: cameraMatrix1 First camera matrix. (相机1矩阵)
D1: distCoeffs1 First camera distortion parameters.(distortion coefficient,相机1畸变系数)
M2: cameraMatrix2 Second camera matrix.(相机2矩阵)
D2: distCoeffs2 Second camera distortion parameters.(相机2畸变系数)
来源函数
/// @overload
CV_EXPORTS_W double stereoCalibrate( InputArrayOfArrays objectPoints,
InputArrayOfArrays imagePoints1, InputArrayOfArrays imagePoints2,
InputOutputArray cameraMatrix1, InputOutputArray distCoeffs1,
InputOutputArray cameraMatrix2, InputOutputArray distCoeffs2,
Size imageSize, OutputArray R,OutputArray T, OutputArray E, OutputArray F,
int flags = CALIB_FIX_INTRINSIC,
TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 1e-6) );
外参 extrinsic.yml
R: @param R Rotation matrix between the coordinate systems of the first and the second cameras.(两相机坐标系的旋转矩阵)
T: @param T Translation vector between coordinate systems of the cameras.(两相机坐标系的平移向量)
R1: @param R1 Output 3x3 rectification transform (rotation matrix) for the first camera.(相机1的3x3整流变换(旋转矩阵))
R2: @param R2 Output 3x3 rectification transform (rotation matrix) for the second camera.(相机2的3x3整流变换(旋转矩阵))
P1: @param P1 Output 3x4 projection matrix in the new (rectified) coordinate systems for the first camera.(相机1对于新坐标系的(调整后的)投影矩阵)
P2: @param P2 Output 3x4 projection matrix in the new (rectified) coordinate systems for the second camera.(相机2对于新坐标系的(调整后的)投影矩阵)
Q: @param Q Output 4x4 disparity-to-depth mapping matrix (see reprojectImageTo3D ).(4X4视差 到 深度映射矩阵)
来源函数
CV_EXPORTS_W void stereoRectify( InputArray cameraMatrix1, InputArray distCoeffs1,
InputArray cameraMatrix2, InputArray distCoeffs2,
Size imageSize, InputArray R, InputArray T,
OutputArray R1, OutputArray R2,
OutputArray P1, OutputArray P2,
OutputArray Q, int flags = CALIB_ZERO_DISPARITY,
double alpha = -1, Size newImageSize = Size(),
CV_OUT Rect* validPixROI1 = 0, CV_OUT Rect* validPixROI2 = 0 );
学习(TODO)
参考
- 链接: cmake+windows 编译 opencv 自带示例.
- 链接: link.