22
33int main (void )
44{
5+ bool select_images = false ;
56 cv::Size board_pattern (10 , 7 );
67 double board_cellsize = 0.025 ;
78
89 // Open an video
910 cv::VideoCapture video;
10- if (!video.open (" data/chessboard.avi " )) return -1 ;
11+ if (!video.open (" data/chessboard/%02d.png " )) return -1 ;
1112
12- // Select images and find 2D corner points from them
13- cv::Size image_size;
14- std::vector<std::vector<cv::Point2f> > image_points;
13+ // Select images
14+ std::vector<cv::Mat> images;
1515 while (true )
1616 {
1717 // Grab an image from the video
1818 cv::Mat image;
1919 video >> image;
2020 if (image.empty ()) break ;
21- image_size = image.size ();
2221
23- // Show the image and keep it if necessary
24- cv::imshow (" 3DV Tutorial: Camera Calibration" , image);
25- int key = cv::waitKey (1 );
26- if (key == 27 ) break ; // 'ESC' key
27- else if (key == 32 ) // 'Space' key
22+ if (select_images)
2823 {
29- std::vector<cv::Point2f> pts;
30- bool complete = cv::findChessboardCorners (image, board_pattern, pts);
31- cv::drawChessboardCorners (image, board_pattern, pts, complete);
24+ // Show the image and keep it if necessary
3225 cv::imshow (" 3DV Tutorial: Camera Calibration" , image);
33- key = cv::waitKey ();
34- if (key == 27 ) break ; // 'ESC' key
35- else if (complete && key == 13 ) // 'Enter ' key
26+ int key = cv::waitKey (1 );
27+ if (key == 27 ) break ; // 'ESC' key
28+ else if (key == 32 ) // 'Space ' key
3629 {
37- image_points.push_back (pts);
38- std::cout << image_points.size () << " images are selected for camera calibration." << std::endl;
30+ std::vector<cv::Point2f> pts;
31+ bool complete = cv::findChessboardCorners (image, board_pattern, pts);
32+ cv::Mat display = image.clone ();
33+ cv::drawChessboardCorners (display, board_pattern, pts, complete);
34+ cv::imshow (" 3DV Tutorial: Camera Calibration" , display);
35+ key = cv::waitKey ();
36+ if (key == 27 ) break ; // 'ESC' key
37+ else if (key == 13 ) images.push_back (image); // 'Enter' key
3938 }
4039 }
40+ else images.push_back (image);
4141 }
4242 video.release ();
43+ if (images.empty ()) return -1 ;
44+
45+ // Find 2D corner points from the given images
46+ std::vector<std::vector<cv::Point2f> > image_points;
47+ for (size_t i = 0 ; i < images.size (); i++)
48+ {
49+ std::vector<cv::Point2f> pts;
50+ if (cv::findChessboardCorners (images[i], board_pattern, pts))
51+ image_points.push_back (pts);
52+ }
4353 if (image_points.empty ()) return -1 ;
4454
4555 // Prepare 3D points from the chess board
@@ -53,7 +63,7 @@ int main(void)
5363 cv::Mat K = cv::Mat::eye (3 , 3 , CV_64F);
5464 cv::Mat dist_coeff = cv::Mat::zeros (4 , 1 , CV_64F);
5565 std::vector<cv::Mat> rvecs, tvecs;
56- double rms = cv::calibrateCamera (object_points, image_points, image_size , K, dist_coeff, rvecs, tvecs);
66+ double rms = cv::calibrateCamera (object_points, image_points, images[ 0 ]. size () , K, dist_coeff, rvecs, tvecs);
5767
5868 // Report calibration results
5969 std::ofstream report (" camera_calibration.txt" );
@@ -65,4 +75,4 @@ int main(void)
6575 report << " * Distortion coefficient (k1, k2, p1, p2, k3, ...) = " << std::endl << " " << dist_coeff.t () << std::endl;
6676 report.close ();
6777 return 0 ;
68- }
78+ }
0 commit comments