Ocean
Loading...
Searching...
No Matches
MetricCalibrationBoard.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_METRIC_CALIBRATION_BOARD_H
9#define META_OCEAN_CV_CALIBRATION_METRIC_CALIBRATION_BOARD_H
10
15
16#include "ocean/base/Accessor.h"
18
20
22
23namespace Ocean
24{
25
26namespace CV
27{
28
29namespace Calibration
30{
31
32/**
33 * This class extends the calibration board with metric information.
34 * The metric information comes from the measured width and height of the measurement indication of the calibration board in reality.<br>
35 * The calibration board is defined in the xz-plane with y-axis upwards, the origin of the coordinate system is defined in the center of the board.
36 * @ingroup cvcalibration
37 */
38class OCEAN_CV_CALIBRATION_EXPORT MetricCalibrationBoard : public CalibrationBoard
39{
40 protected:
41
42 /**
43 * Definition of an unordered set holding marker coordinates.
44 */
45 using MarkerCoordinateSet = std::unordered_set<MarkerCoordinate, MarkerCoordinate>;
46
47 public:
48
49 /**
50 * Creates an invalid calibration board.
51 */
53
54 /**
55 * Copies a metric calibration board.
56 * @param metricCalibrationBoard The calibration board to copy
57 */
58 MetricCalibrationBoard(const MetricCalibrationBoard& metricCalibrationBoard) = default;
59
60 /**
61 * Creates a new metric calibration board based on a calibration board and measured width and height of the measurement indication of the calibration board in reality.
62 * @param calibrationBoard The calibration board for which the metric calibration board will be created, must be valid
63 * @param measurementMetricIndicationWidth The measured metric distance between the left and right measurement indication on the real calibration board, with range (0, infinity)
64 * @param measurementMetricIndicationHeight The measured metric distance between the top and bottom measurement indication on the real calibration board, with range (0, infinity)
65 */
66 MetricCalibrationBoard(CalibrationBoard&& calibrationBoard, const MetricSize& measurementMetricIndicationWidth, const MetricSize& measurementMetricIndicationHeight);
67
68 /**
69 * Returns the metric width of the measurement indication of the calibration board in the real world.
70 * @return The board's measured width, with range (0, infinity)
71 */
72 inline const MetricSize& measurementMetricIndicationWidth() const;
73
74 /**
75 * Returns the metric height of the measurement indication of the calibration board in the real world.
76 * @return The board's measured height, with range (0, infinity)
77 */
78 inline const MetricSize& measurementMetricIndicationHeight() const;
79
80 /**
81 * Returns the metric horizontal edge length of a marker in the real calibration board.
82 * @return The board's horizontal marker edge length, with range (0, infinity)
83 */
84 inline Scalar xMetricMarkerSize() const;
85
86 /**
87 * Returns the metric vertical edge length of a marker in the real calibration board.
88 * @return The board's vertical marker edge length, with range (0, infinity)
89 */
90 inline Scalar zMetricMarkerSize() const;
91
92 /**
93 * Returns the 3D center location of a marker within this calibration board.
94 * @param markerCoordinate The coordinate of the marker for which the center point will be returned, must be valid
95 * @return The marker's center point in the coordinate system of the calibration board, defined in the board's xz-plane
96 */
97 inline Vector3 markerCenterPosition(const MarkerCoordinate& markerCoordinate) const;
98
99 /**
100 * Returns the 3D location of a point of a marker within this calibration board.
101 * @param markerCoordinate The coordinate of the marker for which the point will be returned, must be valid
102 * @param indexInMarker The index of the point within the marker, with range [0, 24]
103 * @return The marker's point in the coordinate system of the calibration board, defined in the board's xz-plane
104 */
105 inline Vector3 objectPoint(const MarkerCoordinate& markerCoordinate, const size_t indexInMarker) const;
106
107 /**
108 * Returns all object points of this calibration board.
109 * @param objectPointIds Optional resulting ids of the object points, nullptr if not of interest
110 * @return The object points of this calibration board, will be markers() * Marker::numberPoints()
111 */
112 Vectors3 objectPoints(ObjectPointIds* objectPointIds = nullptr) const;
113
114 /**
115 * Determines the camera pose using only 2D/3D correspondences from given marker candidates.
116 * @param camera The camera profile to be used, must be valid
117 * @param markerCandidates The marker candidates with valid marker coordinate, at least one
118 * @param points The detected points associated with the marker candidates
119 * @param randomGenerator The random generator to be used
120 * @param board_T_camera The resulting camera pose transforming camera to board, with default camera looking towards the positive z-space with y-axis upwards
121 * @param maximalProjectionError The maximal projection error between projected 3D object points and their corresponding 2D observations, with range [0, infinity)
122 * @param useMarkerCandidateIndices Optional resulting indices of the marker candidates which were used to determine the camera pose, nullptr if not of interest
123 * @param usedObjectPointIds Optional resulting ids of the object points which were used to determine the camera pose, nullptr if not of interest
124 * @param usedObjectPoints Optional resulting 3D object points which were used to determine the camera pose, nullptr if not
125 * @param usedImagePoints Optional resulting 2D image points which were used to determine the camera pose, nullptr if not
126 * @return True, if succeeded
127 */
128 bool determineCameraPose(const AnyCamera& camera, const ConstIndexedAccessor<MarkerCandidate>& markerCandidates, const Points& points, RandomGenerator& randomGenerator, HomogenousMatrix4& board_T_camera, const Scalar maximalProjectionError, Indices32* usedMarkerCandidates = nullptr, ObjectPointIds* usedObjectPointIds = nullptr, Vectors3* usedObjectPoints = nullptr, Vectors2* usedImagePoints = nullptr) const;
129
130 /**
131 * Optimizes the camera pose using 2D/3D correspondences from known valid marker candidates and from known additional marker coordinates.
132 * @param camera The camera profile to be used, must be valid
133 * @param board_T_camera The initial camera pose to be optimized, with default camera looking towards the positive z-space with y-axis upwards, must be valid
134 * @param validMarkerCandidates The known valid marker candidates, at least one
135 * @param additionalMarkerCoordinates The known additional marker coordinates, at least one
136 * @param points The detected points associated with the image
137 * @param pointsDistributionArray The distribution array of the detected points
138 * @param board_T_optimizedCamera The resulting optimized camera pose, with default camera looking towards the positive z-space with y-axis upwards
139 * @param maximalProjectionError The maximal projection error between projected 3D object points and their corresponding 2D observations, with range [0, infinity)
140 * @param usedObjectPointIds Optional resulting ids of the object points which were used to determine the camera pose, nullptr if not of interest
141 * @param usedImagePoints Optional resulting 2D image points which were used to determine the camera pose, nullptr if not of interest
142 * @param usedObjectPoints Optional resulting 3D object points which were used to determine the camera pose, nullptr if not of interest
143 * @return True, if succeeded
144 */
145 bool optimizeCameraPose(const AnyCamera& camera, const HomogenousMatrix4& board_T_camera, const ConstIndexedAccessor<MarkerCandidate>& validMarkerCandidates, const CV::PixelPositions& additionalMarkerCoordinates, const Points& points, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, HomogenousMatrix4& board_T_optimizedCamera, const Scalar maximalProjectionError, ObjectPointIds* usedObjectPointIds, Vectors2* usedImagePoints = nullptr, Vectors3* usedObjectPoints = nullptr) const;
146
147 /**
148 * Returns whether this calibration board holds valid data and valid measured indication distances.
149 * @return True, if so
150 */
151 inline bool isValid() const;
152
153 /**
154 * Creates a unique metric calibration board based on a unique board id (the seed) and the number of markers the board has.
155 * @param id The unique board id defining the seed for the random number generator, with range [0, infinity)
156 * @param xMarkers The number of horizontal markers the calibration board will have, with range [1, infinity)
157 * @param yMarkers The number of vertical markers the calibration board will have, with range [1, infinity)
158 * @param measurementMetricIndicationWidth The measured metric distance between the left and right measurement indication on the real calibration board, with range (0, infinity)
159 * @param measurementMetricIndicationHeight The measured metric distance between the top and bottom measurement indication on the real calibration board, with range (0, infinity)
160 * @param calibrationBoard The resulting calibration board
161 * @return True, if succeeded
162 */
163 static bool createMetricCalibrationBoard(const unsigned int id, const size_t xMarkers, const size_t yMarkers, const MetricSize& measurementMetricIndicationWidth, const MetricSize& measurementMetricIndicationHeight, MetricCalibrationBoard& metricCalibrationBoard);
164
165 /**
166 * Returns the padding factor of this metric calibration board.
167 * The padding factor specifies the size of the padding around the actual board markers and the measurement indications in relation to the size of the individual board markers.
168 * @return The padding factor, with range (0, 1)
169 */
170 static constexpr double paddingFactor();
171
172 /**
173 * Determines the optimal marker grid for a calibration board with specific paper width and height and minimal marker size.
174 * @param paperWidth The width of the paper, with range (0, infinity)
175 * @param paperHeight The height of the paper, with range (0, infinity)
176 * @param xMarkers The resulting number of horizontal markers, with range [1, infinity)
177 * @param yMarkers The resulting number of vertical markers, with range [1, infinity)
178 * @param minMarkerSize The minimal size of a marker, with range (0, infinity)
179 * @param margin The margin between the paper border and the measurement indications (the margin due to physical limitations of the printer), with range [0, infinity)
180 * @param paddingFactor The padding factor, with range (0, 1)
181 */
182 static bool determineOptimalMarkerGrid(const MetricSize& paperWidth, const MetricSize& paperHeight, size_t& xMarkers, size_t& yMarkers, const MetricSize& minMarkerSize = MetricSize(30.0, MetricSize::UT_MILLIMETER), const MetricSize& margin = MetricSize(6.0, MetricSize::UT_MILLIMETER), const double paddingFactor = MetricCalibrationBoard::paddingFactor());
183
184 protected:
185
186 /// The measured metric distance between the left and right measurement indication on the real calibration board, with range (0, infinity).
188
189 /// The measured metric distance between the top and bottom measurement indication on the real calibration board, with range (0, infinity).
191
192 /// The board's horizontal marker edge length, with range (0, infinity).
193 Scalar xMetricMarkerSize_ = 0;
194
195 /// The board's vertical marker edge length, with range (0, infinity).
196 Scalar zMetricMarkerSize_ = 0;
197};
198
203
208
213
218
220{
221 ocean_assert(markerCoordinate.x() < xMarkers_ && markerCoordinate.y() < yMarkers_);
222
223 const Scalar markersSizeX = Scalar(xMarkers_) * xMetricMarkerSize_;
224 const Scalar markersSizeZ = Scalar(yMarkers_) * zMetricMarkerSize_;
225
226 const Scalar metricMarkerCenterX = (Scalar(markerCoordinate.x()) + Scalar(0.5)) * xMetricMarkerSize_ - markersSizeX / Scalar(2);
227 const Scalar metricMarkerCenterZ = (Scalar(markerCoordinate.y()) + Scalar(0.5)) * zMetricMarkerSize_ - markersSizeZ / Scalar(2);
228
229 return Vector3(metricMarkerCenterX, 0, metricMarkerCenterZ);
230}
231
232inline Vector3 MetricCalibrationBoard::objectPoint(const MarkerCoordinate& markerCoordinate, const size_t indexInMarker) const
233{
234 ocean_assert(markerCoordinate.x() < xMarkers_ && markerCoordinate.y() < yMarkers_);
235
236 const Vector3 markerCenter = markerCenterPosition(markerCoordinate);
237
238 const BoardMarker& boardMarker = marker(markerCoordinate);
239
240 return boardMarker.objectPoint(markerCenter, xMetricMarkerSize_, zMetricMarkerSize_, indexInMarker);
241}
242
248
250{
251 return 0.1; // 10% of the marker size
252}
253
254}
255
256}
257
258}
259
260#endif // META_OCEAN_CV_CALIBRATION_METRIC_CALIBRATION_BOARD_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:130
This class implements a marker in a calibration board.
Definition CalibrationBoard.h:50
Vector3 objectPoint(const Vector3 &markerPosition, const Scalar xMarkerSize, const Scalar zMarkerSize, const size_t indexInMarker) const
Returns the 3D object point of a marker point of this board marker in the coordinate system of the ca...
This class implements a basic calibration board.
Definition CalibrationBoard.h:38
const BoardMarker & marker(const MarkerCoordinate &markerCoordinate) const
Returns the board marker at a specific position.
Definition CalibrationBoard.h:528
size_t xMarkers_
The number of horizontal markers of this calibration board, with range [1, infinity).
Definition CalibrationBoard.h:411
std::vector< ObjectPointId > ObjectPointIds
Definition of a vector holding object point ids.
Definition CalibrationBoard.h:219
bool isValid() const
Returns whether this calibration board is valid.
Definition CalibrationBoard.h:564
size_t yMarkers_
The number of vertical markers of this calibration board, with range [1, infinity).
Definition CalibrationBoard.h:414
This class extends the calibration board with metric information.
Definition MetricCalibrationBoard.h:39
const MetricSize & measurementMetricIndicationHeight() const
Returns the metric height of the measurement indication of the calibration board in the real world.
Definition MetricCalibrationBoard.h:204
MetricSize measurementMetricIndicationHeight_
The measured metric distance between the top and bottom measurement indication on the real calibratio...
Definition MetricCalibrationBoard.h:190
static bool determineOptimalMarkerGrid(const MetricSize &paperWidth, const MetricSize &paperHeight, size_t &xMarkers, size_t &yMarkers, const MetricSize &minMarkerSize=MetricSize(30.0, MetricSize::UT_MILLIMETER), const MetricSize &margin=MetricSize(6.0, MetricSize::UT_MILLIMETER), const double paddingFactor=MetricCalibrationBoard::paddingFactor())
Determines the optimal marker grid for a calibration board with specific paper width and height and m...
Vector3 markerCenterPosition(const MarkerCoordinate &markerCoordinate) const
Returns the 3D center location of a marker within this calibration board.
Definition MetricCalibrationBoard.h:219
Vector3 objectPoint(const MarkerCoordinate &markerCoordinate, const size_t indexInMarker) const
Returns the 3D location of a point of a marker within this calibration board.
Definition MetricCalibrationBoard.h:232
Scalar zMetricMarkerSize() const
Returns the metric vertical edge length of a marker in the real calibration board.
Definition MetricCalibrationBoard.h:214
Scalar zMetricMarkerSize_
The board's vertical marker edge length, with range (0, infinity).
Definition MetricCalibrationBoard.h:196
MetricSize measurementMetricIndicationWidth_
The measured metric distance between the left and right measurement indication on the real calibratio...
Definition MetricCalibrationBoard.h:187
MetricCalibrationBoard(const MetricCalibrationBoard &metricCalibrationBoard)=default
Copies a metric calibration board.
std::unordered_set< MarkerCoordinate, MarkerCoordinate > MarkerCoordinateSet
Definition of an unordered set holding marker coordinates.
Definition MetricCalibrationBoard.h:45
static constexpr double paddingFactor()
Returns the padding factor of this metric calibration board.
Definition MetricCalibrationBoard.h:249
bool determineCameraPose(const AnyCamera &camera, const ConstIndexedAccessor< MarkerCandidate > &markerCandidates, const Points &points, RandomGenerator &randomGenerator, HomogenousMatrix4 &board_T_camera, const Scalar maximalProjectionError, Indices32 *usedMarkerCandidates=nullptr, ObjectPointIds *usedObjectPointIds=nullptr, Vectors3 *usedObjectPoints=nullptr, Vectors2 *usedImagePoints=nullptr) const
Determines the camera pose using only 2D/3D correspondences from given marker candidates.
MetricCalibrationBoard(CalibrationBoard &&calibrationBoard, const MetricSize &measurementMetricIndicationWidth, const MetricSize &measurementMetricIndicationHeight)
Creates a new metric calibration board based on a calibration board and measured width and height of ...
bool optimizeCameraPose(const AnyCamera &camera, const HomogenousMatrix4 &board_T_camera, const ConstIndexedAccessor< MarkerCandidate > &validMarkerCandidates, const CV::PixelPositions &additionalMarkerCoordinates, const Points &points, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, HomogenousMatrix4 &board_T_optimizedCamera, const Scalar maximalProjectionError, ObjectPointIds *usedObjectPointIds, Vectors2 *usedImagePoints=nullptr, Vectors3 *usedObjectPoints=nullptr) const
Optimizes the camera pose using 2D/3D correspondences from known valid marker candidates and from kno...
Scalar xMetricMarkerSize_
The board's horizontal marker edge length, with range (0, infinity).
Definition MetricCalibrationBoard.h:193
static bool createMetricCalibrationBoard(const unsigned int id, const size_t xMarkers, const size_t yMarkers, const MetricSize &measurementMetricIndicationWidth, const MetricSize &measurementMetricIndicationHeight, MetricCalibrationBoard &metricCalibrationBoard)
Creates a unique metric calibration board based on a unique board id (the seed) and the number of mar...
const MetricSize & measurementMetricIndicationWidth() const
Returns the metric width of the measurement indication of the calibration board in the real world.
Definition MetricCalibrationBoard.h:199
bool isValid() const
Returns whether this calibration board holds valid data and valid measured indication distances.
Definition MetricCalibrationBoard.h:243
MetricCalibrationBoard()=default
Creates an invalid calibration board.
Scalar xMetricMarkerSize() const
Returns the metric horizontal edge length of a marker in the real calibration board.
Definition MetricCalibrationBoard.h:209
Vectors3 objectPoints(ObjectPointIds *objectPointIds=nullptr) const
Returns all object points of this calibration board.
This class implements an object holding a metric size like width, height, length, or thickness.
Definition MetricSize.h:32
T y() const
Returns the vertical coordinate position of this object.
Definition PixelPosition.h:468
T x() const
Returns the horizontal coordinate position of this object.
Definition PixelPosition.h:456
This class implements a base class for all indexed-based accessors allowing a constant reference acce...
Definition Accessor.h:241
This class implements a distribution array.
Definition SpatialDistribution.h:228
static constexpr T eps()
Returns a small epsilon.
This class implements a generator for random numbers.
Definition RandomGenerator.h:42
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition Base.h:96
std::vector< PixelPosition > PixelPositions
Definition of a vector holding pixel positions (with positive coordinate values).
Definition PixelPosition.h:46
std::vector< Point > Points
Definition of a vector holding points.
Definition cv/calibration/Point.h:31
float Scalar
Definition of a scalar type.
Definition Math.h:129
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition Vector3.h:29
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition Vector2.h:64
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