OpenCV相机标定与3D重建(20)将单应性矩阵分解为旋转矩阵、平移向量和法向量函数decomposeHomographyMat的使用-代码示例

时间:2024-12-15 18:30:17

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

int main()
{
    // 假设我们已经得到了单应性矩阵 H 和相机内参矩阵 K
    cv::Mat H = ( cv::Mat_< double >( 3, 3 ) << 0.998, -0.062, 50, 0.062, 0.998, 100, 0.007, 0.05, 1 );

    cv::Mat K = ( cv::Mat_< double >( 3, 3 ) << 500, 0, 320, 0, 500, 240, 0, 0, 1 );

    // 创建输出容器
    std::vector< cv::Mat > rotations;
    std::vector< cv::Mat > translations;
    std::vector< cv::Mat > normals;

    // 分解单应性矩阵
    int numSolutions = cv::decomposeHomographyMat( H, K, rotations, translations, normals );

    // 打印结果
    std::cout << "Number of solutions: " << numSolutions << "\n";

    for ( int i = 0; i < numSolutions; ++i )
    {
        std::cout << "Solution " << i + 1 << ":\n";
        std::cout << "Rotation Matrix:\n" << rotations[ i ] << "\n";
        std::cout << "Translation Vector:\n" << translations[ i ] << "\n";
        std::cout << "Normal Vector:\n" << normals[ i ] << "\n";
    }

    return 0;
}