SLAMflex SE  0.1.0
SLAMflex provides detection and tracking of dominant planes for smartphone devices. This plane can then be used to show AR content relative to the plane orientation. The detection of plane is performed in the field of view of the smartphone camera. In subsequent frames it is tracked. The interface returns the plane position and orientation.
Tracker.h
Go to the documentation of this file.
1 //-*- C++ -*-
2 // Copyright 2008 Isis Innovation Limited
3 //
4 // This header declares the Tracker class.
5 // The Tracker is one of main components of the system,
6 // and is responsible for determining the pose of a camera
7 // from a video feed. It uses the Map to track, and communicates
8 // with the MapMaker (which runs in a different thread)
9 // to help construct this map.
10 //
11 // Initially there is no map, so the Tracker also has a mode to
12 // do simple patch tracking across a stereo pair. This is handled
13 // by the TrackForInitialMap() method and associated sub-methods.
14 // Once there is a map, TrackMap() is used.
15 //
16 // Externally, the tracker should be used by calling TrackFrame()
17 // with every new input video frame. This then calls either
18 // TrackForInitialMap() or TrackMap() as appropriate.
19 //
20 
21 #ifndef __TRACKER_H
22 #define __TRACKER_H
23 
24 #include "MapMaker.h"
25 #include "ATANCamera.h"
26 #include "MiniPatch.h"
27 #include "Relocaliser.h"
28 
29 #include <sstream>
30 #include <vector>
31 #include <list>
32 #include "globals.h"
33 #include "SendMessage.h"
34 #include "SLAMflex.h"
35 
36 
37 
38 class TrackerData;
39 struct Trail // This struct is used for initial correspondences of the first stereo pair.
40 {
44 };
45 
46 class Tracker
47 {
48 public:
49  Tracker(CVD::ImageRef irVideoSize, const ATANCamera &c, Map &m, MapMaker &mm);
50 
51  // TrackFrame is the main working part of the tracker: call this every frame.
52  void TrackFrame(CVD::Image<CVD::byte> &imFrame, uint hnd,bool bDraw);
53 
55 
56  // Gets messages to be printed on-screen for the user.
57  std::string GetMessageForUser();
58  void StartTracking();
59  void StopTracking();
60  std::string getVCorners();
61  void SetSendMessageToUnity(SendMessage sm); //Used for setting one way communication between Tracker and Unity3D
63 
64 protected:
65  KeyFrame mCurrentKF; // The current working frame as a keyframe struct
66 
67  // The major components to which the tracker needs access:
68  Map &mMap; // The map, consisting of points and keyframes
69  MapMaker &mMapMaker; // The class which maintains the map
70  ATANCamera mCamera; // Projection model
71  Relocaliser mRelocaliser; // Relocalisation module
72 
73  CVD::ImageRef mirSize; // Image size of whole image
74 
75  void Reset(); // Restart from scratch. Also tells the mapmaker to reset itself.
76  void GetEulerAnglesFromRotationMatrix(Matrix<3,3, double> &m, float &heading, float &attitude, float &bank);
77  inline float RadiansToDegrees(float r) { return r*(180/M_PI); }
78  // The following members are used for initial map tracking (to get the first stereo pair and correspondences):
79  void TrackForInitialMap(); // This is called by TrackFrame if there is not a map yet.
82  TRAIL_TRACKING_COMPLETE} mnInitialStage; // How far are we towards making the initial map?
83  void TrailTracking_Start(); // First frame of initial trail tracking. Called by TrackForInitialMap.
84  int TrailTracking_Advance(); // Steady-state of initial trail tracking. Called by TrackForInitialMap.
85  std::list<Trail> mlTrails; // Used by trail tracking
86  KeyFrame mFirstKF; // First of the stereo pair
87  KeyFrame mPreviousFrameKF; // Used by trail tracking to check married matches
88 
89  // Methods for tracking the map once it has been made:
90  void TrackMap(); // Called by TrackFrame if there is a map.
91  void AssessTrackingQuality(); // Heuristics to choose between good, poor, bad.
92  void ApplyMotionModel(); // Decaying velocity motion model applied prior to TrackMap
93  void UpdateMotionModel(); // Motion model is updated after TrackMap
94  int SearchForPoints(std::vector<TrackerData*> &vTD,
95  int nRange,
96  int nFineIts); // Finds points in the image
97  Vector<6> CalcPoseUpdate(std::vector<TrackerData*> vTD,
98  double dOverrideSigma = 0.0,
99  bool bMarkOutliers = false); // Updates pose from found points.
100  SE3<> mse3CamFromWorld; // Camera pose: this is what the tracker updates every frame.
101  SE3<> mse3StartPos; // What the camera pose was at the start of the frame.
102  Vector<6> mv6CameraVelocity; // Motion model
103  double mdVelocityMagnitude; // Used to decide on coarse tracking
104  double mdMSDScaledVelocityMagnitude; // Velocity magnitude scaled by relative scene depth.
105  bool mbDidCoarse; // Did tracking use the coarse tracking stage?
106 
107  bool mbDraw; // Should the tracker draw anything to OpenGL?
108 
109  // Interface with map maker:
110  int mnFrame; // Frames processed since last reset
111  int mnLastKeyFrameDropped; // Counter of last keyframe inserted.
112  void AddNewKeyFrame(); // Gives the current frame to the mapmaker to use as a keyframe
113 
114  // Tracking quality control:
119 
120  // Relocalisation functions:
121  bool AttemptRecovery(); // Called by TrackFrame if tracking is lost.
122  bool mbJustRecoveredSoUseCoarse;// Always use coarse tracking after recovery!
123 
124  // Frame-to-frame motion init:
127  void CalcSBIRotation();
130 
131  // User interaction for initial tracking:
133  std::ostringstream mMessageForUser;
134 
135  // GUI interface:
136  void GUICommandHandler(std::string sCommand, std::string sParams);
137  static void GUICommandCallBack(void* ptr, std::string sCommand, std::string sParams);
138  struct Command {std::string sCommand; std::string sParams; };
139  std::vector<Command> mvQueuedCommands;
140 
141  SendMessage sendToUnity; //Used to send message to Unity
142  int threshold; //Threshold for fast corner detection
143  bool thresholdReached; //If threshold is reached stop automatic threshold adjustment
144  DetectionState currentState; //Current state of tracking
145 
146 
147 };
148 
149 #endif
150 
151 
152 
153 
154 
155 
Tracker(CVD::ImageRef irVideoSize, const ATANCamera &c, Map &m, MapMaker &mm)
Definition: Tracker.cpp:28
Definition: Tracker.h:39
Vector< 6 > CalcPoseUpdate(std::vector< TrackerData * > vTD, double dOverrideSigma=0.0, bool bMarkOutliers=false)
Definition: Tracker.cpp:839
SmallBlurryImage * mpSBILastFrame
Definition: Tracker.h:125
std::vector< Command > mvQueuedCommands
Definition: Tracker.h:139
static void GUICommandCallBack(void *ptr, std::string sCommand, std::string sParams)
Definition: Tracker.cpp:295
Vector< 6 > mv6CameraVelocity
Definition: Tracker.h:102
MapMaker & mMapMaker
Definition: Tracker.h:69
#define LEVELS
Definition: KeyFrame.h:33
MiniPatch mPatch
Definition: Tracker.h:41
CVD::ImageRef irCurrentPos
Definition: Tracker.h:42
void TrackMap()
Definition: Tracker.cpp:493
int SearchForPoints(std::vector< TrackerData * > &vTD, int nRange, int nFineIts)
Definition: Tracker.cpp:776
bool thresholdReached
Definition: Tracker.h:143
void StopTracking()
Definition: Tracker.cpp:54
int manMeasFound[LEVELS]
Definition: Tracker.h:116
void TrailTracking_Start()
Definition: Tracker.cpp:413
void Reset()
Definition: Tracker.cpp:74
void ApplyMotionModel()
Definition: Tracker.cpp:932
void GetEulerAnglesFromRotationMatrix(Matrix< 3, 3, double > &m, float &heading, float &attitude, float &bank)
Definition: Tracker.cpp:254
double mdVelocityMagnitude
Definition: Tracker.h:103
int mnLostFrames
Definition: Tracker.h:118
std::string getVCorners()
Definition: Tracker.cpp:1043
bool mbDidCoarse
Definition: Tracker.h:105
CVD::ImageRef mirSize
Definition: Tracker.h:73
bool AttemptRecovery()
Definition: Tracker.cpp:279
double mdMSDScaledVelocityMagnitude
Definition: Tracker.h:104
enum Tracker::@13 mnInitialStage
void AssessTrackingQuality()
Definition: Tracker.cpp:975
void SetSendMessageToUnity(SendMessage sm)
Definition: Tracker.cpp:60
SendMessage sendToUnity
Definition: Tracker.h:141
std::string GetMessageForUser()
Definition: Tracker.cpp:1027
CVD::ImageRef irInitialPos
Definition: Tracker.h:43
void GUICommandHandler(std::string sCommand, std::string sParams)
Definition: Tracker.cpp:304
DetectionState currentState
Definition: Tracker.h:144
Definition: se3.h:50
ATANCamera mCamera
Definition: Tracker.h:70
SmallBlurryImage * mpSBIThisFrame
Definition: Tracker.h:126
Relocaliser mRelocaliser
Definition: Tracker.h:71
void CalcSBIRotation()
Definition: Tracker.cpp:1032
std::ostringstream mMessageForUser
Definition: Tracker.h:133
int TrailTracking_Advance()
Definition: Tracker.cpp:442
bool mbUserPressedSpacebar
Definition: Tracker.h:132
void UpdateMotionModel()
Definition: Tracker.cpp:948
void StartTracking()
Definition: Tracker.cpp:49
KeyFrame mPreviousFrameKF
Definition: Tracker.h:87
Definition: Map.h:24
std::list< Trail > mlTrails
Definition: Tracker.h:85
enum Tracker::@14 mTrackingQuality
Vector< 6 > mv6SBIRot
Definition: Tracker.h:128
int mnFrame
Definition: Tracker.h:110
bool mbDraw
Definition: Tracker.h:107
void TrackForInitialMap()
Definition: Tracker.cpp:345
SE3 mse3CamFromWorld
Definition: Tracker.h:100
std::string sCommand
Definition: Tracker.h:138
void TrackFrame(CVD::Image< CVD::byte > &imFrame, uint hnd, bool bDraw)
Definition: Tracker.cpp:110
int manMeasAttempted[LEVELS]
Definition: Tracker.h:115
bool mbUseSBIInit
Definition: Tracker.h:129
bool mbJustRecoveredSoUseCoarse
Definition: Tracker.h:122
KeyFrame mCurrentKF
Definition: Tracker.h:65
SE3 mse3StartPos
Definition: Tracker.h:101
Map & mMap
Definition: Tracker.h:68
SE3 GetCurrentPose()
Definition: Tracker.h:54
DetectionState GetCurrentDetectionState()
Definition: Tracker.cpp:65
void AddNewKeyFrame()
Definition: Tracker.cpp:966
std::string sParams
Definition: Tracker.h:138
DetectionState
Enum for state of SLAM detection process.
Definition: SLAMflex.h:11
KeyFrame mFirstKF
Definition: Tracker.h:86
int mnLastKeyFrameDropped
Definition: Tracker.h:111
int threshold
Definition: Tracker.h:142
float RadiansToDegrees(float r)
Definition: Tracker.h:77