Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/videoio/src/cap_images.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// Intel License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2008, Nils Hasler, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of Intel Corporation may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the Intel Corporation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
42
// Author: Nils Hasler <[email protected]>
43
//
44
// Max-Planck-Institut Informatik
45
46
//
47
// capture video from a sequence of images
48
// the filename when opening can either be a printf pattern such as
49
// video%04d.png or the first frame of the sequence i.e. video0001.png
50
//
51
52
#include "precomp.hpp"
53
#include <sys/stat.h>
54
55
#ifdef NDEBUG
56
#define CV_WARN(message)
57
#else
58
#define CV_WARN(message) fprintf(stderr, "warning: %s (%s:%d)\n", message, __FILE__, __LINE__)
59
#endif
60
61
#ifndef _MAX_PATH
62
#define _MAX_PATH 1024
63
#endif
64
65
class CvCapture_Images : public CvCapture
66
{
67
public:
68
CvCapture_Images()
69
{
70
filename = NULL;
71
currentframe = firstframe = 0;
72
length = 0;
73
frame = NULL;
74
grabbedInOpen = false;
75
}
76
77
virtual ~CvCapture_Images() CV_OVERRIDE
78
{
79
close();
80
}
81
82
virtual bool open(const char* _filename);
83
virtual void close();
84
virtual double getProperty(int) const CV_OVERRIDE;
85
virtual bool setProperty(int, double) CV_OVERRIDE;
86
virtual bool grabFrame() CV_OVERRIDE;
87
virtual IplImage* retrieveFrame(int) CV_OVERRIDE;
88
89
int getCaptureDomain() /*const*/ CV_OVERRIDE { return cv::CAP_IMAGES; }
90
protected:
91
char* filename; // actually a printf-pattern
92
unsigned currentframe;
93
unsigned firstframe; // number of first frame
94
unsigned length; // length of sequence
95
96
IplImage* frame;
97
bool grabbedInOpen;
98
};
99
100
101
void CvCapture_Images::close()
102
{
103
if( filename )
104
{
105
free(filename);
106
filename = NULL;
107
}
108
currentframe = firstframe = 0;
109
length = 0;
110
cvReleaseImage( &frame );
111
}
112
113
114
bool CvCapture_Images::grabFrame()
115
{
116
char str[_MAX_PATH];
117
sprintf(str, filename, firstframe + currentframe);
118
119
if (grabbedInOpen)
120
{
121
grabbedInOpen = false;
122
++currentframe;
123
124
return frame != NULL;
125
}
126
127
cvReleaseImage(&frame);
128
frame = cvLoadImage(str, CV_LOAD_IMAGE_UNCHANGED);
129
if( frame )
130
currentframe++;
131
132
return frame != NULL;
133
}
134
135
IplImage* CvCapture_Images::retrieveFrame(int)
136
{
137
return grabbedInOpen ? NULL : frame;
138
}
139
140
double CvCapture_Images::getProperty(int id) const
141
{
142
switch(id)
143
{
144
case CV_CAP_PROP_POS_MSEC:
145
CV_WARN("collections of images don't have framerates\n");
146
return 0;
147
case CV_CAP_PROP_POS_FRAMES:
148
return currentframe;
149
case CV_CAP_PROP_FRAME_COUNT:
150
return length;
151
case CV_CAP_PROP_POS_AVI_RATIO:
152
return (double)currentframe / (double)(length - 1);
153
case CV_CAP_PROP_FRAME_WIDTH:
154
return frame ? frame->width : 0;
155
case CV_CAP_PROP_FRAME_HEIGHT:
156
return frame ? frame->height : 0;
157
case CV_CAP_PROP_FPS:
158
CV_WARN("collections of images don't have framerates\n");
159
return 1;
160
case CV_CAP_PROP_FOURCC:
161
CV_WARN("collections of images don't have 4-character codes\n");
162
return 0;
163
}
164
return 0;
165
}
166
167
bool CvCapture_Images::setProperty(int id, double value)
168
{
169
switch(id)
170
{
171
case CV_CAP_PROP_POS_MSEC:
172
case CV_CAP_PROP_POS_FRAMES:
173
if(value < 0) {
174
CV_WARN("seeking to negative positions does not work - clamping\n");
175
value = 0;
176
}
177
if(value >= length) {
178
CV_WARN("seeking beyond end of sequence - clamping\n");
179
value = length - 1;
180
}
181
currentframe = cvRound(value);
182
if (currentframe != 0)
183
grabbedInOpen = false; // grabbed frame is not valid anymore
184
return true;
185
case CV_CAP_PROP_POS_AVI_RATIO:
186
if(value > 1) {
187
CV_WARN("seeking beyond end of sequence - clamping\n");
188
value = 1;
189
} else if(value < 0) {
190
CV_WARN("seeking to negative positions does not work - clamping\n");
191
value = 0;
192
}
193
currentframe = cvRound((length - 1) * value);
194
if (currentframe != 0)
195
grabbedInOpen = false; // grabbed frame is not valid anymore
196
return true;
197
}
198
CV_WARN("unknown/unhandled property\n");
199
return false;
200
}
201
202
static char* icvExtractPattern(const char *filename, unsigned *offset)
203
{
204
char *name = (char *)filename;
205
206
if( !filename )
207
return 0;
208
209
// check whether this is a valid image sequence filename
210
char *at = strchr(name, '%');
211
if(at)
212
{
213
unsigned int dummy;
214
if(sscanf(at + 1, "%ud", &dummy) != 1)
215
return 0;
216
name = strdup(filename);
217
}
218
else // no pattern filename was given - extract the pattern
219
{
220
at = name;
221
222
// ignore directory names
223
char *slash = strrchr(at, '/');
224
if (slash) at = slash + 1;
225
226
#ifdef _WIN32
227
slash = strrchr(at, '\\');
228
if (slash) at = slash + 1;
229
#endif
230
231
while (*at && !isdigit(*at)) at++;
232
233
if(!*at)
234
return 0;
235
236
sscanf(at, "%u", offset);
237
238
int size = (int)strlen(filename) + 20;
239
name = (char *)malloc(size);
240
CV_Assert(name != NULL);
241
strncpy(name, filename, at - filename);
242
name[at - filename] = 0;
243
244
strcat(name, "%0");
245
246
int i;
247
char *extension;
248
for(i = 0, extension = at; isdigit(at[i]); i++, extension++)
249
;
250
char places[13] = {0};
251
sprintf(places, "%dd", i);
252
253
strcat(name, places);
254
strcat(name, extension);
255
}
256
257
return name;
258
}
259
260
261
bool CvCapture_Images::open(const char * _filename)
262
{
263
unsigned offset = 0;
264
close();
265
266
filename = icvExtractPattern(_filename, &offset);
267
if(!filename)
268
return false;
269
270
// determine the length of the sequence
271
length = 0;
272
char str[_MAX_PATH];
273
for(;;)
274
{
275
sprintf(str, filename, offset + length);
276
struct stat s;
277
if(stat(str, &s))
278
{
279
if(length == 0 && offset == 0) // allow starting with 0 or 1
280
{
281
offset++;
282
continue;
283
}
284
}
285
286
if(!cvHaveImageReader(str))
287
break;
288
289
length++;
290
}
291
292
if(length == 0)
293
{
294
close();
295
return false;
296
}
297
298
firstframe = offset;
299
300
// grab frame to enable properties retrieval
301
bool grabRes = grabFrame();
302
grabbedInOpen = true;
303
currentframe = 0;
304
305
return grabRes;
306
}
307
308
309
CvCapture* cvCreateFileCapture_Images(const char * filename)
310
{
311
CvCapture_Images* capture = new CvCapture_Images;
312
313
if( capture->open(filename) )
314
return capture;
315
316
delete capture;
317
return NULL;
318
}
319
320
//
321
//
322
// image sequence writer
323
//
324
//
325
class CvVideoWriter_Images CV_FINAL : public CvVideoWriter
326
{
327
public:
328
CvVideoWriter_Images()
329
{
330
filename = 0;
331
currentframe = 0;
332
}
333
virtual ~CvVideoWriter_Images() { close(); }
334
335
virtual bool open( const char* _filename );
336
virtual void close();
337
virtual bool setProperty( int, double ); // FIXIT doesn't work: IVideoWriter interface only!
338
virtual bool writeFrame( const IplImage* ) CV_OVERRIDE;
339
340
int getCaptureDomain() const CV_OVERRIDE { return cv::CAP_IMAGES; }
341
protected:
342
char* filename;
343
unsigned currentframe;
344
std::vector<int> params;
345
};
346
347
bool CvVideoWriter_Images::writeFrame( const IplImage* image )
348
{
349
char str[_MAX_PATH];
350
sprintf(str, filename, currentframe);
351
std::vector<int> image_params = params;
352
image_params.push_back(0); // append parameters 'stop' mark
353
image_params.push_back(0);
354
int ret = cvSaveImage(str, image, &image_params[0]);
355
356
currentframe++;
357
358
return ret > 0;
359
}
360
361
void CvVideoWriter_Images::close()
362
{
363
if( filename )
364
{
365
free( filename );
366
filename = 0;
367
}
368
currentframe = 0;
369
params.clear();
370
}
371
372
373
bool CvVideoWriter_Images::open( const char* _filename )
374
{
375
unsigned offset = 0;
376
377
close();
378
379
filename = icvExtractPattern(_filename, &offset);
380
if(!filename)
381
return false;
382
383
char str[_MAX_PATH];
384
sprintf(str, filename, 0);
385
if(!cvHaveImageWriter(str))
386
{
387
close();
388
return false;
389
}
390
391
currentframe = offset;
392
params.clear();
393
return true;
394
}
395
396
397
bool CvVideoWriter_Images::setProperty( int id, double value )
398
{
399
if (id >= cv::CAP_PROP_IMAGES_BASE && id < cv::CAP_PROP_IMAGES_LAST)
400
{
401
params.push_back( id - cv::CAP_PROP_IMAGES_BASE );
402
params.push_back( static_cast<int>( value ) );
403
return true;
404
}
405
return false; // not supported
406
}
407
408
409
CvVideoWriter* cvCreateVideoWriter_Images( const char* filename )
410
{
411
CvVideoWriter_Images *writer = new CvVideoWriter_Images;
412
413
if( writer->open( filename ))
414
return writer;
415
416
delete writer;
417
return 0;
418
}
419
420