OSVR Framework (Internal Development Docs)  0.6-1962-g59773924
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
FakeImageSource.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 #include <vector>
33 #include <sstream>
34 #include <iomanip>
35 #include <iostream>
36 #include <thread>
37 #include <chrono>
38 
39 namespace osvr {
40 namespace vbtracker {
41  class FakeImageSource : public ImageSource {
42  public:
43  FakeImageSource(std::string const &imagesDir);
44  virtual ~FakeImageSource() {}
45 
46  bool ok() const override { return !m_images.empty(); }
47  bool grab() override;
48  void retrieveColor(cv::Mat &color) override;
49  cv::Size resolution() const override;
50 
51  private:
52  std::vector<cv::Mat> m_images;
53  size_t m_currentImage = 0;
54  cv::Size m_res;
55  };
56 
57  ImageSourcePtr openImageFileSequence(std::string const &dir) {
58  auto ret = ImageSourcePtr{new FakeImageSource{dir}};
59  if (!ret->ok()) {
60  // if we couldn't load, reset the pointer right now.
61  ret.reset();
62  }
63  return ret;
64  }
65  FakeImageSource::FakeImageSource(std::string const &imagesDir) {
66 
67  // Read a vector of images, which we'll loop through.
68  for (int imageNum = 1;; ++imageNum) {
69  std::ostringstream fileName;
70  fileName << imagesDir << "/";
71  fileName << std::setfill('0') << std::setw(4) << imageNum;
72  fileName << ".tif";
73  cv::Mat image;
74  std::cout << "Trying to read image from " << fileName.str()
75  << std::endl;
76  image = cv::imread(fileName.str(), CV_LOAD_IMAGE_COLOR);
77  if (!image.data) {
78  break;
79  }
80  m_images.push_back(image);
81  }
82  }
83  bool FakeImageSource::grab() {
84  std::this_thread::sleep_for(std::chrono::milliseconds(10));
85  m_currentImage = (m_currentImage + 1) % m_images.size();
86  return ok();
87  }
88 
89  void FakeImageSource::retrieveColor(cv::Mat &color) {
90  m_images[m_currentImage].copyTo(color);
91  }
92 
93  cv::Size FakeImageSource::resolution() const {
94  return m_images.front().size();
95  }
96 
97 } // namespace vbtracker
98 } // namespace osvr
cv::Size resolution() const override
Get resolution of the images from this source.
void retrieveColor(cv::Mat &color, osvr::util::time::TimeValue &timestamp) override