Repository: opencv
Issue: Opencv single camera calibration problem
Labels: priority: low,category: calib3d,platform: ios/osx

The opencv 2.4 operation I used was not a problem, and opencv 3.4 was wrong. OpenCV Error: an Assertion failed (_dst fixedType ()) in convertPointsHomogeneous, file/Volumes/build - storage/build/master_iOS - MAC/OpenCV/modules/calib3d/SRC/fundam CPP, line 1029

CvCalibrateCamera2 parameters should be wrong, but I contrast comment, type is no problem.


##### System information (version)

```
int cube_length=8;
IplImage *show;
int number_image=7;
int a=1;
CvSize image_size; //Size of image
CvSize board_size=cvSize(8, 6);// Cvsize: one of the basic data types of OpenCV is to construct the function, width and height of the Cvsize type, indicating the size of the matrix box and the accuracy of pixels. Similar to the CvPoint structure, but the data members are the width and height of the integer type.
int board_width=board_size.width;
int board_height=board_size.height;
int total_per_image=board_width*board_height;//The total number of corners of each graph.
CvPoint2D32f * image_points_buf = new CvPoint2D32f[total_per_image];  //An array of stored angular coordinates.
CvPoint2D32f * corners=new CvPoint2D32f[total_per_image];//The angular points group of an image.

//Mainly used to transform into matrix form.CvMat* cvCreateMat( int rows, int cols, int type );Rows rows. Cols matrix columns. Type matrix element type, single-channel image of floating-point type.
// the type can be any predefined type, and the structure of the predefined type is as follows: CV_<bit_depth> (S|U|F)C<number_of_channels>.
CvMat * image_points=cvCreateMat(number_image*total_per_image,2,CV_32FC1); //The matrix of the image coordinates of the corner point.
CvMat * object_points=cvCreateMat(number_image*total_per_image,3,CV_32FC1); //The matrix that stores the world coordinates of the angular point.
CvMat * point_counts=cvCreateMat(number_image,1,CV_32SC1); //Stores the number of recognition points for each frame.
CvMat * intrinsic_matrix=cvCreateMat(3,3,CV_32FC1);
CvMat * distortion_coeffs=cvCreateMat(5,1,CV_32FC1);
int count; //Store the actual number of points in each frame.
int found; //Identify the mark bit of the calibrated plate corner and whether the corner point can be detected.
int step;  //Store the step length，step=successes*total_per_image;
int successes=0; //The image frame number initialization of all corner points on the calibration board is successfully found.


for (int K=1; K<7; K++) {
    UIImage *image=[UIImage imageNamed:[NSString stringWithFormat:@"%d",K]];
    //    UIImage *image=[UIImage imageNamed:@"2"];

    show=[self CreateIplImageFromUIImage:(image)];
    image_size=cvGetSize(show);//Size of image

    found=cvFindChessboardCorners(show,board_size,image_points_buf,&count,
                                  CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS);

    if(found==0){

    }else{

        IplImage * gray_image= cvCreateImage(cvGetSize(show),8,1); //Create headers and assign data to IplImage*.
        cvCvtColor(show,gray_image,CV_BGR2GRAY); // cvCvtColor(...),It is the color space conversion function in Opencv, which can realize the conversion of RGB color to HSV,HSI and other color space, and can also be converted to grayscale image.
        //Gets the subpixel corner.
        cvFindCornerSubPix(gray_image,image_points_buf,count,cvSize(11,11),cvSize(-1,-1),
                           cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
        cvDrawChessboardCorners(show,board_size,image_points_buf,count,found);
        if(total_per_image==count){
            step=successes*total_per_image;
            //  for(int i=step,j=0;j<total_per_image;++i,++j)
            for (int j=0;j<total_per_image;j++)
            {
                //total_per_imageIs the total number of angles in an image.
                //Opencv is used to access matrix of each element in the macro, the macro applies only to the single channel matrix, CV_MAT_ELEM (matrix, elemtype, row, col) parameter matrix, to access matrix elemtype: type row of matrix element: the element to allow access to the number of rows col: the number of columns to be access elements
                // cvFindCornerSubPix So, we have the image_point_buf at each Angle point, and now we have it in image_points, and we have one row in each row, and the quotient is row x, and the remainder is column y.
                //Press the array of angular coordinates into the matrix image_points.
                CV_MAT_ELEM(*image_points,float,successes*total_per_image+j,0)=image_points_buf[j].x;
                CV_MAT_ELEM(*image_points,float,successes*total_per_image+j,1)=image_points_buf[j].y;//The points found are stored in coordinates.
                CV_MAT_ELEM(*object_points,float,successes*total_per_image+j,0)=(float)(j/cube_length);
                CV_MAT_ELEM(*object_points,float,successes*total_per_image+j,1)=(float)(j%cube_length); //The number of points found is stored in rows and columns.
                CV_MAT_ELEM(*object_points,float,successes*total_per_image+j,2)=0.0f; //0Single-precision floating point
            }

            CV_MAT_ELEM(*point_counts,int,successes,0)=total_per_image;//Access matrix Angle points.

            successes++;
        }
    }
}
printf("/*****Get the identified images.*****/\n");


//Initialize the camera internal reference matrix.
CV_MAT_ELEM(*intrinsic_matrix,float,0,0)=1.0f;//fx
CV_MAT_ELEM(*intrinsic_matrix,float,1,1)=1.0f;//fy
//
// To assign a value
CvMat * object_points0= cvCreateMat(total_per_image*successes,3,CV_32FC1);
CvMat * image_points0=cvCreateMat(total_per_image*successes,2,CV_32FC1);
CvMat * point_counts0=cvCreateMat(successes,1,CV_32SC1);
for (int i=0;i<successes*total_per_image;i++)
{

    CV_MAT_ELEM(*image_points0,float,i,0)=CV_MAT_ELEM(*image_points,float,i,0);
    CV_MAT_ELEM(*image_points0,float,i,1)=CV_MAT_ELEM(*image_points,float,i,1);
    CV_MAT_ELEM(*object_points0,float,i,0)=CV_MAT_ELEM(*object_points,float,i,0);
    CV_MAT_ELEM(*object_points0,float,i,1)=CV_MAT_ELEM(*object_points,float,i,1);
    CV_MAT_ELEM(*object_points0,float,i,2)=0.0f;
}
for (int i=0;i<successes;i++)
{
    CV_MAT_ELEM(*point_counts0,int,i,0)=CV_MAT_ELEM(*point_counts,int,i,0);
}

CvMat * camera_matrix=cvCreateMat(3,3,CV_32FC1);
CvMat * distortion_coe=cvCreateMat(5,1,CV_32FC1);
CvMat * rotation_vectors=cvCreateMat(successes,3,CV_32FC1);
CvMat * translation_vectors=cvCreateMat(successes,3,CV_32FC1);



cvCalibrateCamera2(object_points0,image_points0,point_counts0,image_size,camera_matrix
                   //,distortion_coeffs,NULL,NULL,flags);
                   ,distortion_coeffs,rotation_vectors,translation_vectors,0);
```