Skip to content

Commit 253f8e1

Browse files
committed
Improve readability of camera calibration (update camera parameters)
1 parent 6bbe54a commit 253f8e1

4 files changed

Lines changed: 35 additions & 25 deletions

File tree

README.mdk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
## Invitation to 3D Vision (Tutorial for Everyone)
1+
## Invitation to 3D Vision: A Tutorial for Everyone

src/camera_calibration.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,54 @@
22

33
int 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+
}

src/distortion_correction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
int main(void)
44
{
5-
cv::Mat K = (cv::Mat_<double>(3, 3) << 434.3588729143197, 0, 476.1230925877231, 0, 432.7138830286992, 289.2709802508451, 0, 0, 1);
6-
cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2918143346191932, 0.1095347774113121, -0.000105133686343854, 4.350475599617356e-005, -0.02083205595737927);
5+
cv::Mat K = (cv::Mat_<double>(3, 3) << 432.7390364738057, 0, 476.0614994349778, 0, 431.2395555913084, 288.7602152621297, 0, 0, 1);
6+
cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2852754904152874, 0.1016466459919075, -0.0004420196146339175, 0.0001149909868437517, -0.01803978785585194);
77

88
// Open an video
99
cv::VideoCapture video;

src/pose_estimation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
int main(void)
44
{
5-
cv::Mat K = (cv::Mat_<double>(3, 3) << 434.3588729143197, 0, 476.1230925877231, 0, 432.7138830286992, 289.2709802508451, 0, 0, 1);
6-
cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2918143346191932, 0.1095347774113121, -0.000105133686343854, 4.350475599617356e-005, -0.02083205595737927);
5+
cv::Mat K = (cv::Mat_<double>(3, 3) << 432.7390364738057, 0, 476.0614994349778, 0, 431.2395555913084, 288.7602152621297, 0, 0, 1);
6+
cv::Mat dist_coeff = (cv::Mat_<double>(5, 1) << -0.2852754904152874, 0.1016466459919075, -0.0004420196146339175, 0.0001149909868437517, -0.01803978785585194);
77
cv::Size board_pattern(10, 7);
88
double board_cellsize = 0.025;
99

0 commit comments

Comments
 (0)