SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVTracked Class Referenceabstract

CVTracked is the pure virtual base class for tracking features in video. More...

#include <CVTracked.h>

Inheritance diagram for CVTracked:
[legend]

Public Member Functions

 CVTracked ()
 
virtual ~CVTracked ()=default
 
virtual bool track (CVMat imageGray, CVMat imageBgr, CVCalibration *calib)=0
 
void drawDetection (bool draw)
 
bool isVisible ()
 
bool drawDetection ()
 
CVMatx44f objectViewMat ()
 

Static Public Member Functions

static cv::Matx44f createGLMatrix (const CVMat &tVec, const CVMat &rVec)
 Create an OpenGL 4x4 matrix from an OpenCV translation & rotation vector. More...
 
static void createRvecTvec (const CVMatx44f &glMat, CVMat &tVec, CVMat &rVec)
 Creates the OpenCV rvec & tvec vectors from an column major OpenGL 4x4 matrix. More...
 
static CVMatx44f calcObjectMatrix (const CVMatx44f &cameraObjectMat, const CVMatx44f &objectViewMat)
 
static CVVec3f averageVector (vector< CVVec3f > vectors, vector< float > weights)
 
static SLQuat4f averageQuaternion (vector< SLQuat4f > quaternions, vector< float > weights)
 
static void resetTimes ()
 Resets all static variables. More...
 

Static Public Attributes

static AvgFloat trackingTimesMS
 Averaged time for video tracking in ms. More...
 
static AvgFloat detectTimesMS
 Averaged time for video feature detection & description in ms. More...
 
static AvgFloat detect1TimesMS
 Averaged time for video feature detection subpart 1 in ms. More...
 
static AvgFloat detect2TimesMS
 Averaged time for video feature detection subpart 2 in ms. More...
 
static AvgFloat matchTimesMS
 Averaged time for video feature matching in ms. More...
 
static AvgFloat optFlowTimesMS
 Averaged time for video feature optical flow tracking in ms. More...
 
static AvgFloat poseTimesMS
 Averaged time for video feature pose estimation in ms. More...
 

Protected Attributes

bool _isVisible
 Flag if marker is visible. More...
 
bool _drawDetection
 Flag if detection should be drawn into image. More...
 
CVMatx44f _objectViewMat
 view transformation matrix More...
 
HighResTimer _timer
 High resolution timer. More...
 

Detailed Description

CVTracked is the pure virtual base class for tracking features in video.

The static vector trackers can hold multiple of CVTracked that are tracked in scenes that require a live video image from the device camera. A tracker is bound to a scene node. If the node is the camera node the tracker calculates the relative position of the camera to the tracker. This is the standard augmented reality case. If the camera is a normal scene node, the tracker calculates the object matrix relative to the scene camera. See also the derived classes CVTrackedAruco, CVTrackedChessboard, CVTrackedFaces and CVTrackedFeature for example implementations. The update of the tracking per frame is implemented in onUpdateTracking in AppDemoTracking.cpp and called once per frame within the main render loop.

Definition at line 49 of file CVTracked.h.

Constructor & Destructor Documentation

◆ CVTracked()

CVTracked::CVTracked ( )
inlineexplicit

Definition at line 52 of file CVTracked.h.

52 : _isVisible(false), _drawDetection(true) {}
bool _isVisible
Flag if marker is visible.
Definition: CVTracked.h:91
bool _drawDetection
Flag if detection should be drawn into image.
Definition: CVTracked.h:92

◆ ~CVTracked()

virtual CVTracked::~CVTracked ( )
virtualdefault

Member Function Documentation

◆ averageQuaternion()

SLQuat4f CVTracked::averageQuaternion ( vector< SLQuat4f quaternions,
vector< float >  weights 
)
static

Definition at line 153 of file CVTracked.cpp.

155 {
156  // Based on: https://math.stackexchange.com/questions/61146/averaging-quaternions
157 
158  if (quaternions.size() == 1)
159  return quaternions[0];
160 
161  SLQuat4f total(0.0f, 0.0f, 0.0f, 0.0f);
162 
163  for (int i = 0; i < quaternions.size(); i++)
164  {
165  SLQuat4f quaternion = quaternions[i];
166  float weight = weights[i];
167 
168  if (i > 0 && quaternion.dot(quaternions[0]) < 0.0)
169  weight = -weight;
170 
171  total.set(total.x() + weight * quaternion.x(),
172  total.y() + weight * quaternion.y(),
173  total.z() + weight * quaternion.z(),
174  total.w() + weight * quaternion.w());
175  }
176 
177  return total.normalized();
178 }
T w() const
Definition: SLQuat4.h:39
void set(T x, T y, T z, T w)
Definition: SLQuat4.h:140
T z() const
Definition: SLQuat4.h:38
T dot(const SLQuat4< T > &q) const
Definition: SLQuat4.h:536
T y() const
Definition: SLQuat4.h:37
T x() const
Definition: SLQuat4.h:36

