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) override;
50  cv::Size resolution() const override;
51 
52  private:
53  void storeRes();
54  CVCapturePtr m_camera;
55  cv::Size m_res;
56  };
57 
58  ImageSourcePtr openOpenCVCamera(int which) {
59  auto ret = ImageSourcePtr{};
60  auto cam = CVCapturePtr{new cv::VideoCapture(which)};
61  if (!cam->isOpened()) {
62  // couldn't open the desired camera
63  return ret;
64  }
65  ret.reset(new OpenCVImageSource{std::move(cam)});
66  return ret;
67  }
68 
69  bool OpenCVImageSource::grab() { return m_camera->grab(); }
70 
71  void OpenCVImageSource::retrieveColor(cv::Mat &color) {
72  m_camera->retrieve(color);
73  }
74 
75  cv::Size OpenCVImageSource::resolution() const { return m_res; }
76 
77  void OpenCVImageSource::storeRes() {
78  int height = static_cast<int>(m_camera->get(CV_CAP_PROP_FRAME_HEIGHT));
79  int width = static_cast<int>(m_camera->get(CV_CAP_PROP_FRAME_WIDTH));
80  m_res = cv::Size(width, height);
81  }
82 } // namespace vbtracker
83 } // namespace osvr
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