cvCalibrateCamera2() crashes in OpenCV 3.x

I have some code which performs camera calibration using a chessboard. This code was derived fro msome tutorials and works well with OpenCV 2.4:

   CvSize board_sz = cvSize( board_w, board_h );
   //Allocate storage for the parameters according to total number of corners and number of snapshots
   CvMat* image_points      = cvCreateMat(n_boards*board_total,2,CV_32FC1); if (!image_points) return NULL;
   CvMat* object_points     = cvCreateMat(n_boards*board_total,3,CV_32FC1); if (!object_points) return NULL;
   CvMat* point_counts      = cvCreateMat(n_boards,1,CV_32SC1);             if (!point_counts) return NULL;
   CvMat* intrinsic_matrix  = cvCreateMat(3,3,CV_32FC1);                    if (!intrinsic_matrix) return NULL;
   CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);                    if (!distortion_coeffs) return NULL;

   CvPoint2D32f* corners = new CvPoint2D32f[ board_total ];
   int successes = 0;
   int step;

   IplImage *gray_image=ocvImageFromBin(bin);

   //Loop while successful captures equals total snapshots
   //Successful captures implies when all the enclosed corners are detected from a snapshot

      //Find chessboard corners:
      cvFindChessboardCorners(gray_image, board_sz, corners,corner_count,CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS|CV_CALIB_CB_NORMALIZE_IMAGE );
      if (*corner_count!=board_total) cvFindChessboardCorners(gray_image, board_sz, corners,corner_count,CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_NORMALIZE_IMAGE);
      if (*corner_count!=board_total) break;

      cvFindCornerSubPix(gray_image,corners,*corner_count,cvSize(11,11),cvSize(-1,-1),cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));

      step = successes*board_total;
      for(int k=step, l=0; l<board_total; ++k,++l )
         CV_MAT_ELEM(*image_points, float,k,0) = corners[l].x;
         CV_MAT_ELEM(*image_points, float,k,1) = corners[l].y;
         CV_MAT_ELEM(*object_points,float,k,0) = (float) l/board_w;
         CV_MAT_ELEM(*object_points,float,k,1) = (float) (l%board_w);
         CV_MAT_ELEM(*object_points,float,k,2) = 0.0f;
	   CV_MAT_ELEM(*point_counts, int,successes,0) = board_total;
   while (false);

   if (*corner_count==board_total )
      // Initialize the intrinsic matrix with both the two focal lengths in a ratio of 1.0
      CV_MAT_ELEM(*intrinsic_matrix,float,0,0)=1.0f;   CV_MAT_ELEM(*intrinsic_matrix,float,0,1)=0.0f;   CV_MAT_ELEM(*intrinsic_matrix,float,0,2)=0.0f;
      CV_MAT_ELEM(*intrinsic_matrix,float,1,0)=0.0f;   CV_MAT_ELEM(*intrinsic_matrix,float,1,1)=1.0f;   CV_MAT_ELEM(*intrinsic_matrix,float,1,2)=0.0f;
      CV_MAT_ELEM(*intrinsic_matrix,float,2,0)=0.0f;   CV_MAT_ELEM(*intrinsic_matrix,float,2,1)=0.0f;   CV_MAT_ELEM(*intrinsic_matrix,float,2,2)=0.0f;

      CV_MAT_ELEM(*intrinsic_matrix,float,0,0)=1.0; // fx
      CV_MAT_ELEM(*intrinsic_matrix,float,1,1)=(1.0*calib_data->height)/calib_data->width; // fy

                         intrinsic_matrix, distortion_coeffs,

Amazingly with OpenCV 3.x the call to cvCalibrateCamera2() crashes:

OpenCV Error: Assertion failed (_dst.fixedType()) in convertPointsHomogeneous, file /build/opencv-L2vuMj/opencv-3.2.0+dfsg/modules/calib3d/src/fundam.cpp, line 1029

The crash itself is caused by

CV_Assert( _dst.fixedType() );

in function cv::convertPointsHomogeneous(). It seems, _dst has the wrong type. But: _dst is MatM which is created within cvCalibrateCamera2(). So…is this a problem with the function cvCalibrateCamera2()? Or what else can cause this crash?


you’re trying to use opencv’s deprecated (and already removed in 4.x !) 1.0 C-api.
(you’re ~20 years too late for that)

stop that immediately, and have a look, how it looks in c++ nowadays

and damn, 3.2 is 5 years old …

3.2 is still shipped with Linux LTS versions - so this is what I would name a recent version…

And why do these C-interfaces still exist when they are not supported or not working any more?

it’s not. rather an “artefact” from package maagers

to keep legacy code running (at your own risk).
again, you must not use it to develop anything new,
and if it breaks, you’re on your own