◆ averageVector()

CVVec3f CVTracked::averageVector ( vector< CVVec3f vectors,
vector< float >  weights 
)
static

Definition at line 134 of file CVTracked.cpp.

136 {
137  if (vectors.size() == 1)
138  return vectors[0];
139 
140  CVVec3f total;
141  float totalWeights = 0.0f;
142 
143  for (int i = 0; i < vectors.size(); i++)
144  {
145  float weight = weights[i];
146  total += vectors[i] * weight;
147  totalWeights += weight;
148  }
149 
150  return total / totalWeights;
151 }
cv::Vec3f CVVec3f
Definition: CVTypedefs.h:52

◆ calcObjectMatrix()

CVMatx44f CVTracked::calcObjectMatrix ( const CVMatx44f cameraObjectMat,
const CVMatx44f objectViewMat 
)
static

Calculates the object matrix from the cameraObject and the object view matrix.
Nomenclature: T = homogenous transformation matrix
aTb = homogenous transformation matrix with subscript b and superscript a
Subscrips and superscripts: w = world; o = object; c = camera
cTo = Transformation of object with respect to camera coordinate system. It describes the position of an object in the camera coordinate system. We get this transformation from OpenCVs solvePNP function.
wTc = (cTw)-1 = Transformation of camera with respect to world coord.-system. Inversion exchanges sub- and superscript.
The inverse of the camera to world matrix is the view matrix or camera matrix.
We can combine two or more homogenous transformations to a new one if the inner sub- and superscript fit together. The resulting transformation inherits the superscript from the left and the subscript from the right transformation. The following transformation is what we want to do:
wTo = wTc * cTo = Transformation of object with respect to world coordinate system (object matrix)

Definition at line 125 of file CVTracked.cpp.

127 {
128  // new object matrix = camera object matrix * object-view matrix
129  return cameraObjectMat * objectViewMat;
130 }
CVMatx44f objectViewMat()
Definition: CVTracked.h:65

◆ createGLMatrix()

CVMatx44f CVTracked::createGLMatrix ( const CVMat tVec,
const CVMat rVec 
)
static

Create an OpenGL 4x4 matrix from an OpenCV translation & rotation vector.

Definition at line 46 of file CVTracked.cpp.

47 {
48  // 1) convert the passed rotation vector to a rotation matrix
49  CVMat rMat;
50  Rodrigues(rVec, rMat);
51 
52  // 2) Create an OpenGL 4x4 column major matrix from the rotation matrix and
53  // translation vector from openCV as described in this post:
54  // www.morethantechnical.com/2015/02/17/
55  // augmented-reality-on-libqglviewer-and-opencv-opengl-tips-wcode
56 
57  // The y- and z- axis have to be inverted:
58  /*
59  tVec = | t0, t1, t2 |
60  | r00 r01 r02 t0 |
61  | r00, r10, r20 | | -r10 -r11 -r12 -t1 |
62  rMat = | r01, r11, r21 | glMat = | -r20 -r21 -r22 -t2 |
63  | r02, r12, r22 | | 0 0 0 1 |
64  */
65 
66  CVMatx44f glM((float) rMat.at<double>(0, 0), (float) rMat.at<double>(0, 1), (float) rMat.at<double>(0, 2), (float) tVec.at<double>(0, 0),
67  (float)-rMat.at<double>(1, 0), (float)-rMat.at<double>(1, 1), (float)-rMat.at<double>(1, 2), (float)-tVec.at<double>(1, 0),
68  (float)-rMat.at<double>(2, 0), (float)-rMat.at<double>(2, 1), (float)-rMat.at<double>(2, 2), (float)-tVec.at<double>(2, 0),
69  0.0f, 0.0f, 0.0f, 1.0f);
70  return glM;
71 }
cv::Matx44f CVMatx44f
Definition: CVTypedefs.h:59
cv::Mat CVMat
Definition: CVTypedefs.h:38

◆ createRvecTvec()

void CVTracked::createRvecTvec ( const CVMatx44f glMat,
CVMat tVec,
CVMat rVec 
)
static

Creates the OpenCV rvec & tvec vectors from an column major OpenGL 4x4 matrix.

Definition at line 74 of file CVTracked.cpp.

