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 * Returns the 3D positions of all unique marker corner points of this calibration board.
116 * The corners form a (xMarkers + 1) x (yMarkers + 1) grid and are returned in row-major order (iterating xMarker first, then yMarker).<br>
117 * Note: corner coordinates are not identical to marker coordinates as they include one additional row and column.
118 * @param cornerCoordinates Optional resulting coordinates of the corners, with range [0, xMarkers]x[0, yMarkers], one for each returned position, nullptr if not of interest
119 * @return The unique corner positions in the coordinate system of the calibration board, defined in the board's xz-plane, will be (xMarkers + 1) * (yMarkers + 1)
120 */
121 Vectors3 markerCornerPositions(CV::PixelPositions* cornerCoordinates = nullptr) const;
122
123 /**
124 * Determines the camera pose using only 2D/3D correspondences from given marker candidates.
125 * @param camera The camera profile to be used, must be valid
126 * @param markerCandidates The marker candidates with valid marker coordinate, at least one
127 * @param points The detected points associated with the marker candidates
128 * @param randomGenerator The random generator to be used
129 * @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
130 * @param maximalProjectionError The maximal projection error between projected 3D object points and their corresponding 2D observations, with range [0, infinity)
131 * @param usedMarkerCandidates Optional resulting indices of the marker candidates which were used to determine the camera pose, nullptr if not of interest
132 * @param usedObjectPointIds Optional resulting ids of the object points which were used to determine the camera pose, nullptr if not of interest
133 * @param usedObjectPoints Optional resulting 3D object points which were used to determine the camera pose, nullptr if not
134 * @param usedImagePoints Optional resulting 2D image points which were used to determine the camera pose, nullptr if not
135 * @return True, if succeeded
136 */
137 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;
138
139 /**
140 * Optimizes the camera pose using 2D/3D correspondences from known valid marker candidates and from known additional marker coordinates.
141 * @param camera The camera profile to be used, must be valid
142 * @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
143 * @param validMarkerCandidates The known valid marker candidates, at least one
144 * @param additionalMarkerCoordinates The known additional marker coordinates, at least one
145 * @param points The detected points associated with the image
146 * @param pointsDistributionArray The distribution array of the detected points
147 * @param board_T_optimizedCamera The resulting optimized camera pose, with default camera looking towards the positive z-space with y-axis upwards
148 * @param maximalProjectionError The maximal projection error between projected 3D object points and their corresponding 2D observations, with range [0, infinity)
149 * @param usedObjectPointIds Optional resulting ids of the object points which were used to determine the camera pose, nullptr if not of interest
150 * @param usedImagePoints Optional resulting 2D image points which were used to determine the camera pose, nullptr if not of interest
151 * @param usedObjectPoints Optional resulting 3D object points which were used to determine the camera pose, nullptr if not of interest
152 * @return True, if succeeded
153 */
154 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;
155
156 /**
157 * Returns whether this calibration board holds valid data and valid measured indication distances.
158 * @return True, if so
159 */
160 inline bool isValid() const;
161
162 /**
163 * Default copy assignment operator.
164 * @param calibrationBoard The calibration board to assign
165 * @return Reference to this object
166 */
167 MetricCalibrationBoard& operator=(const MetricCalibrationBoard& calibrationBoard) = default;
168
169 /**
170 * Creates a unique metric calibration board based on a unique board id (the seed) and the number of markers the board has.
171 * @param id The unique board id defining the seed for the random number generator, with range [0, infinity)
172 * @param xMarkers The number of horizontal markers the calibration board will have, with range [1, infinity)
173 * @param yMarkers The number of vertical markers the calibration board will have, with range [1, infinity)
174 * @param measurementMetricIndicationWidth The measured metric distance between the left and right measurement indication on the real calibration board, with range (0, infinity)
175 * @param measurementMetricIndicationHeight The measured metric distance between the top and bottom measurement indication on the real calibration board, with range (0, infinity)
176 * @param metricCalibrationBoard The resulting calibration board
177 * @return True, if succeeded
178 */
179 static bool createMetricCalibrationBoard(const unsigned int id, const size_t xMarkers, const size_t yMarkers, const MetricSize& measurementMetricIndicationWidth, const MetricSize& measurementMetricIndicationHeight, MetricCalibrationBoard& metricCalibrationBoard);
180
181 /**
182 * Returns the padding factor of this metric calibration board.
183 * 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.
184 * @return The padding factor, with range (0, 1)
185 */
186 static constexpr double paddingFactor();
187
188 /**
189 * Determines the optimal marker grid for a calibration board with specific paper width and height and minimal marker size.
190 * @param paperWidth The width of the paper, with range (0, infinity)
191 * @param paperHeight The height of the paper, with range (0, infinity)
192 * @param xMarkers The resulting number of horizontal markers, with range [1, infinity)
193 * @param yMarkers The resulting number of vertical markers, with range [1, infinity)
194 * @param minMarkerSize The minimal size of a marker, with range (0, infinity)
195 * @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)
196 * @param paddingFactor The padding factor, with range (0, 1)
197 */
198 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());
199
200 protected:
201
202 /// The measured metric distance between the left and right measurement indication on the real calibration board, with range (0, infinity).
204
205 /// The measured metric distance between the top and bottom measurement indication on the real calibration board, with range (0, infinity).
207
208 /// The board's horizontal marker edge length, with range (0, infinity).
209 Scalar xMetricMarkerSize_ = 0;
210
211 /// The board's vertical marker edge length, with range (0, infinity).
212 Scalar zMetricMarkerSize_ = 0;
213};
214
219
224
229
234
236{
237 ocean_assert(markerCoordinate.x() < xMarkers_ && markerCoordinate.y() < yMarkers_);
238
239 const Scalar markersSizeX = Scalar(xMarkers_) * xMetricMarkerSize_;
240 const Scalar markersSizeZ = Scalar(yMarkers_) * zMetricMarkerSize_;
241
242 const Scalar metricMarkerCenterX = (Scalar(markerCoordinate.x()) + Scalar(0.5)) * xMetricMarkerSize_ - markersSizeX / Scalar(2);
243 const Scalar metricMarkerCenterZ = (Scalar(markerCoordinate.y()) + Scalar(0.5)) * zMetricMarkerSize_ - markersSizeZ / Scalar(2);
244
245 return Vector3(metricMarkerCenterX, 0, metricMarkerCenterZ);
246}
247
248inline Vector3 MetricCalibrationBoard::objectPoint(const MarkerCoordinate& markerCoordinate, const size_t indexInMarker) const
249{
250 ocean_assert(markerCoordinate.x() < xMarkers_ && markerCoordinate.y() < yMarkers_);
251
252 const Vector3 markerCenter = markerCenterPosition(markerCoordinate);
253
254 const BoardMarker& boardMarker = marker(markerCoordinate);
255
256 return boardMarker.objectPoint(markerCenter, xMetricMarkerSize_, zMetricMarkerSize_, indexInMarker);
257}
258
264
266{
267 return 0.1; // 10% of the marker size
268}
269
270}
271
272}
273
274}
275
276#endif // META_OCEAN_CV_CALIBRATION_METRIC_CALIBRATION_BOARD_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:131
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:543
size_t xMarkers_
The number of horizontal markers of this calibration board, with range [1, infinity).
Definition CalibrationBoard.h:423
std::vector< ObjectPointId > ObjectPointIds
Definition of a vector holding object point ids.
Definition CalibrationBoard.h:224
bool isValid() const
Returns whether this calibration board is valid.
Definition CalibrationBoard.h:579
size_t yMarkers_
The number of vertical markers of this calibration board, with range [1, infinity).
Definition CalibrationBoard.h:426
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:220
MetricSize measurementMetricIndicationHeight_
The measured metric distance between the top and bottom measurement indication on the real calibratio...
Definition MetricCalibrationBoard.h:206
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:235
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:248
Scalar zMetricMarkerSize() const
Returns the metric vertical edge length of a marker in the real calibration board.
Definition MetricCalibrationBoard.h:230
Scalar zMetricMarkerSize_
The board's vertical marker edge length, with range (0, infinity).
Definition MetricCalibrationBoard.h:212
MetricSize measurementMetricIndicationWidth_
The measured metric distance between the left and right measurement indication on the real calibratio...
Definition MetricCalibrationBoard.h:203
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:265
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:209
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:215
bool isValid() const
Returns whether this calibration board holds valid data and valid measured indication distances.
Definition MetricCalibrationBoard.h:259
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:225
Vectors3 objectPoints(ObjectPointIds *objectPointIds=nullptr) const
Returns all object points of this calibration board.
Vectors3 markerCornerPositions(CV::PixelPositions *cornerCoordinates=nullptr) const
Returns the 3D positions of all unique marker corner points of this calibration board.
MetricCalibrationBoard & operator=(const MetricCalibrationBoard &calibrationBoard)=default
Default copy assignment operator.
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:272
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
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition Vector2.h:64
float Scalar
Definition of a scalar type.
Definition Math.h:129
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition Vector3.h:65
VectorT3< Scalar > Vector3
Definition of a 3D vector.
Definition Vector3.h:29
The namespace covering the entire Ocean framework.
Definition Accessor.h:15