Ocean
Loading...
Searching...
No Matches
CameraCalibrator.h
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8#ifndef META_OCEAN_CV_CALIBRATION_CAMERA_CALIBRATOR_H
9#define META_OCEAN_CV_CALIBRATION_CAMERA_CALIBRATOR_H
10
15
16#include "ocean/base/Frame.h"
17#include "ocean/base/Worker.h"
18
21
23
24namespace Ocean
25{
26
27namespace CV
28{
29
30namespace Calibration
31{
32
33/**
34 * This class implements a camera calibrator using a couple of images of a calibration board.
35 * @ingroup cvcalibration
36 */
37class OCEAN_CV_CALIBRATION_EXPORT CameraCalibrator
38{
39 public:
40
41 /**
42 * This class holds the properties of the initial camera used during the calibration.
43 */
45 {
46 public:
47
48 /**
49 * Creates initial camera properties with default parameters.
50 */
52
53 /**
54 * Creates initial camera properties.
55 * @param anyCameraType The type of the initial camera to be used, AnyCameraType::INVALID to determine the camera type automatically
56 */
57 explicit inline InitialCameraProperties(const AnyCameraType anyCameraType);
58
59 /**
60 * Creates initial camera properties.
61 * @param anyCameraType The type of the initial camera to be used, AnyCameraType::INVALID to determine the camera type automatically
62 * @param minFovX The minimal horizontal field of view of the initial camera, in radian, with range (0, maxFovX)
63 * @param maxFovX The maximal horizontal field of view of the initial camera, in radian, with range (minFovX, PI)
64 */
65 inline InitialCameraProperties(const AnyCameraType anyCameraType, const Scalar minFovX, const Scalar maxFovX);
66
67 /**
68 * Returns the type of the initial camera to be used.
69 * @return The camera type, AnyCameraType::INVALID to determine the camera type automatically
70 */
71 inline AnyCameraType anyCameraType() const;
72
73 /**
74 * Returns the minimal horizontal field of view of the initial camera.
75 * @return Minimal horizontal field of view, in radian
76 */
77 inline Scalar minFovX() const;
78
79 /**
80 * Returns the maximal horizontal field of view of the initial camera.
81 * @return Maximal horizontal field of view, in radian
82 */
83 inline Scalar maxFovX() const;
84
85 protected:
86
87 /// The type of the initial camera to be used, AnyCameraType::INVALID to determine the camera type automatically.
88 AnyCameraType anyCameraType_ = AnyCameraType::INVALID;
89
90 /// The minimal horizontal field of view of the initial camera, in radian, with range (0, maxFovX_).
91 Scalar minFovX_ = Numeric::deg2rad(20);
92
93 /// The maximal horizontal field of view of the initial camera, in radian, with range (minFovX_, PI)
94 Scalar maxFovX_ = Numeric::deg2rad(175);
95 };
96
97 /**
98 * Definition of individual calibration stages, **TODO** not yet used.
99 */
100 enum CalibrationStage : uint32_t
101 {
102 /// The calibration stage is unknown.
103 CS_UNKNOWN = 0u,
104 /// The calibration stage is determining the initial camera field of view.
106 /// The calibration stage is calibrating the camera.
107 CS_CALIBRATE_CAMERA
108 };
109
110 /**
111 * Definition of individual image results.
112 */
113 enum ImageResult : uint32_t
114 {
115 /// An error occurred during the handling of the image.
116 IR_ERROR = 0u,
117 /// The image was handled but the calibration board was not detected.
119 /// The calibration board was detected successfully.
120 IR_BOARD_WAS_DETECTED
121 };
122
123 /**
124 * Alias for the camera's optimization strategy.
125 */
127
128 public:
129
130 /**
131 * Default constructor creating an invalid camera calibrator.
132 */
133 CameraCalibrator() = default;
134
135 /**
136 * Creates a new camera calibrator.
137 * @param metricCalibrationBoard The metric calibration board which will be visible in the camera images, must be valid
138 * @param initialCameraProperties The initial camera properties which can be used to jump start the calibration, must be valid
139 */
140 explicit CameraCalibrator(const MetricCalibrationBoard& metricCalibrationBoard, const InitialCameraProperties& initialCameraProperties = InitialCameraProperties());
141
142 /**
143 * Handles a new image.
144 * @param imageId The unique id of the image, must be valid
145 * @param frame The frame to handle, must be valid
146 * @param worker Optional worker object to distribute the computation
147 * @return The result of the image handling
148 */
149 ImageResult handleImage(const size_t imageId, const Frame& frame, Worker* worker = nullptr);
150
151 /**
152 * Finalizes the calibration and determines the precise camera profile.
153 * This function should be call after all images have been handled.
154 * @return True, if succeeded
155 */
156 bool finalize();
157
158 /**
159 * Returns the current calibration stage.
160 * @return The calibrator's calibration stage
161 */
162 inline CalibrationStage calibrationStage() const;
163
164 /**
165 * Returns the metric calibration board of this calibrator.
166 * @return The calibrator's calibration board
167 */
168 inline const MetricCalibrationBoard& metricCalibrationBoard() const;
169
170 /**
171 * Returns the calibrator's current camera profile.
172 * @param projectionError Optional resulting projection error, with range [0, infinity)
173 * @return The calibrator's current camera profile, nullptr if the camera profile has not been determined yet
174 */
175 inline SharedAnyCamera camera(Scalar* projectionError = nullptr) const;
176
177 /**
178 * Returns whether this calibrator holds at least one valid observation.
179 * @return True, if so
180 */
181 inline bool hasObservations() const;
182
183 /**
184 * Returns all observations which have been made so far.
185 * @return The calibration board observations
186 */
187 inline const CalibrationBoardObservations& observations() const;
188
189 /**
190 * Returns the latest observation which has been made.
191 * @return The latest calibration board observation
192 */
193 inline const CalibrationBoardObservation& latestObservation() const;
194
195 /**
196 * Returns the overall number of correspondences which have been gathered so far.
197 * @return The number of correspondences across all observations
198 */
199 size_t numberCorrespondences() const;
200
201 protected:
202
203 /**
204 * Determines the initial camera pose based on marker candidates with known marker coordinate.
205 * @param camera The camera profile defining the projection, must be valid
206 * @param points The points which have been detected in the image, must be valid
207 * @param board_T_camera The resulting initial camera pose, with default camera pointing towards the negative z-space with y-axis upwards
208 * @param usedMarkerCandidateIndices The resulting indices of the marker candidates which have been used to determine the initial pose
209 * @return True, if succeeded
210 */
211 bool determineInitialPoseWithValidMarkerCandidates(const AnyCamera& camera, const Points& points, HomogenousMatrix4& board_T_camera, Indices32& usedMarkerCandidateIndices) const;
212
213 /**
214 * Optimizes the camera pose using known valid marker candidates with known marker coordinates and iteratively adds addtional correspondences from neighboring marker candidates.
215 * The area around the valid marker candidates is iteratively growing until no additonal marker candidates can be added.<br>
216 * The growing strategy helps with handling fisheye cameras.
217 * @param camera The camera profile defining the projection, must be valid
218 * @param board_T_camera The initial camera pose to improve, with default camera pointing towards the negative z-space with y-axis upwards, must be valid
219 * @param points The points which have been detected in the image
220 * @param pointsDistributionArray The distribution array of the points, must be valid
221 * @param validMarkerCandidateIndices The indices of the marker candidates which have been used to determine the initial pose, must be valid
222 * @param maximalProjectionError The maximal projection error between image point and reprojected object point, in pixel, with range [0, infinity)
223 * @param board_T_optimizedCamera The resulting optimized camera pose, with default camera pointing towards the negative z-space with y-axis upwards
224 * @param objectPointIds The resulting ids of the object points which have been used to determine the optimized camera pose
225 * @param objectPoints The resulting object points which have been used to determine the optimized camera pose, one for each object point id
226 * @param imagePoints The resulting image points which have been used to determine the optimized camera pose, one for each object point id
227 */
228 bool optimizeCameraPoseWithAdditionalPointsFromMarkerCandidates(const AnyCamera& camera, const HomogenousMatrix4& board_T_camera, const Points& points, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, const Indices32& validMarkerCandidateIndices, const Scalar maximalProjectionError, HomogenousMatrix4& board_T_optimizedCamera, CalibrationBoard::ObjectPointIds& objectPointIds, Vectors3& objectPoints, Vectors2& imagePoints) const;
229
230 /**
231 * Determines additional 2D/3D correspondences for a valid observation of a calibration board which has not yet used all possible marker points of the calibration board.
232 * @param calibrationBoard The calibration board which has been observed, must be valid
233 * @param observation The observation of the calibration board, must be valid
234 * @param points The points which have been detected in the image
235 * @param pointsDistributionArray The distribution array of the points, must be valid
236 * @param additionalObjectPointIds The resulting ids of additional object points which are not part of the provided observation but which are part of the calibration board
237 * @param additionalObjectPoints The resulting additional object points, one for each object point id
238 * @param additionalImagePoints The resulting additional image points, one for each object point id
239 * @param maximalProjectionError The maximal projection error between image point and reprojected object point, in pixel, with range [0, infinity)
240 * @return True, if succeeded
241 */
242 bool determineAdditionalCorrespondences(const MetricCalibrationBoard& calibrationBoard, const CalibrationBoardObservation& observation, const Points& points, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, CalibrationBoard::ObjectPointIds& additionalObjectPointIds, Vectors3& additionalObjectPoints, Vectors2& additionalImagePoints, const Scalar maximalProjectionError);
243
244 /**
245 * Optimizes the camera profile based on all marker points from known valid marker candidates with known marker coordinates.
246 * @param camera The camera profile to optimize, must be valid
247 * @param board_T_camera The initial camera pose to improve, with default camera pointing towards the negative z-space with y-axis upwards, must be valid
248 * @param points The points which have been detected in the image
249 * @param validMarkerCandidateIndices The indices of the marker candidates with known marker coordinates, at least one
250 * @param optimizationStrategy The optimization strategy to be used
251 * @param board_T_optimizedCamera Optional resulting optimized camera pose, nullptr if not of interest
252 * @param estimatorType The estimator type to be used
253 * @param initialError Optional resulting initial projection error, with range [0, infinity)
254 * @param finalError Optional resulting final projection error, with range [0, infinity)
255 * @return The resulting optimized camera profile, nullptr if the camera profile could not be determined
256 */
257 SharedAnyCamera optimizeCamera(const AnyCamera& camera, const HomogenousMatrix4& board_T_camera, const Points& points, const Indices32& validMarkerCandidateIndices, const OptimizationStrategy optimizationStrategy, HomogenousMatrix4* board_T_optimizedCamera = nullptr, const Geometry::Estimator::EstimatorType estimatorType = Geometry::Estimator::ET_SQUARE, Scalar* initialError = nullptr, Scalar* finalError = nullptr) const;
258
259 /**
260 * Determines the initial field of view of the camera for a single image of a calibration board.
261 * @param width The width of the image in pixel, with range [1, infinity)
262 * @param height The height of the image in pixel, with range [1, infinity)
263 * @param points The points which have been detected in the image
264 * @param markerCandidates The marker candidates which have been detected in the image with valid border indices (and signs), at least one
265 * @param initialCameraProperties The initial camera properties which can be used to jump start the calibration
266 * @return The resulting initial camera profile, nullptr if the initial camera profile could not be determined
267 */
268 static SharedAnyCamera determineInitialCameraFieldOfView(const unsigned int width, const unsigned int height, const Points& points, const MarkerCandidates& markerCandidates, const InitialCameraProperties& initialCameraProperties);
269
270 /**
271 * Determines the precise camera profile based on several observations of a calibration board.
272 * @param observations The observations of the calibration board, must be valid
273 * @param numberObservations The number of observations, with range [1, infinity)
274 * @param optimizationStrategy The optimization strategy to be used
275 * @param board_T_optimizedCameras Optional resulting camera poses, one for each observation, with default camera pointing towards the negative z-space with y-axis upwards; nullptr if not of interest
276 * @param estimatorType The estimator type to be used
277 * @param initialError Optional resulting initial projection error, with range [0, infinity)
278 * @param finalError Optional resulting final projection error, with range [0, infinity)
279 * @return The resulting precise camera profile, nullptr if the camera profile could not be determined
280 */
281 static SharedAnyCamera determinePreciseCamera(const CalibrationBoardObservation* observations, const size_t numberObservations, const OptimizationStrategy optimizationStrategy, HomogenousMatrices4* board_T_optimizedCameras = nullptr, const Geometry::Estimator::EstimatorType estimatorType = Geometry::Estimator::ET_SQUARE, Scalar* initialError = nullptr, Scalar* finalError = nullptr);
282
283 protected:
284
285 /// The current calibration stage.
286 CalibrationStage calibrationStage_ = CS_UNKNOWN;
287
288 /// The metric calibration board which is used for calibration.
290
291 /// The initial camera properties which can be used to jump start the calibration.
293
294 /// The current frame with pixel format FORMAT_Y8.
296
297 /// The point detector which is used to detect the marker points of the calibration board.
299
300 /// The marker candidates which have been detected in the current image.
302
303 /// The random generator to be used.
305
306 /// Reusable frame object to avoid memory re-allocations.
308
309 /// The observations which have been made so far.
311
312 /// The final camera profile.
314
315 /// The resulting projection error of the final camera profile, with range [0, infinity)
316 Scalar cameraProjectionError_ = Numeric::maxValue();
317};
318
323
325 anyCameraType_(anyCameraType)
326{
327 // nothing to do here
328}
329
331 anyCameraType_(anyCameraType),
332 minFovX_(minFovX),
333 maxFovX_(maxFovX)
334{
335 // nothing to do here
336}
337
339{
340 return anyCameraType_;
341}
342
344{
345 ocean_assert(minFovX_ <= maxFovX_);
346 return minFovX_;
347}
348
350{
351 ocean_assert(minFovX_ <= maxFovX_);
352 return maxFovX_;
353}
354
359
364
365inline SharedAnyCamera CameraCalibrator::camera(Scalar* projectionError) const
366{
367 if (projectionError != nullptr)
368 {
369 *projectionError = cameraProjectionError_;
370 }
371
372 return camera_;
373}
374
376{
377 return !observations_.empty();
378}
379
384
386{
387 return observations_.back();
388}
389
390}
391
392}
393
394}
395
396#endif // META_OCEAN_CV_CALIBRATION_CAMERA_CALIBRATOR_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:130
std::vector< ObjectPointId > ObjectPointIds
Definition of a vector holding object point ids.
Definition CalibrationBoard.h:219
This class implements an observation of a calibration board.
Definition CalibrationBoardObservation.h:45
This class holds the properties of the initial camera used during the calibration.
Definition CameraCalibrator.h:45
Scalar maxFovX() const
Returns the maximal horizontal field of view of the initial camera.
Definition CameraCalibrator.h:349
AnyCameraType anyCameraType() const
Returns the type of the initial camera to be used.
Definition CameraCalibrator.h:338
Scalar minFovX() const
Returns the minimal horizontal field of view of the initial camera.
Definition CameraCalibrator.h:343
InitialCameraProperties()
Creates initial camera properties with default parameters.
Definition CameraCalibrator.h:319
This class implements a camera calibrator using a couple of images of a calibration board.
Definition CameraCalibrator.h:38
size_t numberCorrespondences() const
Returns the overall number of correspondences which have been gathered so far.
CalibrationStage calibrationStage() const
Returns the current calibration stage.
Definition CameraCalibrator.h:355
CameraCalibrator()=default
Default constructor creating an invalid camera calibrator.
SharedAnyCamera optimizeCamera(const AnyCamera &camera, const HomogenousMatrix4 &board_T_camera, const Points &points, const Indices32 &validMarkerCandidateIndices, const OptimizationStrategy optimizationStrategy, HomogenousMatrix4 *board_T_optimizedCamera=nullptr, const Geometry::Estimator::EstimatorType estimatorType=Geometry::Estimator::ET_SQUARE, Scalar *initialError=nullptr, Scalar *finalError=nullptr) const
Optimizes the camera profile based on all marker points from known valid marker candidates with known...
ImageResult handleImage(const size_t imageId, const Frame &frame, Worker *worker=nullptr)
Handles a new image.
CalibrationStage calibrationStage_
The current calibration stage.
Definition CameraCalibrator.h:286
InitialCameraProperties initialCameraProperties_
The initial camera properties which can be used to jump start the calibration.
Definition CameraCalibrator.h:292
const MetricCalibrationBoard & metricCalibrationBoard() const
Returns the metric calibration board of this calibrator.
Definition CameraCalibrator.h:360
Frame yFrame_
The current frame with pixel format FORMAT_Y8.
Definition CameraCalibrator.h:295
SharedAnyCamera camera_
The final camera profile.
Definition CameraCalibrator.h:313
ImageResult
Definition of individual image results.
Definition CameraCalibrator.h:114
@ IR_BOARD_WAS_NOT_DETECTED
The image was handled but the calibration board was not detected.
Definition CameraCalibrator.h:118
CameraCalibrator(const MetricCalibrationBoard &metricCalibrationBoard, const InitialCameraProperties &initialCameraProperties=InitialCameraProperties())
Creates a new camera calibrator.
bool determineInitialPoseWithValidMarkerCandidates(const AnyCamera &camera, const Points &points, HomogenousMatrix4 &board_T_camera, Indices32 &usedMarkerCandidateIndices) const
Determines the initial camera pose based on marker candidates with known marker coordinate.
bool determineAdditionalCorrespondences(const MetricCalibrationBoard &calibrationBoard, const CalibrationBoardObservation &observation, const Points &points, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, CalibrationBoard::ObjectPointIds &additionalObjectPointIds, Vectors3 &additionalObjectPoints, Vectors2 &additionalImagePoints, const Scalar maximalProjectionError)
Determines additional 2D/3D correspondences for a valid observation of a calibration board which has ...
PointDetector pointDetector_
The point detector which is used to detect the marker points of the calibration board.
Definition CameraCalibrator.h:298
CalibrationBoardObservations observations_
The observations which have been made so far.
Definition CameraCalibrator.h:310
static SharedAnyCamera determineInitialCameraFieldOfView(const unsigned int width, const unsigned int height, const Points &points, const MarkerCandidates &markerCandidates, const InitialCameraProperties &initialCameraProperties)
Determines the initial field of view of the camera for a single image of a calibration board.
RandomGenerator randomGenerator_
The random generator to be used.
Definition CameraCalibrator.h:304
bool optimizeCameraPoseWithAdditionalPointsFromMarkerCandidates(const AnyCamera &camera, const HomogenousMatrix4 &board_T_camera, const Points &points, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, const Indices32 &validMarkerCandidateIndices, const Scalar maximalProjectionError, HomogenousMatrix4 &board_T_optimizedCamera, CalibrationBoard::ObjectPointIds &objectPointIds, Vectors3 &objectPoints, Vectors2 &imagePoints) const
Optimizes the camera pose using known valid marker candidates with known marker coordinates and itera...
CalibrationStage
Definition of individual calibration stages, TODO not yet used.
Definition CameraCalibrator.h:101
@ CS_DETERMINE_INITIAL_CAMERA_FOV
The calibration stage is determining the initial camera field of view.
Definition CameraCalibrator.h:105
Frame reusableMarkerCoordinateUsageFrame_
Reusable frame object to avoid memory re-allocations.
Definition CameraCalibrator.h:307
Scalar cameraProjectionError_
The resulting projection error of the final camera profile, with range [0, infinity)
Definition CameraCalibrator.h:316
SharedAnyCamera camera(Scalar *projectionError=nullptr) const
Returns the calibrator's current camera profile.
Definition CameraCalibrator.h:365
const CalibrationBoardObservation & latestObservation() const
Returns the latest observation which has been made.
Definition CameraCalibrator.h:385
const CalibrationBoardObservations & observations() const
Returns all observations which have been made so far.
Definition CameraCalibrator.h:380
bool hasObservations() const
Returns whether this calibrator holds at least one valid observation.
Definition CameraCalibrator.h:375
MetricCalibrationBoard metricCalibrationBoard_
The metric calibration board which is used for calibration.
Definition CameraCalibrator.h:289
static SharedAnyCamera determinePreciseCamera(const CalibrationBoardObservation *observations, const size_t numberObservations, const OptimizationStrategy optimizationStrategy, HomogenousMatrices4 *board_T_optimizedCameras=nullptr, const Geometry::Estimator::EstimatorType estimatorType=Geometry::Estimator::ET_SQUARE, Scalar *initialError=nullptr, Scalar *finalError=nullptr)
Determines the precise camera profile based on several observations of a calibration board.
bool finalize()
Finalizes the calibration and determines the precise camera profile.
MarkerCandidates markerCandidates_
The marker candidates which have been detected in the current image.
Definition CameraCalibrator.h:301
This class extends the calibration board with metric information.
Definition MetricCalibrationBoard.h:39
This class implements a point detector for marker points.
Definition PointDetector.h:39
This class implements Ocean's image class.
Definition Frame.h:1808
EstimatorType
Definition of individual robust estimator types.
Definition Estimator.h:34
OptimizationStrategy
Definition of individual optimization strategies for how the camera parameters can be optimized.
Definition NonLinearOptimizationCamera.h:37
This class implements a distribution array.
Definition SpatialDistribution.h:228
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
This class implements a worker able to distribute function calls over different threads.
Definition Worker.h:33
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition Base.h:96
std::vector< Point > Points
Definition of a vector holding points.
Definition cv/calibration/Point.h:31
std::vector< MarkerCandidate > MarkerCandidates
Definition of a vector holding marker candidates.
Definition MarkerCandidate.h:33
std::vector< CalibrationBoardObservation > CalibrationBoardObservations
Definition of a vector holding CalibrationBoardObservation objects.
Definition CalibrationBoardObservation.h:37
float Scalar
Definition of a scalar type.
Definition Math.h:129
std::vector< HomogenousMatrix4 > HomogenousMatrices4
Definition of a vector holding HomogenousMatrix4 objects.
Definition HomogenousMatrix4.h:73
std::shared_ptr< AnyCamera > SharedAnyCamera
Definition of a shared pointer holding an AnyCamera object with Scalar precision.
Definition AnyCamera.h:60
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition Vector2.h:64
AnyCameraType
Definition of individual camera types.
Definition AnyCamera.h:111
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition Vector3.h:65
The namespace covering the entire Ocean framework.
Definition Accessor.h:15