c++ - OpenCV calibrateCamera assertation failed -


few days i'm fighting camera calibration chessboard example. going fine (corners found , displayed, feed arrays) till call final function calibratecamera. assertation error:

opencv error: assertion failed (nimages > 0) in calibratecamera, file /home/ig/downloads/opencv-2.4.8/modules/calib3d/src/calibration.cpp, line 3415

here classic code:

    #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 video camera no. 0 (right)          if (!captr.isopened())  // if not success, exit program         {             cout << "cannot open video cam 0" << endl;             return -1;         }          namedwindow("myvideo (right)",cv_window_autosize); //create window called "myvideo"          namedwindow("grayscale",cv_window_autosize); //create window called "grayscale"          int = 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          (int i=0; i<boardsize.height; i++) {             (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 new frame video              if (!bcaptsuccessr) //if capture not succeded, break loop             {                  cout << "cannot read frame video stream" << endl;                  break;             }              vector<vector<point3f> > object_points;             vector<vector<point2f> > image_points;              // make grayscale frame version conersubpix             cvtcolor(framer, gray_frame, cv_bgr2gray);              // chessboard corners             bool found = findchessboardcorners(framer, boardsize, corners);              if (found) {                 // increase accuracy 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 found pressing [space]                     image_points.push_back(corners);                     object_points.push_back(obj);                     cout << "captured calibration image, no " << << 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 'esc' key press 30ms. if 'esc' key pressed, break loop                 cout << "esc key pressed 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;      } 

and here opencv file excerpt line numbers:

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 

using linux ubuntu 12.04, opencv 2.4.8, gcc 4.6.3, eclipse, ...

you declaring object_points , image_points within while loop, want put declarations outside of loop. otherwise, lists (effectively) cleared after each iteration.

the user presses first key, detect checkerboards. boards written object_points , image_points. user presses key calibrate camera. processing of first key ends, object_points , image_points loose scope (and cleared), processing of second key starts, calibratecamera called empty object_points array , fails.

you should check whether object_points not empty before calling calibratecamera.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -