43 #include <opencv2/opencv.hpp>
47 #include <boost/filesystem.hpp>
48 using namespace boost::filesystem;
55 static std::string supported_file_formats(
"*.bmp;*.dib;*.jpeg;*.jpg;*.jpe;*.jp2;*.png;*.pbm;*.pgm;*.ppm;*.sr;*.ras;*.tiff;*.tif;");
59 :
public std::runtime_error
64 explicit fetch_error(
const std::string& _Message,
const std::string& _File,
size_t _Line,
const std::string& _Func)
67 std::ostringstream oss;
68 oss <<
"Fetch error at " << _Func << std::endl;
69 oss << _File <<
"(" << _Line <<
"): " << _Message;
73 explicit fetch_error(
const char *_Message,
const char *_File,
size_t _Line,
char *_Func)
76 std::ostringstream oss;
77 oss <<
"Fetch error at " << _Func << std::endl;
78 oss << _File <<
"(" << _Line <<
"): " << _Message;
84 const char*
what()
const throw() {
return msg_.c_str(); }
90 #define FETCH_ERROR(msg) throw fetch_error(msg, __FILE__, __LINE__, __FUNCTION__)
94 #if defined(_WIN32) || defined(_WIN64)
105 void open(
const std::string& path)
107 #if defined(USE_BOOST) && defined(USE_OPENCV)
108 boost::filesystem::path p(path);
109 if (boost::filesystem::is_directory(p)) {
112 std::for_each(boost::filesystem::directory_iterator(p),
113 boost::filesystem::directory_iterator(),
114 [&](boost::filesystem::directory_entry& entry) {
116 boost::filesystem::directory_iterator end;
117 for (boost::filesystem::directory_iterator iter(p) ; iter != end ; ++iter) {
118 boost::filesystem::directory_entry& entry = *iter;
120 if (boost::filesystem::is_regular_file(entry.status()) &&
121 supported_file_formats.find(entry.path().extension().string()) != std::string::npos)
122 files_.push_back(entry.path().string());
133 }
else if (boost::filesystem::is_regular_file(p)) {
138 #elif defined(USE_OPENCV)
146 return open_pack(path);
152 fin_.open(path.c_str(), std::ios::binary);
154 if (fin_.is_open()) {
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));
170 if (!cap_.open(device_id))
174 boost::filesystem::path full_path( boost::filesystem::current_path() );
175 dir_ = full_path.string();
187 if (cap_.isOpened())
return cap_.grab();
189 return files_.empty() ? pos_ < numframes_ : (pos_ < files_.size());
193 template <
typename pixel_type>
199 if (cap_.isOpened()) {
200 cap_.retrieve(frame);
201 image = bgr2gray<pixel_type>(frame);
202 }
else if(!files_.empty()) {
204 frame = cv::imread(files_[pos_++]);
206 #ifdef USE_16BIT_IMAGE
208 std::cout <<
"Simulate 16 bit image" << std::endl;
209 frame.clone().convertTo(frame, CV_16U);
211 image = bgr2gray<pixel_type>(frame);
212 }
else if (fin_.is_open()) {
214 if (fin_.is_open()) {
217 fin_.read((
char *)temp.memptr(),
sizeof(pixel_type) * width_ * height_);
231 cv::VideoCapture cap_;
234 unsigned int width_, height_;
235 unsigned int numframes_;
238 std::vector<std::string> files_;
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