Skip to content

Commit b965d26

Browse files
committed
Scale the initial focal length passed to the calibration method by the current image resolution.
1 parent 971d67a commit b965d26

1 file changed

Lines changed: 57 additions & 32 deletions

File tree

apps/calibration/intrinsic/visp-calibrate-camera.cpp

Lines changed: 57 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,17 @@ void usage(const char *argv[], int error)
102102
<< " to unset any constraint for px and py parameters." << std::endl
103103
<< " Default: -1." << std::endl
104104
<< std::endl
105+
<< " --init-focal" << std::endl
106+
<< " By default, the initial camera focal length is computed such as:" << std::endl
107+
<< " (image_width / 640) x 600." << std::endl
108+
<< " The user can instead supply a desired camera focal length using this parameter." << std::endl
109+
<< std::endl
105110
<< " --save" << std::endl
106111
<< " Flag to automatically save the image processing results in a new directory." << std::endl
107112
<< std::endl
113+
<< " --save-jpg" << std::endl
114+
<< " Flag to save image results in jpeg instead of png format." << std::endl
115+
<< std::endl
108116
<< " --output <file.xml>" << std::endl
109117
<< " XML file containing estimated camera parameters." << std::endl
110118
<< " Default: \"camera.xml\"." << std::endl
@@ -139,7 +147,10 @@ int main(int argc, const char *argv[])
139147
std::string opt_init_camera_xml_file;
140148
std::string opt_camera_name = "Camera";
141149
double opt_aspect_ratio = -1; // Not used
142-
bool save_results = false;
150+
bool opt_use_focal_cmd_line = false;
151+
double opt_init_focal = 600.0;
152+
std::string opt_img_ext = ".png";
153+
bool opt_save_results = false;
143154

