OpenCV的projectPoints函数用法

函数cvProjectPoints2通过给定的内参数和外参数计算三维点投影到二维图像平面上的坐标。 

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

#include <iostream>
#include <string>

using namespace std;

vector<cv::Point3f> Generate3DPoints();

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

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

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

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

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

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

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

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

    std::vector<cv::Point2f> projectedPoints;
    cv::projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, projectedPoints);

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

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

    return 0;
}

vector<cv::Point3f> Generate3DPoints()
{
    vector<cv::Point3f> points;

    float x, y, z;

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

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

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

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

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

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

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

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

    return points;
}

参考:http://www.itkeyword.com/doc/8898489402460001x144/opencvs-projectpoints-function

猜你喜欢

转载自blog.csdn.net/lyq_12/article/details/82800790
今日推荐