-
Notifications
You must be signed in to change notification settings - Fork 6
/
IDSVideoCapture.cpp
177 lines (133 loc) · 3.41 KB
/
IDSVideoCapture.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
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
#include "IDSVideoCapture.h"
#include <opencv2/core.hpp>
IDSVideoCapture::IDSVideoCapture() {
}
IDSVideoCapture::IDSVideoCapture(int index) {
open(index);
}
IDSVideoCapture::~IDSVideoCapture() {
release();
}
bool IDSVideoCapture::open(int _index) {
HIDS index = _index;
// Release any existing resources.
release();
// Ask API to open camera.
if (is_InitCamera(&index, NULL) == IS_SUCCESS) {
// If success set the camera ID field and fetch camera and sensor info
m_CamId = index;
is_GetCameraInfo(m_CamId, &m_CameraInfo);
is_GetSensorInfo(m_CamId, &m_SensorInfo);
// Use maximum dimensions
m_Width = m_SensorInfo.nMaxWidth;
m_Height = m_SensorInfo.nMaxHeight;
// Allocate memory for API to load frames into
for (int i = 0; i < NUM_BUFFERS; ++i) {
is_AllocImageMem(
m_CamId,
m_SensorInfo.nMaxWidth,
m_SensorInfo.nMaxHeight,
8 * 3,
&m_ImageMemoryAddr[i],
&m_ImageMemoryId[i]
);
is_AddToSequence(
m_CamId,
m_ImageMemoryAddr[i],
m_ImageMemoryId[i]
);
}
// Tell API to load frames into memory and set the format
is_SetDisplayMode(m_CamId, IS_SET_DM_DIB);
is_SetColorMode(m_CamId, IS_CM_BGR8_PACKED);
// Initialise start time and start the camera running
m_StartTime = time(NULL);
is_CaptureVideo(m_CamId, IS_DONT_WAIT);
}
return isOpened();
}
void IDSVideoCapture::release() {
if (isOpened()) {
// Stop the camera
is_StopLiveVideo(m_CamId, IS_FORCE_VIDEO_STOP);
// Release memory
is_ClearSequence(m_CamId);
for (int i = 0; i < NUM_BUFFERS; ++i) {
if (m_ImageMemoryAddr[i] != nullptr) {
is_FreeImageMem(m_CamId, m_ImageMemoryAddr[i], m_ImageMemoryId[i]);
}
}
// Close the camera handle
is_ExitCamera(m_CamId);
m_CamId = 0;
}
}
bool IDSVideoCapture::isOpened() const {
return m_CamId > 0;
}
bool IDSVideoCapture::grab() {
if (isOpened()) {
// Unlock image memory to allow the API to write to the previously
// locked buffer
if (m_LockedMemory != nullptr) {
is_UnlockSeqBuf(m_CamId, IS_IGNORE_PARAMETER, m_LockedMemory);
}
// Grab the last (and not current) image buffer
is_GetActSeqBuf(m_CamId, NULL, NULL, &m_LockedMemory);
is_LockSeqBuf(m_CamId, IS_IGNORE_PARAMETER, m_LockedMemory);
return true;
}
else {
return false;
}
}
bool IDSVideoCapture::retrieve(OutputArray image, int flag) {
if (isOpened()) {
// Initialise Mat using locked memory and then duplicate into output
cv::Mat ref(m_Height, m_Width, CV_8UC3, (void*)m_LockedMemory);
ref.copyTo(image);
return true;
}
else {
return false;
}
}
double IDSVideoCapture::get(int propid) const {
switch (propid) {
case CAP_PROP_POS_MSEC: {
return (double)(time(NULL) - m_StartTime) * 1e2;
}
case CAP_PROP_POS_FRAMES: {
long count;
is_GetVsyncCount(m_CamId, &count, NULL);
return (double)count;
}
case CAP_PROP_POS_AVI_RATIO:
return 0.0;
case CAP_PROP_FRAME_WIDTH:
return m_Width;
case CAP_PROP_FRAME_HEIGHT:
return m_Height;
case CAP_PROP_FPS: {
double fps;
is_GetFramesPerSecond(m_CamId, &fps);
return fps;
}
case CAP_PROP_FOURCC:
return CAP_FFMPEG;
case CAP_PROP_FORMAT:
return CV_8UC3;
}
return 0.0;
}
bool IDSVideoCapture::set(int propId, double value) {
switch (propId) {
/*case CAP_PROP_FRAME_WIDTH:
setResolution((int)value, m_Height);
return true;
case CAP_PROP_FRAME_HEIGHT:
setResolution(m_Width, (int)value);
return true;*/
}
return false;
}