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

`projectPoints`是OpenCV库中的一个函数,它用于将3D点投影到2D图像平面上,通常用于相机校准或者3D场景重建中。该函数的原型如下: ```cpp void cv::projectPoints(InputArray objectPoints, InputArray rvec, InputArray tvec, InputArray cameraMatrix, InputArray distCoeffs, OutputArray imagePoints, OutputArray jacobian = noArray(), double aspectRatio = 0 ); ``` 这里的参数意义如下: - `objectPoints`: 输入的三维点,每个点可以是一个3x1向量,表示点的(x,y,z)坐标。 - `rvec`: 旋转向量,表示从世界坐标到相机坐标的旋转。 - `tvec`: 平移向量,表示从世界坐标到相机坐标的平移。 - `cameraMatrix`: 相机内参矩阵,包含焦距和主点坐标。 - `distCoeffs`: 相机畸变系数,用于校正镜头畸变。 - `imagePoints`: 输出的二维点,表示在图像平面的(x,y)坐标。 - `jacobian`: 可选参数,计算投影点的雅可比矩阵。 - `aspectRatio`: 可选参数,用于畸变模型,当设置为0时,使用`cameraMatrix`中的fx和fy值。 若要使用`projectPoints`函数将二维坐标转换为三维坐标,需要有额外的信息,包括相机的内外参数(即`cameraMatrix`和`distCoeffs`),以及必要的旋转和平移向量(即`rvec`和`tvec`)。但需要注意的是,`projectPoints`是将三维坐标投影到二维平面,如果你想要从图像平面的二维点恢复到三维空间的点,通常需要使用三角测量的方法,这涉及至少两个相机视角对同一三维点的观测。 因此,如果你的目标是实现从二维到三维的转换,你需要考虑使用立体视觉的原理,结合多视角几何以及相机标定参数来推算出三维坐标。
评论 22
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值