forked from stg/RoboCortex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture.cpp
executable file
·125 lines (100 loc) · 3.18 KB
/
capture.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
#include <stdio.h>
#define MAX_DEVICES 10
#ifdef _WIN32
#include <videoinput.h>
typedef struct {
unsigned char dev;
videoInput VI;
unsigned char *buffer;
} capture_t;
static int width;;
static int height;
static capture_t *devs[ MAX_DEVICES ];
static int devs_count = 0;
extern "C" int capture_init( char *device, int fps, int *w, int *h ) {
int size, dev;
capture_t *p_dev = new capture_t();
dev = -1;
if( device ) {
if( strlen( device ) ) {
dev = atoi( device );
}
}
//Prints out a list of available devices and returns num of devices found
if( dev >= 0 ) {
if( !p_dev->VI.setupDevice( dev, *w, *h ) ) return( -1 );
} else {
int numDevices = p_dev->VI.listDevices();
return( -1 );
}
p_dev->VI.setIdealFramerate( dev, fps );
// Automatically reconnect on freeze, may fix bugs with some devices/drivers
p_dev->VI.setAutoReconnectOnFreeze( dev, true, 25 );
width = p_dev->VI.getWidth ( dev );
height = p_dev->VI.getHeight( dev );
size = p_dev->VI.getSize ( dev );
*w = width;
*h = height;
p_dev->buffer = new unsigned char[ size ];
p_dev->dev = dev;
devs[ devs_count ] = p_dev;
return( devs_count++ );
}
extern "C" unsigned char * capture_fetch( int dev ) {
if( devs[ dev ]->VI.isFrameNew( dev ) ) {
devs[ dev ]->VI.getPixels( devs[ dev ]->dev, devs[ dev ]->buffer, false, true ); // BGR, flipped
}
return( devs[ dev ]->buffer );
}
extern "C" void capture_free() {
unsigned int dev;
for( dev = 0; dev < devs_count; dev++ ) {
devs[ dev ]->VI.stopDevice( devs[ dev ]->dev );
devs[ dev ] = NULL;
}
devs_count = 0;
}
#else
#include "opencv/cv.h"
#include "opencv/highgui.h"
static int width;;
static int height;
typedef struct {
unsigned char *buffer;
struct CvCapture *capture;
} capture_t;
static capture_t devs[ MAX_DEVICES ];
static int devs_count = 0;
extern "C" int capture_init( char *device, int fps, int *w, int *h ) {
int size, dev;
dev = atoi( device );
// Initialize camera
devs[ devs_count ].capture = cvCaptureFromCAM( dev );
if( !devs[devs_count].capture ) return( -1 );
cvSetCaptureProperty(devs[ devs_count ].capture, CV_CAP_PROP_FRAME_WIDTH, (double)*w);
cvSetCaptureProperty(devs[ devs_count ].capture, CV_CAP_PROP_FRAME_HEIGHT, (double)*h);
IplImage* img = 0;
img = cvQueryFrame( devs[ devs_count ].capture );
width = ( int )cvGetCaptureProperty( devs[ devs_count ].capture, CV_CAP_PROP_FRAME_WIDTH );
height = ( int )cvGetCaptureProperty( devs[ devs_count ].capture, CV_CAP_PROP_FRAME_HEIGHT );
size = img->imageSize;
devs[ devs_count ].buffer = new unsigned char[ size ];
*w = width;
*h = height;
return( devs_count++ );
}
extern "C" unsigned char * capture_fetch( int dev ) {
IplImage* img = 0;
if( !cvGrabFrame( devs[ dev ].capture ) ) { // capture a frame
printf( "Could not grab a frame\n" );
return( NULL );
}
img = cvRetrieveFrame( devs[ dev ].capture ); // retrieve the captured frame
memcpy( devs[ dev ].buffer, img->imageData, img->imageSize );
return( devs[ dev ].buffer );
}
extern "C" void capture_free() {
// TODO: ?
devs_count = 0;
}
#endif