144155
for (int i = 2; i < argc; i++) {
145156
if (std::string(argv[i]) == "--init-from-xml" && i + 1 < argc) {
@@ -154,8 +165,15 @@ int main(int argc, const char *argv[])
154165
else if (std::string(argv[i]) == "--aspect-ratio" && i + 1 < argc) {
155166
opt_aspect_ratio = std::atof(argv[++i]);
156167
}
168+
else if (std::string(argv[i]) == "--init-focal" && i + 1 < argc) {
169+
opt_use_focal_cmd_line = true;
170+
opt_init_focal = std::atof(argv[++i]);
171+
}
157172
else if (std::string(argv[i]) == "--save") {
158-
save_results = true;
173+
opt_save_results = true;
174+
}
175+
else if (std::string(argv[i]) == "--save-jpg") {
176+
opt_img_ext = ".jpg";
159177
}
160178
else if (std::string(argv[i]) == "--help" || std::string(argv[i]) == "-h") {
161179
usage(argv, 0);
@@ -172,7 +190,7 @@ int main(int argc, const char *argv[])
172190
vpImage<vpRGBa> I_save_results;
173191
std::ofstream log_file;
174192
Tee tee(std::cout, log_file);
175-
if (save_results) {
193+
if (opt_save_results) {
176194
save_results_folder = vpTime::getDateTime("calib_results_%Y-%m-%d_%H.%M.%S");
177195
vpIoTools::makeDirectory(save_results_folder);
178196

@@ -264,8 +282,9 @@ int main(int argc, const char *argv[])
264282
else {
265283
tee << "Initialize camera parameters with default values " << "\n";
266284
// Initialize camera parameters
267-
double px = cam_init.get_px();
268-
double py = cam_init.get_py();
285+
double scale = I.getWidth() / 640.0;
286+
double px = opt_use_focal_cmd_line ? opt_init_focal : scale * cam_init.get_px();
287+
double py = opt_use_focal_cmd_line ? opt_init_focal : scale * cam_init.get_py();
269288
// Set (u0,v0) in the middle of the image
270289
double u0 = I.getWidth() / 2;
271290
double v0 = I.getHeight() / 2;
@@ -337,14 +356,19 @@ int main(int argc, const char *argv[])
337356
std::multimap<double, vpCameraParameters>::const_iterator it_cam;
338357
for (it_cam = map_cam_sorted.begin(); it_cam != map_cam_sorted.end(); ++it_cam) {
339358
vpCameraParameters cam = it_cam->second;
340-
if (calib.computeCalibration(vpCalibration::CALIB_VIRTUAL_VS, cMo, cam, false) == EXIT_SUCCESS) {
341-
calibrator.push_back(calib);
342-
// Add calibration info
343-
calib_info.push_back(CalibInfo(I, calib_points, data, frame_name));
344-
calib_status = true;
345-
double residual = calib.getResidual();
346-
map_cam_sorted.insert(std::make_pair(residual, cam));
347-
break;
359+
try {
360+
if (calib.computeCalibration(vpCalibration::CALIB_VIRTUAL_VS, cMo, cam, false) == EXIT_SUCCESS) {
361+
calibrator.push_back(calib);
362+
// Add calibration info
363+
calib_info.push_back(CalibInfo(I, calib_points, data, frame_name));
364+
calib_status = true;
365+
double residual = calib.getResidual();
366+
map_cam_sorted.insert(std::make_pair(residual, cam));
367+
break;
368+
}
369+
}
370+
catch (const vpException &e) {
371+
tee << "Catch a ViSP exception when doing computeCalibration(): " << e.what() << "\n";
348372
}
349373
}
350374
if (!calib_status) {
@@ -371,10 +395,10 @@ int main(int argc, const char *argv[])
371395
vpTime::wait(s.tempo * 1000);
372396
}
373397

374-
if (save_results) {
398+
if (opt_save_results) {
375399
vpDisplay::getImage(I, I_save_results);
376400
std::ostringstream oss;
377-
oss << save_results_folder << "/" << vpIoTools::getNameWE(reader.getFrameName()) << ".png";
401+
oss << save_results_folder << "/" << vpIoTools::getNameWE(reader.getFrameName()) << opt_img_ext;
378402
vpImageIo::write(I_save_results, oss.str());
379403
}
380404
} while (!reader.end());
@@ -434,10 +458,10 @@ int main(int argc, const char *argv[])
434458
}
435459

436460
std::vector<vpImage<vpRGBa>> list_img_reproj, list_img_reproj_dist, list_img_undist;
437-
if (save_results) {
461+
if (opt_save_results) {
438462
vpDisplay::getImage(I_color, I_save_results);
439463
std::ostringstream oss;
440-
oss << save_results_folder << "/calibration_pattern_occupancy.png";
464+
oss << save_results_folder << "/calibration_pattern_occupancy" << opt_img_ext;
441465
vpImageIo::write(I_save_results, oss.str());
442466
}
443467

@@ -519,12 +543,12 @@ int main(int argc, const char *argv[])
519543
vpTime::wait(s.tempo * 1000);
520544
}
521545

522-
if (save_results) {
546+
if (opt_save_results) {
523547
vpDisplay::getImage(I, I_save_results);
524548
list_img_reproj.push_back(I_save_results);
525549
std::ostringstream oss;
526550
oss << save_results_folder << "/reproj_err_without_dist_" << vpIoTools::getNameWE(calib.m_frame_name)
527-
<< ".png";
551+
<< opt_img_ext;
528552
vpImageIo::write(I_save_results, oss.str());
529553
}
530554
}
@@ -607,11 +631,12 @@ int main(int argc, const char *argv[])
607631
vpTime::wait(s.tempo * 1000);
608632
}
609633

610-
if (save_results) {
634+
if (opt_save_results) {
611635
vpDisplay::getImage(I, I_save_results);
612636
list_img_reproj_dist.push_back(I_save_results);
613637
std::ostringstream oss;
614-
oss << save_results_folder << "/reproj_err_with_dist_" << vpIoTools::getNameWE(calib.m_frame_name) << ".png";
638+
oss << save_results_folder << "/reproj_err_with_dist_" << vpIoTools::getNameWE(calib.m_frame_name)
639+
<< opt_img_ext;
615640
vpImageIo::write(I_save_results, oss.str());
616641
}
617642
}
@@ -669,18 +694,18 @@ int main(int argc, const char *argv[])
669694

670695
bool found = extractCalibrationPoints(s, cvI, pointBuf);
671696
if (found) {
672-
std::vector<vpImagePoint> grid_points;
697+
std::vector<vpImagePoint> found_grid_points;
673698
for (unsigned int i = 0; i < pointBuf.size(); i++) {
674699
vpImagePoint ip(pointBuf[i].y, pointBuf[i].x);
675-
grid_points.push_back(ip);
700+
found_grid_points.push_back(ip);
676701
}
677702

678703
vpDisplay::displayText(I_dist_undist, 15 * vpDisplay::getDownScalingFactor(I_dist_undist),
679704
I.getWidth() + 15 * vpDisplay::getDownScalingFactor(I_dist_undist),
680705
calib_info[idx].m_frame_name + std::string(" undistorted"), vpColor::red);
681706
for (int i = 0; i < s.boardSize.height; i++) {
682-
std::vector<vpImagePoint> current_line(grid_points.begin() + i * s.boardSize.width,
683-
grid_points.begin() + (i + 1) * s.boardSize.width);
707+
std::vector<vpImagePoint> current_line(found_grid_points.begin() + i * s.boardSize.width,
708+
found_grid_points.begin() + (i + 1) * s.boardSize.width);
684709

685710
double a = 0, b = 0, c = 0;
686711
double line_fitting_error = vpMath::lineFitting(current_line, a, b, c);
@@ -715,11 +740,11 @@ int main(int argc, const char *argv[])
715740
vpTime::wait(s.tempo * 1000);
716741
}
717742

718-
if (save_results) {
743+
if (opt_save_results) {
719744
vpDisplay::getImage(I_dist_undist, I_save_results);
720745
std::ostringstream oss;
721746
oss << save_results_folder << "/dist_vs_undist_" << vpIoTools::getNameWE(calib_info[idx].m_frame_name)
722-
<< ".png";
747+
<< opt_img_ext;
723748
vpImage<vpRGBa> I_right;
724749
vpImageTools::crop(I_save_results, vpRect(I_save_results.getWidth()/2, 0,
725750
I_save_results.getWidth()/2, I_save_results.getHeight()), I_right);
@@ -808,10 +833,10 @@ int main(int argc, const char *argv[])
808833
vpTime::wait(s.tempo * 1000);
809834
}
810835

811-
if (save_results) {
836+
if (opt_save_results) {
812837
vpDisplay::getImage(I_dist_map, I_save_results);
813838
std::ostringstream oss;
814-
oss << save_results_folder << "/distortion_displacement_map.png";
839+
oss << save_results_folder << "/distortion_displacement_map" << opt_img_ext;
815840
vpImageIo::write(I_save_results, oss.str());
816841

817842
// Save mosaic images
@@ -820,7 +845,7 @@ int main(int argc, const char *argv[])
820845
createMosaic(list_img_reproj, mosaics);
821846
for (size_t i = 0; i < mosaics.size(); i++) {
822847
oss.str("");
823-
oss << save_results_folder << "/mosaics_reproj_err_without_dist_%04d.png";
848+
oss << save_results_folder << "/mosaics_reproj_err_without_dist_%04d" << opt_img_ext;
824849
char name[FILENAME_MAX];
825850
snprintf(name, FILENAME_MAX, oss.str().c_str(), i);
826851
vpImageIo::write(mosaics[i], name);
@@ -831,7 +856,7 @@ int main(int argc, const char *argv[])
831856
createMosaic(list_img_reproj_dist, mosaics);
832857
for (size_t i = 0; i < mosaics.size(); i++) {
833858
oss.str("");
834-
oss << save_results_folder << "/mosaics_reproj_err_with_dist_%04d.png";
859+
oss << save_results_folder << "/mosaics_reproj_err_with_dist_%04d" << opt_img_ext;
835860
char name[FILENAME_MAX];
836861
snprintf(name, FILENAME_MAX, oss.str().c_str(), i);
837862
vpImageIo::write(mosaics[i], name);
@@ -842,7 +867,7 @@ int main(int argc, const char *argv[])
842867
createMosaic(list_img_undist, mosaics);
843868
for (size_t i = 0; i < mosaics.size(); i++) {
844869
oss.str("");
845-
oss << save_results_folder << "/mosaics_undist_%04d.png";
870+
oss << save_results_folder << "/mosaics_undist_%04d" << opt_img_ext;
846871
char name[FILENAME_MAX];
847872
snprintf(name, FILENAME_MAX, oss.str().c_str(), i);
848873
vpImageIo::write(mosaics[i], name);

0 commit comments

Comments
 (0)