我目前正在尝试实现OpenCV的projectPoints方法的示例。该方法背后的思想是将一组3D点,给定摄像机的平移/旋转 vector 及其失真系数作为输入,在图像平面中输出相应的2D点。
代码源如下:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <string>

std::vector<cv::Point3d> Generate3DPoints();

int main(int argc, char* argv[])
{
    // Read 3D points
    std::vector<cv::Point3d> objectPoints = Generate3DPoints();
    std::vector<cv::Point2d> imagePoints;

    cv::Mat intrisicMat(3, 3, cv::DataType<double>::type); // Intrisic matrix
    intrisicMat.at<double>(0, 0) = 1.6415318549788924e+003;
    intrisicMat.at<double>(1, 0) = 0;
    intrisicMat.at<double>(2, 0) = 0;

    intrisicMat.at<double>(0, 1) = 0;
    intrisicMat.at<double>(1, 1) = 1.7067753507885654e+003;
    intrisicMat.at<double>(2, 1) = 0;

    intrisicMat.at<double>(0, 2) = 5.3262822453148601e+002;
    intrisicMat.at<double>(1, 2) = 3.8095355839052968e+002;
    intrisicMat.at<double>(2, 2) = 1;

    cv::Mat rVec(3, 1, cv::DataType<double>::type); // Rotation vector
    rVec.at<double>(0) = -3.9277902400761393e-002;
    rVec.at<double>(1) = 3.7803824407602084e-002;
    rVec.at<double>(2) = 2.6445674487856268e-002;

    cv::Mat tVec(3, 1, cv::DataType<double>::type); // Translation vector
    tVec.at<double>(0) = 2.1158489381208221e+000;
    tVec.at<double>(1) = -7.6847683212704716e+000;
    tVec.at<double>(2) = 2.6169795190294256e+001;

    cv::Mat distCoeffs(5, 1, cv::DataType<double>::type);   // Distortion vector
    distCoeffs.at<double>(0) = -7.9134632415085826e-001;
    distCoeffs.at<double>(1) = 1.5623584435644169e+000;
    distCoeffs.at<double>(2) = -3.3916502741726508e-002;
    distCoeffs.at<double>(3) = -1.3921577146136694e-002;
    distCoeffs.at<double>(4) = 1.1430734623697941e+002;

    std::cout << "Intrisic matrix: " << intrisicMat << std::endl << std::endl;
    std::cout << "Rotation vector: " << rVec << std::endl << std::endl;
    std::cout << "Translation vector: " << tVec << std::endl << std::endl;
    std::cout << "Distortion coef: " << distCoeffs << std::endl << std::endl;

    std::vector<cv::Point2f> projectedPoints;

    cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints);

    /*for (unsigned int i = 0; i < projectedPoints.size(); ++i)
    {
        std::cout << "Image point: " << imagePoints[i] << " Projected to " << projectedPoints[i] << std::endl;
    }*/

    std::cout << "Press any key to exit.";
    std::cin.ignore();
    std::cin.get();

    return 0;
}

std::vector<cv::Point3d> Generate3DPoints()
{
    std::vector<cv::Point3d> points;

    double x, y, z;

    x = .5; y = .5; z = -.5;
    points.push_back(cv::Point3d(x, y, z));

    x = .5; y = .5; z = .5;
    points.push_back(cv::Point3d(x, y, z));

    x = -.5; y = .5; z = .5;
    points.push_back(cv::Point3d(x, y, z));

    x = -.5; y = .5; z = -.5;
    points.push_back(cv::Point3d(x, y, z));

    x = .5; y = -.5; z = -.5;
    points.push_back(cv::Point3d(x, y, z));

    x = -.5; y = -.5; z = -.5;
    points.push_back(cv::Point3d(x, y, z));

    x = -.5; y = -.5; z = .5;
    points.push_back(cv::Point3d(x, y, z));

    for(unsigned int i = 0; i < points.size(); ++i)
    {
        std::cout << points[i] << std::endl << std::endl;
    }

    return points;
}

当我尝试运行projectPoints方法并且不知道为什么时,应用程序崩溃。任何帮助将不胜感激。

最佳答案

似乎在提示点的输出 vector 的类型。尝试将您的调用替换为projectPoints:

cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints);

通过这个电话:
cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, imagePoints);

这将使用std::vector<cv::Point2d>类型的变量而不是std::vector<cv::Point2f>

关于c++ - OpenCV的projectPoints函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25244603/

10-11 07:21