/
opencv.c
146 lines (112 loc) · 3.09 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
#ifdef RL_OPENCV
void convert_cvimage_to_raster(IplImage *frame, raster_t *image, const int mode)
{
int32_t i, ic;
uint8_t *ps0, *ps1;
if (frame==NULL) return ;
if (frame->imageData==NULL) return ;
if (image->dim.x != frame->width || image->dim.y != frame->height)
{
image->dim.x = frame->width;
image->dim.y = frame->height;
free (image->srgb);
image->srgb = calloc(image->dim.x*image->dim.y, sizeof(srgb_t));
}
for (i=0; i < image->dim.x*image->dim.y; i++)
{
ps0 = &frame->imageData[i*3];
ps1 = &image->srgb[i];
for (ic=0; ic<3; ic++)
ps1[ic] = ps0[2-ic];
}
convert_image_srgb8(image, image->srgb, mode); // fills all the necessary buffers with the image data
}
void convert_srgb_to_cvimage(raster_t *image, IplImage *frame)
{
int32_t ix, iy, ic;
uint8_t *ps0, *ps1;
for (iy=0; iy<image->dim.y; iy++)
for (ix=0; ix<image->dim.x; ix++)
{
ps0 = &image->srgb[iy*image->dim.x+ix];
ps1 = &frame->imageData[(iy*image->dim.x+ix)*3];
for (ic=0; ic<3; ic++)
ps1[ic] = ps0[ic];
}
}
int get_webcam_frame(raster_t *image, char *debug_str, const int mode)
{
static CvCapture *capture;
IplImage *frame;
static int init=1;
raster_t im_local={0};
if (init)
{
init = 0;
capture = cvCreateCameraCapture(0);
if (capture==NULL)
return 0;
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 1280.);
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 960.);
}
frame = cvQueryFrame(capture);
if (image==NULL)
image = &im_local;
convert_cvimage_to_raster(frame, image, mode);
if (debug_str)
sprintf(debug_str, "%gx%g, %.3g FPS, %g, %g",
cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT),
cvGetCaptureProperty(capture, CV_CAP_PROP_FPS),
cvGetCaptureProperty(capture, CV_CAP_PROP_GAIN),
cvGetCaptureProperty(capture, CV_CAP_PROP_EXPOSURE),
0);
//cvReleaseCapture(&capture);
return 1;
}
int get_video_frame(raster_t *image, char *path, const int mode)
{
static CvCapture *capture;
IplImage *frame;
static int init=1;
if (init)
{
init = 0;
capture = cvCaptureFromFile(path);
if (capture==NULL)
return 0;
}
frame = cvQueryFrame(capture);
convert_cvimage_to_raster(frame, image, mode);
//cvReleaseCapture(&capture); // TODO close it at some point
return 1;
}
void write_video_frame(raster_t *image, char *path, double fps, int action) // FIXME can only handle one video file at a time
{
static CvVideoWriter *writer=NULL;
static IplImage *frame=NULL;
return ;
if (action==0 && path) // initiate new file
{
if (writer)
{
cvReleaseVideoWriter(writer);
cvReleaseImage(frame);
}
writer = cvCreateVideoWriter(path, CV_FOURCC('X','V','I','D'), fps, cvSize(image->dim.x, image->dim.y), 1);
frame = cvCreateImage(cvSize(image->dim.x, image->dim.y), IPL_DEPTH_8U, 3);
}
if (action==2 && writer) // close and clean up
{
cvReleaseImage(frame); // FIXME crashes
frame = NULL;
cvReleaseVideoWriter(writer);
writer = NULL;
}
if (action==1 && writer)
{
convert_srgb_to_cvimage(image, frame);
cvWriteFrame(writer, frame);
}
}
#endif