8 #ifndef META_OCEAN_TRACKING_POINT_ADVANCED_POINT_TRACKER_H
9 #define META_OCEAN_TRACKING_POINT_ADVANCED_POINT_TRACKER_H
23 #include "ocean/tracking/DataContainer.h"
38 template <
unsigned int tSuccessiveFrames>
104 template <
unsigned int tSuccessiveFrames>
106 candidatePoseIds(firstPoseId),
107 candidateIsValid(true)
116 if (useCameraDistortionParameters)
118 const Vector2 firstUndistortedImagePoint(pinholeCamera.
undistort<
true>(firstImagePoint));
119 ocean_assert(firstImagePoint.
sqrDistance(pinholeCamera.
distort<
true>(firstUndistortedImagePoint)) < 1);
127 template <
unsigned int tSuccessiveFrames>
130 ocean_assert(!isValid());
133 ocean_assert(candidateSuccessiveImagePoints.size() == candidateSuccessiveRays.size());
134 for (
size_t n = 0; n < candidateSuccessiveImagePoints.size(); ++n)
136 candidateSuccessiveImagePoints[n].weakClear();
137 candidateSuccessiveRays[n].weakClear();
140 candidateSuccessiveRays.weakResize(1);
142 if (useCameraDistortionParameters)
144 const Vector2 firstUndistortedImagePoint(pinholeCamera.
undistort<
true>(firstImagePoint));
145 ocean_assert(firstImagePoint.
sqrDistance(pinholeCamera.
distort<
true>(firstUndistortedImagePoint)) < 1);
147 candidateSuccessiveRays.front().pushBack(pinholeCamera.
ray(firstUndistortedImagePoint, firstPose));
150 candidateSuccessiveRays.front().pushBack(pinholeCamera.
ray(firstImagePoint, firstPose));
152 candidatePoseIds.weakResize(1);
153 candidatePoseIds.front() = firstPoseId;
155 candidateIsValid =
true;
157 candidateSuccessiveImagePoints.weakResize(1);
158 candidateSuccessiveImagePoints.front().pushBack(firstImagePoint);
161 template <
unsigned int tSuccessiveFrames>
164 return candidateIsValid;
167 template <
unsigned int tSuccessiveFrames>
170 ocean_assert(candidateIsValid);
172 ocean_assert(!candidateSuccessiveImagePoints.empty());
173 ocean_assert(!candidateSuccessiveImagePoints.occupied());
180 for (
size_t n = 0; n < previousImagePoints.
size(); ++n)
184 for (Indices32::const_iterator i = indices.begin(); !currentCandidateIndices.
occupied() && i != indices.end(); ++i)
188 for (
size_t t = 0; t < currentCandidateIndices.
size(); ++t)
189 if (*i == currentCandidateIndices[t])
196 currentCandidateIndices.
pushBack(*i);
202 for (
size_t n = 0; n < currentCandidateIndices.
size(); ++n)
203 currentCandidates.
pushBack(imagePoints[currentCandidateIndices[n]]);
207 previousProjectedRays.
weakResize(candidateSuccessiveRays.size());
209 for (
size_t a = 0; a < candidateSuccessiveRays.size(); ++a)
211 previousProjectedRays[a].
weakResize(candidateSuccessiveRays[a].size());
213 for (
size_t b = 0; b < candidateSuccessiveRays[a].size(); ++b)
214 previousProjectedRays[a][b] = pinholeCamera.
projectToImageIF<
true>(poseIF, candidateSuccessiveRays[a][b], useCameraDistortionParameters);
218 for (
size_t c = 0; c < currentCandidates.
size(); )
220 const Vector2& candidate = currentCandidates[c];
222 size_t nearToLineCounter = 0;
224 for (
size_t a = 0; a < previousProjectedRays.
size(); ++a)
225 for (
size_t b = 0; b < previousProjectedRays[a].
size(); ++b)
227 const Line2& projectedRay = previousProjectedRays[a][b];
229 if (projectedRay && projectedRay.
sqrDistance(candidate) <= sqrProjectionDistance)
236 if (nearToLineCounter == previousProjectedRays.
size())
242 candidateSuccessiveImagePoints.pushBack(currentCandidates);
246 candidateSuccessiveRays.weakResize(candidateSuccessiveRays.size() + 1);
247 for (
size_t n = 0; n < currentCandidates.
size(); ++n)
249 const Vector2 undistortedCandidate(pinholeCamera.
undistort<
true>(currentCandidates[n]));
250 ocean_assert(currentCandidates[n].
sqrDistance(pinholeCamera.
distort<
true>(undistortedCandidate)) < 1);
252 candidateSuccessiveRays.back().securePushBack(pinholeCamera.
ray(undistortedCandidate, pose));
257 ocean_assert(previousProjectedRays.
size() + 1 == candidateSuccessiveImagePoints.size());
258 ocean_assert(previousProjectedRays.
size() >= 1);
260 for (
size_t a = 0; a < previousProjectedRays.
size() - 1; ++a)
264 ocean_assert(projectedRays.
size() == candidateSuccessiveImagePoints[a].size());
266 for (
size_t b = 0; b < projectedRays.
size(); )
268 const Line2& projectedRay = projectedRays[b];
270 bool hasFound =
false;
272 for (
size_t c = 0; c < currentCandidates.
size(); ++c)
273 if (projectedRay.
sqrDistance(currentCandidates[c]) <= sqrProjectionDistance)
284 candidateSuccessiveRays[a].unstableErase(b);
285 candidateSuccessiveImagePoints[a].unstableErase(b);
290 if (projectedRays.
empty())
292 candidateIsValid =
false;
297 candidatePoseIds.pushBack(poseId);
303 for (
size_t a = 0; a + 1 < _imagePoints.
size(); ++a)
304 for (
size_t b = a + 1; b < _imagePoints.
size(); ++b)
305 ocean_assert(_imagePoints[a] != _imagePoints[b]);
309 if (candidateSuccessiveImagePoints.size() >= tSuccessiveFrames)
311 for (
unsigned int n = 0u; n < candidateSuccessiveImagePoints.size(); ++n)
312 if (candidateSuccessiveImagePoints[n].size() != 1)
314 candidateIsValid =
false;
321 for (
size_t n = 0; n < candidateSuccessiveRays.size(); ++n)
322 rays.
pushBack(candidateSuccessiveRays[n].front());
328 candidateIsValid =
false;
332 ocean_assert(candidatePoseIds.size() == candidateSuccessiveRays.size());
334 DataContainer& dataContainer = DataContainer::get();
339 for (
size_t n = 0; n < candidatePoseIds.size(); ++n)
341 ocean_assert(dataContainer.hasPose<
true>(candidatePoseIds[n]));
342 posesIF.
pushBack(dataContainer.poseIF<
true>(candidatePoseIds[n]));
344 imagePoints.
pushBack(candidateSuccessiveImagePoints[n].front());
349 if (!Utilities::optimizeObjectPointIF(pinholeCamera, posesIF.
data(), imagePoints.
data(), (
unsigned int)imagePoints.
size(), initialObjectPoint, optimizedObjectPoint, &finalError) || finalError >
Scalar(0.7 * 0.7))
351 candidateIsValid =
false;
356 posePointPairs.reserve(imagePoints.
size());
358 for (
size_t n = 0; n < imagePoints.
size(); ++n)
360 const unsigned int imageId = dataContainer.registerImagePoint<
true>(imagePoints[n]);
361 posePointPairs.push_back(
PosePointPair(candidatePoseIds[n], imageId));
364 const unsigned int objectId = dataContainer.registerObjectPoint<
true>(optimizedObjectPoint);
366 candidateIsValid =
false;
371 ocean_assert(candidateSuccessiveImagePoints.size() == candidateSuccessiveRays.size());
372 for (
size_t n = 0; n < candidateSuccessiveImagePoints.size(); ++n)
373 ocean_assert(candidateSuccessiveImagePoints[n].size() == candidateSuccessiveRays[n].size());
379 template <
unsigned int tSuccessiveFrames>
382 ocean_assert(!candidateSuccessiveImagePoints.empty());
384 for (
size_t n = 0; n < candidateSuccessiveImagePoints.back().size(); ++n)
385 occupancyArray += candidateSuccessiveImagePoints.back()[n];
388 template <
unsigned int tSuccessiveFrames>
396 template <
unsigned int tSuccessiveFrames>
399 ocean_assert(DataContainer::get().hasPose<true>(poseId));
407 if (!trackerCandidates.empty())
411 for (
size_t n = 0; n < trackerCandidates.size(); ++n)
413 Candidate& candidate = trackerCandidates[n];
417 PosePointPairsObject posePointPairsObject(candidate.
newPose(pinholeCamera, pose, poseIF, poseId, imagePoints, useCameraDistortionParameters, imagePointsDistibution, trackerSearchRadius, trackerSqrProjectionDistance));
419 if (posePointPairsObject)
420 posePointPairsObjects.push_back(std::move(posePointPairsObject));
424 trackerInvalidCandidates.push_back((
unsigned int)n);
434 if (trackerCandidates.empty())
435 trackerCandidates.reserve(imagePoints.size());
440 if (externalOccupancyArray)
442 for (Vectors2::const_iterator i = imagePoints.begin(); i != imagePoints.end(); ++i)
460 if (trackerInvalidCandidates.empty())
461 trackerCandidates.push_back(
Candidate(pinholeCamera, pose, *i, useCameraDistortionParameters, poseId));
464 trackerCandidates[trackerInvalidCandidates.back()].replace(pinholeCamera, pose, *i, useCameraDistortionParameters, poseId);
465 trackerInvalidCandidates.pop_back();
468 internalOccupancyArray += *i;
477 for (Vectors2::const_iterator i = imagePoints.begin(); i != imagePoints.end(); ++i)
480 if (trackerInvalidCandidates.empty())
481 trackerCandidates.push_back(
Candidate(pinholeCamera, pose, *i, useCameraDistortionParameters, poseId));
484 trackerCandidates[trackerInvalidCandidates.back()].replace(pinholeCamera, pose, *i, useCameraDistortionParameters, poseId);
485 trackerInvalidCandidates.pop_back();
523 return posePointPairsObjects;
526 template <
unsigned int tSuccessiveFrames>
529 ocean_assert(pinholeCamera.
isValid());
530 ocean_assert(horizontalBins >= 0u && verticalBins >= 0u);
534 for (Candidates::const_iterator i = trackerCandidates.begin(); i != trackerCandidates.end(); ++i)
536 i->add2OccupancyArray(occupancyArray);
538 return occupancyArray;
This class implements a distribution array.
Definition: SpatialDistribution.h:228
This class implements an occupancy array.
Definition: SpatialDistribution.h:370
bool isOccupiedNeighborhood9(const unsigned int horizontal, const unsigned int vertical) const
Returns whether at least one bin in the 8-neighborhood or the specified bin itself is occupied.
Definition: SpatialDistribution.h:1328
static Indices32 determineNeighbors(const ImagePoint &imagePoint, const ImagePoint *candidatePoints, const size_t numberCandidatePoints, const Scalar radius, const DistributionArray &distributionCandidatePoints)
Determines all candidate points for a given image point (interest point) lying inside a specified cir...
bool isValid() const
Returns whether this matrix is a valid homogeneous transformation.
Definition: HomogenousMatrix4.h:1806
This class implements an infinite line in 2D space.
Definition: Line2.h:83
T sqrDistance(const VectorT2< T > &point) const
Returns the square distance between the line and a given point.
Definition: Line2.h:498
unsigned int width() const
Returns the width of the camera image.
Definition: PinholeCamera.h:1300
LineT3< T > ray(const VectorT2< T > &position, const HomogenousMatrixT4< T > &world_T_camera, const T zoom=T(1)) const
Returns a ray starting at the camera's center and intersection a given 2D point on the image plane.
Definition: PinholeCamera.h:2378
VectorT2< T > distort(const VectorT2< T > &undistorted) const
Returns the distorted position of a given undistorted position defined in pixel coordinates.
Definition: PinholeCamera.h:1418
VectorT2< T > undistort(const VectorT2< T > &distorted, const unsigned int iterations=10u, const T zoom=T(1)) const
Returns the undistorted position of a given distorted position defined in pixel coordinates.
Definition: PinholeCamera.h:1373
bool isValid() const
Returns whether this camera is valid.
Definition: PinholeCamera.h:1572
unsigned int height() const
Returns the height of the camera image.
Definition: PinholeCamera.h:1306
VectorT2< T > projectToImageIF(const HomogenousMatrixT4< T > &flippedCamera_T_world, const VectorT3< T > &objectPoint, const bool distortImagePoint, const T zoom=T(1)) const
Projects a 3D object point to the 2D image plane of the camera by a given inverse camera pose.
Definition: PinholeCamera.h:1816
const T * data() const
Returns the buffer data pointer.
Definition: StaticBuffer.h:240
size_t size() const
Returns the size of this vector.
Definition: StaticVector.h:340
bool occupied() const
Returns whether no free space is left.
Definition: StaticVector.h:346
void unstableErase(const size_t index)
Erases one element from this vector.
Definition: StaticVector.h:506
void weakResize(const size_t size)
Resizes this vector.
Definition: StaticVector.h:534
bool empty() const
Returns whether this vector hold no element.
Definition: StaticVector.h:487
void pushBack(const T &value)
Adds a new element to this vector.
Definition: StaticVector.h:352
const T & back() const
Returns the last elements of this vector.
Definition: StaticVector.h:471
Definition: AdvancedPointTracker.h:44
bool candidateIsValid
Definition: AdvancedPointTracker.h:78
void add2OccupancyArray(Geometry::SpatialDistribution::OccupancyArray &occupancyArray) const
Definition: AdvancedPointTracker.h:380
StaticVector< StaticVectors2, tSuccessiveFrames > StaticVectors2Set
Definition: AdvancedPointTracker.h:50
void replace(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &firstPose, const Vector2 &firstImagePoint, const bool useCameraDistortionParameters, const unsigned int firstPoseId)
Definition: AdvancedPointTracker.h:128
bool isValid() const
Definition: AdvancedPointTracker.h:162
StaticLines3Set candidateSuccessiveRays
Definition: AdvancedPointTracker.h:74
StaticVector< Line2, 10u > StaticLines2
Definition: AdvancedPointTracker.h:52
PosePointPairsObject newPose(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &pose, const HomogenousMatrix4 &poseIF, const unsigned int poseId, const Vectors2 &imagePoints, const bool useCameraDistortionParameters, const Geometry::SpatialDistribution::DistributionArray &imagePointsDistribution, const Scalar searchRadius, const Scalar sqrProjectionDistanceconst)
Definition: AdvancedPointTracker.h:168
StaticVector< Vector2, 10u > StaticVectors2
Definition: AdvancedPointTracker.h:49
StaticVectors2Set candidateSuccessiveImagePoints
Definition: AdvancedPointTracker.h:72
StaticPoseIndices candidatePoseIds
Definition: AdvancedPointTracker.h:76
Candidate(const PinholeCamera &pinholeCamera, const HomogenousMatrix4 &firstPose, const Vector2 &firstImagePoint, const bool useCameraDistortionParameters, const unsigned int firstPoseId)
Definition: AdvancedPointTracker.h:105
StaticVector< unsigned int, tSuccessiveFrames > StaticPoseIndices
Definition: AdvancedPointTracker.h:47
StaticVector< StaticLines2, tSuccessiveFrames > StaticLines2Set
Definition: AdvancedPointTracker.h:53
StaticVector< Line3, 10u > StaticLines3
Definition: AdvancedPointTracker.h:55
StaticVector< StaticLines3, tSuccessiveFrames > StaticLines3Set
Definition: AdvancedPointTracker.h:56
This class implements an advanced point tracker.
Definition: AdvancedPointTracker.h:40
AdvancedPointTracker()
Definition: AdvancedPointTracker.h:389
Scalar trackerSqrProjectionDistance
Definition: AdvancedPointTracker.h:97
PosePointPairsObjects newPose(const PinholeCamera &pinholeCamera, const unsigned int poseId, const Vectors2 &imagePoints, const bool useCameraDistortionParameters, Geometry::SpatialDistribution::OccupancyArray *externalOccupancyArray=nullptr, const bool updateOccupancyArray=true)
Definition: AdvancedPointTracker.h:397
std::vector< Candidate > Candidates
Definition: AdvancedPointTracker.h:81
Geometry::SpatialDistribution::OccupancyArray determineOccupancyArray(const PinholeCamera &pinholeCamera, const unsigned int horizontalBins, const unsigned int verticalBins)
Definition: AdvancedPointTracker.h:527
Candidates trackerCandidates
Definition: AdvancedPointTracker.h:99
Scalar trackerSearchRadius
Definition: AdvancedPointTracker.h:95
Indices32 trackerInvalidCandidates
Definition: AdvancedPointTracker.h:101
Definition of a simple struct combining a pose and an image point id.
Definition: PosePointPair.h:51
Definition of an object that combines an object id with the pairs of poses and image points.
Definition: PosePointPair.h:100
static bool determineInitialObjectPoint(const Line3 *rays, const size_t numberRays, Vector3 &objectPoint, const bool checkFrontPosition=true)
Determines an initial 3D position from several 3D rays that start at the camera center and intersect ...
T sqrDistance(const VectorT2< T > &right) const
Returns the square distance between this 2D position and a second 2D position.
Definition: Vector2.h:633
unsigned int sqrDistance(const char first, const char second)
Returns the square distance between two values.
Definition: base/Utilities.h:1089
std::vector< Index32 > Indices32
Definition of a vector holding 32 bit index values.
Definition: Base.h:96
float Scalar
Definition of a scalar type.
Definition: Math.h:128
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition: Vector2.h:64
std::vector< PosePointPair > PosePointPairs
Definition of a vector holding pose point pairs.
Definition: PosePointPair.h:29
std::vector< PosePointPairsObject > PosePointPairsObjects
Definition of a vector holding pose objects.
Definition: PosePointPair.h:38
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15