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"
28 
29 // Library/third-party includes
30 #include <opencv2/highgui/highgui.hpp> // for image capture
31 
32 // Standard includes
33 #include <chrono>
34 #include <iomanip>
35 #include <iostream>
36 #include <sstream>
37 #include <thread>
38 #include <vector>
39 
40 namespace osvr {
41 namespace vbtracker {
42  class FakeImageSource : public ImageSource {
43  public:
44  FakeImageSource(std::string const &imagesDir);
45  virtual ~FakeImageSource() {}
46 
47  bool ok() const override { return !m_images.empty(); }
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  std::vector<cv::Mat> m_images;
55  size_t m_currentImage = 0;
56  cv::Size m_res;
57  osvr::util::time::TimeValue m_timestamp = {};
58  };
59 
60  ImageSourcePtr openImageFileSequence(std::string const &dir) {
61  auto ret = ImageSourcePtr{new FakeImageSource{dir}};
62  if (!ret->ok()) {
63  // if we couldn't load, reset the pointer right now.
64  ret.reset();
65  }
66  return ret;
67  }
68  FakeImageSource::FakeImageSource(std::string const &imagesDir) {
69 
70  // Read a vector of images, which we'll loop through.
71  for (int imageNum = 1;; ++imageNum) {
72  std::ostringstream fileName;
73  fileName << imagesDir << "/";
74  fileName << std::setfill('0') << std::setw(4) << imageNum;
75  fileName << ".tif";
76  cv::Mat image;
77  std::cout << "Trying to read image from " << fileName.str()
78  << std::endl;
79  image = cv::imread(fileName.str(), CV_LOAD_IMAGE_COLOR);
80  if (!image.data) {
81  break;
82  }
83  m_images.push_back(image);
84  }
85  }
87  std::this_thread::sleep_for(std::chrono::milliseconds(10));
88  m_timestamp = m_timestamp + std::chrono::milliseconds(10);
89  m_currentImage = (m_currentImage + 1) % m_images.size();
90  return ok();
91  }
92 
93  void
95  osvr::util::time::TimeValue &timestamp) {
96  m_images[m_currentImage].copyTo(color);
97  timestamp = m_timestamp;
98  }
99 
100  cv::Size FakeImageSource::resolution() const {
101  return m_images.front().size();
102  }
103 
104 } // namespace vbtracker
105 } // namespace osvr
cv::Size resolution() const override
Get resolution of the images from this source.
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
void retrieveColor(cv::Mat &color, osvr::util::time::TimeValue &timestamp) override