Calibration Library 1.0.0
A C++ library for camera calibration and vision-related geometric transformations
Loading...
Searching...
No Matches
intrinsic_stage.cpp
Go to the documentation of this file.
5
6namespace calib::pipeline {
7
8namespace {
9
11
12struct SensorCalibrationResult {
13 bool success{false};
14 nlohmann::json summary;
15};
16
17SensorCalibrationResult calibrate_sensor(const PlanarIntrinsicCalibrationFacade& facade,
18 const IntrinsicCalibrationConfig& cfg,
19 const PlanarDetections& detections,
20 PipelineContext& context) {
21 SensorCalibrationResult result;
22 const std::string sensor_id = !detections.sensor_id.empty() ? detections.sensor_id : "cam0";
23 const auto* cam_cfg = find_camera_config(cfg, sensor_id);
24 if (cam_cfg == nullptr) {
25 result.summary = nlohmann::json{
26 {"sensor_id", sensor_id},
27 {"status", "missing_camera_config"},
28 };
29 return result;
30 }
31
32 try {
33 auto run = facade.calibrate(cfg, *cam_cfg, detections);
34 context.intrinsic_results[sensor_id] = run;
35
36 const auto report = build_planar_intrinsics_report(cfg, *cam_cfg, detections, run);
37 nlohmann::json entry;
38 to_json(entry, report);
39 entry["sensor_id"] = sensor_id;
40 nlohmann::json tags = nlohmann::json::array();
41 std::copy(detections.tags.begin(), detections.tags.end(), std::back_inserter(tags));
42 entry["tags"] = std::move(tags);
43
44 result.success = true;
45 result.summary = std::move(entry);
46 } catch (const std::exception& ex) {
47 result.summary = nlohmann::json{
48 {"sensor_id", sensor_id}, {"status", "calibration_failed"}, {"error", ex.what()}};
49 }
50 return result;
51}
52
53void collect_gating_flags(const std::vector<PlanarDetections>& detections, bool& has_synth,
54 bool& has_recorded) {
55 has_synth = false;
56 has_recorded = false;
57 for (const auto& det : detections) {
58 if (det.tags.count("synthetic")) {
59 has_synth = true;
60 }
61 if (det.tags.count("recorded")) {
62 has_recorded = true;
63 }
64 }
65}
66
67} // namespace
68
71 result.name = name();
72
73 if (!context.has_intrinsics_config()) {
74 result.summary["error"] = "No intrinsics configuration supplied.";
75 result.success = false;
76 return result;
77 }
78 if (context.dataset.planar_cameras.empty()) {
79 result.summary["error"] = "Dataset does not contain planar camera captures.";
80 result.success = false;
81 return result;
82 }
83
84 const auto& cfg = context.intrinsics_config();
86
87 bool overall_success = true;
88 nlohmann::json cameras = nlohmann::json::array();
89 for (const auto& detections : context.dataset.planar_cameras) {
90 auto sensor_result = calibrate_sensor(facade, cfg, detections, context);
91 cameras.push_back(std::move(sensor_result.summary));
92 overall_success = overall_success && sensor_result.success;
93 }
94
95 bool has_synthetic = false;
96 bool has_recorded = false;
97 collect_gating_flags(context.dataset.planar_cameras, has_synthetic, has_recorded);
98
99 result.summary["cameras"] = std::move(cameras);
100 result.summary["gating"] = {{"synthetic", has_synthetic}, {"recorded", has_recorded}};
101 result.success = overall_success && !context.intrinsic_results.empty();
102 return result;
103}
104
105} // namespace calib::pipeline
auto run(PipelineContext &context) -> PipelineStageResult override
bool success
nlohmann::json summary
PlanarDetections detections
Definition loaders.cpp:15
auto find_camera_config(const IntrinsicCalibrationConfig &cfg, const std::string &camera_id) -> const CameraConfig *
auto build_planar_intrinsics_report(const IntrinsicCalibrationConfig &cfg, const CameraConfig &cam_cfg, const PlanarDetections &detections, const IntrinsicCalibrationOutputs &outputs) -> CalibrationReport
void to_json(nlohmann::json &j, const T &value)
Definition json.h:49
std::set< std::string > tags
Definition dataset.h:35