title: OpenCV calibrateCamera() Assertion failed
body: <p>I have been trying to calibrate my camera quite for a while using Opencv calibrateCamera() function. I have followed the same procedure as described in opencv sample program. I am trying to first load 10 9 x 6 chessboard images. Then finding chessboard corners. If corners are found then corners' pixel location is stored in  vector&lt; vector &lt; Point2f>> ImagePoints. After doing this for all images, runCalibrationAndSave part is executed. In runCalibrationAndSave , first runCalibration part is executed where ObjectPoints (of type vector&lt; vector &lt; Point3f>>) are filled
  with corners' real coordinate values. Upto this point code works well and no problem occurs.Chessboard corners are accurately found and ImagePoints vector is also filled with vectors.   But when it goes to calibrateCamera() part OpenCV:: assertion is failed with following error:</p>
  
  <p>OpenCV Error: </p>
  
  <blockquote>
    <p>Assertion failed (nimages > 0 &amp;&amp; nimages == (int)imagePoints1.total()
    &amp;&amp; (!imgPtMat2 || nimages == (int)imagePoints2.total())) in
    collectCalibrationData, file
    /............/modules/calib3d/src/calibration.cpp, line 3164</p>
  </blockquote>
  
  <p>I did some research for the same problem and found that this problem usually occurs when ObjectPoints vector and ImagePoints vector are not of equal length or if they are not properly filled. But In my case I have checked in debugging mode that both vectors are properly filled with equal lengths.
  For the reference I have attached the code part which  properly runs upto before calibrateCamera() part and then assertion is failed.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;time.h&gt;
  #include &lt;stdio.h&gt;
  
  #include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &lt;opencv2/calib3d/calib3d.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  
  
  using namespace cv;
  using namespace std;
  class Settings
  {public:
  Size boardSize;            
  float squareSize;          
  
  
  };
  bool runCalibrationAndSave(Settings&amp; s, Size imageSize, Mat&amp;  cameraMatrix, Mat&amp; distCoeffs,
                         vector&lt;vector&lt;Point2f&gt; &gt; imagePoints );
  
  int main()
  {
  Settings s;
  s.boardSize.width =9;
  s.boardSize.height=6;
  s.squareSize=50;
    Mat cameraMatrix, distCoeffs;
  Size imageSize;
  char filename[512];
  vector&lt;vector&lt;Point2f&gt; &gt; imagePoints;
  for(int counter=0; counter&lt;10; counter++)        
  {sprintf( filename, "chessboard%d.jpg", counter );
  
  IplImage* img = cvLoadImage(filename);
  
  
   cv::Mat&amp; m = cv::cvarrToMat(img);
  
  Mat pointBuf = Mat::zeros(54,2,CV_32FC1);
  vector&lt;Point2f&gt; pointBuf_vec;
  
  bool found=false;
  
  
  found = findChessboardCorners( m,s.boardSize, pointBuf,CV_CALIB_CB_ADAPTIVE_THRESH |    CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE);
  if(found)
  {
  cout&lt;&lt;"check"&lt;&lt;endl;
  Mat viewGray;
                  cvtColor(m, viewGray, CV_BGR2GRAY);
                  cornerSubPix( viewGray, pointBuf, Size(11    ,11),Size(-1,-1),         TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
   drawChessboardCorners( m, s.boardSize, Mat(pointBuf), found );
  
  
  pointBuf_vec.clear();
  
   for(int i=0;i&lt;54;i++)
   {
   Point2f temp;
   temp.x=pointBuf.at&lt;float&gt;(i,0);
   temp.y=pointBuf.at&lt;float&gt;(i,1);
   pointBuf_vec.push_back(temp);
  
   }
  
   imagePoints.push_back(pointBuf_vec);
  
  
  
  
  }
  
  
  imshow("Example1",m);
  cvWaitKey(); 
  
  imageSize = m.size();
  }
  runCalibrationAndSave(s, imageSize,  cameraMatrix, distCoeffs, imagePoints);
  return 0;
  
  
  }
  
  
  
  static void calcBoardCornerPositions(Size boardSize, float squareSize,     vector&lt;Point3f&gt;&amp; corners)
  
  {
  corners.clear();
  
      for( int i = 0; i &lt; boardSize.height; i++ )
          for( int j = 0; j &lt; boardSize.width; j++ )
             { corners.push_back(Point3f(float( j*squareSize ), float( i*squareSize ), 0));
  
          }
  
  }
  
   static bool runCalibration( Settings&amp; s, Size&amp; imageSize, Mat&amp; cameraMatrix, Mat&amp;   distCoeffs,
                          vector&lt;vector&lt;Point2f&gt; &gt; imagePoints, vector&lt;Mat&gt;&amp; rvecs,   vector&lt;Mat&gt;&amp; tvecs,
                          vector&lt;float&gt;&amp; reprojErrs,  double&amp; totalAvgErr)
  
   {
  
  cameraMatrix = Mat::eye(3, 3, CV_64F);
   //   if( s.flag &amp; CV_CALIB_FIX_ASPECT_RATIO )
   //      cameraMatrix.at&lt;double&gt;(0,0) = 1.0;
  
    distCoeffs = Mat::zeros(8, 1, CV_64F);
  
    vector&lt;vector&lt;Point3f&gt; &gt; objectPoints;
    Mat object_pointBuf = Mat::zeros(s.boardSize.width*s.boardSize.height,3,CV_32FC1);
      vector&lt;Point3f&gt; object_pointBuf_vec;
  
    calcBoardCornerPositions(s.boardSize, s.squareSize, object_pointBuf_vec);
    for(int k=0;k&lt;imagePoints.size();k++)
    {
      objectPoints.push_back(object_pointBuf_vec);
    }
  
  
  
  
  
   // objectPoints.resize(imagePoints.size(),objectPoints[0]);
  
    //Find intrinsic and extrinsic camera parameters
    double rms = calibrateCamera(objectPoints, imagePoints, imageSize, cameraMatrix,
                               distCoeffs, rvecs, tvecs, /*s.flag|*  /CV_CALIB_FIX_K4|CV_CALIB_FIX_K5);
  
    cout &lt;&lt; "Re-projection error reported by calibrateCamera: "&lt;&lt; rms &lt;&lt; endl;
  
    bool ok = checkRange(cameraMatrix) &amp;&amp; checkRange(distCoeffs);
  
   //   totalAvgErr = computeReprojectionErrors(objectPoints, imagePoints,
                                    //       rvecs, tvecs, cameraMatrix, distCoeffs,   reprojErrs);
  
    return ok;
  }
  
  
  
  bool runCalibrationAndSave(Settings&amp; s, Size imageSize, Mat&amp;  cameraMatrix, Mat&amp;     distCoeffs,vector&lt;vector&lt;Point2f&gt; &gt; imagePoints )
  
  {
  vector&lt;Mat&gt; rvecs, tvecs;
  vector&lt;float&gt; reprojErrs;
  double totalAvgErr = 0;
  
   bool ok = runCalibration(s,imageSize, cameraMatrix, distCoeffs, imagePoints, rvecs,   tvecs,
                           reprojErrs, totalAvgErr);
    cout &lt;&lt; (ok ? "Calibration succeeded" : "Calibration failed")
      &lt;&lt; ". avg re projection error = "  &lt;&lt; totalAvgErr ;
  
  
   return ok;
  } 
  </code></pre>
  
  <p>I am using Visual C++ and Opencv 2.4.9. Please help me in figuring out the problem. This is first time I am asking any question in SO , please let me know if I made any mistake in asking question. Thanks for help in advance.</p>