【opencv】教程代码 —features2D(3)Homography—分解单应性矩阵

decompose_homography.cpp 分解单应性矩阵

1f3cf8759d2a1979c1054e4d24bfe9c1.jpeg

left01.jpg  boardSize:9x6  squareSize:0.025

b5dda4984f7911928b3769da8026b2c1.jpeg

left02.jpg

92de7d4b88c7756e55cb3eb041aa944e.png

c31a1fe2f8c8c527266998e13bb16387.png

相机内参

1eaf211b2dced8899b341b6ae026820c.png

#include <iostream> // 引入输入输出流库
#include <opencv2/core.hpp> // 引入OpenCV的核心功能头文件
#include <opencv2/highgui.hpp> // 引入OpenCV的GUI功能头文件
#include <opencv2/calib3d.hpp> // 引入OpenCV的相机标定和三维重建功能头文件


using namespace std; // 使用标准命名空间,例如std::vector可以省略std::
using namespace cv; // 使用opencv命名空间,例如cv::Mat可以省略cv::


namespace // 匿名命名空间
{
// 定义了几种标定板的模式的枚举类型
enum Pattern { CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };


// 函数用于计算棋盘格的角点
void calcChessboardCorners(Size boardSize, float squareSize, vector<Point3f>& corners, Pattern patternType = CHESSBOARD)
{
    corners.resize(0); // 清空角点向量


    // 根据不同的标定板类型计算角点
    switch (patternType) {
    case CHESSBOARD:
    case CIRCLES_GRID:
        // 对于棋盘格和圆点网格,遍历每行每列生成角点
        for( int i = 0; i < boardSize.height; i++ )
            for( int j = 0; j < boardSize.width; j++ )
                corners.push_back(Point3f(float(j*squareSize),
                                          float(i*squareSize), 0));
        break;


    case ASYMMETRIC_CIRCLES_GRID:
        // 对于非对称圆点网格,遍历每行每列生成角点,同时考虑横向位置的偏移
        for( int i = 0; i < boardSize.height; i++ )
            for( int j = 0; j < boardSize.width; j++ )
                corners.push_back(Point3f(float((2*j + i % 2)*squareSize),
                                          float(i*squareSize), 0));
        break;


    // 如果模式类型未知则报错
    default:
        CV_Error(Error::StsBadArg, "Unknown pattern type\n");
    }
}


// 计算单应性矩阵的函数,基于旋转矩阵、平移向量、平面到相机的逆距离和平面的法向量
Mat computeHomography(const Mat &R_1to2, const Mat &tvec_1to2, const double d_inv, const Mat &normal)
{
    Mat homography = R_1to2 + d_inv * tvec_1to2*normal.t(); // 计算单应性矩阵
    return homography; // 返回计算出的单应性矩阵
}


// 根据两次相机位姿计算两个相机之间的位姿关系
void computeC2MC1(const Mat &R1, const Mat &tvec1, const Mat &R2, const Mat &tvec2,
                  Mat &R_1to2, Mat &tvec_1to2)
{
    //c2Mc1 = c2Mo * oMc1 = c2Mo * c1Mo.inv()
    R_1to2 = R2 * R1.t(); // 计算旋转矩阵
    tvec_1to2 = R2 * (-R1.t()*tvec1) + tvec2; // 计算平移向量
}


// 主函数,使用两个图像、棋盘格大小、方块大小和内参路径进行标定板的单应性分解
void decomposeHomography(const string &img1Path, const string &img2Path, const Size &patternSize,
                         const float squareSize, const string &intrinsicsPath)
{
    // 根据路径读取两张图像
    Mat img1 = imread( samples::findFile( img1Path) );
    Mat img2 = imread( samples::findFile( img2Path) );


    // 查找两幅图像中的角点
    vector<Point2f> corners1, corners2;
    bool found1 = findChessboardCorners(img1, patternSize, corners1);
    bool found2 = findChessboardCorners(img2, patternSize, corners2);


    // 如果任一图像无法找到角点则返回错误
    if (!found1 || !found2)
    {
        cout << "Error, cannot find the chessboard corners in both images." << endl;
        return;
    }


    //! [compute-poses]
    vector<Point3f> objectPoints; // 存储物体在世界坐标系中的点
    // 计算棋盘格角点的世界坐标
    calcChessboardCorners(patternSize, squareSize, objectPoints); 


    // 读取相机的内参
    FileStorage fs( samples::findFile( intrinsicsPath ), FileStorage::READ);
    Mat cameraMatrix, distCoeffs;
    fs["camera_matrix"] >> cameraMatrix; // 相机矩阵
    fs["distortion_coefficients"] >> distCoeffs; // 畸变系数


    // 对两个图像使用solvePnP求解相机位姿
    // 使用solvePnP来估计两幅图片的相机外参即旋转向量和平移向量
    Mat rvec1, tvec1; // 旋转向量和平移向量
    solvePnP(objectPoints, corners1, cameraMatrix, distCoeffs, rvec1, tvec1);
    Mat rvec2, tvec2;
    solvePnP(objectPoints, corners2, cameraMatrix, distCoeffs, rvec2, tvec2);
    //! [compute-poses]


    //! [compute-camera-displacement]
    Mat R1, R2;
    Rodrigues(rvec1, R1); // 旋转向量转换成旋转矩阵
    Rodrigues(rvec2, R2);


    // 计算两个相机位姿之间的相对旋转和平移
    Mat R_1to2, t_1to2;
    computeC2MC1(R1, tvec1, R2, tvec2, R_1to2, t_1to2);
    Mat rvec_1to2;
    Rodrigues(R_1to2, rvec_1to2); // 计算得到的旋转矩阵再转换成旋转向量
    //! [compute-camera-displacement]


    //! [compute-plane-normal-at-camera-pose-1]
    Mat normal = (Mat_<double>(3,1) << 0, 0, 1); // 定义一个垂直于棋盘格表面的法向量
    Mat normal1 = R1*normal; //相机1坐标轴z在世界坐标系的表示// 使用相机1的旋转矩阵变换法向量到相机坐标系下
    //! [compute-plane-normal-at-camera-pose-1]


    //! [compute-plane-distance-to-the-camera-frame-1]
    // 计算棋盘格平面到相机坐标系原点的距离
    Mat origin(3, 1, CV_64F, Scalar(0)); // 定义原点
    Mat origin1 = R1*origin + tvec1; // 相机坐标系1原点在世界坐标系下的表示
    //相机1坐标系原点到标定板平面的距离的逆
    double d_inv1 = 1.0 / normal1.dot(origin1); // 计算平面到相机1原点的逆距离
    //! [compute-plane-distance-to-the-camera-frame-1]


    //! [compute-homography-from-camera-displacement]
    // 根据相机位姿差异计算单应性矩阵
    Mat homography_euclidean = computeHomography(R_1to2, t_1to2, d_inv1, normal1);
    Mat homography =cameraMatrix * homography_euclidean * cameraMatrix.inv();
    // 根据欧几里得单应性矩阵计算实际的单应性矩阵
    // 内参解极约束
    homography /= homography.at<double>(2,2);
    homography_euclidean /= homography_euclidean.at<double>(2,2);
    //! [compute-homography-from-camera-displacement]


    //! [decompose-homography-from-camera-displacement]
    // 分解从相机位移计算得到的单应性矩阵
    vector<Mat> Rs_decomp, ts_decomp, normals_decomp;
    // 使用OpenCV函数decomposeHomographyMat进行分解
    int solutions = decomposeHomographyMat(homography, cameraMatrix, Rs_decomp, ts_decomp, normals_decomp);
    // 显示分解得到的解决方案数和每个解决方案
    cout << "Decompose homography matrix computed from the camera displacement:" << endl << endl;
    for (int i = 0; i < solutions; i++)
    {
      double factor_d1 = 1.0 / d_inv1;
      Mat rvec_decomp;
      Rodrigues(Rs_decomp[i], rvec_decomp); // 将旋转矩阵转换成旋转向量
      cout << "Solution " << i << ":" << endl;
      cout << "rvec from homography decomposition: " << rvec_decomp.t() << endl;
      cout << "rvec from camera displacement: " << rvec_1to2.t() << endl;
      cout << "tvec from homography decomposition: " << ts_decomp[i].t() << " and scaled by d: " << factor_d1 * ts_decomp[i].t() << endl;
      cout << "tvec from camera displacement: " << t_1to2.t() << endl;
      cout << "plane normal from homography decomposition: " << normals_decomp[i].t() << endl;
      cout << "plane normal at camera 1 pose: " << normal1.t() << endl << endl;
    }
    //! [decompose-homography-from-camera-displacement]


    //! [estimate homography]
    // 使用findHomography()估计两幅图像间点的单应性矩阵
    Mat H = findHomography(corners1, corners2);
    //! [estimate homography]


    //! [decompose-homography-estimated-by-findHomography]
    // 分解由findHomography()函数估计得到的单应性矩阵
    solutions = decomposeHomographyMat(H, cameraMatrix, Rs_decomp, ts_decomp, normals_decomp);
    cout << "Decompose homography matrix estimated by findHomography():" << endl << endl;
    for (int i = 0; i < solutions; i++)
    {
      double factor_d1 = 1.0 / d_inv1;
      Mat rvec_decomp;
      Rodrigues(Rs_decomp[i], rvec_decomp); // 将旋转矩阵转换成旋转向量
      cout << "Solution " << i << ":" << endl;
      cout << "rvec from homography decomposition: " << rvec_decomp.t() << endl;
      cout << "rvec from camera displacement: " << rvec_1to2.t() << endl;
      cout << "tvec from homography decomposition: " << ts_decomp[i].t() << " and scaled by d: " << factor_d1 * ts_decomp[i].t() << endl;
      cout << "tvec from camera displacement: " << t_1to2.t() << endl;
      cout << "plane normal from homography decomposition: " << normals_decomp[i].t() << endl;
      cout << "plane normal at camera 1 pose: " << normal1.t() << endl << endl;
    }
    //! [decompose-homography-estimated-by-findHomography]
}


// 命令行参数解析中用到的参数模板字符串
const char* params
    = "{ help h         |       | print usage }"
      "{ image1         | left02.jpg | path to the source chessboard image }"
      "{ image2         | left01.jpg | path to the desired chessboard image }"
      "{ intrinsics     | left_intrinsics.yml | path to camera intrinsics }"
      "{ width bw       | 9     | chessboard width }"
      "{ height bh      | 6     | chessboard height }"
      "{ square_size    | 0.025 | chessboard square size }";
}


