简体   繁体   中英

OpenCV run-time error while calibrating camera in line calibratecamera

I wrote a calibration code as bellow:

int numBoards = 20;
int numCornersHor=6;
int numCornersVer=9;
int numSquares = numCornersHor * numCornersVer;
cv::Size board_sz = cv::Size(numCornersHor, numCornersVer);

std::vector<std::vector<cv::Point3f> > object_points;
std::vector<std::vector<cv::Point2f> > image_points;
std::vector<cv::Point2f> corners;
std::vector<cv::Point3f> obj;
    for(int j=0;j<numSquares;j++)
        obj.push_back(cv::Point3f(j/numCornersHor, j%numCornersHor, 0.0f));

int successes=0;

After initialization of useful variables, I get frames from webcam and store it in buffer.

    while(successes<numBoards)
    {        
        unsigned char* buffer=eyeCamera->getFrame();
        cv::Mat rawImg=cv::Mat(cv::Size(widthCam,heightCam),CV_8UC4, buffer,cv::Mat::AUTO_STEP);
        cv::Mat grayImg;
        cv::cvtColor(rawImg,grayImg,CV_BGR2GRAY);

        bool found = findChessboardCorners(rawImg, board_sz, corners,
                                           CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS);
        if(found)
        {
            cv::cornerSubPix(grayImg, corners, cv::Size(11, 11), cv::Size(-1, -1),
                             cv::TermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 30, 1.1));
            cv::drawChessboardCorners(grayImg, board_sz, corners, found);
        }

        cv::imshow("win2", grayImg);

Everything is ok so far.I show grayImg and chessboard corners are painted.

        int key = cv::waitKey(1);
        if(key==27)
            return;

        if(key==' ' && found!=0)
        {
          image_points.push_back(corners);
          object_points.push_back(obj);

          successes++;

          if(successes>=numBoards)
              break;
        }

    }

    cv::Mat intrinsic = cv::Mat(3, 3, CV_64F);
    cv::Mat distCoeffs= cv::Mat(8, 1, CV_64F);
    std::vector<cv::Mat> rvecs;
    std::vector<cv::Mat> tvecs;
    intrinsic.at<double>(0,0) = 1.0;

    int widthCam=640; 
    int heightCam=480;

object_points and image_points are filled with 54 elements ~ 9 * 6

    cv::calibrateCamera(object_points, image_points, cv::Size(widthCam,heightCam), intrinsic, distCoeffs, rvecs, tvecs);

I'm using Qt creator. I always get run-time error while calling the last line: calibrateCamera()

Edit: I tried the same code with cvCalibrateCamera2 and again i got the same error. I provide opencv Exeption:

OpenCV error: Bad argument (the output array of translation vectors must be 3-channel
1xn or nx1 array or 1-channel nx3 array, where n is the bumber of views) in
cvCalibrateCamera2, file F:\OpenCV\opencv\modules\calib3d\src\calibration.cpp,line 1506
terminate called after throwing an instance of 'cv::Exeption'

I am using 10 snapshot and my defined rvec and tvec are as follows:

CvMat* rvec  = cvCreateMat(10,3,CV_32FC1);
CvMat* tvec  = cvCreateMat(10,3,CV_32FC1);

Can anyone help me solve this error?

Thanks.

Okay, tried this now locally on my system, turns out that the definition of rVecs and tVecs was actually differently from what I expected it to be.

CvMat* rVecs = cvCreateMat( 1, 1, CV_32FC3 );
CvMat* tVecs = cvCreateMat( 1, 1, CV_32FC3 );

did the job for me.

I wonder if CvMat* tvec = cvCreateMat(3,10,CV_32FC1); will help.

also, try CvMat* tvec = cvCreateMat(1,10,CV_32FC3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM