-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_fetcher.hpp
264 lines (232 loc) · 8.22 KB
/
image_fetcher.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/**
* @file image_fetcher.hpp
* @brief An image fetcher class implementation
* @author seonho.oh@gmail.com
* @date 2013-07-01
* @copyright
2013-2015 seonho.oh@gmail.com
* @version 1.0
*
* @section LICENSE
*
* Copyright (c) 2013-2015, Seonho Oh
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the <ORGANIZATION> nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#ifdef USE_OPENCV
#include <opencv2/opencv.hpp>
#endif
#ifdef USE_BOOST
#include <boost/filesystem.hpp> // for filesystem access
using namespace boost::filesystem;
#endif
#define RAW_16BIT_WIDTH 320
#define RAW_16BIT_HEIGHT 240
//! An auxiliary interface functions for armadillo library.
namespace auxiliary
{
/// supporting file formats
static std::string supported_file_formats("*.bmp;*.dib;*.jpeg;*.jpg;*.jpe;*.jp2;*.png;*.pbm;*.pgm;*.ppm;*.sr;*.ras;*.tiff;*.tif;");
//! defines fetch error exception
class fetch_error
: public std::runtime_error
{
public:
typedef std::runtime_error _Mybase;
explicit fetch_error(const std::string& _Message, const std::string& _File, size_t _Line, const std::string& _Func)
: _Mybase(_Message)
{
std::ostringstream oss;
oss << "Fetch error at " << _Func << std::endl;
oss << _File << "(" << _Line << "): " << _Message;
msg_ = oss.str();
}
explicit fetch_error(const char *_Message, const char *_File, size_t _Line, char *_Func)
: _Mybase(_Message)
{
std::ostringstream oss;
oss << "Fetch error at " << _Func << std::endl;
oss << _File << "(" << _Line << "): " << _Message;
msg_ = oss.str();
}
~fetch_error() throw() {}
const char* what() const throw() { return msg_.c_str(); }
private:
std::string msg_;
};
#define FETCH_ERROR(msg) throw fetch_error(msg, __FILE__, __LINE__, __FUNCTION__)
inline char PathSeparator()
{
#if defined(_WIN32) || defined(_WIN64)
return '\\';
#else
return '/';
#endif
}
//! An implementation of image fetcher.
class image_fetcher
{
public:
//! Open file or directory
void open(const std::string& path)
{
#if defined(USE_BOOST) && defined(USE_OPENCV)
boost::filesystem::path p(path);
if (boost::filesystem::is_directory(p)) {
dir_ = boost::filesystem::absolute(p).string();
#ifdef USE_CXX11
std::for_each(boost::filesystem::directory_iterator(p),
boost::filesystem::directory_iterator(),
[&](boost::filesystem::directory_entry& entry) {
#else
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator iter(p) ; iter != end ; ++iter) {
boost::filesystem::directory_entry& entry = *iter;
#endif
#ifdef USE_16BIT_IMAGE
if (boost::filesystem::is_regular_file(entry.status())) // match the extension
#else
if (boost::filesystem::is_regular_file(entry.status()) &&
supported_file_formats.find(entry.path().extension().string()) != std::string::npos) // match the extension
#endif
files_.push_back(entry.path().string());
#ifdef USE_CXX11
});
#else
}
#endif
if (files_.empty())
FETCH_ERROR("Nothing to fetch");
pos_ = 0;
} else if (boost::filesystem::is_regular_file(p)) {
cap_.open(path);
dir_ = path.substr(0, path.find_last_of(PathSeparator())); // video directory
} else
FETCH_ERROR("Given path does not exist!");
#elif defined(USE_OPENCV)
cap_.open(path);
if (cap_.isOpened())
dir_ = path.substr(0, path.find_last_of(PathSeparator())); // video directory
else
FETCH_ERROR("Given path does not exist!");
#else
return open_pack(path);
#endif
}
void open_pack(const std::string& path)
{
fin_.open(path.c_str(), std::ios::binary); // std::string param is supported C++11
if (fin_.is_open()) {
// try to read pack file
cout << "Read pack file" << endl;
fin_.read((char *)&width_, sizeof(unsigned int));
fin_.read((char *)&height_, sizeof(unsigned int));
fin_.read((char *)&numframes_, sizeof(unsigned int));
dir_ = path.substr(0, path.find_last_of(PathSeparator()));
pos_ = 0;
} else
FETCH_ERROR("Given path does not exist!");
}
//! Connect to device
void open(int device_id)
{
#ifdef USE_OPENCV
if (!cap_.open(device_id))
FETCH_ERROR("Cannot connect camera");
#ifdef USE_BOOST
boost::filesystem::path full_path( boost::filesystem::current_path() );
dir_ = full_path.string();
#else
#endif
#endif
}
//! Grabs the next frame from video file or directory.
bool grab()
{
#ifdef USE_OPENCV
if (cap_.isOpened()) return cap_.grab();
#endif
return files_.empty() ? pos_ < numframes_ : (pos_ < files_.size());
}
//! Decodes and returns the grabbed video frame or image.
template <typename pixel_type>
void retrieve(Image<pixel_type>& image)
{
#ifdef USE_OPENCV
cv::Mat frame;
if (cap_.isOpened()) {
cap_.retrieve(frame);
image = bgr2gray<pixel_type>(frame);
} else if(!files_.empty()) {
//std::cout << files[pos].c_str() << " "; /*std::endl;*/
#ifdef USE_16BIT_IMAGE
// simulate different image format
//std::cout << "Simulate 16 bit image" << std::endl;
//frame.clone().convertTo(frame, CV_16U);
//frame = cv::Mat(320, 240, cv::DataType<pixel_type>::type);
//pixel_type* ptr = frame.ptr<pixel_type>();
arma::Mat<pixel_type> gray(RAW_16BIT_WIDTH, RAW_16BIT_HEIGHT);
pixel_type* ptr = gray.memptr();
FILE* f = fopen(files_[pos_++].c_str(), "r");
for (int r = 0 ; r < RAW_16BIT_HEIGHT ; r++) {
for (int c = 0 ; c < RAW_16BIT_WIDTH ; c++)
fscanf(f, "%uh ", ptr++);
}
fclose(f);
image = Image<pixel_type>(gray.t());
#else
frame = cv::imread(files_[pos_++]);
image = bgr2gray<pixel_type>(frame);
#endif
} else if (fin_.is_open()) {
#else
if (fin_.is_open()) {
#endif
Image<pixel_type> temp(height_, width_);
fin_.read((char *)temp.memptr(), sizeof(pixel_type) * width_ * height_);
image = Image<pixel_type>(temp.t());
++pos_;
}
}
//! Get current directory
inline std::string current_directory() const
{
return dir_;
}
private:
#ifdef USE_OPENCV
cv::VideoCapture cap_; ///< video capture
#endif
std::ifstream fin_; ///<
unsigned int width_, height_;
unsigned int numframes_;
std::string dir_; ///< the current directory
std::vector<std::string> files_; ///< the image file names
size_t pos_; ///< the current frame number
};
}