OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
CVImageSource.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 
28 // Library/third-party includes
29 #include <opencv2/highgui/highgui.hpp> // for image capture
30 
31 // Standard includes
32 // - none
33 
34 namespace osvr {
35 namespace vbtracker {
36  using CVCapturePtr = std::unique_ptr<cv::VideoCapture>;
37 
38  class OpenCVImageSource : public ImageSource {
39  public:
40  OpenCVImageSource(CVCapturePtr &&cam) : m_camera(std::move(cam)) {
41  if (m_camera->isOpened()) {
42  storeRes();
43  }
44  }
45  virtual ~OpenCVImageSource() {}
46 
47  bool ok() const override { return m_camera && m_camera->isOpened(); }
48  bool grab() override;
49  void retrieveColor(cv::Mat &color,
50  osvr::util::time::TimeValue &timestamp) override;
51  cv::Size resolution() const override;
52 
53  private:
54  void storeRes();
55  CVCapturePtr m_camera;
56  cv::Size m_res;
57  osvr::util::time::TimeValue m_timestamp = {};
58  };
59 
60  ImageSourcePtr openOpenCVCamera(int which) {
61  auto ret = ImageSourcePtr{};
62  auto cam = CVCapturePtr{new cv::VideoCapture(which)};
63  if (!cam->isOpened()) {
64  // couldn't open the desired camera
65  return ret;
66  }
67  ret.reset(new OpenCVImageSource{std::move(cam)});
68  return ret;
69  }
70 
72  bool ret = m_camera->grab();
73  if (ret) {
74  m_timestamp = util::time::getNow();
75  }
76  return ret;
77  }
78 
79  void
81  osvr::util::time::TimeValue &timestamp) {
82  m_camera->retrieve(color);
83  timestamp = m_timestamp;
84  }
85 
86  cv::Size OpenCVImageSource::resolution() const { return m_res; }
87 
88  void OpenCVImageSource::storeRes() {
89  int height = static_cast<int>(m_camera->get(CV_CAP_PROP_FRAME_HEIGHT));
90  int width = static_cast<int>(m_camera->get(CV_CAP_PROP_FRAME_WIDTH));
91  m_res = cv::Size(width, height);
92  }
93 } // namespace vbtracker
94 } // namespace osvr
void getNow(TimeValue &tv)
Set the given TimeValue to the current time.
Definition: TimeValue.h:51
cv::Size resolution() const override
Get resolution of the images from this source.
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