gpt4 book ai didi

c++ - OpenCV的projectPoints函数

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:04:25 35 4
gpt4 key购买 nike

我目前正在尝试实现 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/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com