// main函数 - 程序入口点
int main(int argc, char *argv[])
{
    // 解析命令行输入的参数
    CommandLineParser parser(argc, argv, params);


    // 如果参数中包含help,则打印程序的帮助信息
    if ( parser.has("help") )
    {
        parser.about("Code for homography tutorial.\n"
                     "Example 4: decompose the homography matrix.\n");
        parser.printMessage();
        return 0;
    }


    // 获取棋盘格尺寸参数、方块大小并调用decomposeHomography函数
    Size patternSize(parser.get<int>("width"), parser.get<int>("height"));
    float squareSize = (float) parser.get<double>("square_size");
    decomposeHomography(parser.get<String>("image1"),
                        parser.get<String>("image2"),
                        patternSize, squareSize,
                        parser.get<String>("intrinsics"));


    return 0; // 程序正常结束
}

代码的主要功能是通过两张棋盘格图像来计算它们之间的单应性矩阵,并对该矩阵进行分解以获得相对旋转向量、平移向量和正常向量。这个过程通常用于计算机视觉中相机的位姿估计和校准。代码使用了OpenCV的相关函数完成图像处理和矩阵运算。

decomposeHomography用于执行相机位移下单应性矩阵的分解与计算。整个代码流程包括:

1. 读取两幅图像并在棋盘格中找到角点。2. 从外部文件读取相机内参。3. 使用solvePnP函数估计两幅图像相对于世界坐标系的姿态。4. 计算相机1到相机2的旋转和平移。5. 计算参考平面的法线向量和到相机1的距离。6. 根据相机间的位移计算出单应性矩阵。7. 使用decomposeHomographyMat函数分解由相机位移计算出的单应性矩阵。8. 通过findHomography函数估计两个视图间的单应性矩阵,并分解之。 代码的最终目的是从两幅图像计算出相机的位移,并从位移中分解出单应性矩阵,这通常用于场景的三维重构和姿态估计。

