使用数码相机校准相机

时间:2011-12-22 15:09:10

标签: c++ opencv camera-calibration

我一直在研究Camera Calibration大约一周知道..我从在线文章和博客中看到的例子使用网络摄像头拍摄图像..

但是对于我的场景我正在使用数码相机即Casio Exilim EX-Z77 ..我将图像添加到设定的程序参数中并使用for循环单独访问它们。这样我就能模仿出来了网络摄像头工作..

我有可能得到正确的扭曲和内在吗? 如果我错了或有误解,请纠正我。

谢谢你,并向大家致意..

我是编程的新手所以请光临我.. here是基于我的代码的文章..下面的代码是我能够做的..

     int n_boards = 0;
     int board_w;
     int board_h;
     using namespace std;
    int main( int argc, char *argv[] )
    {
 board_w = 5; // Board width in squares
 board_h = 8; // Board height
 n_boards = 16; // Number of boards
 int board_n = board_w * board_h;
 CvSize board_sz = cvSize( board_w, board_h );

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

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

      int a;
      for(a =1; a<=n_boards; a++){

   while( successes < n_boards ){

        IplImage *image = cvLoadImage(argv[a]);
        IplImage *gray_image = cvCreateImage( cvGetSize( image ), 8, 1 );

        int found = cvFindChessboardCorners( image, board_sz, corners,
            &corner_count, CV_CALIB_CB_ADAPTIVE_THRESH |         CV_CALIB_CB_FILTER_QUADS );

        // Get subpixel accuracy on those corners
        cvCvtColor( image, gray_image, CV_BGR2GRAY );
        cvFindCornerSubPix( gray_image, corners, corner_count, cvSize( 11, 11 ),
            cvSize( -1, -1 ), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));

        // Draw it
        cvDrawChessboardCorners( image, board_sz, corners, corner_count, found );
        //cvShowImage( "Calibration", image );

        // If we got a good board, add it to our data
        if( corner_count == board_n ){
            step = successes*board_n;
            for( int i=step, j=0; j < board_n; ++i, ++j ){
                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 ) = j/board_w;
                CV_MAT_ELEM( *object_points, float, i, 1 ) = j%board_w;
                CV_MAT_ELEM( *object_points, float, i, 2 ) = 0.0f;
            }
            CV_MAT_ELEM( *point_counts, int, successes, 0 ) = board_n;
            successes++;
        }

}
  IplImage *image1 = cvLoadImage(argv[1]);
CvMat* object_points2 = cvCreateMat( successes*board_n, 3, CV_32FC1 );
CvMat* image_points2 = cvCreateMat( successes*board_n, 2, CV_32FC1 );
CvMat* point_counts2 = cvCreateMat( successes, 1, CV_32SC1 );


// Transfer the points into the correct size matrices
for( int i = 0; i < successes*board_n; ++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 < successes; ++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.0;
CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1 ) = 1.0;

cvCalibrateCamera2( object_points2, image_points2, point_counts2, cvGetSize( image1 ),
    intrinsic_matrix, distortion_coeffs, NULL, NULL, CV_CALIB_FIX_ASPECT_RATIO );

cvSave( "Intrinsics.xml", intrinsic_matrix );
cvSave( "Distortion.xml", distortion_coeffs );

// Example of loading these matrices back in
CvMat *intrinsic = (CvMat*)cvLoad( "Intrinsics.xml" );
CvMat *distortion = (CvMat*)cvLoad( "Distortion.xml" );

IplImage* mapx = cvCreateImage( cvGetSize( image1 ), IPL_DEPTH_32F, 1 );
IplImage* mapy = cvCreateImage( cvGetSize( image1 ), IPL_DEPTH_32F, 1 );
cvInitUndistortMap( intrinsic, distortion, mapx, mapy );

cvNamedWindow( "Undistort" );

while( image1 ){
    IplImage *t = cvCloneImage( image1 );
    cvShowImage( "Calibration", image ); // Show raw image
    cvRemap( t, image1, mapx, mapy ); // undistort image
    cvReleaseImage( &t );
    cvShowImage( "Undistort", image1 ); // Show corrected image

    }
}

return 0;

}

我正在使用Code block 10.05和Opencv 2.3.0,Mingw GNU GCC编译器..

1 个答案:

答案 0 :(得分:2)

卡西欧Exilim EX-Z77等数码相机通常会在相机内执行一定量的图像校正。

相信你从这台相机拍摄的图像已经得到纠正(关于镜头失真),但我找不到支持这种说法的参考。

对于您正在使用的多个图像,实际上您只需要一个来查找失真。有关使用OpenCV检查此过程的更多信息,请检查this answer

修改

由于您提到了图像拼接,OpenCV started to support this feature on version 2.2

  

OpenCV 2.2已经发布!此版本发布之后已经很久了:全景拼接

关于此主题,此interesting post分享了一些source code