gpt4 book ai didi

OpenCV::solvePNP() - 断言失败

转载 作者:太空宇宙 更新时间:2023-11-03 23:02:50 25 4
gpt4 key购买 nike

我正在尝试借助 OpenCV 的 solvePNP() 获取相机的姿势。

运行程序后出现以下错误:

OpenCV Error: Assertion failed (npoints >= 0 && npoints == std::max(ipoints.checkVector(2, CV_32F), ipoints.checkVector(2, CV_64F))) in solvePnP, file /opt/local/var/macports/build/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_graphics_opencv/opencv/work/OpenCV-2.4.2/modules/calib3d/src/solvepnp.cpp, line 55
libc++abi.dylib: terminate called throwing an exception

我试图搜索如何解决这些错误,但不幸的是我无法解决!

这是我的代码,非常感谢所有评论/帮助:

enum Pattern { NOT_EXISTING, CHESSBOARD, CIRCLES_GRID, ASYMMETRIC_CIRCLES_GRID };

void calcBoardCornerPositions(Size boardSize, float squareSize, vector<Point3f>& corners,
Pattern patternType)
{
corners.clear();

switch(patternType)
{
case CHESSBOARD:
case CIRCLES_GRID:
for( int i = 0; i < boardSize.height; ++i )
for( int j = 0; j < boardSize.width; ++j )
corners.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));
break;

case ASYMMETRIC_CIRCLES_GRID:
for( int i = 0; i < boardSize.height; i++ )
for( int j = 0; j < boardSize.width; j++ )
corners.push_back(Point3f(float((2*j + i % 2)*squareSize), float(i*squareSize), 0));
break;
}
}

int main(int argc, char* argv[])
{
float squareSize = 50.f;

Pattern calibrationPattern = CHESSBOARD;

//vector<Point2f> boardCorners;
vector<vector<Point2f> > imagePoints(1);
vector<vector<Point3f> > boardPoints(1);

Size boardSize;
boardSize.width = 9;
boardSize.height = 6;

vector<Mat> intrinsics, distortion;
string filename = "out_camera_xml.xml";
FileStorage fs(filename, FileStorage::READ);
fs["camera_matrix"] >> intrinsics;
fs["distortion_coefficients"] >> distortion;
fs.release();

vector<Mat> rvec, tvec;
Mat img = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE); // at kell adnom egy kepet

bool found = findChessboardCorners(img, boardSize, imagePoints[0], CV_CALIB_CB_ADAPTIVE_THRESH);

calcBoardCornerPositions(boardSize, squareSize, boardPoints[0], calibrationPattern);
boardPoints.resize(imagePoints.size(),boardPoints[0]);

//***Debug start***
cout << imagePoints.size() << endl << boardPoints.size() << endl << intrinsics.size() << endl << distortion.size() << endl;
//***Debug end***

solvePnP(Mat(boardPoints), Mat(imagePoints), intrinsics, distortion, rvec, tvec);

for(int i=0; i<rvec.size(); i++) {
cout << rvec[i] << endl;
}

return 0;
}

编辑(一些调试信息):

我一行一行调试的。我进入了所有的功能。我在 SolvePNP(...) 中得到断言失败。您可以在下面看到我进入 solvePNP 函数时看到的内容。首先它跳过第一个 if 语句/if(vec.empty())/,然后进入第二个 if 语句/if( !copyData )/,当它执行最后一行/*datalimit = dataend = datastart + rows*step[0]*/跳回到第一个 if 语句并返回 => 比我得到断言失败错误。

template<typename _Tp> inline Mat::Mat(const vector<_Tp>& vec, bool copyData)
: flags(MAGIC_VAL | DataType<_Tp>::type | CV_MAT_CONT_FLAG),
dims(2), rows((int)vec.size()), cols(1), data(0), refcount(0),
datastart(0), dataend(0), allocator(0), size(&rows)
{
if(vec.empty())
return;
if( !copyData )
{
step[0] = step[1] = sizeof(_Tp);
data = datastart = (uchar*)&vec[0];
datalimit = dataend = datastart + rows*step[0];
}
else
Mat((int)vec.size(), 1, DataType<_Tp>::type, (uchar*)&vec[0]).copyTo(*this);
}

最佳答案

进入调试器中的函数并准确查看哪个断言失败。 (可能它需要 double 值(CV_64F)而不是 float 。)

支持 OpenCV 的新“输入数组”包装器,允许您调用具有任何形状的垫子、点矢量等的函数 - 它会对其进行排序。但是许多函数采用特定的输入格式或具有强制执行特定格式的过时断言。

立体声/校准系统对特定布局的要求最差,而且频繁的连续操作需要不同的布局。

关于OpenCV::solvePNP() - 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12808702/

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