终端输出:

Decompose homography matrix computed from the camera displacement:


Solution 0:
rvec from homography decomposition: [-0.09198300601684746, -0.5372581107203847, 1.310868858823169]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [-0.7747960935447347, -0.02751122265362334, -0.6791979966698382] and scaled by d: [-0.1578091502009057, -0.005603439026252055, -0.1383378924669762]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [-0.1973513031094161, 0.6283452092297845, -0.7524857215914424]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]


Solution 1:
rvec from homography decomposition: [-0.09198300601684746, -0.5372581107203847, 1.310868858823169]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [0.7747960935447347, 0.02751122265362334, 0.6791979966698382] and scaled by d: [0.1578091502009057, 0.005603439026252055, 0.1383378924669762]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [0.1973513031094161, -0.6283452092297845, 0.7524857215914424]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]


Solution 2:
rvec from homography decomposition: [0.1053487844994422, -0.1561929302268701, 1.40135654775989]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [-0.4666552458868745, 0.105003306671971, -0.9130076449288979] and scaled by d: [-0.09504754657871854, 0.02138689486465782, -0.1859598438525694]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [-0.3131715302799941, 0.8421206250806385, -0.4390403687998199]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]


Solution 3:
rvec from homography decomposition: [0.1053487844994422, -0.1561929302268701, 1.40135654775989]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [0.4666552458868745, -0.105003306671971, 0.9130076449288979] and scaled by d: [0.09504754657871854, -0.02138689486465782, 0.1859598438525694]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [0.3131715302799941, -0.8421206250806385, 0.4390403687998199]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]


