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;
}
相关推荐
AndrewHZ4 小时前
【3D重建技术】如何基于遥感图像和DEM等数据进行城市级高精度三维重建?
图像处理·人工智能·深度学习·3d·dem·遥感图像·3d重建
OperateCode12 小时前
AutoVideoMerge:让二刷更沉浸的自动化视频处理脚本工具
python·opencv·ffmpeg
lxmyzzs13 小时前
【图像算法 - 14】精准识别路面墙体裂缝:基于YOLO12与OpenCV的实例分割智能检测实战(附完整代码)
人工智能·opencv·算法·计算机视觉·裂缝检测·yolo12
二川bro18 小时前
第16节:自定义几何体 - 从顶点构建3D世界
3d
迈火1 天前
ComfyUI-3D-Pack:3D创作的AI神器
人工智能·gpt·3d·ai·stable diffusion·aigc·midjourney
jndingxin2 天前
OpenCV图像注册模块
人工智能·opencv·计算机视觉
R-G-B2 天前
【P14 3-6 】OpenCV Python——视频加载、摄像头调用、视频基本信息获取(宽、高、帧率、总帧数)
python·opencv·视频加载·摄像头调用·获取视频基本信息·获取视频帧率·获取视频帧数
荼蘼2 天前
OpenCv(三)——图像平滑处理
人工智能·opencv·计算机视觉
R-G-B2 天前
OpenCV Python——报错AttributeError: module ‘cv2‘ has no attribute ‘bgsegm‘,解决办法
人工智能·python·opencv·opencv python·attributeerror·module ‘cv2‘·no attribute
似乎很简单3 天前
【opencv-Python学习笔记(5):几何变换】
笔记·opencv·学习