Rev 3 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3 | Rev 5 | ||
---|---|---|---|
Line 16... | Line 16... | ||
16 | \*------------------------------------------------------------------------------------------*/
|
16 | \*------------------------------------------------------------------------------------------*/
|
17 | 17 | ||
18 | #include "CameraCalibrator.h"
|
18 | #include "CameraCalibrator.h"
|
19 | 19 | ||
20 | // Open chessboard images and extract corner points
|
20 | // Open chessboard images and extract corner points
|
21 | int CameraCalibrator::addChessboardPoints( |
21 | int
|
22 | const std::vector<std::string>& filelist, |
22 | CameraCalibrator::addChessboardPoints(const std::vector<std::string>& filelist, |
23 | cv::Size & boardSize) { |
23 | cv::Size & boardSize) |
- | 24 | {
|
|
24 | 25 | ||
25 | // the points on the chessboard
|
26 | // the points on the chessboard
|
26 | std::vector<cv::Point2f> imageCorners; |
27 | std::vector<cv::Point2f> imageCorners; |
27 | std::vector<cv::Point3f> objectCorners; |
28 | std::vector<cv::Point3f> objectCorners; |
28 | 29 | ||
29 | // 3D Scene Points:
|
30 | // 3D Scene Points:
|
30 | // Initialize the chessboard corners
|
31 | // Initialize the chessboard corners
|
31 | // in the chessboard reference frame
|
32 | // in the chessboard reference frame
|
32 | // The corners are at 3D location (X,Y,Z)= (i,j,0)
|
33 | // The corners are at 3D location (X,Y,Z)= (i,j,0)
|
33 | for (int i=0; i<boardSize.height; i++) { |
34 | for (int i = 0; i < boardSize.height; i++) |
- | 35 | {
|
|
34 | for (int j=0; j<boardSize.width; j++) { |
36 | for (int j = 0; j < boardSize.width; j++) |
- | 37 | {
|
|
35 | 38 | ||
36 | objectCorners.push_back(cv::Point3f(i, j, 0.0f)); |
39 | objectCorners.push_back(cv::Point3f(i, j, 0.0f)); |
37 | }
|
40 | }
|
38 | }
|
41 | }
|
39 | 42 | ||
40 | // 2D Image points:
|
43 | // 2D Image points:
|
41 | cv::Mat image; // to contain chessboard image |
44 | cv::Mat image; // to contain chessboard image |
42 | int successes = 0; |
45 | int successes = 0; |
43 | // for all viewpoints
|
46 | // for all viewpoints
|
44 | for (int i=0; i<filelist.size(); i++) { |
47 | for (unsigned int i = 0; i < filelist.size(); i++) |
- | 48 | {
|
|
45 | 49 | ||
46 | // Open the image
|
50 | // Open the image
|
47 | image = cv::imread(filelist[i],0); |
51 | image = cv::imread(filelist[i], 0); |
48 | 52 | ||
49 | // Get the chessboard corners
|
53 | // Get the chessboard corners
|
50 | bool found = cv::findChessboardCorners( |
- | |
51 | image, boardSize, imageCorners); |
54 | bool found = cv::findChessboardCorners(image, boardSize, imageCorners); |
52 | 55 | ||
53 | // Get subpixel accuracy on the corners
|
56 | // Get subpixel accuracy on the corners
|
54 | cv::cornerSubPix(image, imageCorners, |
57 | cv::cornerSubPix(image, imageCorners, cv::Size(5, 5), cv::Size(-1, -1), |
55 | cv::Size(5,5), |
- | |
56 | cv::Size(-1,-1), |
- | |
57 | cv::TermCriteria(cv::TermCriteria::MAX_ITER + |
58 | cv::TermCriteria(cv::TermCriteria::MAX_ITER + cv::TermCriteria::EPS, |
58 | cv::TermCriteria::EPS, |
- | |
59 | 30, // max number of iterations |
59 | 30, // max number of iterations |
60 | 0.1)); // min accuracy |
60 | 0.1)); // min accuracy |
61 | 61 | ||
62 | // If we have a good board, add it to our data
|
62 | // If we have a good board, add it to our data
|
63 | if (imageCorners.size() == boardSize.area()) { |
63 | if (imageCorners.size() == (unsigned int) boardSize.area()) |
- | 64 | {
|
|
64 | 65 | ||
65 | // Add image and scene points from one view
|
66 | // Add image and scene points from one view
|
66 | addPoints(imageCorners, objectCorners); |
67 | addPoints(imageCorners, objectCorners); |
67 | successes++; |
68 | successes++; |
68 | }
|
69 | }
|
Line 75... | Line 76... | ||
75 | 76 | ||
76 | return successes; |
77 | return successes; |
77 | }
|
78 | }
|
78 | 79 | ||
79 | // Add scene points and corresponding image points
|
80 | // Add scene points and corresponding image points
|
- | 81 | void
|
|
80 | void CameraCalibrator::addPoints(const std::vector<cv::Point2f>& imageCorners, const std::vector<cv::Point3f>& objectCorners) { |
82 | CameraCalibrator::addPoints(const std::vector<cv::Point2f>& imageCorners, |
- | 83 | const std::vector<cv::Point3f>& objectCorners) |
|
- | 84 | {
|
|
81 | 85 | ||
82 | // 2D image points from one view
|
86 | // 2D image points from one view
|
83 | imagePoints.push_back(imageCorners); |
87 | imagePoints.push_back(imageCorners); |
84 | // corresponding 3D scene points
|
88 | // corresponding 3D scene points
|
85 | objectPoints.push_back(objectCorners); |
89 | objectPoints.push_back(objectCorners); |
86 | }
|
90 | }
|
87 | 91 | ||
88 | // Calibrate the camera
|
92 | // Calibrate the camera
|
89 | // returns the re-projection error
|
93 | // returns the re-projection error
|
- | 94 | double
|
|
90 | double CameraCalibrator::calibrate(cv::Size &imageSize) |
95 | CameraCalibrator::calibrate(cv::Size imageSize) |
91 | {
|
96 | {
|
92 | // undistorter must be reinitialized
|
97 | // undistorter must be reinitialized
|
93 | mustInitUndistort= true; |
98 | mustInitUndistort = true; |
94 | 99 | ||
95 | //Output rotations and translations
|
100 | //Output rotations and translations
|
96 | std::vector<cv::Mat> rvecs, tvecs; |
101 | std::vector<cv::Mat> rvecs, tvecs; |
97 | 102 | ||
98 | // start calibration
|
103 | // start calibration
|
99 | return
|
- | |
100 | calibrateCamera(objectPoints, // the 3D points |
104 | return calibrateCamera(objectPoints, // the 3D points |
101 | imagePoints, // the image points
|
105 | imagePoints, // the image points
|
102 | imageSize, // image size
|
106 | imageSize, // image size
|
103 | cameraMatrix, // output camera matrix
|
107 | cameraMatrix, // output camera matrix
|
104 | distCoeffs, // output distortion matrix
|
108 | distCoeffs, // output distortion matrix
|
105 | rvecs, tvecs, // Rs, Ts
|
109 | rvecs, tvecs, // Rs, Ts
|
Line 107... | Line 111... | ||
107 | // ,CV_CALIB_USE_INTRINSIC_GUESS);
|
111 | // ,CV_CALIB_USE_INTRINSIC_GUESS);
|
108 | 112 | ||
109 | }
|
113 | }
|
110 | 114 | ||
111 | // remove distortion in an image (after calibration)
|
115 | // remove distortion in an image (after calibration)
|
- | 116 | cv::Mat |
|
112 | cv::Mat CameraCalibrator::remap(const cv::Mat &image) { |
117 | CameraCalibrator::remap(const cv::Mat &image) |
- | 118 | {
|
|
113 | 119 | ||
114 | cv::Mat undistorted; |
120 | cv::Mat undistorted; |
115 | 121 | ||
- | 122 | if (mustInitUndistort) |
|
116 | if (mustInitUndistort) { // called once per calibration |
123 | { // called once per calibration |
117 | 124 | ||
118 | cv::initUndistortRectifyMap( |
- | |
119 | cameraMatrix, // computed camera matrix
|
125 | cv::initUndistortRectifyMap(cameraMatrix, // computed camera matrix |
120 | distCoeffs, // computed distortion matrix
|
126 | distCoeffs, // computed distortion matrix
|
121 | cv::Mat(), // optional rectification (none) |
127 | cv::Mat(), // optional rectification (none) |
122 | cv::Mat(), // camera matrix to generate undistorted |
128 | cv::Mat(), // camera matrix to generate undistorted |
123 | cv::Size(640,480), |
129 | cv::Size(640, 480), |
124 | // image.size(), // size of undistorted
|
130 | // image.size(), // size of undistorted
|
Line 127... | Line 133... | ||
127 | 133 | ||
128 | mustInitUndistort= false; |
134 | mustInitUndistort = false; |
129 | }
|
135 | }
|
130 | 136 | ||
131 | // Apply mapping functions
|
137 | // Apply mapping functions
|
132 | cv::remap(image, undistorted, map1, map2, |
138 | cv::remap(image, undistorted, map1, map2, cv::INTER_LINEAR); // interpolation type |
133 | cv::INTER_LINEAR); // interpolation type |
- | |
134 | 139 | ||
135 | return undistorted; |
140 | return undistorted; |
136 | }
|
141 | }
|
137 | 142 | ||
138 | - | ||
139 | // Set the calibration options
|
143 | // Set the calibration options
|
140 | // 8radialCoeffEnabled should be true if 8 radial coefficients are required (5 is default)
|
144 | // 8radialCoeffEnabled should be true if 8 radial coefficients are required (5 is default)
|
141 | // tangentialParamEnabled should be true if tangeantial distortion is present
|
145 | // tangentialParamEnabled should be true if tangeantial distortion is present
|
- | 146 | void
|
|
142 | void CameraCalibrator::setCalibrationFlag(bool radial8CoeffEnabled, bool tangentialParamEnabled) { |
147 | CameraCalibrator::setCalibrationFlag(bool radial8CoeffEnabled, |
- | 148 | bool tangentialParamEnabled) |
|
- | 149 | {
|
|
143 | 150 | ||
144 | // Set the flag used in cv::calibrateCamera()
|
151 | // Set the flag used in cv::calibrateCamera()
|
145 | flag = 0; |
152 | flag = 0; |
- | 153 | if (!tangentialParamEnabled) |
|
146 | if (!tangentialParamEnabled) flag += CV_CALIB_ZERO_TANGENT_DIST; |
154 | flag += CV_CALIB_ZERO_TANGENT_DIST; |
- | 155 | if (radial8CoeffEnabled) |
|
147 | if (radial8CoeffEnabled) flag += CV_CALIB_RATIONAL_MODEL; |
156 | flag += CV_CALIB_RATIONAL_MODEL; |
148 | }
|
157 | }
|
149 | 158 |