-
Notifications
You must be signed in to change notification settings - Fork 390
/
intrinsic_calib.cc
244 lines (207 loc) · 8.12 KB
/
intrinsic_calib.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <boost/algorithm/string.hpp>
#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <iomanip>
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "camodocal/chessboard/Chessboard.h"
#include "camodocal/calib/CameraCalibration.h"
#include "../gpl/gpl.h"
int main(int argc, char** argv)
{
cv::Size boardSize;
float squareSize;
std::string inputDir;
std::string cameraModel;
std::string cameraName;
std::string prefix;
std::string fileExtension;
bool useOpenCV;
bool viewResults;
bool verbose;
//========= Handling Program options =========
boost::program_options::options_description desc("Allowed options");
desc.add_options()
("help", "produce help message")
("width,w", boost::program_options::value<int>(&boardSize.width)->default_value(9), "Number of inner corners on the chessboard pattern in x direction")
("height,h", boost::program_options::value<int>(&boardSize.height)->default_value(6), "Number of inner corners on the chessboard pattern in y direction")
("size,s", boost::program_options::value<float>(&squareSize)->default_value(120.f), "Size of one square in mm")
("input,i", boost::program_options::value<std::string>(&inputDir)->default_value("images"), "Input directory containing chessboard images")
("prefix,p", boost::program_options::value<std::string>(&prefix)->default_value("image"), "Prefix of images")
("file-extension,e", boost::program_options::value<std::string>(&fileExtension)->default_value(".bmp"), "File extension of images")
("camera-model", boost::program_options::value<std::string>(&cameraModel)->default_value("mei"), "Camera model: kannala-brandt | mei | pinhole")
("camera-name", boost::program_options::value<std::string>(&cameraName)->default_value("camera"), "Name of camera")
("opencv", boost::program_options::bool_switch(&useOpenCV)->default_value(false), "Use OpenCV to detect corners")
("view-results", boost::program_options::bool_switch(&viewResults)->default_value(false), "View results")
("verbose,v", boost::program_options::bool_switch(&verbose)->default_value(false), "Verbose output")
;
boost::program_options::positional_options_description pdesc;
pdesc.add("input", 1);
boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::command_line_parser(argc, argv).options(desc).positional(pdesc).run(), vm);
boost::program_options::notify(vm);
if (vm.count("help"))
{
std::cout << desc << std::endl;
return 1;
}
if (!boost::filesystem::exists(inputDir) && !boost::filesystem::is_directory(inputDir))
{
std::cerr << "# ERROR: Cannot find input directory " << inputDir << "." << std::endl;
return 1;
}
camodocal::Camera::ModelType modelType;
if (boost::iequals(cameraModel, "kannala-brandt"))
{
modelType = camodocal::Camera::KANNALA_BRANDT;
}
else if (boost::iequals(cameraModel, "mei"))
{
modelType = camodocal::Camera::MEI;
}
else if (boost::iequals(cameraModel, "pinhole"))
{
modelType = camodocal::Camera::PINHOLE;
}
else if (boost::iequals(cameraModel, "scaramuzza"))
{
modelType = camodocal::Camera::SCARAMUZZA;
}
else
{
std::cerr << "# ERROR: Unknown camera model: " << cameraModel << std::endl;
return 1;
}
switch (modelType)
{
case camodocal::Camera::KANNALA_BRANDT:
std::cout << "# INFO: Camera model: Kannala-Brandt" << std::endl;
break;
case camodocal::Camera::MEI:
std::cout << "# INFO: Camera model: Mei" << std::endl;
break;
case camodocal::Camera::PINHOLE:
std::cout << "# INFO: Camera model: Pinhole" << std::endl;
break;
case camodocal::Camera::SCARAMUZZA:
std::cout << "# INFO: Camera model: Scaramuzza-Omnidirect" << std::endl;
break;
}
// look for images in input directory
std::vector<std::string> imageFilenames;
boost::filesystem::directory_iterator itr;
for (boost::filesystem::directory_iterator itr(inputDir); itr != boost::filesystem::directory_iterator(); ++itr)
{
if (!boost::filesystem::is_regular_file(itr->status()))
{
continue;
}
std::string filename = itr->path().filename().string();
// check if prefix matches
if (!prefix.empty())
{
if (filename.compare(0, prefix.length(), prefix) != 0)
{
continue;
}
}
// check if file extension matches
if (filename.compare(filename.length() - fileExtension.length(), fileExtension.length(), fileExtension) != 0)
{
continue;
}
imageFilenames.push_back(itr->path().string());
if (verbose)
{
std::cerr << "# INFO: Adding " << imageFilenames.back() << std::endl;
}
}
if (imageFilenames.empty())
{
std::cerr << "# ERROR: No chessboard images found." << std::endl;
return 1;
}
if (verbose)
{
std::cerr << "# INFO: # images: " << imageFilenames.size() << std::endl;
}
cv::Mat image = cv::imread(imageFilenames.front(), -1);
const cv::Size frameSize = image.size();
camodocal::CameraCalibration calibration(modelType, cameraName, frameSize, boardSize, squareSize);
calibration.setVerbose(verbose);
std::vector<bool> chessboardFound(imageFilenames.size(), false);
for (size_t i = 0; i < imageFilenames.size(); ++i)
{
image = cv::imread(imageFilenames.at(i), -1);
camodocal::Chessboard chessboard(boardSize, image);
chessboard.findCorners(useOpenCV);
if (chessboard.cornersFound())
{
if (verbose)
{
std::cerr << "# INFO: Detected chessboard in image " << i + 1 << std::endl;
}
calibration.addChessboardData(chessboard.getCorners());
cv::Mat sketch;
chessboard.getSketch().copyTo(sketch);
cv::imshow("Image", sketch);
cv::waitKey(50);
}
else if (verbose)
{
std::cerr << "# INFO: Did not detect chessboard in image " << i + 1 << std::endl;
}
chessboardFound.at(i) = chessboard.cornersFound();
}
cv::destroyWindow("Image");
if (calibration.sampleCount() < 10)
{
std::cerr << "# ERROR: Insufficient number of detected chessboards." << std::endl;
return 1;
}
if (verbose)
{
std::cerr << "# INFO: Calibrating..." << std::endl;
}
double startTime = camodocal::timeInSeconds();
calibration.calibrate();
calibration.writeParams(cameraName + "_camera_calib.yaml");
calibration.writeChessboardData(cameraName + "_chessboard_data.dat");
if (verbose)
{
std::cout << "# INFO: Calibration took a total time of "
<< std::fixed << std::setprecision(3) << camodocal::timeInSeconds() - startTime
<< " sec.\n";
}
if (verbose)
{
std::cerr << "# INFO: Wrote calibration file to " << cameraName + "_camera_calib.yaml" << std::endl;
}
if (viewResults)
{
std::vector<cv::Mat> cbImages;
std::vector<std::string> cbImageFilenames;
for (size_t i = 0; i < imageFilenames.size(); ++i)
{
if (!chessboardFound.at(i))
{
continue;
}
cbImages.push_back(cv::imread(imageFilenames.at(i), -1));
cbImageFilenames.push_back(imageFilenames.at(i));
}
// visualize observed and reprojected points
calibration.drawResults(cbImages);
for (size_t i = 0; i < cbImages.size(); ++i)
{
cv::putText(cbImages.at(i), cbImageFilenames.at(i), cv::Point(10,20),
cv::FONT_HERSHEY_COMPLEX, 0.5, cv::Scalar(255, 255, 255),
1, CV_AA);
cv::imshow("Image", cbImages.at(i));
cv::waitKey(0);
}
}
return 0;
}