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 {
37  class DK2WrappedImageSource : public ImageSource {
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) override;
50  cv::Size resolution() const override;
51  void retrieveColor(cv::Mat &color) override;
52 
53  private:
54  ImageSourcePtr m_camera;
55  cv::Mat m_scratch;
56  std::unique_ptr<oculus_dk2::Oculus_DK2_HID> m_hid;
57  };
58 
59  ImageSourcePtr openDK2WrappedCamera(ImageSourcePtr &&cam, bool doHid) {
60  auto ret = ImageSourcePtr{};
61  if (!cam->ok()) {
62  // ditch failed cams right away
63  return ret;
64  }
65  ret.reset(new DK2WrappedImageSource(std::move(cam), doHid));
66  return ret;
67  }
68 
70  if (m_hid) {
71  m_hid->poll();
72  }
73  return m_camera->grab();
74  }
75 
76  void DK2WrappedImageSource::retrieve(cv::Mat &color, cv::Mat &gray) {
77 
78  m_camera->retrieveColor(m_scratch);
79 
80  gray = osvr::oculus_dk2::unscramble_image(m_scratch);
81  cv::cvtColor(gray, color, CV_GRAY2RGB);
82  }
83 
84  void DK2WrappedImageSource::retrieveColor(cv::Mat &color) {
85  cv::Mat dummy;
86  retrieve(color, dummy);
87  }
88 
89  cv::Size DK2WrappedImageSource::resolution() const {
90  return m_camera->resolution();
91  }
92 
93 } // namespace vbtracker
94 } // 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.
STL namespace.
cv::Size resolution() const override
Get resolution of the images from this source.
void retrieveColor(cv::Mat &color, osvr::util::time::TimeValue &timestamp) override
Header file describing interface for an Oculus DK2 device.