SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVTrackedFeatures.h
Go to the documentation of this file.
1 /**
2  * \file CVTrackedFeatures.h
3  * \date Spring 2017
4  * \remarks Please use clangformat to format the code. See more code style on
5  * https://github.com/cpvrlab/SLProject4/wiki/SLProject-Coding-Style
6  * \authors Pascal Zingg, Timon Tschanz, Marcus Hudritsch, Michael Goettlicher
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #ifndef CVTRACKEDFEATURES_H
11 #define CVTRACKEDFEATURES_H
12 
13 /*
14 The OpenCV library version 3.4 or above with extra module must be present.
15 If the application captures the live video stream with OpenCV you have
16 to define in addition the constant APP_USES_CVCAPTURE.
17 All classes that use OpenCV begin with CV.
18 See also the class docs for CVCapture, CVCalibration and CVTracked
19 for a good top down information.
20 */
21 #include <CVTypedefs.h>
22 #include <CVFeatureManager.h>
23 #include <CVRaulMurOrb.h>
24 #include <CVTracked.h>
25 
26 #define SL_SPLIT_DETECT_COMPUTE 0
27 #define SL_DO_FEATURE_BENCHMARKING 0
28 
29 // Settings for drawing things into current camera frame
30 #define SL_DRAW_REPROJECTION_POINTS 1
31 #define SL_DRAW_REPROJECTION_ERROR 1
32 #define SL_DRAW_PATCHES 1
33 
34 //#define SL_SAVE_DEBUG_OUTPUT
35 #ifdef SL_SAVE_DEBUG_OUTPUT
36 # if defined(SL_OS_LINUX) || defined(SL_OS_MACOS) || defined(SL_OS_MACIOS)
37 # define SL_DEBUG_OUTPUT_PATH "/tmp/cv_tracking/"
38 # elif defined(SL_OS_WINDOWS)
39 # define SL_DEBUG_OUTPUT_PATH "cv_tracking/"
40 # endif
41 #endif
42 
43 // Feature detection and extraction
44 const int nFeatures = 2000;
45 const float minRatio = 0.7f;
46 
47 // RANSAC parameters
48 const int iterations = 500;
49 const float reprojection_error = 2.0f;
50 const double confidence = 0.95;
51 
52 // Repose patch size
53 const int reposeFrequency = 10;
54 const int initialPatchSize = 2;
55 const int maxPatchSize = 60;
56 
57 //-----------------------------------------------------------------------------
58 //! CVTrackedFeatures is the main part of the AR Christoffelturm scene
59 /*! The implementation tries to find a valid pose based on feature points in
60 realtime. The feature matching algorithm checks the points of the current camera
61 frame with against a reference. There are two important parts of this procedure:
62 The relocalisation, which will be called if we have to find the pose with no hint
63 where the camera could be. The other one is called feature tracking: If a pose
64 was found, the implementation tries to track them and update the pose respectively.
65 */
67 {
68 public:
69  explicit CVTrackedFeatures(string markerFilename);
71  bool track(CVMat imageGray,
72  CVMat image,
73  CVCalibration* calib) final;
74  // Getters
75  bool forceRelocation() { return _forceRelocation; }
77 
78  // Setters
79  void forceRelocation(bool fR) { _forceRelocation = fR; }
80  void type(CVDetectDescribeType ddType);
81 
82 private:
83  void loadMarker(string markerFilename);
84  void initFeaturesOnMarker();
85  void relocate();
86  void tracking();
88  void transferFrameData();
91  bool calculatePose();
92  void optimizeMatches();
93  bool trackWithOptFlow(CVMat rvec, CVMat tvec);
94 
95  cv::Ptr<cv::DescriptorMatcher> _matcher; //!< Descriptor matching algorithm
96  CVCalibration* _calib; //!< Current calibration in use
97  int _frameCount; //!< NO. of frames since process start
98  bool _isTracking; //!< True if tracking
99 
100  //! Data of a 2D marker image
102  {
103  CVMat imageGray; //!< Grayscale image of the marker
104  CVMat imageDrawing; //!< Color debug image
105  CVVKeyPoint keypoints2D; //!< 2D keypoints in pixels
106  CVVPoint3f keypoints3D; //!< 3D feature points in mm
107  CVMat descriptors; //!< Descriptors of the 2D keypoints
108  };
109 
110  //! Feature date for a video frame
111  struct SLFrameData
112  {
113  CVMat image; //!< Reference to color video frame
114  CVMat imageGray; //!< Reference to grayscale video frame
115  CVVPoint2f inlierPoints2D; //!< Inlier 2D points after RANSAC
116  CVVPoint3f inlierPoints3D; //!< Inlier 3D points after RANSAC on the marker
117  CVVKeyPoint keypoints; //!< 2D keypoints detected in video frame
118  CVMat descriptors; //!< Descriptors of keypoints
119  CVVDMatch matches; //!< matches between video decriptors and marker descriptors
120  CVVDMatch inlierMatches; //!< matches that lead to correct transform
121  CVMat rvec; //!< Rotation of the camera pose
122  CVMat tvec; //!< Translation of the camera pose
123  bool foundPose; //!< True if pose was found
124  float reprojectionError; //!< Reprojection error of the pose
125  bool useExtrinsicGuess; //!< flag if extrinsic gues should be used
126  };
127 
128  SLFeatureMarker2D _marker; //!< 2D marker data
129  SLFrameData _currentFrame; //!< The current video frame data
130  SLFrameData _prevFrame; //!< The previous video frame data
131  bool _forceRelocation; //!< Force relocation every frame (no opt. flow tracking)
132  CVFeatureManager _featureManager; //!< Feature detector-descriptor wrapper instance
133 };
134 //-----------------------------------------------------------------------------
135 #endif // CVTrackedFeatures_H
CVDetectDescribeType
Feature detector-decriptor types.
Declares the Raul Mur ORB feature detector and descriptor.
const int reposeFrequency
const float reprojection_error
const int nFeatures
const int iterations
const int maxPatchSize
const int initialPatchSize
const double confidence
const float minRatio
vector< cv::Point3f > CVVPoint3f
Definition: CVTypedefs.h:79
cv::Mat CVMat
Definition: CVTypedefs.h:38
vector< cv::Point2f > CVVPoint2f
Definition: CVTypedefs.h:77
vector< cv::DMatch > CVVDMatch
Definition: CVTypedefs.h:89
vector< cv::KeyPoint > CVVKeyPoint
Definition: CVTypedefs.h:88
Live video camera calibration class with OpenCV an OpenCV calibration.
Definition: CVCalibration.h:71
Wrapper class around OpenCV feature detector & describer.
CVDetectDescribeType type()
CVTrackedFeatures is the main part of the AR Christoffelturm scene.
bool _isTracking
True if tracking.
void loadMarker(string markerFilename)
Loads the marker image form the filesystem.
CVVDMatch getFeatureMatches()
SLFrameData _prevFrame
The previous video frame data.
CVTrackedFeatures(string markerFilename)
CVFeatureManager _featureManager
Feature detector-descriptor wrapper instance.
~CVTrackedFeatures()
Show statistics if program terminates.
bool _forceRelocation
Force relocation every frame (no opt. flow tracking)
SLFeatureMarker2D _marker
2D marker data
cv::Ptr< cv::DescriptorMatcher > _matcher
Descriptor matching algorithm.
void forceRelocation(bool fR)
CVCalibration * _calib
Current calibration in use.
void drawDebugInformation(bool drawDetection)
CVDetectDescribeType type()
bool track(CVMat imageGray, CVMat image, CVCalibration *calib) final
int _frameCount
NO. of frames since process start.
bool trackWithOptFlow(CVMat rvec, CVMat tvec)
SLFrameData _currentFrame
The current video frame data.
CVTracked is the pure virtual base class for tracking features in video.
Definition: CVTracked.h:50
bool drawDetection()
Definition: CVTracked.h:64
CVMat imageGray
Grayscale image of the marker.
CVVPoint3f keypoints3D
3D feature points in mm
CVVKeyPoint keypoints2D
2D keypoints in pixels
CVMat descriptors
Descriptors of the 2D keypoints.
Feature date for a video frame.
CVVDMatch inlierMatches
matches that lead to correct transform
bool useExtrinsicGuess
flag if extrinsic gues should be used
bool foundPose
True if pose was found.
CVMat rvec
Rotation of the camera pose.
CVMat imageGray
Reference to grayscale video frame.
float reprojectionError
Reprojection error of the pose.
CVMat tvec
Translation of the camera pose.
CVVPoint2f inlierPoints2D
Inlier 2D points after RANSAC.
CVVDMatch matches
matches between video decriptors and marker descriptors
CVMat image
Reference to color video frame.
CVVKeyPoint keypoints
2D keypoints detected in video frame
CVVPoint3f inlierPoints3D
Inlier 3D points after RANSAC on the marker.
CVMat descriptors
Descriptors of keypoints.