Well, this is my attempt at calibration for the iPhone 5. It sucks, and I don't know why. It appears to be finding the corners appropriately, and yes 27 images to calibrate. The results are cubist reinterperetations of chess boards. Can anyone help me find the errors? The final version I'm happy to share with the world as well as the final parameters so that others may not have to do all this.
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;