OpenCV相机标定与3D重建(9)相机标定函数calibrateCameraRO()的使用

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

cv::calibrateCameraRO 是 OpenCV 中用于相机标定的函数,它允许固定某些点来进行更精确的标定。

函数原型

cpp 复制代码
double cv::calibrateCameraRO	
(
	InputArrayOfArrays 	objectPoints,
	InputArrayOfArrays 	imagePoints,
	Size 	imageSize,
	int 	iFixedPoint,
	InputOutputArray 	cameraMatrix,
	InputOutputArray 	distCoeffs,
	OutputArrayOfArrays 	rvecs,
	OutputArrayOfArrays 	tvecs,
	OutputArray 	newObjPoints,
	OutputArray 	stdDeviationsIntrinsics,
	OutputArray 	stdDeviationsExtrinsics,
	OutputArray 	stdDeviationsObjPoints,
	OutputArray 	perViewErrors,
	int 	flags = 0,
	TermCriteria 	criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, DBL_EPSILON) 
)		

参数

  • 参数objectPoints: 标志点在世界坐标系中的位置。
  • 参数imagePoints: 标志点在图像平面上的位置。
  • 参数imageSize: 图像的大小。
  • 参数iFixedPoint: 固定点的数量。
  • 参数cameraMatrix: 相机内参矩阵。
  • 参数distCoeffs: 径向畸变和切向畸变系数。
  • 参数rvecs: 每个视角的旋转向量。
  • 参数tvecs: 每个视角的平移向量。
  • 参数newObjPoints: 优化后的标志点在世界坐标系中的位置。
  • 参数stdDeviationsIntrinsics: 内参的标准偏差。
  • 参数stdDeviationsExtrinsics: 外参的标准偏差。
  • 参数stdDeviationsObjPoints: 标志点位置的标准偏差。
  • 参数perViewErrors: 每个视角的重投影误差。
  • 参数flags: 标定选项。
  • 参数criteria: 终止条件。

代码示例

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
    // 已知的世界坐标系中标记点的位置
    vector< vector< Point3f > > objectPoints;  // 存储所有棋盘格角点的真实坐标
    Size boardSize( 7, 5 );                    // 棋盘格的尺寸
    float squareSize = 1.0f;                   // 实际单位下的边长

    for ( int i = 0; i < boardSize.height; i++ )
    {
        for ( int j = 0; j < boardSize.width; j++ )
        {
            objectPoints.push_back( vector< Point3f >( 1, Point3f( j * squareSize, i * squareSize, 0 ) ) );
        }
    }

    // 图像中找到的标记点的位置
    vector< vector< Point2f > > imagePoints;  // 存储每个棋盘格角点的图像坐标

    // 加载图像并检测棋盘格角点
    Mat image = imread( "path_to_your_image.jpg" );
    if ( image.empty() )
    {
        cout << "Could not open or find the image!" << endl;
        return -1;
    }

    bool found = false;
    vector< Point2f > corners;
    found = findChessboardCorners( image, boardSize, corners );

    if ( found )
    {
        cornerSubPix( image, corners, Size( 11, 11 ), Size( -1, -1 ), TermCriteria( TermCriteria::EPS + TermCriteria::COUNT, 30, 0.1 ) );
        imagePoints.push_back( corners );
    }
    else
    {
        cout << "Chessboard not found in the image." << endl;
        return -1;
    }

    // 相机内参和畸变系数
    Mat cameraMatrix = Mat::eye( 3, 3, CV_64F );
    Mat distCoeffs;

    // 输出参数
    vector< Mat > rvecs, tvecs;
    Mat newObjPoints;
    Mat stdDeviationsIntrinsics, stdDeviationsExtrinsics, stdDeviationsObjPoints;
    Mat perViewErrors;

    int iFixedPoint = 0;  // 固定点数量
    int flags       = 0;  // 默认标定选项
    TermCriteria criteria( TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON );

    // 执行相机标定
    double rms = calibrateCameraRO( objectPoints, imagePoints, image.size(), iFixedPoint, cameraMatrix, distCoeffs, rvecs, tvecs, newObjPoints, stdDeviationsIntrinsics, stdDeviationsExtrinsics,
                                    stdDeviationsObjPoints, perViewErrors, flags, criteria );

    cout << "RMS re-projection error: " << rms << endl;
    cout << "Camera matrix:\n" << cameraMatrix << endl;
    cout << "Distortion coefficients:\n" << distCoeffs << endl;

    return 0;
}
相关推荐
多恩Stone5 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
多恩Stone5 天前
【3D-AICG 系列-15】Trellis 2 的 O-voxel Shape: Flexible Dual Grid 代码与论文对应
人工智能·python·算法·3d·aigc
sali-tec5 天前
C# 基于OpenCv的视觉工作流-章27-图像分割
图像处理·人工智能·opencv·算法·计算机视觉
saoys5 天前
Opencv 学习笔记:腐蚀操作 + 轮廓标记 + 分水岭分割
笔记·opencv·学习
saoys5 天前
Opencv 学习笔记:距离变换(DIST_L1 算法实战 + 归一化)
笔记·opencv·学习
在下胡三汉5 天前
为什么“资产土地”标准化主资产的 glTF、glb格式模型,为什么非常流行
3d
2401_863801465 天前
3DTiles(.b3dm,i3dm,cmpt) 数据转换fbx转obj转max转su,cesium格式模型转换
3d
3Dmax效果图渲染研习社5 天前
2026年3ds Max云渲染平台哪个好?
3d
guygg885 天前
图像匹配技术:相关匹配、Hausdorff距离匹配与基于距离变换的Hausdorff距离匹配
图像处理·opencv·计算机视觉