gpt4 book ai didi

c++ - 尝试运行 projectPoints 时断言失败

转载 作者:行者123 更新时间:2023-11-28 00:32:43 26 4
gpt4 key购买 nike

我不断收到 断言失败(0 <= i && i < (int)vv.size())in cv::_InputArray::getMat, in尝试运行 projectPoints 时文件 matrix.cpp。我已经检查了所有我能想到的东西,但我无法找出问题所在。我的猜测是校准相机出了点问题,但我不知道如何找出问题所在。这是我的代码,谢谢。

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

float x,y,z;

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

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

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

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

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

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

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

/*
for(unsigned int i = 0; i < points.size(); ++i)
{
std::cout << points[i] << std::endl;
}
*/
return points;
}
int main()
{
int numBoards = 4;
int numCornersHor = 6;
int numCornersVer = 9;
int numSquares = numCornersHor * numCornersVer;
Size board_sz = Size(numCornersHor, numCornersVer);

VideoCapture capture = VideoCapture(0);

vector<vector<Point3f>> object_points;
vector<vector<Point2f>> imagePoints;
vector<vector<Point2f>> image_points;
vector<Point3f> objectPoints1 = Generate3DPoints();

vector<Point2f> corners;
int successes=0;

Mat image;
Mat gray_image;
//capture >> image;
Sleep(1000);
capture.read(image);

imshow("Welcome", image);
vector<Point3f> obj;
for(int j=0;j<numSquares;j++)
obj.push_back(Point3f(j/numCornersHor, j%numCornersHor, 0.0f));

while(successes<numBoards)
{
cvtColor(image, gray_image, CV_BGR2GRAY);
bool found = findChessboardCorners(image, board_sz, corners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);

if(found)
{
cornerSubPix(gray_image, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
drawChessboardCorners(gray_image, board_sz, corners, found);
cout << "SUCESS\n";

}
else
cout << "FAIL\n";

imshow("win1", image);
imshow("win2", gray_image);

capture.read(image);
image_points.push_back(corners);
object_points.push_back(obj);
printf("Snap stored!\n");
char continues;
cout << "Press c to continue\n";
cin >> continues;
successes++;

//if(successes>=numBoards)
// break;


}

Mat intrinsic = Mat(3, 3, CV_32FC1);
Mat distCoeffs;
vector<Mat> rvecs;
vector<Mat> tvecs;

intrinsic.ptr<float>(0)[0] = 1;
intrinsic.ptr<float>(1)[1] = 1;

calibrateCamera(object_points, image_points, image.size(), intrinsic, distCoeffs, rvecs, tvecs);

cout << "Done\n";


projectPoints(objectPoints1, rvecs, tvecs, intrinsic, distCoeffs, imagePoints);

cout << "done2";

return 0;
}

最佳答案

您正试图通过 rvecstvecs到函数 projectPoints ,期望 Mat而不是 vector<Mat> .

基本上,calibrateCamera函数使用棋盘的多个图像估计相机的内在矩阵和畸变系数。该函数还返回多个版本的外部参数(旋转+平移),一个用于您使用的每个图像。这些外部参数存储在 rvecs 中和 tvecs .

但是,您只想在这些图像中的一个中投影 3D 点,因此您应该选择所需的一组外部参数(即 rvecs[i]tvecs[i]i 对应于您在其中的图像想要投影您的对象)并调用 projectPoints仅针对这些外部参数,而不是整个 vectors<Mat> .

关于c++ - 尝试运行 projectPoints 时断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22202178/

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