2013-04-16 72 views
0

嗯,这是我为iPhone 5进行校准的尝试。它很糟糕,我不知道为什么。它似乎在适当找到角落,并且有27个图像需要校准。结果是棋盘的立体主义重新引导。任何人都可以帮我找到错误吗?我很高兴与世界分享最终版本以及最终参数,以便其他人可能不必这样做。OpenCV校准代码

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 

我也是在做这个,所以不知道它的作品完美与否,但这是一个问题,我发现。