OpenCV相机标定与3D重建(21)投影矩阵分解函数decomposeProjectionMatrix()的使用

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

算法描述

将投影矩阵分解为旋转矩阵和相机内参矩阵。

cv::decomposeProjectionMatrix 是 OpenCV 库中的一个函数,用于将投影矩阵(Projection Matrix)分解为相机内参矩阵(Camera Matrix)、旋转矩阵(Rotation Matrix)和平移向量(Translation Vector),以及可选的绕各轴的旋转矩阵和欧拉角。这个函数对于理解相机在三维空间中的位置和姿态非常有用。

函数原型

cpp 复制代码
void cv::decomposeProjectionMatrix
(
	InputArray 	projMatrix,
	OutputArray 	cameraMatrix,
	OutputArray 	rotMatrix,
	OutputArray 	transVect,
	OutputArray 	rotMatrixX = noArray(),
	OutputArray 	rotMatrixY = noArray(),
	OutputArray 	rotMatrixZ = noArray(),
	OutputArray 	eulerAngles = noArray() 
)		

参数

  • 参数projMatrix:3x4 输入投影矩阵P。

  • 参数cameraMatrix:输出 3x3 相机内参矩阵 A = [ f x 0 c x 0 f y c y 0 0 1 ] \textbf A = \begin{bmatrix} f_x & 0 & c_x \\ 0 & f_y & c_y \\ 0 & 0 & 1 \end{bmatrix} A= fx000fy0cxcy1

  • 参数rotMatrix:输出 3x3 外部旋转矩阵R。

  • 参数transVect:输出 4x1 平移向量T。

  • 参数rotMatrixX:可选的绕 x 轴的 3x3 旋转矩阵。

  • 参数rotMatrixY:可选的绕 y 轴的 3x3 旋转矩阵。

  • 参数rotMatrixZ:可选的绕 z 轴的 3x3 旋转矩阵。

  • 参数eulerAngles:可选的包含三个旋转欧拉角(以度为单位)的三元素向量。

该函数计算一个投影矩阵分解为校准矩阵(相机内参矩阵)、旋转矩阵和相机位置。它还可以选择性地返回三个旋转矩阵,每个轴一个,以及三个欧拉角,这些可以在 OpenGL 中使用。注意,总是存在多于一种的绕三个主轴旋转的序列,它们会导致物体相同的朝向,例如见 [243] 。返回的三个旋转矩阵和对应的三个欧拉角只是可能解中的一个。

该函数基于 RQDecomp3x3。

代码示例

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

int main()
{
    // 假设我们已经得到了投影矩阵 P
    cv::Mat projMatrix = ( cv::Mat_< double >( 3, 4 ) << 500, 0, 320, 0, 0, 500, 240, 0, 0, 0, 1, 0 );

    // 创建输出容器
    cv::Mat cameraMatrix;
    cv::Mat rotMatrix;
    cv::Mat transVect;

    // 分解投影矩阵
    cv::decomposeProjectionMatrix( projMatrix, cameraMatrix, rotMatrix, transVect );

    // 打印结果
    std::cout << "Camera Matrix:\n" << cameraMatrix << "\n";
    std::cout << "Rotation Matrix:\n" << rotMatrix << "\n";

    // 归一化平移向量以获得实际的平移向量
    double w = transVect.at< double >( 3 );
    if ( w != 0 )
    {
        transVect /= w;
    }
    std::cout << "Translation Vector:\n" << transVect.rowRange( 0, 3 ) << "\n";  // 只取前3行

    return 0;
}

运行结果

cpp 复制代码
Camera Matrix:
[500, 0, 320;
 0, 500, 240;
 0, 0, 1]
Rotation Matrix:
[1, 0, 0;
 0, 1, 0;
 0, 0, 1]
Translation Vector:
[0;
 0;
 0]
相关推荐
在下胡三汉4 小时前
什么是 3D 文件?
3d
看到我,请让我去学习8 小时前
OpenCV编程- (图像基础处理:噪声、滤波、直方图与边缘检测)
c语言·c++·人工智能·opencv·计算机视觉
蹦蹦跳跳真可爱58917 小时前
Python----OpenCV(图像増强——高通滤波(索贝尔算子、沙尔算子、拉普拉斯算子),图像浮雕与特效处理)
人工智能·python·opencv·计算机视觉
点云登山者1 天前
登山第二十六梯:单目3D检测一切——一只眼看世界
3d·3d检测·检测一切·单目3d检测
xhload3d1 天前
智慧航天运载体系全生命周期监测 | 图扑数字孪生
物联网·3d·智慧城市·html5·webgl·数字孪生·可视化·工业互联网·三维建模·工控·航空航天·火箭升空·智慧航空·智慧航天·火箭发射·火箭回收
小赖同学啊1 天前
光伏园区3d系统管理
前端·javascript·3d
彭祥.2 天前
Jetson边缘计算主板:Ubuntu 环境配置 CUDA 与 cudNN 推理环境 + OpenCV 与 C++ 进行目标分类
c++·opencv·分类
Tony沈哲2 天前
macOS 上为 Compose Desktop 构建跨架构图像处理 dylib:OpenCV + libraw + libheif 实践指南
opencv·算法
SDUERPANG2 天前
三维目标检测|Iou3D 代码解读一
人工智能·目标检测·3d
视觉人机器视觉2 天前
Visual Studio2022和C++opencv的配置保姆级教程
c++·opencv·visual studio