auxiliary
 All Classes Namespaces Files Functions Typedefs Enumerations Enumerator Macros
image_fetcher.hpp
Go to the documentation of this file.
1 
40 #pragma once
41 
42 #ifdef USE_OPENCV
43 #include <opencv2/opencv.hpp>
44 #endif
45 
46 #ifdef USE_BOOST
47 #include <boost/filesystem.hpp> // for filesystem access
48 using namespace boost::filesystem;
49 #endif
50 
52 namespace auxiliary
53 {
55  static std::string supported_file_formats("*.bmp;*.dib;*.jpeg;*.jpg;*.jpe;*.jp2;*.png;*.pbm;*.pgm;*.ppm;*.sr;*.ras;*.tiff;*.tif;");
56 
59  : public std::runtime_error
60  {
61  public:
62  typedef std::runtime_error _Mybase;
63 
64  explicit fetch_error(const std::string& _Message, const std::string& _File, size_t _Line, const std::string& _Func)
65  : _Mybase(_Message)
66  {
67  std::ostringstream oss;
68  oss << "Fetch error at " << _Func << std::endl;
69  oss << _File << "(" << _Line << "): " << _Message;
70  msg_ = oss.str();
71  }
72 
73  explicit fetch_error(const char *_Message, const char *_File, size_t _Line, char *_Func)
74  : _Mybase(_Message)
75  {
76  std::ostringstream oss;
77  oss << "Fetch error at " << _Func << std::endl;
78  oss << _File << "(" << _Line << "): " << _Message;
79  msg_ = oss.str();
80  }
81 
82  ~fetch_error() throw() {}
83 
84  const char* what() const throw() { return msg_.c_str(); }
85 
86  private:
87  std::string msg_;
88  };
89 
90 #define FETCH_ERROR(msg) throw fetch_error(msg, __FILE__, __LINE__, __FUNCTION__)
91 
92  inline char PathSeparator()
93  {
94 #if defined(_WIN32) || defined(_WIN64)
95  return '\\';
96 #else
97  return '/';
98 #endif
99  }
102  {
103  public:
105  void open(const std::string& path)
106  {
107 #if defined(USE_BOOST) && defined(USE_OPENCV)
108  boost::filesystem::path p(path);
109  if (boost::filesystem::is_directory(p)) {
110  dir_ = path;
111 #ifdef USE_CXX11
112  std::for_each(boost::filesystem::directory_iterator(p),
113  boost::filesystem::directory_iterator(),
114  [&](boost::filesystem::directory_entry& entry) {
115 #else
116  boost::filesystem::directory_iterator end;
117  for (boost::filesystem::directory_iterator iter(p) ; iter != end ; ++iter) {
118  boost::filesystem::directory_entry& entry = *iter;
119 #endif
120  if (boost::filesystem::is_regular_file(entry.status()) &&
121  supported_file_formats.find(entry.path().extension().string()) != std::string::npos) // match the extension
122  files_.push_back(entry.path().string());
123 #ifdef USE_CXX11
124  });
125 #else
126  }
127 #endif
128 
129  if (files_.empty())
130  FETCH_ERROR("Nothing to fetch");
131 
132  pos_ = 0;
133  } else if (boost::filesystem::is_regular_file(p)) {
134  cap_.open(path);
135  dir_ = path.substr(0, path.find_last_of(PathSeparator())); // video directory
136  } else
137  FETCH_ERROR("Given path does not exist!");
138 #elif defined(USE_OPENCV)
139  cap_.open(path);
140 
141  if (cap_.isOpened())
142  dir_ = path.substr(0, path.find_last_of(PathSeparator())); // video directory
143  else
144  FETCH_ERROR("Given path does not exist!");
145 #else
146  return open_pack(path);
147 #endif
148  }
149 
150  void open_pack(const std::string& path)
151  {
152  fin_.open(path.c_str(), std::ios::binary); // std::string param is supported C++11
153 
154  if (fin_.is_open()) {
155  // try to read pack file
156  cout << "Read pack file" << endl;
157  fin_.read((char *)&width_, sizeof(unsigned int));
158  fin_.read((char *)&height_, sizeof(unsigned int));
159  fin_.read((char *)&numframes_, sizeof(unsigned int));
160  dir_ = path.substr(0, path.find_last_of(PathSeparator()));
161  pos_ = 0;
162  } else
163  FETCH_ERROR("Given path does not exist!");
164  }
165 
167  void open(int device_id)
168  {
169 #ifdef USE_OPENCV
170  if (!cap_.open(device_id))
171  FETCH_ERROR("Cannot connect camera");
172 
173 #ifdef USE_BOOST
174  boost::filesystem::path full_path( boost::filesystem::current_path() );
175  dir_ = full_path.string();
176 #else
177 
178 #endif
179 
180 #endif
181  }
182 
184  bool grab()
185  {
186 #ifdef USE_OPENCV
187  if (cap_.isOpened()) return cap_.grab();
188 #endif
189  return files_.empty() ? pos_ < numframes_ : (pos_ < files_.size());
190  }
191 
193  template <typename pixel_type>
195  {
196 #ifdef USE_OPENCV
197  cv::Mat frame;
198 
199  if (cap_.isOpened()) {
200  cap_.retrieve(frame);
201  image = bgr2gray<pixel_type>(frame);
202  } else if(!files_.empty()) {
203  //std::cout << files[pos].c_str() << " "; /*std::endl;*/
204  frame = cv::imread(files_[pos_++]);
205 
206 #ifdef USE_16BIT_IMAGE
207  // simulate different image format
208  std::cout << "Simulate 16 bit image" << std::endl;
209  frame.clone().convertTo(frame, CV_16U);
210 #endif
211  image = bgr2gray<pixel_type>(frame);
212  } else if (fin_.is_open()) {
213 #else
214  if (fin_.is_open()) {
215 #endif
216  Image<pixel_type> temp(height_, width_);
217  fin_.read((char *)temp.memptr(), sizeof(pixel_type) * width_ * height_);
218  image = Image<pixel_type>(temp.t());
219  ++pos_;
220  }
221  }
222 
224  inline std::string current_directory() const
225  {
226  return dir_;
227  }
228 
229  private:
230 #ifdef USE_OPENCV
231  cv::VideoCapture cap_;
232 #endif
233  std::ifstream fin_;
234  unsigned int width_, height_;
235  unsigned int numframes_;
236 
237  std::string dir_;
238  std::vector<std::string> files_;
239  size_t pos_;
240  };
241 }
bool grab()
Grabs the next frame from video file or directory.
Definition: image_fetcher.hpp:184
#define FETCH_ERROR(msg)
Definition: image_fetcher.hpp:90
void open(const std::string &path)
Open file or directory.
Definition: image_fetcher.hpp:105
void open(int device_id)
Connect to device.
Definition: image_fetcher.hpp:167
An implementation of image fetcher.
Definition: image_fetcher.hpp:101
fetch_error(const std::string &_Message, const std::string &_File, size_t _Line, const std::string &_Func)
Definition: image_fetcher.hpp:64
fetch_error(const char *_Message, const char *_File, size_t _Line, char *_Func)
Definition: image_fetcher.hpp:73
A template image class.
Definition: imgproc_aux.hpp:86
void open_pack(const std::string &path)
Definition: image_fetcher.hpp:150
const char * what() const
Definition: image_fetcher.hpp:84
char PathSeparator()
Definition: image_fetcher.hpp:92
void retrieve(Image< pixel_type > &image)
Decodes and returns the grabbed video frame or image.
Definition: image_fetcher.hpp:194
std::runtime_error _Mybase
Definition: image_fetcher.hpp:62
~fetch_error()
Definition: image_fetcher.hpp:82
defines fetch error exception
Definition: image_fetcher.hpp:58
std::string current_directory() const
Get current directory.
Definition: image_fetcher.hpp:224