-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopencv.c
495 lines (451 loc) · 12.9 KB
/
opencv.c
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
#include <TH.h>
#include <luaT.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
/* These are containers for the VideoFromFile and VideoFromCam code */
#define MAXFOPEN 100
static CvCapture* vcapture[MAXFOPEN];
static IplImage* frame[MAXFOPEN];
static int nextvid[MAXFOPEN];
static int fidx = 0;
/* These are containers for the VideoWriting */
#define MAXWOPEN 10
static CvVideoWriter* vwriter[MAXWOPEN];
static int nextwriter[MAXWOPEN];
static int widx = 0;
#ifdef DEBUG
static int print_nextvid(lua_State *L){
int i;
for (i=0;i<MAXFOPEN;i++){
i==fidx?printf("[%02d]",nextvid[i]):printf("<%02d>",nextvid[i]);
}
printf("\n");
return 0;
}
#endif
static int libopencv_videoOpenCamera(lua_State *L) {
int ret = 0;
// max file open ?
if (fidx == MAXFOPEN) {
THError("max nb of devices open...");
ret = -1;
goto free_and_return;
}
int idx = -1;
if (lua_isnumber(L, 1)) {
idx = lua_tonumber(L, 1);
printf("opening camera device: %d\n", idx);
}
vcapture[fidx] = cvCaptureFromCAM(idx);
if( vcapture[fidx] == NULL ) {
THError("Could not open video file");
ret = -1;
goto free_and_return;
}
frame[fidx] = cvQueryFrame ( vcapture[fidx] );
if ( frame[fidx] == NULL ) {
THError("Failed to load first frame");
ret = -1;
goto free_and_return;
}
free_and_return:
/* return the id for the video just opened*/
if (ret == 0){
lua_pushnumber(L, fidx);
fidx = nextvid[fidx];
}else{
lua_pushnumber(L,-1);
}
return 1;
}
static int libopencv_videoLoadFile(lua_State *L) {
int ret = 0;
// max file open ?
if (fidx == MAXFOPEN) {
THError("max nb of files open...");
ret = -1;
goto free_and_return;
}
if (lua_isstring(L, 1)) {
const char *fname = lua_tostring(L, 1);
printf("opening video file: %s\n", fname);
vcapture[fidx] = cvCaptureFromFile(fname);
if( vcapture[fidx] == NULL ) {
THError("Could not open video file");
ret = -1;
goto free_and_return;
}
frame[fidx] = cvQueryFrame ( vcapture[fidx] );
if ( frame[fidx] == NULL ) {
THError("Failed to load first frame");
ret = -1;
goto free_and_return;
}
} else {
// ARG error
THError("Argument error need to pass a filename as first arg");
ret = -1;
goto free_and_return;
}
free_and_return:
/* return the id for the video just opened*/
if (ret == 0){
lua_pushnumber(L, fidx);
fidx = nextvid[fidx];
}else{
lua_pushnumber(L,-1);
}
return 1;
}
static int fileopen(int cidx){
// is vid file open ?
if ((cidx > MAXFOPEN)||(vcapture[cidx] == NULL)) {
return 0;
} else {
return 1;
}
}
/*
* close a video file
*/
static int libopencv_videoCloseFile(lua_State *L) {
int cidx = MAXFOPEN;
if (lua_isnumber(L,1)){
cidx = lua_tonumber(L,1);
}
// is vid file open ?
if (!fileopen(cidx)) {
THError("Can't close file: no open video at index");
goto free_and_return;
}
if (vcapture[cidx] != NULL) {
cvReleaseCapture(&vcapture[cidx]);
/* return vid so that it can be reused */
vcapture[cidx] = NULL;
nextvid[cidx] = fidx;
fidx = cidx;
}
free_and_return:
return 0;
}
static int libopencv_videoWriter(lua_State *L) {
int ret = 0;
const char *fname;
const char *fccstr;
double fps = 10.0;
int fourcc;
int width, height;
CvSize frame_size;
// max file open ?
if (widx == MAXWOPEN) {
THError("max nb of files open...");
ret = -1;
goto free_and_return;
}
/* Parse arguments */
if (lua_isstring(L, 1)) {
fname = lua_tostring(L, 1);
} else {
// ARG error
THError("\n\nUsage: videoWriter(<filename>,<w>,<h>,[<fps>,<fourcc>])\n 1st arg is a filename\n");
ret = -1;
goto free_and_return;
}
printf("Opening video output file: %s\n", fname);
if (lua_isnumber(L,2) && lua_isnumber(L,3)) {
width = lua_tonumber(L,2);
height = lua_tonumber(L,3);
} else {
// ARG error
THError("\n\nUsage: videoWriter(<filename>,<w>,<h>,[<fps>,<fourcc>])\n 2,3 args are width and height\n");
ret = -1;
goto free_and_return;
}
frame_size = cvSize(width,height);
printf(" (width,height) = (%d,%d)\n", width, height);
/* optional arguments */
if (lua_isnumber(L,4)){
fps = lua_tonumber(L,4);
}
printf("Using FPS: %f\n",fps);
/* http://ffmpeg.org/doxygen/trunk/isom_8c-source.html */
if (lua_isstring(L, 5)) {
fccstr = lua_tostring(L, 5);
fourcc = CV_FOURCC(fccstr[0],fccstr[1],fccstr[2],fccstr[3]);
printf("Using fourcc: %s\n",fccstr);
} else {
fourcc = CV_FOURCC('m','j','p','g');
printf("Using fourcc: 'mjpg'\n");
}
vwriter[widx] = cvCreateVideoWriter(fname, fourcc, fps, frame_size, 1);
if( vwriter[widx] == NULL ) {
THError("\nCould not open video file");
ret = -1;
goto free_and_return;
}
free_and_return:
/* return the id for the video just opened*/
if (ret == 0){
lua_pushnumber(L, widx);
widx = nextwriter[widx];
}else{
lua_pushnumber(L,-1);
}
return 1;
}
static int writeropen(int cidx){
// is vid file open ?
if ((cidx > MAXWOPEN)||(vwriter[cidx] == NULL)) {
return 0;
} else {
return 1;
}
}
/*
* close a video writer file
*/
static int libopencv_videoCloseWriter(lua_State *L) {
int cidx = MAXWOPEN;
if (lua_isnumber(L,1)){
cidx = lua_tonumber(L,1);
}
// is vid file open ?
if (!writeropen(cidx)) {
THError("Can't close file: no open video at index");
goto free_and_return;
}
if (vwriter[cidx] != NULL) {
cvReleaseVideoWriter(&vwriter[cidx]);
/* return vid so that it can be reused */
vwriter[cidx] = NULL;
nextwriter[cidx] = widx;
widx = cidx;
}
free_and_return:
return 0;
}
/*
* seek in video file by milli second
* seeking by frame CV_CAP_PROP_POS_FRAMES is broken
*/
static int libopencv_videoSeek(lua_State *L) {
double msec = 0, psec = 0;
double sprop = 0, fps, mspf;
int i, ret = 0;
int cidx = MAXFOPEN;
if (lua_isnumber(L,1)){
cidx = lua_tonumber(L,1);
}
// is vid file open ?
if (!fileopen(cidx)) {
THError("can't seek: no open video at index");
goto free_and_return;
}
if (lua_isnumber(L,2)){
msec = lua_tonumber(L,2) * 1000.0;
}
psec = msec - 500.0;
if (psec < 0){
psec = 0;
}
cvSetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_MSEC,psec);
sprop = cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_MSEC);
fps = cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FPS);
mspf = 1000/fps;
/* Make sure that the seek has put us before the frame we actually
want. */
while ((sprop > msec) && (psec > 500)){
psec -= 500;
cvSetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_MSEC,psec);
sprop = cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_MSEC);
}
for (i=0;i<fps+1;i++){
/* Get new frame */
ret = cvGrabFrame(vcapture[cidx]);
if (ret != 1){
THError("cvGrabFrame failed.");
goto free_and_return;
}
/* Check current timecode */
sprop = cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_MSEC);
if (sprop + mspf > msec){
/* Found the correct frame. */
break;
} /* if (sprop > msec) */
} /* for (i=0;i<fps+1,i++) */
free_and_return:
/* return the actual msec of the frame found */
lua_pushnumber(L, sprop);
return 1;
}
/*
* dump video properties
*/
static int libopencv_videoDumpProperties(lua_State *L) {
int cidx = MAXFOPEN;
if (lua_isnumber(L,1)){
cidx = lua_tonumber(L,1);
}
// is vid file open ?
if (!fileopen(cidx)) {
THError("Can't dump properties: no open video at index");
goto free_and_return;
}
printf("[%d]CV_CAP_PROP_POS_MSEC: %f\n",
CV_CAP_PROP_POS_MSEC,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_MSEC));
printf("[%d]CV_CAP_PROP_POS_FRAMES: %f\n",
CV_CAP_PROP_POS_FRAMES,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_FRAMES));
printf("[%d]CV_CAP_PROP_POS_AVI_RATIO: %f\n",
CV_CAP_PROP_POS_AVI_RATIO,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_AVI_RATIO));
printf("[%d]CV_CAP_PROP_FRAME_WIDTH: %f\n",
CV_CAP_PROP_FRAME_WIDTH,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FRAME_WIDTH ));
printf("[%d]CV_CAP_PROP_FRAME_HEIGHT: %f\n",
CV_CAP_PROP_FRAME_HEIGHT,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FRAME_HEIGHT));
printf("[%d]CV_CAP_PROP_FPS: %f\n",
CV_CAP_PROP_FPS,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FPS));
printf("[%d]CV_CAP_PROP_FOURCC: %f\n",
CV_CAP_PROP_FOURCC,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FOURCC));
printf("[%d]CV_CAP_PROP_FRAME_COUNT: %f\n",
CV_CAP_PROP_FRAME_COUNT,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FRAME_COUNT));
printf("[%d]CV_CAP_PROP_FORMAT: %f\n",
CV_CAP_PROP_FORMAT,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FORMAT));
printf("[%d]CV_CAP_PROP_MODE: %f\n",
CV_CAP_PROP_MODE,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_MODE));
printf("[%d]CV_CAP_PROP_BRIGHTNESS: %f\n",
CV_CAP_PROP_BRIGHTNESS,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_BRIGHTNESS));
printf("[%d]CV_CAP_PROP_CONTRAST: %f\n",
CV_CAP_PROP_CONTRAST,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_CONTRAST));
printf("[%d]CV_CAP_PROP_SATURATION: %f\n",
CV_CAP_PROP_SATURATION,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_SATURATION));
printf("[%d]CV_CAP_PROP_HUE: %f\n",
CV_CAP_PROP_HUE,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_HUE));
printf("[%d]CV_CAP_PROP_GAIN: %f\n",
CV_CAP_PROP_GAIN,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_GAIN));
printf("[%d]CV_CAP_PROP_EXPOSURE: %f\n",
CV_CAP_PROP_EXPOSURE,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_EXPOSURE));
printf("[%d]CV_CAP_PROP_CONVERT_RGB: %f\n",
CV_CAP_PROP_CONVERT_RGB,
cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_CONVERT_RGB));
free_and_return:
return 0;
}
/*
* get video property
*/
static int libopencv_videoGetProperty(lua_State *L) {
double property_val = 0;
int property_id = -1;
int cidx = MAXFOPEN;
if (lua_isnumber(L,1)){
cidx = lua_tonumber(L,1);
}
// is vid file open ?
if (!fileopen(cidx)) {
THError("Can't get Property: no open video at index");
goto free_and_return;
}
if (lua_isnumber(L,2)){
property_id = lua_tonumber(L,2);
}
property_val = cvGetCaptureProperty(vcapture[cidx],property_id);
free_and_return:
lua_pushnumber(L, property_val);
return 1;
}
/*
* get video FPS
*/
static int libopencv_videoGetFPS(lua_State *L) {
double property_val = 0;
int cidx = MAXFOPEN;
if (lua_isnumber(L,1)){
cidx = lua_tonumber(L,1);
}
// is vid file open ?
if (!fileopen(cidx)) {
THError("Can't get FPS: no open video at index");
goto free_and_return;
}
property_val = cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_FPS);
free_and_return:
lua_pushnumber(L, property_val);
return 1;
}
/*
* get video MSEC
*/
static int libopencv_videoGetMSEC(lua_State *L) {
double property_val = 0;
int cidx = MAXFOPEN;
if (lua_isnumber(L,1)){
cidx = lua_tonumber(L,1);
}
// is vid file open ?
if (!fileopen(cidx)) {
THError("Can't get msec: no open video at index");
goto free_and_return;
}
property_val = cvGetCaptureProperty(vcapture[cidx],CV_CAP_PROP_POS_MSEC);
free_and_return:
lua_pushnumber(L, property_val);
return 1;
}
//============================================================
// Register functions in LUA
//
static const luaL_reg libopencv_init [] =
{
{"videoLoadFile", libopencv_videoLoadFile},
{"videoOpenCamera", libopencv_videoOpenCamera},
{"videoCloseFile", libopencv_videoCloseFile},
{"videoSeek", libopencv_videoSeek},
{"videoGetProperty", libopencv_videoGetProperty},
{"videoGetFPS", libopencv_videoGetFPS},
{"videoGetMSEC", libopencv_videoGetMSEC},
{"videoDumpProperties", libopencv_videoDumpProperties},
{"videoWriter", libopencv_videoWriter},
{"videoCloseWriter", libopencv_videoCloseWriter},
#ifdef DEBUG
{"videoPrintNextList", print_nextvid},
#endif
{NULL,NULL}
};
#define torch_(NAME) TH_CONCAT_3(torch_, Real, NAME)
#define torch_Tensor TH_CONCAT_STRING_3(torch.,Real,Tensor)
#define libopencv_(NAME) TH_CONCAT_3(libopencv_, Real, NAME)
#include "generic/opencv.c"
#include "THGenerateFloatTypes.h"
DLL_EXPORT int luaopen_libopencv(lua_State *L)
{
luaL_register(L, "opencv", libopencv_init);
libopencv_FloatMain_init(L);
libopencv_DoubleMain_init(L);
/* set up pointers to store capture objects and frames */
int i;
for (i=0;i<MAXFOPEN;i++){
vcapture[i] = NULL;
frame[i] = NULL;
nextvid[i] = i+1;
}
for (i=0;i<MAXWOPEN;i++){
vwriter[i] = NULL;
nextwriter[i] = i+1;
}
return 1;
}