gpt4 book ai didi

c++ - 使用数码相机进行相机校准

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:56:48 25 4
gpt4 key购买 nike

我从事相机校准工作大约一周了。我从网上文章和博客上看到的示例使用网络摄像头来捕获图像。

但对于我的场景,我使用的是数码相机,即 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;
}

我正在使用代码块 10.05 和 Opencv 2.3.0,Mingw GNU GCC 编译器。

最佳答案

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

相信您从这台相机获得的图像已经得到校正(关于镜头畸变),但我找不到支持这一说法的引用资料。

至于您使用的多个图像,实际上您只需要一个 即可找到失真。有关使用 OpenCV 的此过程的更多信息,请查看 this answer .

编辑:

既然你提到了图像拼接,OpenCV started to support this feature on version 2.2 :

OpenCV 2.2 is released! Teasers already far along AFTER this release: Panoramic Stitching

关于这个主题,这 interesting post分享一些source code .

关于c++ - 使用数码相机进行相机校准,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8605782/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com