OpenCV的projectPoints函数 [英] OpenCV's projectPoints function

查看:973
本文介绍了OpenCV的projectPoints函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to implement a example of OpenCV's projectPoints method. The idea behind this method is taking as input a set of 3D points, translation/rotation vector's of a given camera and its distortion coeficients, output the corresponding 2D points in the image plane. The source of code is as follows:

#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方法时,应用程序崩溃,为什么。任何帮助将非常感激。

The application crashes when I try to run the projectPoints method and I have no idea why. Any help would be greatly appreciated.

推荐答案

似乎抱怨点的输出向量的类型。尝试将您的调用替换为 projectPoints

It seems to be complaining about the type of the output vector of points. Try to replace your call to 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>

这篇关于OpenCV的projectPoints函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