Decompose homography matrix estimated by findHomography():


Solution 0:
rvec from homography decomposition: [0.1552207660862356, -0.1521327237748819, 1.323678685010914]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [-0.4482361641899351, 0.02485247517069353, -1.034409633494862] and scaled by d: [-0.09129597935439666, 0.005061909862158862, -0.2106867943469173]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [-0.1384902519298367, 0.9063331412907092, -0.3992251083267753]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]


Solution 1:
rvec from homography decomposition: [0.1552207660862356, -0.1521327237748819, 1.323678685010914]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [0.4482361641899351, -0.02485247517069353, 1.034409633494862] and scaled by d: [0.09129597935439666, -0.005061909862158862, 0.2106867943469173]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [0.1384902519298367, -0.9063331412907092, 0.3992251083267753]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]


Solution 2:
rvec from homography decomposition: [-0.2886605520980661, -0.5210498746612311, 1.381242036724107]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [-0.8705960844288273, 0.1353018264406045, -0.7037701811073137] and scaled by d: [-0.1773215293631533, 0.02755804582536839, -0.1433427131894416]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [-0.2284582164830118, 0.6009247378618285, -0.7659610321335505]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]


Solution 3:
rvec from homography decomposition: [-0.2886605520980661, -0.5210498746612311, 1.381242036724107]
rvec from camera displacement: [-0.09198300601684731, -0.5372581107203847, 1.310868858823169]
tvec from homography decomposition: [0.8705960844288273, -0.1353018264406045, 0.7037701811073137] and scaled by d: [0.1773215293631533, -0.02755804582536839, 0.1433427131894416]
tvec from camera displacement: [0.1578091502009057, 0.005603439026252086, 0.1383378924669763]
plane normal from homography decomposition: [0.2284582164830118, -0.6009247378618285, 0.7659610321335505]
plane normal at camera 1 pose: [0.1973513031094159, -0.6283452092297841, 0.7524857215914428]

4e77715ade5f051f6e694f593e818fdd.png

a890d008417b11f5f935509f9f769e7f.png

如何计算相机间的转换矩阵?

88655865a194130991d82e04a535761e.png

如何使用solvePnP函数进行相机标定?

e638f60a6df69ab67916967293d4e732.png

Mat homography_euclidean = computeHomography(R_1to2, t_1to2, d_inv1, normal1);

880f95100bbc44eed34c436e47dd0c66.png

df6d8705862e2c4df59d00bf049f6f27.png

int solutions = decomposeHomographyMat(homography, cameraMatrix, Rs_decomp, ts_decomp, normals_decomp);

e6bc0e899b4837f031f0d251101cd2de.png

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
单应性矩阵Homography Matrix)是计算机视觉中常用的一种变换矩阵,可以将一个平面上的点映射到另一个平面上的对应点。在OpenCV中,可以使用findHomography函数来估计两个平面之间的单应性矩阵。下面是一个简单的示例代码: ``` #include <opencv2/opencv.hpp> using namespace cv; int main() { // 读取图像 Mat src1 = imread("image1.jpg"); Mat src2 = imread("image2.jpg"); // 定义特征点向量和描述子向量 std::vector<KeyPoint> keypoints1, keypoints2; Mat descriptors1, descriptors2; // 提取特征点和描述子 Ptr<ORB> orb = ORB::create(); orb->detectAndCompute(src1, noArray(), keypoints1, descriptors1); orb->detectAndCompute(src2, noArray(), keypoints2, descriptors2); // 匹配特征点 BFMatcher matcher(NORM_HAMMING); std::vector<DMatch> matches; matcher.match(descriptors1, descriptors2, matches); // 筛选出最佳匹配 double min_dist = 1000; for (int i = 0; i < descriptors1.rows; i++) { double dist = matches[i].distance; if (dist < min_dist) min_dist = dist; } std::vector<DMatch> good_matches; for (int i = 0; i < descriptors1.rows; i++) { if (matches[i].distance < 3 * min_dist) { good_matches.push_back(matches[i]); } } // 提取匹配点对 std::vector<Point2f> points1, points2; for (int i = 0; i < good_matches.size(); i++) { points1.push_back(keypoints1[good_matches[i].queryIdx].pt); points2.push_back(keypoints2[good_matches[i].trainIdx].pt); } // 计算单应性矩阵 Mat H = findHomography(points1, points2, RANSAC); return 0; } ``` 在这个示例代码中,我们首先使用ORB算法提取了两幅图像的特征点和描述子。然后使用BFMatcher算法匹配了两幅图像的特征点,并筛选出最佳匹配。接下来,我们使用findHomography函数估计了两个平面之间的单应性矩阵。最后,我们可以使用这个单应性矩阵来进行图像配准或者图像拼接等操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值