forked from zk00006/OpenTLD
-
Notifications
You must be signed in to change notification settings - Fork 194
Expand file tree
/
Copy pathImAcq.cpp
More file actions
289 lines (232 loc) · 6.9 KB
/
Copy pathImAcq.cpp
File metadata and controls
289 lines (232 loc) · 6.9 KB
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
/* Copyright 2011 AIT Austrian Institute of Technology
*
* This file is part of OpenTLD.
*
* OpenTLD is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OpenTLD is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OpenTLD. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "ImAcq.h"
#include <cstdio>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
#include <windows.h>
#else
#include <unistd.h>
#endif
static void msleep(int milliseconds)
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32)
Sleep(milliseconds);
#else
usleep(static_cast<useconds_t>(milliseconds)*1000);
#endif
}
ImAcq *imAcqAlloc()
{
ImAcq *imAcq = (ImAcq *)malloc(sizeof(ImAcq));
// imAcq->method = IMACQ_CAM;
imAcq->method = 2;
imAcq->currentFrame = 1;
imAcq->lastFrame = 0;
imAcq->camNo = 0;
imAcq->fps = 24;
return imAcq;
}
void imAcqInit(ImAcq *imAcq)
{
// imAcq->method = 2;
if(imAcq->method == IMACQ_CAM)
{
imAcq->capture = cvCaptureFromCAM(imAcq->camNo);
if(imAcq->capture == NULL)
{
printf("Error: Unable to initialize camera\n");
exit(0);
}
}
else if(imAcq->method == IMACQ_VID)
{
imAcq->capture = cvCaptureFromAVI(imAcq->imgPath);
//imAcq->capture = cvCaptureFromAVI("D:\\OpenTLD-master\\build\\bin\\Release\\IMG_5903.MOV");
if(imAcq->capture == NULL)
{
printf("Error: Unable to open video\n");
exit(0);
}
// take all frames
if(imAcq->lastFrame == 0)
imAcq->lastFrame = imAcqVidGetNumberOfFrames(imAcq); //This sometimes returns garbage
// lastFrame out of bounds
if(imAcq->lastFrame > imAcqVidGetNumberOfFrames(imAcq))
{
printf("Error: video has only %d frames you selected %d as last frame.\n",
imAcqVidGetNumberOfFrames(imAcq), imAcq->lastFrame);
exit(1);
}
// something is wrong with startFrame and/or lastFrame
if((imAcq->lastFrame < 1) || (imAcq->currentFrame < 1) || ((imAcq->currentFrame > imAcq->lastFrame)))
{
printf("Error: something is wrong with the start and last frame number. startFrame: %d lastFrame: %d\n",
imAcq->currentFrame, imAcq->lastFrame);
exit(1);
}
// set the video position to the correct frame
//This produces strange results on some videos and is deactivated for now.
//imAcqVidSetNextFrameNumber(imAcq, imAcq->currentFrame);
}
else if(imAcq->method == IMACQ_STREAM)
{
imAcq->capture = cvCaptureFromFile(imAcq->imgPath);
if(imAcq->capture == NULL)
{
printf("Error: Unable to open video\n");
exit(0);
}
}
imAcq->startFrame = imAcq->currentFrame;
imAcq->startTime = cvGetTickCount();
}
void imAcqFree(ImAcq *imAcq)
{
if((imAcq->method == IMACQ_CAM) || (imAcq->method == IMACQ_VID) || (imAcq->method == IMACQ_STREAM))
{
cvReleaseCapture(&imAcq->capture);
}
free(imAcq);
}
IplImage *imAcqLoadImg(ImAcq *imAcq, char *path)
{
IplImage *image = cvLoadImage(path);
if(image == NULL)
{
printf("Error: %s does not exist or is not an image.\n", path);
}
return image;
}
IplImage *imAcqLoadFrame(ImAcq *imAcq, int fNo)
{
char path[255];
sprintf(path, imAcq->imgPath, fNo);
printf("load path %s\n", path);
return cvLoadImage(path);
}
IplImage *imAcqLoadCurrentFrame(ImAcq *imAcq)
{
return imAcqLoadFrame(imAcq, imAcq->currentFrame);
}
IplImage *imAcqGetImgByCurrentTime(ImAcq *imAcq)
{
//Calculate current image number
if((imAcq->method == IMACQ_CAM) || (imAcq->method == IMACQ_STREAM))
{
//printf("grabbing image from sensor");
return imAcqGrab(imAcq->capture);
}
float secondsPassed = (cvGetTickCount() - imAcq->startTime) / cvGetTickFrequency();
secondsPassed = secondsPassed / 1000000;
int framesPassed = secondsPassed * imAcq->fps;
int currentFrame = imAcq->startFrame + framesPassed;
if(imAcq->lastFrame > 0 && currentFrame > imAcq->lastFrame) return NULL;
return imAcqLoadFrame(imAcq, currentFrame);
}
IplImage *imAcqGetImg(ImAcq *imAcq)
{
IplImage *img = NULL;
if(imAcq->method == IMACQ_CAM || imAcq->method == IMACQ_VID)
{
img = imAcqGrab(imAcq->capture);
}
if(imAcq->method == IMACQ_IMGS)
{
img = imAcqLoadCurrentFrame(imAcq);
}
if((imAcq->method == IMACQ_LIVESIM) || (imAcq->method == IMACQ_STREAM))
{
img = imAcqGetImgByCurrentTime(imAcq);
}
imAcqAdvance(imAcq);
return img;
}
IplImage *imAcqGrab(CvCapture *capture)
{
IplImage *frame;
frame = cvQueryFrame(capture);
if(frame == NULL)
{
// Sometimes the camera driver needs some time to start
// sleep 100ms and try again
for(int i = 0; i < 5; ++i)
{
printf("Error: Unable to grab image... retry\n");
msleep(100);
frame = cvQueryFrame(capture);
if(frame != NULL)
{
break;
}
}
if(frame == NULL)
{
exit(-1);
}
}
return cvCloneImage(frame);
}
IplImage *imAcqGetImgByFrame(ImAcq *imAcq, int fNo)
{
int oldFNo = imAcq->currentFrame;
imAcq->currentFrame = fNo;
IplImage *img = imAcqGetImg(imAcq);
imAcq->currentFrame = oldFNo;
return img;
}
IplImage *imAcqGetImgAndAdvance(ImAcq *imAcq)
{
IplImage *img = imAcqGetImg(imAcq);
imAcq->currentFrame++;
return img;
}
void imAcqAdvance(ImAcq *imAcq)
{
imAcq->currentFrame++;
}
int imAcqHasMoreFrames(ImAcq *imAcq)
{
if(imAcq->lastFrame < 1) return 1;
if(imAcq->currentFrame > imAcq->lastFrame)
{
return 0;
}
else
{
return 1;
}
}
int imAcqVidGetNextFrameNumber(ImAcq *imAcq)
{
// OpenCV index starts with 0
// maybe a OpenCV bug: cvGetCaptureProperty with CV_CAP_PROP_POS_FRAMES returns the LAST
// frame number to be encoded not the NEXT
return ((int) cvGetCaptureProperty(imAcq->capture , CV_CAP_PROP_POS_FRAMES)) + 2;
}
void imAcqVidSetNextFrameNumber(ImAcq *imAcq, int nFrame)
{
// OpenCV index starts with 0
cvSetCaptureProperty(imAcq->capture , CV_CAP_PROP_POS_FRAMES, nFrame - 2.0);
}
int imAcqVidGetNumberOfFrames(ImAcq *imAcq)
{
return ((int) cvGetCaptureProperty(imAcq->capture , CV_CAP_PROP_FRAME_COUNT));
}