Ocean
Loading...
Searching...
No Matches
CalibrationBoardDetector.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_CALIBRATION_BOARD_DETECTOR_H
9#define META_OCEAN_CV_CALIBRATION_CALIBRATION_BOARD_DETECTOR_H
10
16
17#include "ocean/base/Frame.h"
18#include "ocean/base/Worker.h"
19
21
22#include <deque>
23
24namespace Ocean
25{
26
27namespace CV
28{
29
30namespace Calibration
31{
32
33/**
34 * This class implements the detector for the calibration board.
35 * @ingroup cvcalibration
36 */
37class OCEAN_CV_CALIBRATION_EXPORT CalibrationBoardDetector
38{
39 protected:
40
41 /**
42 * Definition of a double-ended queue holding indices of continuous lines of marker points.
43 */
44 using ContinuousLineIndices = std::deque<Index32>;
45
46 /**
47 * Definition of a vector holding a binary mask indicating whether a marker point has been used already.
48 */
49 using UsedPoints = std::vector<uint8_t>;
50
51 public:
52
53 /**
54 * Determines the marker candidates based on marker points detected in an image.
55 * @param points The marker points detected in the image, must be valid
56 * @param pointsDistributionArray The distribution array associated with the detected marker points, must be valid
57 * @param maximalDistance The maximal distance between two neighboring marker points, in pixels, with range (0, infinity)
58 * @param maxDistancePercentage The percentage defining the maximal distance between the predicted point and the actual point based on the distance between previous point and predicted point, with range [0, infinity)
59 * @param markerCandidates The resulting marker candidates with valid boarder indices
60 * @return True, if succeeded
61 */
62 static bool determineMarkerCandidates(const Points& points, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, const Scalar maximalDistance, const Scalar maxDistancePercentage, MarkerCandidates& markerCandidates);
63
64 /**
65 * Determines the initial camera field-of-view based on several marker candidates with valid border indices.
66 * Each marker candidate and it's potential 4-neighborhood is checked against several field-of-views.<br>
67 * The field-of-view resulting in the lowest projection error is used.
68 * @param width The width of the image in pixel, with range [1, infinity)
69 * @param height The height of the image in pixel, with range [1, infinity)
70 * @param points The marker points detected in the image, must be valid
71 * @param markerCandidates The marker candidates from which the initial field-of-view will be determined, must have valid border indices, at least one
72 * @param anyCameraType The type of the camera profile to be used, must be valid
73 * @param fovX The resulting initial horizontal field-of-view, in radian, with range [minFovX, maxFovX]
74 * @param minFovX The minimal horizontal field-of-view to be used, in radian, with range (0, maxFovX)
75 * @param maxFovX The maximal horizontal field-of-view to be used, in radian, with range (minFovX, PI)
76 * @param steps The number of steps to be used for the field-of-view search, with range [2, infinity)
77 * @return True, if succeeded
78 */
79 static bool determineInitialCameraFieldOfView(const unsigned int width, const unsigned height, const Points& points, const MarkerCandidates& markerCandidates, const AnyCameraType anyCameraType, Scalar& fovX, const Scalar minFovX = Numeric::deg2rad(20), const Scalar maxFovX = Numeric::deg2rad(175), const size_t steps = 20);
80
81 /**
82 * Determines the camera pose for a marker candidate.
83 * This function is intended for marker candidates with only valid border indices.<br>
84 * The resulting camera pose is the transformation between camera and the marker candidate.<br>
85 * The marker is defined in the xz-plane, the origin is in the center of the marker, and marker points have a distance of 1 to each other.
86 * @param camera The camera profile to be used, must be valid
87 * @param markerCandidate The marker candidate for which the camera pose will be determined, must have valid border indices
88 * @param points The marker points detected in the image
89 * @param markerCandidate_T_camera The resulting camera pose in relation to the marker candidate
90 * @param maximalProjectionError The maximal projection error between the 3D object points and the 2D image points, in pixel, with range [0, infinity)
91 * @return True, if succeeded
92 */
93 static bool determineCameraPoseForMarker(const AnyCamera& camera, const MarkerCandidate& markerCandidate, const Points& points, HomogenousMatrix4& markerCandidate_T_camera, const Scalar maximalProjectionError);
94
95 /**
96 * Determines the remaining unknown point indices of a marker candidate.
97 * This function is intended for marker candidates with only valid border indices.
98 * @param camera The camera profile to be used, must be valid
99 * @param markerCandidate_T_camera The known transformation between camera and the marker candidate, must be valid
100 * @param markerCandidate The marker candidate to update
101 * @param points The marker points detected in the image
102 * @param pointsDistributionArray The distribution array associated with the detected marker points, must be valid
103 * @param maximalProjectionError The maximal projection error between the 3D object points and the 2D image points, in pixel, with range [0, infinity)
104 * @return True, if all remaining (9) point indices could be determined
105 */
106 static bool determineRemainingMarkerPointIndices(const AnyCamera& camera, const HomogenousMatrix4& markerCandidate_T_camera, MarkerCandidate& markerCandidate, const Points& points, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, const Scalar maximalProjectionError);
107
108 /**
109 * Determines the marker candidates most suitable to start the detection of the calibration board.
110 * Marker candidates with most neighbors located in the center of the image are preferred due to fisheye cameras.
111 * @param width The width of the image in pixel, with range [1, infinity)
112 * @param height The height of the image in pixel, with range [1, infinity)
113 * @param markerCandidates The marker candidates from which the best candidates will be determined, at least one
114 * @param points The marker points detected in the image, must be valid
115 * @param bestMarkerCandidateIndices The resulting indices of the best marker candidates, with range [0, markerCandidates.size() - 1]
116 * @return True, if succeeded
117 */
118 static bool determineStartMarkerCandidates(const unsigned int width, const unsigned int height, const MarkerCandidates& markerCandidates, const Points& points, Indices32& bestMarkerCandidateIndices);
119
120 /**
121 * Identifies and locates marker candidates on a calibration board.
122 * For each marker candidate, this function determines the camera pose, the remaining (inner) point indices, and the marker ID.
123 * Invalid marker candidates are removed.
124 * Then, the function assigns board coordinates to marker candidates based on their neighborhood relationship.
125 * Marker candidates without valid board coordinates are not removed as they may still be useful.
126 * @param camera The camera profile to be used, must be valid
127 * @param calibrationBoard The calibration board defining the marker layout, must be valid
128 * @param yFrame The grayscale frame in which the points have been detected, used for debug visualizations, must be valid
129 * @param points The marker points detected in the image, must be valid
130 * @param pointsDistributionArray The distribution array associated with the detected marker points, must be valid
131 * @param markerCandidates The marker candidates to be identified and located, will be modified in place (invalid candidates removed, coordinates assigned)
132 * @param maximalProjectionError The maximal projection error between the 3D object points and the 2D image points, in pixel, with range [0, infinity)
133 * @return True, if at least one marker candidate with a valid marker ID remains
134 */
135 static bool identifyAndLocateMarkerCandidates(const AnyCamera& camera, const CalibrationBoard& calibrationBoard, const Frame& yFrame, const Points& points, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, MarkerCandidates& markerCandidates, const Scalar maximalProjectionError);
136
137 /**
138 * Detects a calibration board in an image.
139 * @param camera The camera profile to be used, must be valid
140 * @param yFrame The image in which the calibration board will be detected, must be valid
141 * @param calibrationBoard The calibration board to be detected, must be valid
142 * @param observation The resulting observation of the calibration board
143 * @param maximalProjectionError The maximal projection error between the 3D object points and the 2D image points, in pixel, with range [0, infinity)
144 * @param numberVisibleBoardPoints Optional resulting number of board points that projected inside the camera image and could theoretically be used for pose estimation, nullptr if not of interest
145 * @param worker Optional worker object to distribute the computation
146 * @return True, if succeeded
147 */
148 static bool detectCalibrationBoard(const AnyCamera& camera, const Frame& yFrame, const MetricCalibrationBoard& calibrationBoard, CalibrationBoardObservation& observation, const Scalar maximalProjectionError, size_t* numberVisibleBoardPoints = nullptr, Worker* worker = nullptr);
149
150 /**
151 * Optimizes the camera pose by determining additional 2D/3D correspondences from all board points that are not yet part of the used correspondences.
152 * For each unused board point, the function projects the 3D object point into the image using the given camera pose and checks whether a matching detected point exists nearby.
153 * @param camera The camera profile to be used, must be valid
154 * @param calibrationBoard The calibration board defining the marker layout, must be valid
155 * @param board_T_camera The initial camera pose transforming camera to board, with default camera looking towards the positive z-space with y-axis upwards, must be valid
156 * @param points The detected points in the image, must be valid
157 * @param pointsDistributionArray The distribution array associated with the detected points, must be valid
158 * @param board_T_optimizedCamera The resulting optimized camera pose, with default camera looking towards the positive z-space with y-axis upwards
159 * @param maximalProjectionError The maximal projection error between projected 3D object points and their corresponding 2D observations, with range [0, infinity)
160 * @param usedObjectPointIds The ids of already used object points, will be extended with newly found correspondences
161 * @param usedObjectPoints The 3D object points already used, will be extended with newly found correspondences
162 * @param usedImagePoints The 2D image points already used, will be extended with newly found correspondences
163 * @param numberVisibleBoardPoints Optional resulting number of board points that projected inside the camera image and could theoretically be used for pose estimation, nullptr if not of interest
164 * @return True, if succeeded
165 */
166 static bool optimizeCameraPoseWithAdditionalCorrespondences(const AnyCamera& camera, const MetricCalibrationBoard& calibrationBoard, const HomogenousMatrix4& board_T_camera, const Points& points, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, HomogenousMatrix4& board_T_optimizedCamera, const Scalar maximalProjectionError, CalibrationBoard::ObjectPointIds& usedObjectPointIds, Vectors3& usedObjectPoints, Vectors2& usedImagePoints, size_t* numberVisibleBoardPoints = nullptr);
167
168 protected:
169
170 /**
171 * Determines a continuous line of marker points with same sign.
172 * @param pointIndexA The index of the first marker point, located on the line, with range [0, points.size() - 1]
173 * @param pointIndexB The index of the second neighbor marker point, located on the line, with range [0, points.size() - 1]
174 * @param points The marker points detected in the image
175 * @param usedPoints Binary mask indicating whether a marker point has been used already, one for each point, only points not yet used will be used for the determination of the continuous line
176 * @param pointsDistributionArray The distribution array associated with the detected marker points, must be valid
177 * @param lineIndices The resulting indices of the marker points located on the continuous line
178 * @param maxDistancePercentage The percentage defining the maximal distance between the predicted point and the actual point based on the distance between previous point and predicted point, with range [0, infinity)
179 * @return True, if succeeded
180 */
181 static bool determineContinuousLine(const size_t pointIndexA, const size_t pointIndexB, const Points& points, const UsedPoints& usedPoints, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, ContinuousLineIndices& lineIndices, const Scalar maxDistancePercentage);
182
183 /**
184 * Determines a closed rectangle of marker points (the 16 marker points of the border of a marker).
185 * @param lineIndices The indices of the marker points located on a known continuous line with 5 points, will be extended by the remaining 11 points of the rectangle
186 * @param points The marker points detected in the image
187 * @param usedPoints Binary mask indicating whether a marker point has been used already, one for each point, only points not yet used will be used for the determination of the rectangle
188 * @param pointsDistributionArray The distribution array associated with the detected marker points, must be valid
189 * @param maxDistancePercentage The percentage defining the maximal distance between the predicted point and the actual point based on the distance between previous point and predicted point, with range [0, infinity)
190 * @return True, if succeeded
191 */
192 static bool determineClosedRectangle(ContinuousLineIndices& lineIndices, const Points& points, const UsedPoints& usedPoints, const Geometry::SpatialDistribution::DistributionArray& pointsDistributionArray, const Scalar maxDistancePercentage);
193
194 /**
195 * Determines the neighborhood relationship in marker candidates.
196 * @param markerCandidates The marker candidates for which the neighborhood relationship will be determined
197 * @param points The marker points detected in the image
198 * @param maxDistancePercentage The percentage defining the maximal distance between the predicted point and the actual point based on the distance between previous point and predicted point, with range [0, infinity)
199 */
200 static void determineMarkerCandidateNeighborhood(MarkerCandidates& markerCandidates, const Points& points, const Scalar maxDistancePercentage);
201};
202
203}
204
205}
206
207}
208
209#endif // META_OCEAN_CV_CALIBRATION_CALIBRATION_BOARD_DETECTOR_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:131
This class implements the detector for the calibration board.
Definition CalibrationBoardDetector.h:38
static bool optimizeCameraPoseWithAdditionalCorrespondences(const AnyCamera &camera, const MetricCalibrationBoard &calibrationBoard, const HomogenousMatrix4 &board_T_camera, const Points &points, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, HomogenousMatrix4 &board_T_optimizedCamera, const Scalar maximalProjectionError, CalibrationBoard::ObjectPointIds &usedObjectPointIds, Vectors3 &usedObjectPoints, Vectors2 &usedImagePoints, size_t *numberVisibleBoardPoints=nullptr)
Optimizes the camera pose by determining additional 2D/3D correspondences from all board points that ...
std::vector< uint8_t > UsedPoints
Definition of a vector holding a binary mask indicating whether a marker point has been used already.
Definition CalibrationBoardDetector.h:49
static bool determineCameraPoseForMarker(const AnyCamera &camera, const MarkerCandidate &markerCandidate, const Points &points, HomogenousMatrix4 &markerCandidate_T_camera, const Scalar maximalProjectionError)
Determines the camera pose for a marker candidate.
static bool determineRemainingMarkerPointIndices(const AnyCamera &camera, const HomogenousMatrix4 &markerCandidate_T_camera, MarkerCandidate &markerCandidate, const Points &points, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, const Scalar maximalProjectionError)
Determines the remaining unknown point indices of a marker candidate.
static bool detectCalibrationBoard(const AnyCamera &camera, const Frame &yFrame, const MetricCalibrationBoard &calibrationBoard, CalibrationBoardObservation &observation, const Scalar maximalProjectionError, size_t *numberVisibleBoardPoints=nullptr, Worker *worker=nullptr)
Detects a calibration board in an image.
std::deque< Index32 > ContinuousLineIndices
Definition of a double-ended queue holding indices of continuous lines of marker points.
Definition CalibrationBoardDetector.h:44
static void determineMarkerCandidateNeighborhood(MarkerCandidates &markerCandidates, const Points &points, const Scalar maxDistancePercentage)
Determines the neighborhood relationship in marker candidates.
static bool determineClosedRectangle(ContinuousLineIndices &lineIndices, const Points &points, const UsedPoints &usedPoints, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, const Scalar maxDistancePercentage)
Determines a closed rectangle of marker points (the 16 marker points of the border of a marker).
static bool identifyAndLocateMarkerCandidates(const AnyCamera &camera, const CalibrationBoard &calibrationBoard, const Frame &yFrame, const Points &points, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, MarkerCandidates &markerCandidates, const Scalar maximalProjectionError)
Identifies and locates marker candidates on a calibration board.
static bool determineInitialCameraFieldOfView(const unsigned int width, const unsigned height, const Points &points, const MarkerCandidates &markerCandidates, const AnyCameraType anyCameraType, Scalar &fovX, const Scalar minFovX=Numeric::deg2rad(20), const Scalar maxFovX=Numeric::deg2rad(175), const size_t steps=20)
Determines the initial camera field-of-view based on several marker candidates with valid border indi...
static bool determineContinuousLine(const size_t pointIndexA, const size_t pointIndexB, const Points &points, const UsedPoints &usedPoints, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, ContinuousLineIndices &lineIndices, const Scalar maxDistancePercentage)
Determines a continuous line of marker points with same sign.
static bool determineStartMarkerCandidates(const unsigned int width, const unsigned int height, const MarkerCandidates &markerCandidates, const Points &points, Indices32 &bestMarkerCandidateIndices)
Determines the marker candidates most suitable to start the detection of the calibration board.
static bool determineMarkerCandidates(const Points &points, const Geometry::SpatialDistribution::DistributionArray &pointsDistributionArray, const Scalar maximalDistance, const Scalar maxDistancePercentage, MarkerCandidates &markerCandidates)
Determines the marker candidates based on marker points detected in an image.
This class implements a basic calibration board.
Definition CalibrationBoard.h:38
std::vector< ObjectPointId > ObjectPointIds
Definition of a vector holding object point ids.
Definition CalibrationBoard.h:224
This class implements an observation of a calibration board.
Definition CalibrationBoardObservation.h:45
This class implements a candidate of a marker.
Definition MarkerCandidate.h:48
This class extends the calibration board with metric information.
Definition MetricCalibrationBoard.h:39
This class implements Ocean's image class.
Definition Frame.h:1879
This class implements a distribution array.
Definition SpatialDistribution.h:272
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< 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
AnyCameraType
Definition of individual camera types.
Definition AnyCamera.h:112
The namespace covering the entire Ocean framework.
Definition Accessor.h:15