OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
TestStandalone.cpp
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 // Internal Includes
26 #include "VideoBasedTracker.h"
28 #include "CameraParameters.h"
29 #include "HDKData.h"
30 
31 // Library/third-party includes
32 #include <opencv2/core/core.hpp>
33 #include <opencv2/highgui/highgui.hpp>
34 #include <opencv2/imgproc/imgproc.hpp>
35 
36 // Standard includes
37 #include <iostream>
38 
40 
41 class Main {
42  public:
43  Main() : m_camera(0) {
44  int height = 0;
45  int width = 0;
46  if (!m_camera.isOpened()) {
47  std::cerr << "Couldn't open camera" << std::endl;
48  return;
49  }
50 
51  height = static_cast<int>(m_camera.get(CV_CAP_PROP_FRAME_HEIGHT));
52  width = static_cast<int>(m_camera.get(CV_CAP_PROP_FRAME_WIDTH));
53 
54  // See if this is an Oculus camera by checking the dimensions of
55  // the image. This camera type improperly describes its format
56  // as being a color format when it is in fact a mono format.
57  bool isOculusCamera = (width == 376) && (height == 480);
58  if (isOculusCamera) {
59  std::cerr << "This standalone app doesn't have Oculus support."
60  << std::endl;
61  return;
62  }
63 
64  std::cout << "Got image of size " << width << "x" << height
65  << ", Format " << m_camera.get(CV_CAP_PROP_FORMAT)
66  << ", Mode " << m_camera.get(CV_CAP_PROP_MODE) << std::endl;
67 
70  double cx = width / 2.0;
71  double cy = height / 2.0;
72  double fx = 700.0; // XXX This needs to be in pixels, not mm
73  double fy = fx;
74  CameraParameters camParams(fx, fy, cv::Size(width, height));
75  m_vbtracker.addSensor(osvr::vbtracker::createHDKLedIdentifier(0),
76  camParams,
77  osvr::vbtracker::OsvrHdkLedLocations_SENSOR0,
78  osvr::vbtracker::OsvrHdkLedDirections_SENSOR0);
79  m_vbtracker.addSensor(osvr::vbtracker::createHDKLedIdentifier(1),
80  camParams,
81  osvr::vbtracker::OsvrHdkLedLocations_SENSOR1,
82  osvr::vbtracker::OsvrHdkLedDirections_SENSOR1);
83  m_valid = true;
84  }
85 
87  bool update() {
88  if (!m_valid || !m_camera.isOpened()) {
89  return true;
90  }
91  if (!m_camera.grab()) {
92  // No frame available.
93  return false;
94  }
95 
96  if (!m_camera.retrieve(m_frame, m_channel)) {
97  m_valid = false;
98  return true;
99  }
100 
101  auto tv = osvr::util::time::getNow();
102  //==================================================================
103  // Convert the image into a format we can use.
104  // TODO: Consider reading in the image in gray scale to begin with
105  cv::cvtColor(m_frame, m_imageGray, CV_RGB2GRAY);
106 
107  return m_vbtracker.processImage(
108  m_frame, m_imageGray, tv,
109  [&](OSVR_ChannelCount sensor, OSVR_Pose3 const &pose) {
110  std::cout << "Sensor " << sensor << ": Translation "
111  << pose.translation << " rotation " << pose.rotation
112  << std::endl;
113  });
114  }
115 
116  private:
117  bool m_valid = false;
118  int m_channel = 0;
119  cv::VideoCapture m_camera;
120 
122  cv::Mat m_frame;
123  cv::Mat m_imageGray;
124 };
125 int main() {
126  Main mainObj;
127  bool done = false;
128  while (!done) {
129  done = mainObj.update();
130  }
131  return 0;
132 }
uint32_t OSVR_ChannelCount
The integer type specifying a number of channels/sensors or a channel/sensor index.
Definition: ChannelCountC.h:51
void getNow(TimeValue &tv)
Set the given TimeValue to the current time.
Definition: TimeValue.h:51
OSVR_Quaternion rotation
Orientation as a unit quaternion.
Definition: Pose3C.h:58
Header.
bool update()
bool processImage(cv::Mat frame, cv::Mat grayImage, OSVR_TimeValue const &tv, PoseHandler handler)
The main method that processes an image into tracked poses.
A structure defining a 3D (6DOF) rigid body pose: translation and rotation.
Definition: Pose3C.h:54
OSVR_Vec3 translation
Position vector.
Definition: Pose3C.h:56