OSVR-Core
VideoBasedTracker.h
Go to the documentation of this file.
1 
11 // Copyright 2015 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_VideoBasedTracker_h_GUID_831CC0DD_16A5_43AB_12D3_AE86E1998ED4
26 #define INCLUDED_VideoBasedTracker_h_GUID_831CC0DD_16A5_43AB_12D3_AE86E1998ED4
27 
28 // Internal Includes
29 #include "Types.h"
30 #include "LED.h"
31 #include "LedIdentifier.h"
33 #include "CameraParameters.h"
34 #include "SBDBlobExtractor.h"
36 
37 // Library/third-party includes
38 #include <opencv2/core/core.hpp>
39 #include <boost/assert.hpp>
40 
41 // Standard includes
42 #include <vector>
43 #include <list>
44 #include <functional>
45 #include <algorithm>
46 
47 // Define the constant below to provide debugging (window showing video and
48 // behavior, printing tracked positions)
49 //#define VBHMD_DEBUG
50 
51 namespace osvr {
52 namespace vbtracker {
54  public:
55  VideoBasedTracker(ConfigParams const &params = ConfigParams{});
56 
57  static BeaconIDPredicate getDefaultBeaconFixedPredicate() {
58  return [](int id) { return id <= 4; };
59  }
60 
81  void addSensor(LedIdentifierPtr &&identifier,
82  CameraParameters const &camParams,
83  Point3Vector const &locations,
84  Vec3Vector const &emissionDirection, double variance,
85  BeaconIDPredicate const &autocalibrationFixedPredicate =
86  getDefaultBeaconFixedPredicate(),
87  size_t requiredInliers = 4,
88  size_t permittedOutliers = 2) {
89  addSensor(std::move(identifier), camParams, locations,
90  emissionDirection, std::vector<double>{variance},
91  autocalibrationFixedPredicate, requiredInliers,
92  permittedOutliers);
93  }
94 
99  void addSensor(LedIdentifierPtr &&identifier,
100  CameraParameters const &camParams,
101  Point3Vector const &locations,
102  Vec3Vector const &emissionDirection,
103  BeaconIDPredicate const &autocalibrationFixedPredicate,
104  size_t requiredInliers = 4,
105  size_t permittedOutliers = 2) {
106  addSensor(std::move(identifier), camParams, locations,
107  emissionDirection, std::vector<double>{},
108  autocalibrationFixedPredicate, requiredInliers,
109  permittedOutliers);
110  }
118  void addSensor(
119  LedIdentifierPtr &&identifier, CameraParameters const &camParams,
120  Point3Vector const &locations, Vec3Vector const &emissionDirection,
121  std::vector<double> const &variance = std::vector<double>{},
122  BeaconIDPredicate const &autocalibrationFixedPredicate =
123  getDefaultBeaconFixedPredicate(),
124  size_t requiredInliers = 4, size_t permittedOutliers = 2,
125  double beaconAutocalibErrorScale = 1);
127 
128  typedef std::function<void(OSVR_ChannelCount, OSVR_Pose3 const &)>
129  PoseHandler;
130 
134  bool processImage(cv::Mat frame, cv::Mat grayImage,
135  OSVR_TimeValue const &tv, PoseHandler handler);
136 
139  return *(m_estimators.front());
140  }
141 
144  return *(m_estimators.front());
145  }
146 
147  private:
151  void addSensor(
152  LedIdentifierPtr &&identifier, CameraParameters const &camParams,
153  std::function<void(BeaconBasedPoseEstimator &)> const &beaconAdder,
154  size_t requiredInliers = 4, size_t permittedOutliers = 2);
155 
156  void dumpKeypointDebugData(std::vector<cv::KeyPoint> const &keypoints);
157 
158  void drawLedCircleOnStatusImage(Led const &led, bool filled,
159  cv::Vec3b color);
160  void drawRecognizedLedIdOnStatusImage(Led const &led);
161 
162  bool m_debugHelpDisplayed = false;
165  cv::Mat m_frame;
166  cv::Mat m_imageGray;
167  cv::Mat m_thresholdImage;
168  cv::Mat m_imageWithBlobs;
169  cv::Mat m_statusImage;
170  cv::Mat *m_shownImage = &m_statusImage;
171  int m_debugFrame = 0;
173 
174  ConfigParams m_params;
175  SBDBlobExtractor m_blobExtractor;
176  cv::SimpleBlobDetector::Params m_sbdParams;
177 
180  void m_assertInvariants() const {
181  BOOST_ASSERT_MSG(
182  m_identifiers.size() == m_led_groups.size(),
183  "Expected to have as many identifier objects as LED groups");
184  BOOST_ASSERT_MSG(m_identifiers.size() == m_estimators.size(),
185  "Expected to have as many identifier objects as "
186  "estimator objects");
187 
188  for (auto &e : m_estimators) {
189  BOOST_ASSERT_MSG(
190  e->getNumBeacons() > 4,
191  "Expected each estimator to have at least 4 beacons");
192  }
193  }
196  LedIdentifierList m_identifiers;
197  LedGroupList m_led_groups;
198  EstimatorList m_estimators;
200 
202  OSVR_PoseState m_pose;
203 
205  CameraParameters m_camParams;
206  };
207 
208 } // namespace vbtracker
209 } // namespace osvr
210 
211 #endif // INCLUDED_VideoBasedTracker_h_GUID_831CC0DD_16A5_43AB_12D3_AE86E1998ED4
Helper class to keep track of the state of a blob over time.
Definition: LED.h:47
BeaconBasedPoseEstimator const & getFirstEstimator() const
For debug purposes.
Definition: VideoBasedTracker.h:138
Header file for class that identifies LEDs based on blink codes.
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: CameraParameters.h:41
Header file for class that tracks and identifies LEDs.
Header.
void addSensor(LedIdentifierPtr &&identifier, CameraParameters const &camParams, Point3Vector const &locations, Vec3Vector const &emissionDirection, BeaconIDPredicate const &autocalibrationFixedPredicate, size_t requiredInliers=4, size_t permittedOutliers=2)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: VideoBasedTracker.h:99
Definition: VideoBasedTracker.h:53
BeaconBasedPoseEstimator & getFirstEstimator()
For debug purposes.
Definition: VideoBasedTracker.h:143
Header file for class that tracks and identifies LEDs.
std::function< bool(int)> BeaconIDPredicate
Takes in a 1-based index, returns true or false (true if the beacon should be considered fixed - not ...
Definition: Types.h:81
General configuration parameters.
Definition: ConfigParams.h:82
Class to track an object that has identified LED beacons on it as seen in a camera, where the absolute location of the LEDs with respect to a common frame of reference is known.
Definition: BeaconBasedPoseEstimator.h:63
bool processImage(cv::Mat frame, cv::Mat grayImage, OSVR_TimeValue const &tv, PoseHandler handler)
The main method that processes an image into tracked poses.
Definition: VideoBasedTracker.cpp:132
A class performing blob-extraction duties on incoming frames.
Definition: SBDBlobExtractor.h:45
A structure defining a 3D (6DOF) rigid body pose: translation and rotation.
Definition: Pose3C.h:54
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81