OpenCV相机标定与3D重建(12)计算相机校准的有用数据函数calibrationMatrixValues()的使用

发布于:2024-12-18 ⋅ 阅读:(100) ⋅ 点赞:(0)
  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

从相机内参矩阵计算有用的相机特性。
cv::calibrationMatrixValues 是 OpenCV 库中的一个函数,用于计算相机校准的有用数据。这个函数接收相机内参矩阵(camera matrix)、图像尺寸(image size)以及光圈宽度和高度(aperture width and height),并输出视场角(field of view in the X and Y directions, fovx 和 fovy)、焦距(focal length)、主点位置(principal point position)以及像素纵横比(aspect ratio)。

函数原型


void cv::calibrationMatrixValues	
(
	InputArray 	cameraMatrix,
	Size 	imageSize,
	double 	apertureWidth,
	double 	apertureHeight,
	double & 	fovx,
	double & 	fovy,
	double & 	focalLength,
	Point2d & 	principalPoint,
	double & 	aspectRatio 
)		

参数

  • 参数cameraMatrix:输入的相机内参矩阵,可以通过 calibrateCamera 或 stereoCalibrate 估计得到。
  • 参数imageSize:输入图像的像素尺寸。
  • 参数apertureWidth:传感器的实际宽度(单位:毫米)。
  • 参数apertureHeight:传感器的实际高度(单位:毫米)。
  • 参数fovx:沿水平传感器轴的输出视场角(单位:度)。
  • 参数fovy:沿垂直传感器轴的输出视场角(单位:度)。
  • 参数focalLength:镜头的焦距(单位:毫米)。
  • 参数principalPoint:主点的位置(单位:毫米)。
  • 参数aspectRatio:fy/fx (y方向上的焦距除以x方向上的焦距)

该函数从先前估计的相机矩阵计算各种有用的相机特性。

注意
请记住,统一测量单位 ‘mm’ 表示你为棋盘格间距选择的任意单位(因此它可以是任何值)。这里提到的 ‘mm’ 主要用于描述物理尺寸,但在实际应用中可以代表任何一致的长度单位。

代码示例

这个例子假设你已经有了相机内参矩阵 cameraMatrix,以及图像尺寸 imageSize。请注意,在实际应用中,您应该用通过校准过程获得的真实数据来替换示例中的数值。


#include <iostream>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main()
{
    // 假设的相机内参矩阵 (3x3 矩阵)
    Mat cameraMatrix = ( Mat_< double >( 3, 3 ) << 1000, 0, 320,  // fx, 0, cx
                         0, 1000, 240,                            // 0, fy, cy
                         0, 0, 1 );                               // 0, 0, 1

    // 图像尺寸(宽度 x 高度)
    Size imageSize( 640, 480 );

    // 传感器的实际物理尺寸(单位:毫米)。如果不知道确切值,可以设置为零。
    double apertureWidth  = 0.0;  // 宽度
    double apertureHeight = 0.0;  // 高度

    // 输出变量
    double fovx, fovy, focalLength;
    Point2d principalPoint;
    double aspectRatio;

    // 调用 calibrationMatrixValues 函数计算相机特性
    calibrationMatrixValues( cameraMatrix, imageSize, apertureWidth, apertureHeight, fovx, fovy, focalLength, principalPoint, aspectRatio );

    // 打印结果
    cout << "Field of view in X direction: " << fovx << " degrees" << endl;
    cout << "Field of view in Y direction: " << fovy << " degrees" << endl;
    cout << "Focal length: " << focalLength << " mm" << endl;
    cout << "Principal point: (" << principalPoint.x << ", " << principalPoint.y << ")" << endl;
    cout << "Aspect ratio: " << aspectRatio << endl;

    return 0;
}

运行结果

Field of view in X direction: 35.4893 degrees
Field of view in Y direction: 26.9915 degrees
Focal length: 1000 mm
Principal point: (320, 240)
Aspect ratio: 1

网站公告

今日签到

点亮在社区的每一天
去签到