OpenCV 校准代码

Posted

技术标签:

【中文标题】OpenCV 校准代码【英文标题】:OpenCV calibration Code 【发布时间】:2013-04-16 01:56:41 【问题描述】:

嗯,这是我为 iPhone 5 校准的尝试。它很糟糕,我不知道为什么。它似乎正确地找到了角落,是的,需要校准 27 张图像。结果是对棋盘的立体主义重新解释。谁能帮我找出错误?我很高兴与世界分享最终版本以及最终参数,这样其他人就不必做这一切了。

int board_w = 8; // Board width in squares
int board_h = 5; // Board height
int n_boards = 20; // Maximum number of boards
int board_n = board_w * board_h;//Number of internal corners. 

cv::Size board_sz = cv::Size( board_w, board_h ); // Dimensions of board


cv::vector<cv::vector<cv::Point3f>> object_points; // place to store location of points in 3d. 
cv::vector<cv::vector<cv::Point2f>> image_points; // place to store points in 2d. 


cv::Mat point_counts   =     cv::Mat( n_boards, 1, CV_32SC1 );
cv::Mat intrinsic_matrix  =  cv::Mat( 3, 3, CV_32FC1 );
cv::Mat distortion_coeffs =  cv::Mat( 5, 1, CV_32FC1 );


cv::vector<cv::Mat> rvecs;
cv::vector<cv::Mat> tvecs;

std::vector<cv::Point2f> imageCorners;


for (int i=1; i<27; i++)



    NSLog(@"Processing image %d", i);


    NSString *currentPhoto = [[NSString alloc] initWithFormat:@"Board%d", i];

    NSString* pathToImageFile = [[NSBundle mainBundle] pathForResource:currentPhoto ofType:@"jpeg"];

    UIImage* image = [UIImage imageWithContentsOfFile:pathToImageFile];

    cv::Mat imageMat = [self cvMatFromUIImage:image];
    cv::Mat imageMatGray;
    cv::cvtColor(imageMat,imageMatGray,CV_RGB2GRAY);




    // Find chessboard corners:
    int found = cv::findChessboardCorners( imageMat, board_sz, imageCorners, CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS );




    // Get subpixel accuracy on those corners


    cv::cornerSubPix(imageMatGray, imageCorners,  cvSize( 11, 11 ),
                     cvSize( -1, -1 ), cv::TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));





    cv::drawChessboardCorners(imageMat, board_sz, imageCorners, found);




    self.image.image = [self UIImageFromCVMat:imageMat];



    cv::vector<cv::Point3f> obj;
    for(int j=0;j<40;j++)
    
        obj.push_back(cv::Point3f(j/board_h, j%board_h, 0.0f));

    

    image_points.push_back(imageCorners);
    object_points.push_back(obj);








intrinsic_matrix.ptr<float>(0)[0] = 1;
intrinsic_matrix.ptr<float>(1)[1] = 1;



NSString* pathToImageFile2 = [[NSBundle mainBundle] pathForResource:@"Board200" ofType:@"JPG"];

NSLog(pathToImageFile2);


UIImage* image2 = [UIImage imageWithContentsOfFile:pathToImageFile2];
![enter image description here][1]

self.image.image = image2;

cv::Mat distortedImage = [self cvMatFromUIImage:image2];


cv::Mat undistortedImage;




calibrateCamera(object_points, image_points, distortedImage.size(), intrinsic_matrix, distortion_coeffs, rvecs, tvecs);




cv::undistort(distortedImage, undistortedImage, intrinsic_matrix, distortion_coeffs);



image2 = [self UIImageFromCVMat:undistortedImage];

self.image.image = image2;

【问题讨论】:

【参考方案1】:

小错误。

cv::Size board_sz = cv::Size( board_w, board_h ); // Dimensions of board

应该是:

cv::Size board_sz = cv::Size( board_h, board_w ); // Dimensions of board

我也在研究这个,所以不确定它是否完美,但这是我发现的一个问题。

【讨论】:

以上是关于OpenCV 校准代码的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV校准相机裁剪错误

用于 OpenCV 的 Iphone 6 相机校准

opencv相机校准object_points方向

在线校准相机中校准相机时出现 OpenCV 运行时错误

OpenCV - 校准鱼眼镜头误差(病态矩阵)

校准期间的 Opencv 错误