Ocean
Loading...
Searching...
No Matches
StereoBullseyeDetector.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 OCEAN_CV_DETECTOR_BULLSEYES_STEREOBULLSEYEDETECTOR_H
9#define OCEAN_CV_DETECTOR_BULLSEYES_STEREOBULLSEYEDETECTOR_H
10
12
15
16#include "ocean/base/Frame.h"
17#include "ocean/base/Worker.h"
18
20
22#include "ocean/math/Matrix.h"
23#include "ocean/math/Vector3.h"
24
25namespace Ocean
26{
27
28namespace CV
29{
30
31namespace Detector
32{
33
34namespace Bullseyes
35{
36
37/**
38 * This class implements a stereo detector for bullseye patterns.
39 * @ingroup cvdetectorbullseyes
40 */
41class OCEAN_CV_DETECTOR_BULLSEYES_EXPORT StereoBullseyeDetector
42{
43 public:
44
45 /**
46 * Definition of a pair of bullseyes.
47 */
48 using BullseyePair = std::pair<Bullseye, Bullseye>;
49
50 /**
51 * Definition of a vector holding bullseye pairs.
52 */
53 using BullseyePairs = std::vector<BullseyePair>;
54
55 /**
56 * Definition of a pair of vectors of bullseyes, one from each camera.
57 */
58 using BullseyeGroup = std::array<Bullseyes, 2>;
59
60 /**
61 * This class holds the most important parameters for the stereo detector.
62 * Currently, this class inherits all parameters from the monocular detector.
63 */
65 {
66 public:
67
68 /**
69 * Creates a new parameter object with default parameters.
70 */
71 Parameters() = default;
72
73 /**
74 * Returns the default parameters for the stereo detector.
75 * @return The default parameters
76 */
78
79 /**
80 * Returns the maximum allowed distance from a point to the epipolar line.
81 * @return The maximum distance in pixels, with range [0, infinity)
82 */
84
85 /**
86 * Sets the maximum allowed distance from a point to the epipolar line.
87 * @param distance The maximum distance in pixels, with range [0, infinity)
88 * @return True if the value was valid and set successfully, otherwise false
89 */
91
92 protected:
93
94 /// When matching points, this defines the maximum allowed distance from a point to the epipolar line of the other point, in pixels, with range [0, infinity)
95 Scalar maxDistanceToEpipolarLine_ = Scalar(5);
96 };
97
98 protected:
99
100 /// An alias for the fisheye epipolar geometry.
102
103 public:
104
105 /**
106 * Detects bullseyes in a pair of stereo frames.
107 * @param cameras The camera profiles for the stereo pair, must contain exactly 2 valid cameras
108 * @param yFrames The stereo frames in which bullseyes will be detected, must contain exactly 2 valid 8-bit grayscale frames
109 * @param world_T_device The transformation from the device coordinate system to the world coordinate system, must be valid
110 * @param device_T_cameras The transformations from each camera coordinate system to the device coordinate system, must contain exactly 2 valid transformations
111 * @param bullseyePairs The resulting pairs of detected bullseyes, one from each camera
112 * @param bullseyeCenters The resulting 3D positions of the triangulated bullseye centers in world coordinates
113 * @param parameters The parameters for the detector, must be valid
114 * @param worker Optional worker to distribute the computation
115 * @return True, if succeeded
116 */
117 static bool detectBullseyes(const SharedAnyCameras& cameras, const Frames& yFrames, const HomogenousMatrix4& world_T_device, const HomogenousMatrices4& device_T_cameras, BullseyePairs& bullseyePairs, Vectors3& bullseyeCenters, const Parameters& parameters = Parameters::defaultParameters(), Worker* worker = nullptr);
118
119 protected:
120
121 /**
122 * Matches bullseyes detected in two stereo frames using epipolar geometry.
123 * @param cameras The camera profiles for the stereo pair, must contain exactly 2 valid cameras
124 * @param yFrames The stereo frames in which bullseyes will be detected, must contain exactly 2 valid 8-bit grayscale frames
125 * @param world_T_device The transformation from the device coordinate system to the world coordinate system, must be valid
126 * @param device_T_cameras The transformations from each camera coordinate system to the device coordinate system, must contain exactly 2 valid transformations
127 * @param epipolarGeometry The epipolar geometry describing the relationship between the two stereo cameras, must be valid
128 * @param bullseyeGroup The bullseyes detected in both frames, bullseyeGroup[0] contains bullseyes from the first frame, bullseyeGroup[1] contains bullseyes from the second frame
129 * @param maxDistanceToEpipolarLine The maximum allowed distance from a bullseye to its epipolar line, in pixels, with range (0, infinity)
130 * @param bullseyePairs The resulting matched bullseye pairs
131 * @return True, if succeeded
132 */
133 static bool matchBullseyes(const SharedAnyCameras& cameras, const Frames& yFrames, const HomogenousMatrix4& world_T_device, const HomogenousMatrices4& device_T_cameras, const EpipolarGeometry& epipolarGeometry, const BullseyeGroup& bullseyeGroup, const Scalar maxDistanceToEpipolarLine, BullseyePairs& bullseyePairs);
134
135 /**
136 * Returns an invalid (arbitrarily large) matching cost value used to indicate that two bullseyes cannot be matched.
137 * This value is large enough to prevent matching but not so large as to cause numerical issues.
138 * @return The invalid matching cost value
139 */
140 constexpr static Scalar invalidMatchingCost();
141
142 /**
143 * Computes the matching cost between two bullseyes from different stereo cameras.
144 * The cost is based on epipolar geometry constraints and radius similarity, with both components
145 * transformed using a sigmoid function to produce smooth, bounded costs in the range [0, 1].
146 * Lower costs indicate better matches.
147 * @param bullseyeA The bullseye from the first camera, must be valid
148 * @param bullseyeB The bullseye from the second camera, must be valid
149 * @param epipolarGeometry The epipolar geometry describing the relationship between the two cameras, must be valid
150 * @param maxSqrDistance The squared maximum allowed distance from a point to its matching epipolar line, in pixels squared, with range (0, infinity)
151 * @param cameraB_s_cameraA The scale factor relating measurements in camera B to camera A (e.g., width_B / width_A), with range (0, infinity)
152 * @return The matching cost value, with range [0, 1], where lower values indicate better matches
153 */
154 static Scalar computeBullseyeMatchingCost(const Bullseye& bullseyeA, const Bullseye& bullseyeB, const EpipolarGeometry& epipolarGeometry, const Scalar maxSqrDistance, const Scalar cameraB_s_cameraA);
155
156 /**
157 * Computes a cost matrix containing matching costs between all pairs of bullseyes from two cameras.
158 * Each element (i, j) in the matrix represents the matching cost between bullseye i from camera A and bullseye j from camera B.
159 * The cost matrix can be used with an assignment solver to find optimal bullseye correspondences.
160 * @param bullseyesA The bullseyes detected in the first camera, must not be empty
161 * @param bullseyesB The bullseyes detected in the second camera, must not be empty
162 * @param epipolarGeometry The epipolar geometry describing the relationship between the two cameras, must be valid
163 * @param maxSqrDistanceToEpipolarLine The squared maximum allowed distance from a point to its epipolar line, in pixels squared, with range (0, infinity)
164 * @param cameraB_s_cameraA The scale factor relating measurements in camera B to camera A (e.g., width_B / width_A), with range (0, infinity)
165 * @param costMatrix The resulting cost matrix with dimensions [bullseyesA.size() x bullseyesB.size()]
166 * @return True if the cost matrix was successfully computed
167 */
168 static bool computeBullseyeMatchingCostMatrix(const Bullseyes& bullseyesA, const Bullseyes& bullseyesB, const EpipolarGeometry& epipolarGeometry, const Scalar maxSqrDistanceToEpipolarLine, const Scalar cameraB_s_cameraA, Matrix& costMatrix);
169
170 /**
171 * Triangulates a single matched bullseye pair to compute its 3D position in world coordinates.
172 * This function casts rays from both camera centers through the bullseye positions and finds their nearest point
173 * to determine the 3D location. It also computes reprojection errors to assess triangulation quality.
174 * @param cameraA The camera profile for the first camera, must be valid
175 * @param cameraB The camera profile for the second camera, must be valid
176 * @param world_T_cameraA The transformation from camera A coordinate system to world coordinate system, must be valid
177 * @param world_T_cameraB The transformation from camera B coordinate system to world coordinate system, must be valid
178 * @param epipolarGeometry The epipolar geometry describing the relationship between the two cameras, must be valid
179 * @param bullseyeA The bullseye from camera A, must be valid and within camera A's field of view
180 * @param bullseyeB The bullseye from camera B, must be valid and within camera B's field of view
181 * @param bullseyeCenter The resulting 3D position of the bullseye center in world coordinates
182 * @param reprojectionErrorA The resulting reprojection error for camera A, in pixels
183 * @param reprojectionErrorB The resulting reprojection error for camera B, in pixels
184 * @return True if triangulation succeeded and the 3D point is in front of both cameras, otherwise false
185 */
186 static bool triangulateBullseye(const AnyCamera& cameraA, const AnyCamera& cameraB, const HomogenousMatrix4& world_T_cameraA, const HomogenousMatrix4& world_T_cameraB, const EpipolarGeometry& epipolarGeometry, const Bullseye& bullseyeA, const Bullseye& bullseyeB, Vector3& bullseyeCenter, Scalar& reprojectionErrorA, Scalar& reprojectionErrorB);
187
188 /**
189 * Triangulates matched bullseye pairs to compute their 3D positions.
190 * @param cameras The camera profiles for the stereo pair, must contain exactly 2 valid cameras
191 * @param world_T_device The transformation from the device coordinate system to the world coordinate system, must be valid
192 * @param device_T_cameras The transformations from each camera coordinate system to the device coordinate system, must contain exactly 2 valid transformations
193 * @param epipolarGeometry The epipolar geometry describing the relationship between the two stereo cameras, must be valid
194 * @param candidates The candidate bullseye pairs to triangulate
195 * @param bullseyePairs The resulting validated bullseye pairs
196 * @param bullseyeCenters The resulting 3D positions of the bullseye centers in world coordinates
197 * @param reprojectionErrorsA The resulting reprojection errors for camera A, in pixels, one value per validated bullseye pair
198 * @param reprojectionErrorsB The resulting reprojection errors for camera B, in pixels, one value per validated bullseye pair
199 * @return True, if succeeded
200 */
201 static bool triangulateBullseyes(const SharedAnyCameras& cameras, const HomogenousMatrix4& world_T_device, const HomogenousMatrices4& device_T_cameras, const EpipolarGeometry& epipolarGeometry, const BullseyePairs& candidates, BullseyePairs& bullseyePairs, Vectors3& bullseyeCenters, Scalars& reprojectionErrorsA, Scalars& reprojectionErrorsB);
202};
203
205{
206 // Arbitrarily large value that doesn't cause numerical issues.
207 return Scalar(1000);
208}
209
210} // namespace Bullseyes
211
212} // namespace Detector
213
214} // namespace CV
215
216} // namespace Ocean
217
218#endif // OCEAN_CV_DETECTOR_BULLSEYES_STEREOBULLSEYEDETECTOR_H
This class implements the abstract base class for all AnyCamera objects.
Definition AnyCamera.h:130
Definition of a bullseye composed of a location and a radius.
Definition Bullseye.h:32
This class holds the most important parameters for the detector.
Definition MonoBullseyeDetector.h:46
This class holds the most important parameters for the stereo detector.
Definition StereoBullseyeDetector.h:65
bool setMaxDistanceToEpipolarLine(const Scalar distance)
Sets the maximum allowed distance from a point to the epipolar line.
Scalar maxDistanceToEpipolarLine() const
Returns the maximum allowed distance from a point to the epipolar line.
static Parameters defaultParameters()
Returns the default parameters for the stereo detector.
Parameters()=default
Creates a new parameter object with default parameters.
This class implements a stereo detector for bullseye patterns.
Definition StereoBullseyeDetector.h:42
static Scalar computeBullseyeMatchingCost(const Bullseye &bullseyeA, const Bullseye &bullseyeB, const EpipolarGeometry &epipolarGeometry, const Scalar maxSqrDistance, const Scalar cameraB_s_cameraA)
Computes the matching cost between two bullseyes from different stereo cameras.
std::pair< Bullseye, Bullseye > BullseyePair
Definition of a pair of bullseyes.
Definition StereoBullseyeDetector.h:48
static bool detectBullseyes(const SharedAnyCameras &cameras, const Frames &yFrames, const HomogenousMatrix4 &world_T_device, const HomogenousMatrices4 &device_T_cameras, BullseyePairs &bullseyePairs, Vectors3 &bullseyeCenters, const Parameters &parameters=Parameters::defaultParameters(), Worker *worker=nullptr)
Detects bullseyes in a pair of stereo frames.
static bool computeBullseyeMatchingCostMatrix(const Bullseyes &bullseyesA, const Bullseyes &bullseyesB, const EpipolarGeometry &epipolarGeometry, const Scalar maxSqrDistanceToEpipolarLine, const Scalar cameraB_s_cameraA, Matrix &costMatrix)
Computes a cost matrix containing matching costs between all pairs of bullseyes from two cameras.
static constexpr Scalar invalidMatchingCost()
Returns an invalid (arbitrarily large) matching cost value used to indicate that two bullseyes cannot...
Definition StereoBullseyeDetector.h:204
std::vector< BullseyePair > BullseyePairs
Definition of a vector holding bullseye pairs.
Definition StereoBullseyeDetector.h:53
static bool triangulateBullseyes(const SharedAnyCameras &cameras, const HomogenousMatrix4 &world_T_device, const HomogenousMatrices4 &device_T_cameras, const EpipolarGeometry &epipolarGeometry, const BullseyePairs &candidates, BullseyePairs &bullseyePairs, Vectors3 &bullseyeCenters, Scalars &reprojectionErrorsA, Scalars &reprojectionErrorsB)
Triangulates matched bullseye pairs to compute their 3D positions.
static bool matchBullseyes(const SharedAnyCameras &cameras, const Frames &yFrames, const HomogenousMatrix4 &world_T_device, const HomogenousMatrices4 &device_T_cameras, const EpipolarGeometry &epipolarGeometry, const BullseyeGroup &bullseyeGroup, const Scalar maxDistanceToEpipolarLine, BullseyePairs &bullseyePairs)
Matches bullseyes detected in two stereo frames using epipolar geometry.
static bool triangulateBullseye(const AnyCamera &cameraA, const AnyCamera &cameraB, const HomogenousMatrix4 &world_T_cameraA, const HomogenousMatrix4 &world_T_cameraB, const EpipolarGeometry &epipolarGeometry, const Bullseye &bullseyeA, const Bullseye &bullseyeB, Vector3 &bullseyeCenter, Scalar &reprojectionErrorA, Scalar &reprojectionErrorB)
Triangulates a single matched bullseye pair to compute its 3D position in world coordinates.
std::array< Bullseyes, 2 > BullseyeGroup
Definition of a pair of vectors of bullseyes, one from each camera.
Definition StereoBullseyeDetector.h:58
This class implements fisheye epipolar geometry functionality.
Definition FisheyeEpipolarGeometry.h:29
This class implements a worker able to distribute function calls over different threads.
Definition Worker.h:33
std::vector< Frame > Frames
Definition of a vector holding padding frames.
Definition Frame.h:1771
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::vector< Scalar > Scalars
Definition of a vector holding Scalar objects.
Definition Math.h:145
SharedAnyCamerasT< Scalar > SharedAnyCameras
Definition of a vector holding AnyCamera objects.
Definition AnyCamera.h:90
std::vector< Vector3 > Vectors3
Definition of a vector holding Vector3 objects.
Definition Vector3.h:65
std::vector< Bullseye > Bullseyes
Definition of a vector holding bullseyes.
Definition Bullseye.h:103
The namespace covering the entire Ocean framework.
Definition Accessor.h:15