Ocean
ContourTracker.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_SEGMENTATION_CONTOUR_TRACKER_H
9 #define META_OCEAN_CV_SEGMENTATION_CONTOUR_TRACKER_H
10 
13 
14 #include "ocean/base/Frame.h"
16 
17 #include "ocean/cv/FramePyramid.h"
18 #include "ocean/cv/PixelPosition.h"
19 
21 #include "ocean/math/Vector2.h"
22 
23 namespace Ocean
24 {
25 
26 namespace CV
27 {
28 
29 namespace Segmentation
30 {
31 
32 /**
33  * This class implements a contour tracker able to detect an object and to track the object's contour within a video sequence with realtime performance.
34  * The tracker supports planar backgrounds as well as slightly non-planar backgrounds and can handle changing contour shapes to some extend.
35  * @ingroup cvsegmentation
36  */
37 class OCEAN_CV_SEGMENTATION_EXPORT ContourTracker
38 {
39  public:
40 
41  /**
42  * Creates a new tracker object.
43  */
44  ContourTracker() = default;
45 
46  /**
47  * Returns the dense contour of the most recent detection or tracking interaction.
48  * @return The object's dense contour, an empty contour if no object has been detected or tracked
49  */
50  inline const PixelContour& denseContour() const;
51 
52  /**
53  * Returns the dense contour of the most recent detection or tracking interaction with sub-pixel accuracy.
54  * @return The object's dense contour, an empty contour if no object has been detected or tracked
55  */
56  inline const Vectors2& denseContourSubPixel() const;
57 
58  /**
59  * Returns the most dominant homography which has been determined during the most recent tracking iteration.
60  * The homography will transform points defined in the previous frame to points defined in the current frame (pointCurrent = H * pointPrevious).
61  * @return The most recent homography
62  */
63  inline const SquareMatrix3& homography() const;
64 
65  /**
66  * Re-detects an object (the object's contour respectively) in a given frame within a region enclosed by a rough contour.
67  * @param frame The frame in which the frame is visible, must be valid
68  * @param roughContour The rough contour roughly enclosing the object to be detected, with at least three locations
69  * @param randomGenerator Random number generator object
70  * @param extraContourOffset The explicit additional offset between the actual object and the final resulting contour in pixel, with range [0, infinity)
71  * @param worker Optional worker object to distribute the computation
72  * @param yFrame Optional Y8 frame of the given frame to speed up the computation, with same frame dimension and pixel origin
73  * @return True, if succeeded
74  */
75  bool detectObject(const Frame& frame, const PixelContour& roughContour, RandomGenerator& randomGenerator, const unsigned int extraContourOffset, Worker* worker = nullptr, const Frame& yFrame = Frame());
76 
77  /**
78  * Tracks an already detected contour (around an object) from the previous frame to the current frame.
79  * @param frame The current frame for which the new contour will be determined, with same frame type as used for the detection or a previous tracking iteration, must be valid
80  * @param randomGenerator Random number generator object
81  * @param extraContourOffset The explicit additional offset between the actual object and the final resulting contour in pixel, with range [0, infinity)
82  * @param worker Optional worker object to distribute the computation
83  * @param yFrame Optional Y8 frame of the given frame to speed up the computation, with same frame dimension and pixel origin
84  * @return True, if succeeded
85  */
86  bool trackObject(const Frame& frame, RandomGenerator& randomGenerator, const unsigned int extraContourOffset, Worker* worker = nullptr, const Frame& yFrame = Frame());
87 
88  /**
89  * Clears all resources and resets the tracker.
90  */
91  void clear();
92 
93  private:
94 
95  /// The frame pyramid of the previous frame.
97 
98  /// The frame pyramid of the current frame.
100 
101  /// The contour belonging to the most recent (previous) frame.
103 
104  /// The contour belonging to the most recent (previous) frame with sub-pixel accuracy.
106 
107  /// The strongest (feature) points of the contour of the previous frame.
109 
110  /// The homography which has been determine during the previous tracking iteration.
111  SquareMatrix3 previousHomography_ = SquareMatrix3(true);
112 
113  /// An intermediate rough mask frame.
115 
116  /// True, if the tracker should try to invoke a planar tracker; False, if the tracker should use a more generous approach.
117  bool usePlanarTracking_ = false;
118 };
119 
121 {
122  return previousDenseContour_;
123 }
124 
126 {
128 }
129 
131 {
132  return previousHomography_;
133 }
134 
135 }
136 
137 }
138 
139 }
140 
141 #endif // META_OCEAN_CV_SEGMENTATION_CONTOUR_TRACKER_H
This class implements a frame pyramid.
Definition: FramePyramid.h:37
This class implements a contour tracker able to detect an object and to track the object's contour wi...
Definition: ContourTracker.h:38
Vectors2 previousDenseContourSubPixel_
The contour belonging to the most recent (previous) frame with sub-pixel accuracy.
Definition: ContourTracker.h:105
FramePyramid currentFramePyramid_
The frame pyramid of the current frame.
Definition: ContourTracker.h:99
PixelContour previousDenseContour_
The contour belonging to the most recent (previous) frame.
Definition: ContourTracker.h:102
const PixelContour & denseContour() const
Returns the dense contour of the most recent detection or tracking interaction.
Definition: ContourTracker.h:120
Frame intermediateRoughMask_
An intermediate rough mask frame.
Definition: ContourTracker.h:114
bool detectObject(const Frame &frame, const PixelContour &roughContour, RandomGenerator &randomGenerator, const unsigned int extraContourOffset, Worker *worker=nullptr, const Frame &yFrame=Frame())
Re-detects an object (the object's contour respectively) in a given frame within a region enclosed by...
const Vectors2 & denseContourSubPixel() const
Returns the dense contour of the most recent detection or tracking interaction with sub-pixel accurac...
Definition: ContourTracker.h:125
void clear()
Clears all resources and resets the tracker.
const SquareMatrix3 & homography() const
Returns the most dominant homography which has been determined during the most recent tracking iterat...
Definition: ContourTracker.h:130
FramePyramid previousFramePyramid_
The frame pyramid of the previous frame.
Definition: ContourTracker.h:96
Vectors2 previousContourStrongest_
The strongest (feature) points of the contour of the previous frame.
Definition: ContourTracker.h:108
bool trackObject(const Frame &frame, RandomGenerator &randomGenerator, const unsigned int extraContourOffset, Worker *worker=nullptr, const Frame &yFrame=Frame())
Tracks an already detected contour (around an object) from the previous frame to the current frame.
SquareMatrix3 previousHomography_
The homography which has been determine during the previous tracking iteration.
Definition: ContourTracker.h:111
ContourTracker()=default
Creates a new tracker object.
This class implements Ocean's image class.
Definition: Frame.h:1760
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
This class implements a worker able to distribute function calls over different threads.
Definition: Worker.h:33
SquareMatrixT3< Scalar > SquareMatrix3
Definition of the SquareMatrix3 object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with ...
Definition: SquareMatrix3.h:35
std::vector< Vector2 > Vectors2
Definition of a vector holding Vector2 objects.
Definition: Vector2.h:64
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15