gpt4 book ai didi

c++ - Create Mat from Vector断言失败错误

转载 作者:行者123 更新时间:2023-11-28 05:35:12 26 4
gpt4 key购买 nike

我试图将 Point2f imagePoints 写入 openCV 中的 Mat 图像。我正在关注下面的链接。

Create Mat from vector<point2f>

但我收到“断言失败”错误。请帮忙。

代码:

std::vector<cv::Point3d> objectPoints;
std::vector<cv::Point2d> imagePoints;

cv::Mat intrisicMat(3, 3, cv::DataType<double>::type);
intrisicMat.at<double>(0, 0) = param.focalLength.first;
intrisicMat.at<double>(0, 1) = 0;
intrisicMat.at<double>(0, 2) = param.principalPoint.first;

intrisicMat.at<double>(1, 0) = 0;
intrisicMat.at<double>(1, 1) = param.focalLength.second;
intrisicMat.at<double>(1, 2) = param.principalPoint.second;

intrisicMat.at<double>(2, 0) = 0;
intrisicMat.at<double>(2, 1) = 0;
intrisicMat.at<double>(2, 2) = 1;

cv::Mat rVec(3, 1, cv::DataType<double>::type); // Rotation vector
rVec.at<double>(0) = 0;
rVec.at<double>(1) = 0;
rVec.at<double>(2) = 0;

cv::Mat tVec(3, 1, cv::DataType<double>::type); // Translation vector
tVec.at<double>(0) = 0;
tVec.at<double>(1) = 0;
tVec.at<double>(2) = 0;

cv::Mat distCoeffs(5, 1, cv::DataType<double>::type); // Distortion vector
distCoeffs.at<double>(0) = param.distortionRadial.at(0);
distCoeffs.at<double>(1) = param.distortionRadial.at(1);
distCoeffs.at<double>(2) = param.distortionTangential.first;
distCoeffs.at<double>(3) = param.distortionTangential.second;
distCoeffs.at<double>(4) = param.distortionRadial.at(2);


projectPoints(objectPoints, rVec, tVec, intrisicMat, distCoeffs, imagePoints);
Mat depthImage = Mat(imagePoints);
imwrite("E:/softwares/1.8.0.71/bin/depthImage.jpg", depthImage);
cout << "depthImage.channels()=" << depthImage.channels() << endl;

错误:

OpenCV Error: Assertion failed (image.channels() == 1 || image.channels() == 3 || image.channels() == 4) in cv::imwrite_, file E:\softwares\opencv-3.1.0\opencv-3.1.0\modules\imgcodecs\src\loadsave.cpp, line 455

我的图像有 2 个 channel 。所以 ImWrite() 抛出断言失败错误。如果不是这样,如何使用图像点创建 Mat 图像?

最佳答案

根据您在评论中所写的内容,您似乎正在尝试将 Mat 写入到一个文件。问题是,Mat来自 Vector<Point2f>将给出一个 2 channel 矩阵,它与任何图像格式(灰度、RGB 或 RGBA)都不兼容。

此外,请编辑您的主要帖子以显示代码(使用 markdown),以便更容易阅读并帮助您。

关于c++ - Create Mat from Vector<point2f>断言失败错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38486150/

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