-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathCameraFrames.cpp
More file actions
335 lines (213 loc) · 8.55 KB
/
CameraFrames.cpp
File metadata and controls
335 lines (213 loc) · 8.55 KB
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
CameraFrames.cpp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* This file is part of: freeture
*
* Copyright: (C) 2014-2015 Yoan Audureau
* FRIPON-GEOPS-UPSUD-CNRS
*
* License: GNU General Public License
*
* FreeTure is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* FreeTure is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with FreeTure. If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 20/07/2015
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/**
* \file CameraFrames.cpp
* \author Yoan Audureau -- FRIPON-GEOPS-UPSUD
* \version 1.0
* \date 02/09/2014
* \brief Fits frames in input of acquisition thread.
*/
#include "CameraFrames.h"
boost::log::sources::severity_logger< LogSeverityLevel > CameraFrames::logger;
CameraFrames::Init CameraFrames::initializer;
CameraFrames::CameraFrames(vector<string> locationList, int numPos, bool verbose):
mNumFramePos(numPos), mReadDataStatus(false), mCurrDirId(0),
mFirstFrameNum(0), mLastFrameNum(0) {
if(locationList.size()>0)
mFramesDir = locationList;
else
throw "No frames directory in input.";
mExposureAvailable = false;
mGainAvailable = false;
mInputDeviceType = SINGLE_FITS_FRAME;
mVerbose = verbose;
}
CameraFrames::~CameraFrames(void) {
}
bool CameraFrames::loadNextDataSet(string &location) {
cout << mCurrDirId << endl;
location = mFramesDir.at(mCurrDirId);
//if(mCurrDirId !=0 ) {
mReadDataStatus = false;
if(!searchMinMaxFramesNumber(mFramesDir.at(mCurrDirId)))
return false;
//}
return true;
}
bool CameraFrames::grabInitialization() {
return searchMinMaxFramesNumber(mFramesDir.at(mCurrDirId));
}
bool CameraFrames::getDataSetStatus() {
mCurrDirId++;
if(mCurrDirId >= mFramesDir.size()) return false;
else return true;
}
bool CameraFrames::getCameraName() {
cout << "Fits frames data." << endl;
return true;
}
bool CameraFrames::searchMinMaxFramesNumber(string location) {
namespace fs = boost::filesystem;
path p(location);
if(fs::exists(p)){
if(mVerbose) BOOST_LOG_SEV(logger, normal) << "Frame's directory exists : " << location;
int firstFrame = -1, lastFrame = 0;
string filename = "";
// Search first and last frames numbers in the directory.
for(directory_iterator file(p);file!= directory_iterator(); ++file) {
path curr(file->path());
if(is_regular_file(curr)) {
// Get file name.
string fname = curr.filename().string();
// Split file name according to the separator "_".
vector<string> output;
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep("_");
tokenizer tokens(fname, sep);
for (tokenizer::iterator tok_iter = tokens.begin();tok_iter != tokens.end(); ++tok_iter) {
output.push_back(*tok_iter);
}
// Search frame number according to the number position known in the file name.
int i = 0, number = 0;
for(int j = 0; j < output.size(); j++) {
if(j == mNumFramePos && j != output.size() - 1) {
number = atoi(output.at(j).c_str());
break;
}
// If the frame number is at the end (before the file extension).
if(j == mNumFramePos && j == output.size() - 1) {
vector<string> output2;
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
boost::char_separator<char> sep2(".");
tokenizer tokens2(output.back(), sep2);
for (tokenizer::iterator tok_iter = tokens2.begin();tok_iter != tokens2.end(); ++tok_iter) {
output2.push_back(*tok_iter);
}
number = atoi(output2.front().c_str());
break;
}
i++;
}
if(firstFrame == -1) {
firstFrame = number;
}else if(number < firstFrame) {
firstFrame = number;
}
if(number > lastFrame) {
lastFrame = number;
}
}
}
if(mVerbose) BOOST_LOG_SEV(logger, normal) << "First frame number in frame's directory : " << firstFrame;
if(mVerbose) BOOST_LOG_SEV(logger, normal) << "Last frame number in frame's directory : " << lastFrame;
mLastFrameNum = lastFrame;
mFirstFrameNum = firstFrame;
return true;
}else{
if(mVerbose) BOOST_LOG_SEV(logger, fail) << "Frame's directory not found.";
if(mVerbose) cout << "Frame's directory not found." << endl;
return false;
}
}
bool CameraFrames::getStopStatus() {
return mReadDataStatus;
}
bool CameraFrames::getFPS(double &value) {
value = 0;
return false;
}
bool CameraFrames::grabImage(Frame &img) {
bool fileFound = false;
string filename = "";
path p(mFramesDir.at(mCurrDirId));
/// Search a frame in the directory.
for(directory_iterator file(p);file!= directory_iterator(); ++file){
path curr(file->path());
if(is_regular_file(curr)){
list<string> ch;
string fname = curr.filename().string();
Conversion::stringTok(ch, fname.c_str(), "_");
list<string>::const_iterator lit(ch.begin()), lend(ch.end());
int i = 0;
int number = 0;
for(; lit != lend; ++lit){
if(i == mNumFramePos && i != ch.size() - 1){
number = atoi((*lit).c_str()); break;
}
if(i == ch.size() - 1){
list<string> ch_;
Conversion::stringTok(ch_, (*lit).c_str(), ".");
number = atoi(ch_.front().c_str());
break;
}
i++;
}
if(number == mFirstFrameNum){
mFirstFrameNum++;
fileFound = true;
cout << "FILE:" << file->path().string() << endl;
BOOST_LOG_SEV(logger, normal) << "FILE:" << file->path().string();
filename = file->path().string() ;
break;
}
}
}
if(mFirstFrameNum > mLastFrameNum || !fileFound){
mReadDataStatus = true;
BOOST_LOG_SEV(logger, normal) << "End read frames.";
return false;
}else{
BOOST_LOG_SEV(logger, normal) << "Frame found.";
Fits2D newFits(filename);
int bitpix;
if(!newFits.readIntKeyword("BITPIX", bitpix)){
BOOST_LOG_SEV(logger, fail) << " Fail to read fits keyword : BITPIX";
return false;
}
/// Read the frame.
Mat resMat;
CamPixFmt frameFormat = MONO8;
switch(bitpix){
case 8 :
frameFormat = MONO8;
newFits.readFits8UC(resMat);
break;
case 16 :
frameFormat = MONO12;
newFits.readFits16S(resMat);
break;
}
boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time();
Frame f = Frame(resMat, 0, 0, to_iso_extended_string(time));
img = f;
img.mFrameNumber = mFirstFrameNum -1 ;
img.mFrameRemaining = mLastFrameNum - mFirstFrameNum-1;
img.mFps = 1;
img.mFormat = frameFormat;
//waitKey(1000);
return true;
}
}