-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMultiStackReader.cpp
349 lines (307 loc) · 9.73 KB
/
MultiStackReader.cpp
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*
* File: MultiStackReader.cpp
* Author: Marc
*
* Created on 9/11/2012
*
* (C) Marc Gershow; licensed under the Creative Commons Attribution Share Alike 3.0 United States License.
* To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/us/ or send a letter to
* Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
*
*/
#include <map>
#include <iostream>
#include <sstream>
#include <ctype.h>
#ifdef _WIN32
// Hopefully one day the code may be less windows-centric :)
#include <windows.h> //required to use multithreaded stack writing
#endif
#include "MultiStackReader.h"
#include "highgui.h"
#include "Timer.h"
#include "StaticBackgroundCompressorLoader.h"
#ifdef _WIN32
#include "WindowsThreadStackCompressor.h"
#else
#include "LinearStackCompressor.h"
#endif
using namespace std;
#ifndef _WIN32
static void Sleep (int t);
static void Sleep (int t) {
cvWaitKey(t);
}
#endif
MultiStackReader::MultiStackReader() {
init();
}
MultiStackReader::MultiStackReader(const MultiStackReader& orig) {
}
MultiStackReader::~MultiStackReader() {
closeInputFile();
}
MultiStackReader::MultiStackReader(const char* fname) {
init();
setInputFileName(fname);
openInputFile();
}
void MultiStackReader::init() {
StackReader::init();
sr.clear();
fnames.clear();
}
//
//inline bool isopenbracket(char c) {
// return ('{' == c || '[' == c '(' == c);
//}
//
//inline bool isclosebracket(char c) {
// return ('}' == c || ']' == c || ')' == c);
//}
//
//inline bool isbracket(char c) {
// return isopenbracket(c) || isclosebracket(c);
//}
//
//inline bool isseparator(char c) {
// return (',' == c || ';' == c)
//}
static string& trimwhitespace (string &s);
string& trimwhitespace (string &s) {
const string whitespace(" \t\n\f\r");
size_t pos = s.find_first_not_of(whitespace);
if (pos > s.length()) {
s.clear();
} else {
s = s.substr(pos, s.find_last_not_of(whitespace)-pos+1);
}
return s;
}
std::vector<std::string> MultiStackReader::parseFileNameInput (const char *fname) {
//currently (9/12) only parses a single file name to a vector with that single file name, or
//a list of filenames {filename1, filename2, filename3} to a vector of file names
//extensions could be to determine if the fname is a directory and use all files in that directory
//or to do wild card searches, etc
vector<string> fn;
const string openbrackets("{[");
const string closebrackets("]}");
const string separators(",;");
string s(fname);
size_t pos = s.find_first_of(openbrackets);
if (pos == string::npos) {
fn.push_back(trimwhitespace(s));
return fn;
}
//remove enclosing brackets
s.erase(0,pos+1);
pos = s.find_last_of(closebrackets);
s.erase(pos, string::npos);
// cout << "after removing brackets, s = " << endl << s << endl;
while (!s.empty()) {
pos = s.find_first_of(separators);
// pos = (pos == string::npos) ? pos : pos-1;
string ss = s.substr(0,pos);
// cout << "pos = " << pos << endl;
// cout << "pretrim substring = " << ss << endl;
trimwhitespace(ss);
// cout << "posttrim substring = " << ss << endl;
if (!ss.empty()) {
fn.push_back(ss);
}
s.erase(0,MAX(pos,pos+1)); //max command takes care of wrapping at npos
trimwhitespace(s);
// cout << "post trim s = " << endl << s << endl;
}
return fn;
}
void MultiStackReader::setInputFileName(const char* fname) {
setInputFileName(parseFileNameInput(fname));
}
void MultiStackReader::setInputFileName(const std::vector<std::string> fnames) {
this->fnames = fnames;
}
void MultiStackReader::closeInputFile() {
for (vector<pair<StackReader *, int> >::iterator it = sr.begin(); it != sr.end(); ++it) {
delete it->first;
it->first = NULL;
}
sr.clear();
endFrames.clear();
}
void MultiStackReader::openInputFile() {
closeInputFile();
checkError(); if (isError()) { return; }
int totalframes = 0;
for (vector<string>::iterator it = fnames.begin(); it != fnames.end(); ++ it) {
StackReader *s = new StackReader(it->c_str());
sr.push_back(make_pair(s,totalframes));
totalframes += s->getTotalFrames();
endFrames.push_back(totalframes);
}
totalFrames = totalframes;
checkError();
}
bool MultiStackReader::dataFileOk() {
if (!sr.empty()) {
for (vector<pair<StackReader *, int> >::iterator it = sr.begin(); it != sr.end(); ++it) {
if (it->first == NULL || !it->first->dataFileOk()) {
return false;
}
}
return true;
} else {
return false;
}
return false;
}
CvSize MultiStackReader::getImageSize() {
checkError();
if (sr.empty()) {
setError("get image size called with no stacks open");
}
if (isError()) { return cvSize(0,0); }
int srnum = findStackReader(sr.front().second);
if (srnum < 0 || srnum >= sr.size()) {
if (!isError()) {
setError("findStackReader returned bad value");
}
return cvSize(0,0);
}
return sr.at(srnum).first->getImageSize();
}
void MultiStackReader::getBackground(int frameNum, IplImage** dst, int frameRange) {
int srnum = findStackReader(frameNum);
if (srnum < 0 || srnum >= sr.size()) {
if (!isError()) {
setError("findStackReader returned bad value");
}
if (*dst != NULL) {
cvReleaseImage(dst);
*dst = NULL;
}
return;
}
sr.at(srnum).first->getBackground(frameNum-sr.at(srnum).second, dst, frameRange);
checkError();
}
void MultiStackReader::getFrame(int frameNum, IplImage** dst) {
int srnum = findStackReader(frameNum); checkError();
if (isError() || srnum < 0 || srnum >= sr.size()) {
if (!isError()) {
setError("findStackReader returned bad value");
}
if (*dst != NULL) {
cvReleaseImage(dst);
*dst = NULL;
}
return;
}
sr.at(srnum).first->getFrame(frameNum-sr.at(srnum).second, dst);
checkError();
}
const ImageMetaData *MultiStackReader::getMetaData(int frameNum) {
int srnum = findStackReader(frameNum); checkError();
if (isError() || srnum < 0 || srnum >= sr.size()) {
if (!isError()) {
setError("findStackReader returned bad value");
}
return NULL;
}
return sr.at(srnum).first->getMetaData(frameNum-sr.at(srnum).second);
}
void MultiStackReader::annotatedFrame(int frameNum, IplImage** dst) {
int srnum = findStackReader(frameNum); checkError();
if (isError() || srnum < 0 || srnum >= sr.size()) {
if (!isError()) {
setError("findStackReader returned bad value");
}
if (*dst != NULL) {
cvReleaseImage(dst);
*dst = NULL;
}
return;
}
sr.at(srnum).first->annotatedFrame(frameNum-sr.at(srnum).second, dst);
}
int MultiStackReader::getKeyFrameInterval() {
if (sr.empty() || sr.at(0).first == NULL) {
setError("no open stack readers in getKeyFrameInterval");
return -1;
}
int kfi = sr.at(0).first->getKeyFrameInterval();
checkError();
return kfi;
}
ExtraDataWriter *MultiStackReader::getSupplementalData() {
return addToSupplementalData(NULL,0);
}
ExtraDataWriter *MultiStackReader::addToSupplementalData(ExtraDataWriter* edw, int frameOffset) {
for (vector<pair<StackReader *, int> >::iterator it = sr.begin(); it != sr.end(); ++it) {
edw = it->first->addToSupplementalData(edw, it->second + frameOffset);
}
checkError();
return edw;
}
CvRect MultiStackReader::getLargestROI() {
if (validROI.width <= 0 || validROI.height <= 0) {
if (sr.empty()) {
setError("getLargestROI called with no stack readers open");
return validROI;
}
StackReader *s = sr[0].first;
if (s == NULL) {
setError("first stackReader is NULL");
return validROI;
}
validROI = s->getLargestROI();
}
checkError();
return validROI;
}
int MultiStackReader::findStackReader(int frameNumber) {
checkError(); if (isError()) {return -1;}
int nstacks = MIN (endFrames.size(), sr.size());
for (int j = 0; j < nstacks; ++j) {
if (sr.at(j).second <= frameNumber && endFrames.at(j) > frameNumber) {
return j;
}
}
stringstream s;
if (endFrames.size()*sr.size() == 0) {
s << "tried to set stack reader with no open stacks";
} else {
s << "could not find frame number " << frameNumber << " : frame ranges are: ";
for (int j = 0; j < nstacks; ++j) {
s << "[" <<sr.at(j).second << "," << endFrames.at(j) << "); ";
}
}
setError(s.str());
return -1;
}
void MultiStackReader::checkError() {
if (isError()) {
return;
}
int j = 0;
for (vector<pair<StackReader *, int> >::iterator it = sr.begin(); it != sr.end(); ++it) {
if (it->first->isError()) {
stringstream s;
s << "stack #" << j << ": " <<it->first->getError();
setError(s.str());
return;
}
j++;
}
}
std::string MultiStackReader::diagnostics() {
stringstream s;
if (sr.empty()) {
return "no stacks opened";
}
int j = 0;
for (vector<pair<StackReader *, int> >::iterator it = sr.begin(); it != sr.end(); ++it) {
s << "stack #" << j++ << ": " << it->first->diagnostics();
}
return s.str();
}