gpt4 book ai didi

c++ - projectPoints 的 OpenCV 错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:12:43 26 4
gpt4 key购买 nike

我正在尝试使用 OpenCV 量化相机校准的准确性。在我的程序中,我正在读取棋盘图案的图像并调用 calibrateCamera 函数来初步猜测我的相机内部和外部。我知道仅使用一张图像不会产生完美的校准,并且 calibrateCamera 会返回重投影错误。不过,我想使用 projectPoints 函数,获取校准板上检测到的角点的图像点,以便进一步处理。我正在使用下面的代码进行校准,但当它尝试运行 projectPoints 函数时,程序在运行时崩溃。如果我删除函数调用,代码就可以正常工作。

Mat image_;
Mat gray_image_;
Size chessboard_size_;
vector<Point2f> corners_;
vector< vector< Point2f> > imagePoints_;
vector< Point2f> imagePointsProjected_;
vector< vector< Point3f> > objectPoints_;
bool corners_found;
float measure_ = 35;

chessboard_size_ = Size(CHESSBOARD_INTERSECTIONS_HORIZONTAL, CHESSBOARD_INTERSECTIONS_VERTICAL);

// image of type CV_8UC3 is read, with 8 bit & 3 channels
image_ = imread("/home/fes1rng/left.png");

if(!image_.data )
{
printf( "No image data \n" );
return;
}

// image is converted to grayscale, afterwards it is of type CV_8UC1
cvtColor(image_, gray_image_, CV_RGB2GRAY);

// detect corners and draw them
corners_found = findChessboardCorners(gray_image_, Size(CHESSBOARD_INTERSECTIONS_HORIZONTAL, CHESSBOARD_INTERSECTIONS_VERTICAL), corners_);
if (corners_found)
{
cornerSubPix(gray_image_, corners_, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));

drawChessboardCorners(image_, Size(CHESSBOARD_INTERSECTIONS_HORIZONTAL, CHESSBOARD_INTERSECTIONS_VERTICAL), corners_, corners_found);
}

vector< Point2f> v_tImgPT;
vector< Point3f> v_tObjPT;
//save 2d coordinate and world coordinate
for(int j=0; j< corners_.size(); ++j)
{
Point2d tImgPT;
Point3d tObjPT;

tImgPT.x = corners_[j].x;
tImgPT.y = corners_[j].y;

tObjPT.x = j%CHESSBOARD_INTERSECTIONS_HORIZONTAL*measure_;
tObjPT.y = j/CHESSBOARD_INTERSECTIONS_HORIZONTAL*measure_;
tObjPT.z = 0;

v_tImgPT.push_back(tImgPT);
v_tObjPT.push_back(tObjPT);
}
imagePoints_.push_back(v_tImgPT);
objectPoints_.push_back(v_tObjPT);

Mat rvec(3,1, CV_64FC1);
Mat tvec(3,1, CV_64FC1);
vector<Mat> rvecs;
vector<Mat> tvecs;
rvecs.push_back(rvec);
tvecs.push_back(tvec);
Mat intrinsic_Matrix(3,3, CV_64FC1);
Mat distortion_coeffs(8,1, CV_64FC1);

calibrateCamera(objectPoints_, imagePoints_, image_.size(), intrinsic_Matrix, distortion_coeffs, rvecs, tvecs);

projectPoints(objectPoints_, rvecs, tvecs, intrinsic_Matrix, distortion_coeffs, imagePointsProjected_);

cv::namedWindow( "Display Image", CV_WINDOW_AUTOSIZE );
cv::imshow( "Display Image", image_ );

waitKey(0);

错误信息是:

OpenCV Error: Assertion failed (0 <= i && i < (int)vv.size()) in getMat, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matrix.cpp, line 977
terminate called after throwing an instance of 'cv::Exception'
what(): /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matrix.cpp:977: error: (-215) 0 <= i && i < (int)vv.size() in function getMat

由于错误发生在运行时和子函数调用中,我认为这是由矩阵的错误数据类型引起的。但是由于函数 projectPoints 在 calibrateCamera 内部使用,我很困惑为什么使用相同参数的单个函数调用会导致错误。

最佳答案

作为第一个参数, projectPoints 等待 std::vector<cv::Point3f>而不是 std::vector<std::vector<cv::Point3f>> .

关于c++ - projectPoints 的 OpenCV 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29892044/

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