SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVTracked.h
Go to the documentation of this file.
1 /**
2  * \file CVTracked.h
3  * \date Winter 2016
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 Marcus Hudritsch, Michael Goettlicher, Marino von Wattenwyl
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #ifndef CVTRACKER_H
11 #define CVTRACKER_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 
22 #include <Averaged.h>
23 #include <HighResTimer.h>
24 #include <CVTypedefs.h>
25 #include <CVCalibration.h>
26 
27 #ifndef __EMSCRIPTEN__
28 # include <opencv2/aruco.hpp>
29 # include <opencv2/xfeatures2d.hpp>
30 #endif
31 
32 #include <SLQuat4.h>
33 
34 using Utils::AvgFloat;
35 
36 //-----------------------------------------------------------------------------
37 //! CVTracked is the pure virtual base class for tracking features in video.
38 /*! The static vector trackers can hold multiple of CVTracked that are
39  tracked in scenes that require a live video image from the device camera.
40  A tracker is bound to a scene node. If the node is the camera node the tracker
41  calculates the relative position of the camera to the tracker. This is the
42  standard augmented reality case. If the camera is a normal scene node, the
43  tracker calculates the object matrix relative to the scene camera.
44  See also the derived classes CVTrackedAruco, CVTrackedChessboard,
45  CVTrackedFaces and CVTrackedFeature for example implementations.
46  The update of the tracking per frame is implemented in onUpdateTracking in
47  AppDemoTracking.cpp and called once per frame within the main render loop.
48 */
49 class CVTracked
50 {
51 public:
52  explicit CVTracked() : _isVisible(false), _drawDetection(true) {}
53  virtual ~CVTracked() = default;
54 
55  virtual bool track(CVMat imageGray,
56  CVMat imageBgr,
57  CVCalibration* calib) = 0;
58 
59  // Setters
60  void drawDetection(bool draw) { _drawDetection = draw; }
61 
62  // Getters
63  bool isVisible() { return _isVisible; }
64  bool drawDetection() { return _drawDetection; }
66 
67  // Static functions for commonly performed operations
68  static cv::Matx44f createGLMatrix(const CVMat& tVec,
69  const CVMat& rVec);
70  static void createRvecTvec(const CVMatx44f& glMat,
71  CVMat& tVec,
72  CVMat& rVec);
73  static CVMatx44f calcObjectMatrix(const CVMatx44f& cameraObjectMat,
74  const CVMatx44f& objectViewMat);
75  static CVVec3f averageVector(vector<CVVec3f> vectors,
76  vector<float> weights);
77  static SLQuat4f averageQuaternion(vector<SLQuat4f> quaternions,
78  vector<float> weights);
79 
80  // Statics: These statics are used directly in application code (e.g. in )
81  static void resetTimes(); //!< Resets all static variables
82  static AvgFloat trackingTimesMS; //!< Averaged time for video tracking in ms
83  static AvgFloat detectTimesMS; //!< Averaged time for video feature detection & description in ms
84  static AvgFloat detect1TimesMS; //!< Averaged time for video feature detection subpart 1 in ms
85  static AvgFloat detect2TimesMS; //!< Averaged time for video feature detection subpart 2 in ms
86  static AvgFloat matchTimesMS; //!< Averaged time for video feature matching in ms
87  static AvgFloat optFlowTimesMS; //!< Averaged time for video feature optical flow tracking in ms
88  static AvgFloat poseTimesMS; //!< Averaged time for video feature pose estimation in ms
89 
90 protected:
91  bool _isVisible; //!< Flag if marker is visible
92  bool _drawDetection; //!< Flag if detection should be drawn into image
93  CVMatx44f _objectViewMat; //!< view transformation matrix
94  HighResTimer _timer; //!< High resolution timer
95 };
96 //-----------------------------------------------------------------------------
97 #endif
cv::Matx44f CVMatx44f
Definition: CVTypedefs.h:59
cv::Vec3f CVVec3f
Definition: CVTypedefs.h:52
cv::Mat CVMat
Definition: CVTypedefs.h:38
Live video camera calibration class with OpenCV an OpenCV calibration.
Definition: CVCalibration.h:71
CVTracked is the pure virtual base class for tracking features in video.
Definition: CVTracked.h:50
static AvgFloat trackingTimesMS
Averaged time for video tracking in ms.
Definition: CVTracked.h:82
bool _isVisible
Flag if marker is visible.
Definition: CVTracked.h:91
static AvgFloat optFlowTimesMS
Averaged time for video feature optical flow tracking in ms.
Definition: CVTracked.h:87
CVMatx44f _objectViewMat
view transformation matrix
Definition: CVTracked.h:93
static void createRvecTvec(const CVMatx44f &glMat, CVMat &tVec, CVMat &rVec)
Creates the OpenCV rvec & tvec vectors from an column major OpenGL 4x4 matrix.
Definition: CVTracked.cpp:74
CVTracked()
Definition: CVTracked.h:52
static AvgFloat detectTimesMS
Averaged time for video feature detection & description in ms.
Definition: CVTracked.h:83
virtual bool track(CVMat imageGray, CVMat imageBgr, CVCalibration *calib)=0
bool isVisible()
Definition: CVTracked.h:63
static CVMatx44f calcObjectMatrix(const CVMatx44f &cameraObjectMat, const CVMatx44f &objectViewMat)
Definition: CVTracked.cpp:125
bool _drawDetection
Flag if detection should be drawn into image.
Definition: CVTracked.h:92
static cv::Matx44f createGLMatrix(const CVMat &tVec, const CVMat &rVec)
Create an OpenGL 4x4 matrix from an OpenCV translation & rotation vector.
Definition: CVTracked.cpp:46
static SLQuat4f averageQuaternion(vector< SLQuat4f > quaternions, vector< float > weights)
Definition: CVTracked.cpp:153
static AvgFloat detect1TimesMS
Averaged time for video feature detection subpart 1 in ms.
Definition: CVTracked.h:84
virtual ~CVTracked()=default
static void resetTimes()
Resets all static variables.
Definition: CVTracked.cpp:31
HighResTimer _timer
High resolution timer.
Definition: CVTracked.h:94
static AvgFloat detect2TimesMS
Averaged time for video feature detection subpart 2 in ms.
Definition: CVTracked.h:85
bool drawDetection()
Definition: CVTracked.h:64
static AvgFloat matchTimesMS
Averaged time for video feature matching in ms.
Definition: CVTracked.h:86
static AvgFloat poseTimesMS
Averaged time for video feature pose estimation in ms.
Definition: CVTracked.h:88
void drawDetection(bool draw)
Definition: CVTracked.h:60
CVMatx44f objectViewMat()
Definition: CVTracked.h:65
static CVVec3f averageVector(vector< CVVec3f > vectors, vector< float > weights)
Definition: CVTracked.cpp:134
High Resolution Timer class using C++11.
Definition: HighResTimer.h:31
Utils::Averaged< float > AvgFloat
Definition: Averaged.h:85