gpt4 book ai didi

c++ - OpenCV calibrateCamera 断言失败

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

几天来,我一直在用棋盘示例进行相机校准。一切都很好(找到并显示角,然后馈送到数组)直到我调用最终函数 calibrateCamera。比我得到断言错误:

OpenCV 错误:calibrateCamera 断言失败(nimages > 0),文件/home/ig/Downloads/opencv-2.4.8/modules/calib3d/src/calibration.cpp,第 3415 行

这是经典代码:

    #include <iostream>
#include <fstream>
#include <vector>

#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
VideoCapture captR(0); // open the video camera no. 0 (RIGHT)

if (!captR.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam 0" << endl;
return -1;
}

namedWindow("MyVideo (RIGHT)",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

namedWindow("Grayscale",CV_WINDOW_AUTOSIZE); //create a window called "Grayscale"

int a = 0; // Frame counter

int numCornersHor = 7; // Chessboard dimensions
int numCornersVer = 5;
int numSquares = numCornersHor * numCornersVer;
Size boardSize = Size(numCornersHor, numCornersVer);

Mat frameR;
// Mat frameL;
Mat gray_frame;

vector<Point3f> obj;
vector<Point2f> corners; // output vectors of image points

for (int i=0; i<boardSize.height; i++) {
for (int j=0; j<boardSize.width; j++) {
obj.push_back(Point3f(i, j, 0.0f));
}
}

while (1){

int key = waitKey(30);

bool bCaptSuccessR = captR.read(frameR); // read a new frame from video

if (!bCaptSuccessR) //if capture not succeded, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}

vector<vector<Point3f> > object_points;
vector<vector<Point2f> > image_points;

// make grayscale frame version for conerSubPix
cvtColor(frameR, gray_frame, CV_BGR2GRAY);

// Get the chessboard corners
bool found = findChessboardCorners(frameR, boardSize, corners);

if (found) {
// Increase accuracy by subpixels
cornerSubPix(gray_frame, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 0.1));
drawChessboardCorners(gray_frame, boardSize, corners, found);
imshow("Grayscale", gray_frame);

////////////////////////////////////////////
if(key==32){ // Save good found by pressing [space]
image_points.push_back(corners);
object_points.push_back(obj);
cout << "Captured good calibration image, No " << a << endl;
cout << "corners: " << corners << endl;
//cout << "obj: " << obj << endl;
a++;
}
}

imshow("MyVideo (RIGHT)", frameR); //show right webcam frame in "MyVideo" window

if (key == 27) { //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
cout << "esc key is pressed by user" << endl;
break;
}

if (key == 115){ // If 'S' key pressed begin calibration

//////////// BEGIN CALIBRATION ////////////////////////
cout << "Callibration started..." << endl;

Mat cameraMatrix = Mat(3, 3, CV_64F);
cameraMatrix.at<double>(0,0) = 1.0;

Mat distCoeffs;
distCoeffs = Mat::zeros(8, 1, CV_64F);

vector<Mat> rvecs;
vector<Mat> tvecs;

Size imageSize = frameR.size();

calibrateCamera(object_points, image_points, imageSize, cameraMatrix, distCoeffs, rvecs, tvecs);

cout << "Callibration ended." << endl;



}//callibration
}

captR.release();

return 0;

}

这是带有行号的 OpenCV 文件摘录:

3400    double cv::calibrateCamera( InputArrayOfArrays _objectPoints,
3401 InputArrayOfArrays _imagePoints,
3402 Size imageSize, InputOutputArray _cameraMatrix, InputOutputArray _distCoeffs,
3403 OutputArrayOfArrays _rvecs, OutputArrayOfArrays _tvecs, int flags, TermCriteria criteria )
3404 {
3405 int rtype = CV_64F;
3406 Mat cameraMatrix = _cameraMatrix.getMat();
3407 cameraMatrix = prepareCameraMatrix(cameraMatrix, rtype);
3408 Mat distCoeffs = _distCoeffs.getMat();
3409 distCoeffs = prepareDistCoeffs(distCoeffs, rtype);
3410 if( !(flags & CALIB_RATIONAL_MODEL) )
3411 distCoeffs = distCoeffs.rows == 1 ? distCoeffs.colRange(0, 5) : distCoeffs.rowRange(0, 5);
3412
3413 int i;
3414 size_t nimages = _objectPoints.total();
3415 CV_Assert( nimages > 0 );
3416 Mat objPt, imgPt, npoints, rvecM((int)nimages, 3, CV_64FC1), tvecM((int)nimages, 3, CV_64FC1);
3417 collectCalibrationData( _objectPoints, _imagePoints, noArray(),
3418 objPt, imgPt, 0, npoints );
3419 CvMat c_objPt = objPt, c_imgPt = imgPt, c_npoints = npoints;
3420 CvMat c_cameraMatrix = cameraMatrix, c_distCoeffs = distCoeffs;
3421 CvMat c_rvecM = rvecM, c_tvecM = tvecM;
3422
3423 double reprojErr = cvCalibrateCamera2(&c_objPt, &c_imgPt, &c_npoints, imageSize,
3424 &c_cameraMatrix, &c_distCoeffs, &c_rvecM,
3425 &c_tvecM, flags, criteria );
3426
3427 bool rvecs_needed = _rvecs.needed(), tvecs_needed = _tvecs.needed();
3428
3429 if( rvecs_needed )
3430 _rvecs.create((int)nimages, 1, CV_64FC3);
3431 if( tvecs_needed )
3432 _tvecs.create((int)nimages, 1, CV_64FC3);
3433

使用 Linux Ubuntu 12.04、OpenCV 2.4.8、gcc 4.6.3、Eclipse,...

最佳答案

您正在 while 循环内声明 object_pointsimage_points,但可能希望将声明放在循环之外。否则,列表将在每次迭代后(有效地)被清除。

用户按下第一个键,检测棋盘。板被写入 object_pointsimage_points。然后用户按下键来校准相机。第一个键的处理结束,object_pointsimage_points 松散范围(并被清除),第二个键的处理开始,调用 calibrateCamera一个空的 object_points 数组并失败。

在调用 calibrateCamera 之前,您还应该检查 object_points 是否不为空。

关于c++ - OpenCV calibrateCamera 断言失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23062035/

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