75 {
76  // The y- and z- axis have to be inverted:
77  /*
78  tVec = | t0, t1, t2 |
79  | r00 r01 r02 t0 |
80  | r00, r10, r20 | | -r10 -r11 -r12 -t1 |
81  rMat = | r01, r11, r21 | glMat = | -r20 -r21 -r22 -t2 |
82  | r02, r12, r22 | | 0 0 0 1 |
83  */
84 
85  // 1) Create cv rotation matrix from OpenGL rotation matrix
86  CVMatx33f rMat(glMat.val[0], -glMat.val[1], -glMat.val[2],
87  glMat.val[4], -glMat.val[5], -glMat.val[6],
88  glMat.val[8], -glMat.val[9], -glMat.val[10]);
89 
90  // 2) Convert rotation matrix to Rodrigues rotation vector
91  Rodrigues(rMat, rVec);
92 
93  // 3) Create tvec vector from translation components
94  tVec.at<double>(0, 0) = glMat.val[3];
95  tVec.at<double>(1, 0) = -glMat.val[7];
96  tVec.at<double>(2, 0) = -glMat.val[11];
97 }
cv::Matx33f CVMatx33f
Definition: CVTypedefs.h:58

◆ drawDetection() [1/2]

bool CVTracked::drawDetection ( )
inline

Definition at line 64 of file CVTracked.h.

64 { return _drawDetection; }

◆ drawDetection() [2/2]

void CVTracked::drawDetection ( bool  draw)
inline

Definition at line 60 of file CVTracked.h.

60 { _drawDetection = draw; }

◆ isVisible()

bool CVTracked::isVisible ( )
inline

Definition at line 63 of file CVTracked.h.

63 { return _isVisible; }

◆ objectViewMat()

CVMatx44f CVTracked::objectViewMat ( )
inline

Definition at line 65 of file CVTracked.h.

65 { return _objectViewMat; }
CVMatx44f _objectViewMat
view transformation matrix
Definition: CVTracked.h:93

◆ resetTimes()

void CVTracked::resetTimes ( )
static

Resets all static variables.

Definition at line 31 of file CVTracked.cpp.

32 {
33  // Reset all timing variables
38  CVTracked::matchTimesMS.init(60, 0.0f);
40  CVTracked::poseTimesMS.init(60, 0.0f);
41 }
static AvgFloat trackingTimesMS
Averaged time for video tracking in ms.
Definition: CVTracked.h:82
static AvgFloat optFlowTimesMS
Averaged time for video feature optical flow tracking in ms.
Definition: CVTracked.h:87
static AvgFloat detectTimesMS
Averaged time for video feature detection & description in ms.
Definition: CVTracked.h:83
static AvgFloat detect1TimesMS
Averaged time for video feature detection subpart 1 in ms.
Definition: CVTracked.h:84
static AvgFloat detect2TimesMS
Averaged time for video feature detection subpart 2 in ms.
Definition: CVTracked.h:85
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 init(int numValues, T initValue)
Initializes the average value array to a given value.
Definition: Averaged.h:41

◆ track()

virtual bool CVTracked::track ( CVMat  imageGray,
CVMat  imageBgr,
CVCalibration calib 
)
pure virtual

Member Data Documentation

◆ _drawDetection

bool CVTracked::_drawDetection
protected

Flag if detection should be drawn into image.

Definition at line 92 of file CVTracked.h.

◆ _isVisible

bool CVTracked::_isVisible
protected

Flag if marker is visible.

Definition at line 91 of file CVTracked.h.

◆ _objectViewMat

CVMatx44f CVTracked::_objectViewMat
protected

view transformation matrix

Definition at line 93 of file CVTracked.h.

◆ _timer

HighResTimer CVTracked::_timer
protected

High resolution timer.

Definition at line 94 of file CVTracked.h.

◆ detect1TimesMS

AvgFloat CVTracked::detect1TimesMS
static

Averaged time for video feature detection subpart 1 in ms.

Definition at line 84 of file CVTracked.h.

◆ detect2TimesMS

AvgFloat CVTracked::detect2TimesMS
static

Averaged time for video feature detection subpart 2 in ms.

Definition at line 85 of file CVTracked.h.

◆ detectTimesMS

AvgFloat CVTracked::detectTimesMS
static

Averaged time for video feature detection & description in ms.

Definition at line 83 of file CVTracked.h.

◆ matchTimesMS

AvgFloat CVTracked::matchTimesMS
static

Averaged time for video feature matching in ms.

Definition at line 86 of file CVTracked.h.

◆ optFlowTimesMS

AvgFloat CVTracked::optFlowTimesMS
static

Averaged time for video feature optical flow tracking in ms.

Definition at line 87 of file CVTracked.h.

◆ poseTimesMS

AvgFloat CVTracked::poseTimesMS
static

Averaged time for video feature pose estimation in ms.

Definition at line 88 of file CVTracked.h.

◆ trackingTimesMS

AvgFloat CVTracked::trackingTimesMS
static

Averaged time for video tracking in ms.

Definition at line 82 of file CVTracked.h.


The documentation for this class was generated from the following files: