SLProject  4.2.000
A platform independent 3D computer graphics framework for desktop OS, Android, iOS and online in web browsers
CVTrackedWAI.cpp
Go to the documentation of this file.
1 /**
2  * \file CVTrackedWAI.cpp
3  * \date Spring 2020
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 Michael Goettlicher, Jan Dellsperger
7  * \copyright http://opensource.org/licenses/GPL-3.0
8 */
9 
10 #include <CVTrackedWAI.h>
11 #include <SL.h>
12 #include <Profiler.h>
13 
14 //-----------------------------------------------------------------------------
15 CVTrackedWAI::CVTrackedWAI(const string& vocabularyFile)
16 {
17  _voc = new WAIOrbVocabulary();
18  float startMS = _timer.elapsedTimeInMilliSec();
19 
20  try
21  {
22  _voc->loadFromFile(vocabularyFile);
23  }
24  catch (std::exception& e)
25  {
26  Utils::log("SLProject",
27  "Could not open the ORB vocabulary file: %s",
28  e.what());
29  exit(0);
30  }
31  SL_LOG_DEBUG("Loaded voc file : %f ms", _timer.elapsedTimeInMilliSec() - startMS);
32 }
33 //-----------------------------------------------------------------------------
35 {
36  delete _trackingExtractor;
37  delete _waiSlamer;
38 }
39 //-----------------------------------------------------------------------------
40 /*! This function is called every frame in the apps onUpdateVideo function.
41  It uses the on-the-fly built 3D point cloud generated by the ORB-SLAM2 library
42  that is integrated within the lib-WAI. It only works well if the camera is
43  calibrated.
44  @param imageGray Image for processing
45  @param imageBgr Image for visualizations
46  @param calib Pointer to a valid camera calibration
47  @return returns true if pose estimation was successful
48  */
49 bool CVTrackedWAI::track(CVMat imageGray,
50  CVMat imageBgr,
51  CVCalibration* calib)
52 {
54 
55  bool result = false;
56 
57  float startMS = _timer.elapsedTimeInMilliSec();
58 
59  if (!_waiSlamer)
60  {
61  if (_voc == nullptr)
62  return false;
63 
64  int nf = 1000; // NO. of features
65  float fScaleFactor = 1.2f; // Scale factor for pyramid construction
66  int nLevels = 8; // NO. of pyramid levels
67  int fIniThFAST = 20; // Init threshold for FAST corner detector
68  int fMinThFAST = 7; // Min. threshold for FAST corner detector
69  _trackingExtractor = new ORB_SLAM2::ORBextractor(nf,
70  fScaleFactor,
71  nLevels,
72  fIniThFAST,
73  fMinThFAST);
74 
75  _initializationExtractor = new ORB_SLAM2::ORBextractor(2 * nf,
76  fScaleFactor,
77  nLevels,
78  fIniThFAST,
79  fMinThFAST);
80 
81  WAISlam::Params params;
82  params.cullRedundantPerc = 0.95f;
83  params.ensureKFIntegration = false;
84  params.fixOldKfs = false;
85  params.onlyTracking = false;
86  params.retainImg = false;
87  params.serial = false;
88  params.trackOptFlow = false;
89 
90  _waiSlamer = new WAISlam(calib->cameraMat(),
91  calib->distortion(),
92  _voc,
96  nullptr, // global map
97  params);
98  }
99 
100  if (_waiSlamer->update(imageGray))
101  {
102  cv::Mat pose = _waiSlamer->getPose();
103 
104  // ORB-SLAM uses a right handed Y-down coordinate system
105  // SLProject uses a right handed Y-Up coordinate system
106  cv::Mat rot = cv::Mat::eye(4, 4, CV_32F);
107  rot.at<float>(1, 1) = -1.0f; // mirror y-axis
108  rot.at<float>(2, 2) = -1.0f; // mirror z-axis
109 
110  pose = rot * pose;
111 
112  pose.copyTo(_objectViewMat);
113 
114  result = true;
115  }
116 
117  if (_drawDetection)
118  {
119  _waiSlamer->drawInfo(imageBgr, 1.0f, true, true, true);
120  }
121 
122  // TODO(dgj1): at the moment we cant differentiate between these two
123  // as they are both done in the same call to WAI
126 
127  return result;
128 }
129 //-----------------------------------------------------------------------------
cv::Mat CVMat
Definition: CVTypedefs.h:38
#define PROFILE_FUNCTION()
Definition: Instrumentor.h:41
#define SL_LOG_DEBUG(...)
Definition: SL.h:237
Live video camera calibration class with OpenCV an OpenCV calibration.
Definition: CVCalibration.h:71
const CVMat & cameraMat() const
const CVMat & distortion() const
CVMatx44f _objectViewMat
view transformation matrix
Definition: CVTracked.h:93
static AvgFloat detectTimesMS
Averaged time for video feature detection & description in ms.
Definition: CVTracked.h:83
bool _drawDetection
Flag if detection should be drawn into image.
Definition: CVTracked.h:92
HighResTimer _timer
High resolution timer.
Definition: CVTracked.h:94
static AvgFloat poseTimesMS
Averaged time for video feature pose estimation in ms.
Definition: CVTracked.h:88
WAIOrbVocabulary * _voc
Definition: CVTrackedWAI.h:39
ORB_SLAM2::ORBextractor * _initializationExtractor
Definition: CVTrackedWAI.h:38
ORB_SLAM2::ORBextractor * _trackingExtractor
Definition: CVTrackedWAI.h:37
CVTrackedWAI(const string &vocabularyFile)
~CVTrackedWAI() override
bool track(CVMat imageGray, CVMat imageBgr, CVCalibration *calib) final
WAISlam * _waiSlamer
Definition: CVTrackedWAI.h:36
float elapsedTimeInMilliSec()
Definition: HighResTimer.h:38
void set(T value)
Sets the current value in the value array and builds the average.
Definition: Averaged.h:53
void loadFromFile(std::string strVocFile)
virtual bool update(cv::Mat &imageGray)
Definition: WAISlam.cpp:471
virtual cv::Mat getPose()
Definition: WAISlam.cpp:551
virtual void drawInfo(cv::Mat &imageBGR, float scale, bool showInitLine, bool showKeyPoints, bool showKeyPointsMatched)
Definition: WAISlam.cpp:488
void log(const char *tag, const char *format,...)
logs a formatted string platform independently
Definition: Utils.cpp:1103
bool ensureKFIntegration
Definition: WAISlam.h:32
bool fixOldKfs
Definition: WAISlam.h:41
bool trackOptFlow
Definition: WAISlam.h:43
bool retainImg
Definition: WAISlam.h:36
float cullRedundantPerc
Definition: WAISlam.h:48
bool onlyTracking
Definition: WAISlam.h:38