Ocean
FrameTracker.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_TRACKING_OFFLINE_FRAME_TRACKER_H
9 #define META_OCEAN_TRACKING_OFFLINE_FRAME_TRACKER_H
10 
13 
15 
17 #include "ocean/cv/FramePyramid.h"
18 
21 
22 namespace Ocean
23 {
24 
25 namespace Tracking
26 {
27 
28 namespace Offline
29 {
30 
31 // Forward declaration.
32 class FrameTracker;
33 
34 /**
35  * Definition of a smart object reference holding a FrameTracker object.
36  * @see FrameTracker.
37  * @ingroup trackingoffline
38  */
40 
41 /**
42  * This class implements the base class for all visual offline tracker using frames to provide the tracking data.
43  * @ingroup trackingoffline
44  */
45 class OCEAN_TRACKING_OFFLINE_EXPORT FrameTracker : virtual public OfflineTracker
46 {
47  friend class FrameTrackerComponent;
48 
49  protected:
50 
51  /**
52  * This class implements the base class for all components of a frame tracker.
53  */
55  {
56  protected:
57 
58  /**
59  * Definition of individual results for the component iterations.
60  */
62  {
63  /// The iteration succeeded and thus the component proceeds with the next frame.
65  /// The iteration failed and thus also the component fails.
67  /// The iteration finished and thus also the component finishes.
68  IR_FINISHED
69  };
70 
71  public:
72 
73  /**
74  * Creates a new component object.
75  * @param parent The parent tracker that invokes this component
76  */
78 
79  /**
80  * Destructs a tracker component object.
81  */
82  virtual ~TrackerComponent();
83 
84  /**
85  * Invokes the component.
86  * The component starts at the initial frame index and proceeds frames with increasing indices, afterwards the components restarts at the initial frame index and proceeds frames with decreasing indices
87  * @param startEvent Event that is invoked at the beginning of the component
88  * @param breakEvent Event that is invoked if the component breaks
89  * @param finishEvent Event that is invoked if the component finishes
90  * @param lowerFrameIndex Index of the frame defining the lower (including) tracking boundary
91  * @param initialFrameIndex Index of the frame at that the tracking process will start
92  * @param upperFrameIndex Index of the frame defining the upper (including) tracking boundary
93  * @return True, if the component succeeded
94  */
95  virtual bool invoke(const TrackerEvent& startEvent, TrackerEvent& breakEvent, TrackerEvent& finishEvent, const unsigned int lowerFrameIndex, const unsigned int initialFrameIndex, const unsigned int upperFrameIndex);
96 
97  protected:
98 
99  /**
100  * Component start event function.
101  */
102  virtual bool onStart(const unsigned int lowerFrameIndex, const unsigned int initialFrameIndex, const unsigned int upperFrameIndex);
103 
104  /**
105  * Component increasing start event function.
106  * This function is executed before the increasing component iterations are applied.
107  * @return True, if the start has succeeded
108  */
109  virtual bool onStartIncreasing();
110 
111  /**
112  * Component decreasing start event function.
113  * This function is executed before the decreasing component iterations are applied.
114  * @return True, if the start has succeeded
115  */
116  virtual bool onStartDecreasing();
117 
118  /**
119  * Iteration setup event function.
120  * This function is executed before an iteration is applied
121  * @return True, if the setup has succeeded
122  */
123  virtual bool onSetupIteration(const unsigned int index);
124 
125  /**
126  * Applies one component step.
127  * @param previousIndex Index of the previous frame that has been handled in the previous iteration, -1 for the first iteration
128  * @param currentIndex Index of the current frame that is handled in this iteration
129  * @param iteration Index of the current iteration
130  * @param maximalIterations Number of maximal iterations that will be applied
131  * @return Returns the iteration result
132  */
133  virtual IterationResult onFrame(const unsigned int previousIndex, const unsigned int currentIndex, const unsigned int iteration, const unsigned int maximalIterations) = 0;
134 
135  /**
136  * Component stop event function.
137  * This function is executed after the component iterations have been applied.
138  * @param lowerFrameIndex Index of the frame defining the lower (including) tracking boundary
139  * @param initialFrameIndex Index of the frame at that the tracking process will start
140  * @param upperFrameIndex Index of the frame defining the upper (including) tracking boundary
141  * @return True, if the start has succeeded
142  */
143  virtual bool onStop(const unsigned int lowerFrameIndex, const unsigned int initialFrameIndex, const unsigned int upperFrameIndex);
144 
145  protected:
146 
147  /// Parent tracker object that invokes this component.
149  };
150 
151  /**
152  * This class implements the base class for all components of a frame tracker.
153  */
155  {
156  public:
157 
158  /**
159  * Creates a new component object.
160  * @param tracker The tracker object for that this component will be executed
161  */
163 
164  /**
165  * Destructor
166  */
167  inline ~FrameTrackerComponent() override;
168 
169  protected:
170 
171  /**
172  * Component decreasing start event function.
173  * @see TrackerComponent::onStartDecreasing().
174  */
175  bool onStartDecreasing() override;
176 
177  /**
178  * Iteration setup event function.
179  * @see TrackerComponent::onSetupIteration().
180  */
181  bool onSetupIteration(const unsigned int index) override;
182 
183  protected:
184 
185  /// Frame that has been created in the previous component iteration.
187 
188  /// Frame that has been created for the current component iteration.
190  };
191 
192  /**
193  * This class implements the base class for all components of a frame tracker.
194  */
196  {
197  public:
198 
199  /**
200  * Creates a new component object.
201  * @param tracker The tracker object for that this component will be executed
202  */
204 
205  /**
206  * Destructor
207  */
208  inline ~FramePyramidTrackerComponent() override;
209 
210  protected:
211 
212  /**
213  * Component increasing start event function.
214  * @see TrackerComponent::onStartIncreasing().
215  */
216  bool onStartIncreasing() override;
217 
218  /**
219  * Component decreasing start event function.
220  * @see TrackerComponent::onStartDecreasing().
221  */
222  bool onStartDecreasing() override;
223 
224  /**
225  * Iteration setup event function.
226  * @see TrackerComponent::onSetupIteration().
227  */
228  bool onSetupIteration(const unsigned int index) override;
229 
230  protected:
231 
232  /// Frame pyramid that has been created in the previous component iteration.
234 
235  /// Frame pyramid that has been created for the current component iteration.
237 
238  /// Number of pyramid layers that should be created in each pyramid.
239  unsigned int framePyramidLayers_ = (unsigned int)(-1);
240  };
241 
242  public:
243 
244  /**
245  * Returns whether the offline tracker has finished and succeeded since the last start or has not been started yet.
246  * @return True, if so
247  * @see finished().
248  */
249  bool succeeded() const override;
250 
251  /**
252  * Returns the camera object this is applied by this tracker.
253  * @return The pinhole camera object
254  */
255  inline const PinholeCamera& camera() const;
256 
257  /**
258  * Returns the camera precision of this tracker.
259  * @return The pinhole camera precision, in squared pixel errors, -1 if unknown
260  */
261  inline Scalar cameraPrecision() const;
262 
263  /**
264  * Starts the offline tracker.
265  * Ensure that a frame provider has been set before.<br>
266  * @return True, if succeeded
267  * @see stop(), finished(), setFrameProvider().
268  */
269  bool start() override;
270 
271  /**
272  * Sets the frame provider interface that allows to request the individual frames for the offline tracker.
273  * @param frameProviderInterface Frame provider interface to be set
274  * @return True, if succeeded, returns false if the interface has been set already
275  */
276  virtual bool setFrameProviderInterface(const CV::FrameProviderInterfaceRef& frameProviderInterface);
277 
278  /**
279  * Sets the frame range of this frame tracker.
280  * The range specify a subset of the entire frames that are investigated during tracking.<br>
281  * The given parameters should match the number of available frames provided by the frame provider.<br>
282  * If the frame provider provides less frames than specified by the range parameters, the parameters will be adjusted automatically.<br>
283  * The range cannot be adjusted if the tracker is active.<br>
284  * @param lowerFrameIndex Index of the first frame that will be investigated, with range [0, infinity)
285  * @param upperFrameIndex Index of the last frame that will be investigated, with range [lowerFrameIndex, infinity)
286  * @param startFrameIndex Index of the start frame at which the tracking will start, with range [lowerFrameIndex, upperFrameIndex], or -1 to use no specific start frame
287  * @return True, if succeeded
288  * @see setFrameProviderInterface()
289  */
290  virtual bool setTrackingFrameRange(const unsigned int lowerFrameIndex, const unsigned int upperFrameIndex, const unsigned int startFrameIndex = (unsigned int)(-1));
291 
292  /**
293  * Sets a camera object that will be used by this tracker.
294  * @param pinholeCamera The pinhole camera object to be used
295  * @param cameraPrecision The precision of the given camera object, in squared pixel errors, should be -1 if unknown
296  * @param optimizeCamera True, to take the given camera object as rough estimation only
297  * @return True, if succeeded
298  */
299  virtual bool setCamera(const PinholeCamera& pinholeCamera, const Scalar cameraPrecision, const bool optimizeCamera);
300 
301  protected:
302 
303  /**
304  * Creates a new offline frame tracker object.
305  */
306  FrameTracker() = default;
307 
308  /**
309  * Creates a new offline frame tracker object.
310  * @param frameProviderInterface Frame provider interface to be set
311  */
312  inline FrameTracker(const CV::FrameProviderInterfaceRef& frameProviderInterface);
313 
314  /**
315  * Thread run function.
316  */
317  void threadRun() override;
318 
319  /**
320  * Frame tracker run function.
321  * @param frameType Frame type of the tracking frames
322  * @return True, if the tracking succeeded
323  */
324  virtual bool applyFrameTracking(const FrameType& frameType) = 0;
325 
326  /**
327  * Applies the initialization of the frame tracker.
328  * The function initializes the frame provider interface and waits until a valid frame type is known.
329  * @return The frame type of the frame provider interface, an invalid frame type if the initialization fails
330  */
332 
333  /**
334  * Updates the camera of this tracker and invokes the corresponding state event(s).
335  * @param pinholeCamera The new pinhole camera for this tracker
336  */
337  virtual void updateCamera(const PinholeCamera& pinholeCamera);
338 
339  protected:
340 
341  /// Frame provider interface that allows to request the individual frames for this offline tracker.
343 
344  /// Index of the lower frame range.
345  unsigned int lowerFrameIndex_ = (unsigned int)(-1);
346 
347  /// Index of the frame at that the tracking will start, or -1 if no specific frame is specified
348  unsigned int startFrameIndex_ = (unsigned int)(-1);
349 
350  /// Index of the upper frame range.
351  unsigned int upperFrameIndex_ = (unsigned int)(-1);
352 
353  /// The camera object of this tracker.
355 
356  /// The precision of the camera object in squared pixel errors.
357  Scalar cameraPrecision_ = Scalar(-1);
358 
359  /// True, if the camera needs to be optimized during the tracking process.
360  bool optimizeCamera_ = true;
361 
362  /// Succeeded state of the tracker.
363  bool succeeded_ = true;
364 };
365 
367 {
368  // Nothing else to do.
369 }
370 
372 {
373  // Nothing else to do.
374 }
375 
376 inline FrameTracker::FrameTracker(const CV::FrameProviderInterfaceRef& frameProviderInterface) :
377  OfflineTracker(),
378  frameProviderInterface_(frameProviderInterface)
379 {
380  // nothing to do here
381 }
382 
383 inline const PinholeCamera& FrameTracker::camera() const
384 {
385  return camera_;
386 }
387 
389 {
390  return cameraPrecision_;
391 }
392 
393 }
394 
395 }
396 
397 }
398 
399 #endif // META_OCEAN_TRACKING_OFFLINE_FRAME_TRACKER_H
This class implements a frame pyramid.
Definition: FramePyramid.h:37
This class implements Ocean's image class.
Definition: Frame.h:1792
Definition of a frame type composed by the frame dimension, pixel format and pixel origin.
Definition: Frame.h:30
This template class implements a smart object reference which is a specialization of an ObjectRef obj...
Definition: SmartObjectRef.h:90
This class implements the base class for all components of a frame tracker.
Definition: FrameTracker.h:196
FramePyramidTrackerComponent(FrameTracker &tracker)
Creates a new component object.
CV::FramePyramid previousFramePyramid_
Frame pyramid that has been created in the previous component iteration.
Definition: FrameTracker.h:233
~FramePyramidTrackerComponent() override
Destructor.
Definition: FrameTracker.h:371
CV::FramePyramid currentFramePyramid_
Frame pyramid that has been created for the current component iteration.
Definition: FrameTracker.h:236
bool onStartIncreasing() override
Component increasing start event function.
bool onStartDecreasing() override
Component decreasing start event function.
bool onSetupIteration(const unsigned int index) override
Iteration setup event function.
This class implements the base class for all components of a frame tracker.
Definition: FrameTracker.h:155
bool onStartDecreasing() override
Component decreasing start event function.
~FrameTrackerComponent() override
Destructor.
Definition: FrameTracker.h:366
Frame currentFrame_
Frame that has been created for the current component iteration.
Definition: FrameTracker.h:189
Frame previousFrame_
Frame that has been created in the previous component iteration.
Definition: FrameTracker.h:186
bool onSetupIteration(const unsigned int index) override
Iteration setup event function.
FrameTrackerComponent(FrameTracker &tracker)
Creates a new component object.
This class implements the base class for all components of a frame tracker.
Definition: FrameTracker.h:55
virtual ~TrackerComponent()
Destructs a tracker component object.
virtual IterationResult onFrame(const unsigned int previousIndex, const unsigned int currentIndex, const unsigned int iteration, const unsigned int maximalIterations)=0
Applies one component step.
IterationResult
Definition of individual results for the component iterations.
Definition: FrameTracker.h:62
@ IR_SUCCEEDED
The iteration succeeded and thus the component proceeds with the next frame.
Definition: FrameTracker.h:64
@ IR_FAILED
The iteration failed and thus also the component fails.
Definition: FrameTracker.h:66
virtual bool onStartIncreasing()
Component increasing start event function.
virtual bool invoke(const TrackerEvent &startEvent, TrackerEvent &breakEvent, TrackerEvent &finishEvent, const unsigned int lowerFrameIndex, const unsigned int initialFrameIndex, const unsigned int upperFrameIndex)
Invokes the component.
virtual bool onStart(const unsigned int lowerFrameIndex, const unsigned int initialFrameIndex, const unsigned int upperFrameIndex)
Component start event function.
virtual bool onStop(const unsigned int lowerFrameIndex, const unsigned int initialFrameIndex, const unsigned int upperFrameIndex)
Component stop event function.
FrameTracker & parent_
Parent tracker object that invokes this component.
Definition: FrameTracker.h:148
virtual bool onSetupIteration(const unsigned int index)
Iteration setup event function.
TrackerComponent(FrameTracker &parent)
Creates a new component object.
virtual bool onStartDecreasing()
Component decreasing start event function.
This class implements the base class for all visual offline tracker using frames to provide the track...
Definition: FrameTracker.h:46
const PinholeCamera & camera() const
Returns the camera object this is applied by this tracker.
Definition: FrameTracker.h:383
PinholeCamera camera_
The camera object of this tracker.
Definition: FrameTracker.h:354
virtual bool setTrackingFrameRange(const unsigned int lowerFrameIndex, const unsigned int upperFrameIndex, const unsigned int startFrameIndex=(unsigned int)(-1))
Sets the frame range of this frame tracker.
bool start() override
Starts the offline tracker.
virtual bool setCamera(const PinholeCamera &pinholeCamera, const Scalar cameraPrecision, const bool optimizeCamera)
Sets a camera object that will be used by this tracker.
CV::FrameProviderInterfaceRef frameProviderInterface_
Frame provider interface that allows to request the individual frames for this offline tracker.
Definition: FrameTracker.h:342
Scalar cameraPrecision() const
Returns the camera precision of this tracker.
Definition: FrameTracker.h:388
bool succeeded() const override
Returns whether the offline tracker has finished and succeeded since the last start or has not been s...
FrameTracker()=default
Creates a new offline frame tracker object.
void threadRun() override
Thread run function.
Scalar cameraPrecision_
The precision of the camera object in squared pixel errors.
Definition: FrameTracker.h:357
virtual void updateCamera(const PinholeCamera &pinholeCamera)
Updates the camera of this tracker and invokes the corresponding state event(s).
FrameType applyFrameTrackingInitialization()
Applies the initialization of the frame tracker.
virtual bool setFrameProviderInterface(const CV::FrameProviderInterfaceRef &frameProviderInterface)
Sets the frame provider interface that allows to request the individual frames for the offline tracke...
virtual bool applyFrameTracking(const FrameType &frameType)=0
Frame tracker run function.
This class is the base class for all offline tracker objects.
Definition: OfflineTracker.h:45
This class implements the base class for all tracker events.
Definition: TrackerEvent.h:51
float Scalar
Definition of a scalar type.
Definition: Math.h:128
SmartObjectRef< FrameTracker, OfflineTracker > FrameTrackerRef
Definition of a smart object reference holding a FrameTracker object.
Definition: FrameTracker.h:32
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15