简体   繁体   中英

opencv error: assertion failed in convert.cpp (opencv) when i calibrate a camera

i try to calibrate a camera (opencv 2.3.1, VS 2010 and windows 7), but when i compile my program, there is an opencv error wich is "Assertion Failed:dst.channels<>> in unkown function , file ....modules \\core\\src\\convert.cpp, line 1277".

here a part of code,

CvMat* image_points      = cvCreateMat(n_boards*board_total,2,CV_32FC1);
CvMat* object_points     = cvCreateMat(n_boards*board_total,3,CV_32FC1);
CvMat* point_counts      = cvCreateMat(n_boards,1,CV_32SC1);
CvMat* intrinsic_matrix  = cvCreateMat(3,3,CV_32FC1);
CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);

for ( int ig = 0; ig< n_boards; ig++ ) 
{ 

image= cvLoadImage(names[ig],CV_LOAD_IMAGE_COLOR);  // load image

cvNamedWindow("imageessai", 1);
cvShowImage("imageessai", image);
cvWaitKey(0);
cvDestroyWindow("imageessai");

for (int ik=0; ik<n_boards; ik++)

{       
int found = cvFindChessboardCorners(image, board_sz, corners,        &corner_count,CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS );
cvFindCornerSubPix(gray_image, corners, corner_count, cvSize(11,11),cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 30, 0.1 ));
cvDrawChessboardCorners(image, board_sz, corners, corner_count, found); 

        if( corner_count == board_total ) 
        {

            step= ig*board_total;
            for( int i=step,j=0  ; j<board_total; j++, i++)
                        {

        CV_MAT_ELEM(*image_points, float,i,0) = corners[j].x;
        CV_MAT_ELEM(*image_points, float,i,1) = corners[j].y;
        CV_MAT_ELEM(*object_points,float,i,0) = (float) j/board_w;
        CV_MAT_ELEM(*object_points,float,i,1) = (float) (j%board_w);
        CV_MAT_ELEM(*object_points,float,i,2)=0.0f;
                            }

        CV_MAT_ELEM (*point_counts, int,ig,0) = board_total;  

    printf("\n %d successful Snapshots out of %d collected.\n",ig+1,n_boards);

            }   
    } 

CvMat* object_points2  = cvCreateMat(n_boards*board_total,3,CV_32FC1);
CvMat* image_points2   = cvCreateMat(n_boards*board_total,2,CV_32FC1);
CvMat* point_counts2   = cvCreateMat(n_boards,1,CV_32SC1);

for(int i = 0; i<n_boards*board_total; ++i)
{
  CV_MAT_ELEM( *image_points2, float, i, 0) =CV_MAT_ELEM( *image_points, float, i, 0);
  CV_MAT_ELEM( *image_points2, float,i,1)  =CV_MAT_ELEM( *image_points, float, i, 1);
  CV_MAT_ELEM(*object_points2, float, i, 0) = CV_MAT_ELEM(*object_points, float, i,0) ;
  CV_MAT_ELEM( *object_points2, float, i, 1)= CV_MAT_ELEM(*object_points, float, i,1) ;
  CV_MAT_ELEM( *object_points2, float, i, 2)= CV_MAT_ELEM(*object_points, float, i,2) ;
} 

for(int i=0; i<n_boards; ++i)
{ 
    CV_MAT_ELEM( *point_counts2, int, i, 0)=CV_MAT_ELEM(*point_counts,int,i,0);         

}
cvReleaseMat(&object_points);
cvReleaseMat(&image_points);
cvReleaseMat(&point_counts);


CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0 ) = 1.0f;
CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1 ) = 1.0f;
CvMat* rvec = cvCreateMat(n_boards,3,CV_32F);//matrice de rotation
CvMat* tvec = cvCreateMat(n_boards,3,CV_32F);//matrice de translation
CvMat* H = cvCreateMat(1,3,CV_32FC1);


cvCalibrateCamera2(object_points2, image_points2, point_counts2,  sizeim, intrinsic_matrix, distortion_coeffs, rvec, tvec,0 );

cvFindHomography(object_points2,image_points2,H,0,3,NULL);

i tried to resolve this problem many times, please i need help.

(I'm opening my comment as an answer since Nabiha might not be able to answer to a comment with 1 rep.)

It seems you're passing an image of wrong type (wrong number of channels) to one of the cv functions. From your description it's hard to say but i guess it happens in cvCalibrateCamera2 or cvFindHomograpy.

Assertion also sounds like a runtime error rather than a complie time error.

If it is a runtime error, you'd have to check if the Images you're passing to those functions have the correct setup (channels, same size etc..). This is something the compiler can't check for you since these parameters are dynamic.

You can methodically comment some of the functions (to debug your code) and see if your program still works, in case you have trouble finding out where exactly the code breaks with a debugger. Once you isolate the call, check the documentation on what it expects as parameters of your image and you should be on the right track to solve this.

Calibration is a multi-step process (grabbing images, extracting features, computing homography, computing the instrinic and extrinisic camera parameters). It is also tricky to understand (math-wise) so don't give up. The OpenCV calibration functions work usually pretty well.

调试按钮未激活,解决方案项目的重新扫描按钮未激活,因此无法编译或调试程序样本\\ cpp \\ Calibration.cpp

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