OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
DK2ImageSource.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 "ImageSourceFactories.h"
27 #include "Oculus_DK2.h"
28 
29 // Library/third-party includes
30 #include <opencv2/imgproc/imgproc.hpp>
31 
32 // Standard includes
33 // - none
34 
35 namespace osvr {
36 namespace vbtracker {
38  public:
39  DK2WrappedImageSource(ImageSourcePtr &&cam, bool doHid)
40  : m_camera(std::move(cam)) {
41  if (doHid) {
42  m_hid.reset(new oculus_dk2::Oculus_DK2_HID{});
43  }
44  }
45  virtual ~DK2WrappedImageSource() {}
46 
47  bool ok() const override { return m_camera && m_camera->ok(); }
48  bool grab() override;
49  void retrieve(cv::Mat &color, cv::Mat &gray,
50  osvr::util::time::TimeValue &timestamp) override;
51  cv::Size resolution() const override;
52  void retrieveColor(cv::Mat &color,
53  osvr::util::time::TimeValue &timestamp) override;
54 
55  private:
56  ImageSourcePtr m_camera;
57  cv::Mat m_scratch;
58  std::unique_ptr<oculus_dk2::Oculus_DK2_HID> m_hid;
59  };
60 
61  ImageSourcePtr openDK2WrappedCamera(ImageSourcePtr &&cam, bool doHid) {
62  auto ret = ImageSourcePtr{};
63  if (!cam->ok()) {
64  // ditch failed cams right away
65  return ret;
66  }
67  ret.reset(new DK2WrappedImageSource(std::move(cam), doHid));
68  return ret;
69  }
70 
72  if (m_hid) {
73  m_hid->poll();
74  }
75  return m_camera->grab();
76  }
77 
78  void
79  DK2WrappedImageSource::retrieve(cv::Mat &color, cv::Mat &gray,
80  osvr::util::time::TimeValue &timestamp) {
81 
82  m_camera->retrieveColor(m_scratch, timestamp);
83 
84  gray = osvr::oculus_dk2::unscramble_image(m_scratch);
85  cv::cvtColor(gray, color, CV_GRAY2RGB);
86  }
87 
89  cv::Mat &color, osvr::util::time::TimeValue &timestamp) {
90  // Here we implement retrieveColor by implementing retrieve and just
91  // tossing the gray result...
92  cv::Mat dummy;
93  retrieve(color, dummy, timestamp);
94  }
95 
97  return m_camera->resolution();
98  }
99 
100 } // namespace vbtracker
101 } // namespace osvr
void retrieve(cv::Mat &color, cv::Mat &gray, osvr::util::time::TimeValue &timestamp) override
Call after grab() to get the actual image data.
cv::Size resolution() const override
Get resolution of the images from this source.
Header file describing interface for an Oculus DK2 device.
void retrieveColor(cv::Mat &color, osvr::util::time::TimeValue &timestamp) override
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81