-
Notifications
You must be signed in to change notification settings - Fork 3
/
GenCameraDriver.cpp
331 lines (311 loc) · 10.7 KB
/
GenCameraDriver.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
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
/**
@brief Generic Camera Driver Class
@author Shane Yuan
@date Dec 29, 2017
*/
#include "GenCameraDriver.h"
#include <time.h>
#include <algorithm>
#include <functional> // std::minus
#include <numeric> // std::accumulate
// include NPPJpegCoder
#include "NPPJpegCoder.h"
namespace cam {
GenCamera::GenCamera() : isInit(false), isCapture(false),
isVerbose(false), bufferType(GenCamBufferType::Raw),
camPurpose(GenCamCapturePurpose::Streaming),
JPEGQuality(75), sizeRatio(0.12) {}
GenCamera::~GenCamera() {}
/**
@brief set verbose
@param bool isVerbose: true, verbose mode, output many infomations
for debugging
@return int
*/
int GenCamera::setVerbose(bool isVerbose) {
this->isVerbose = isVerbose;
return 0;
}
/**
@brief set buffer type
@param GenCamBufferType type: buffer type
@return int
*/
int GenCamera::setCamBufferType(GenCamBufferType type) {
this->bufferType = type;
return 0;
}
/**
@brief set jpeg compression quality
@param int quality: JPEG compression quality (1 - 100)
@param float sizeRatio: expected compression ratio used for
pre-malloc memory
@return int
*/
int GenCamera::setJPEGQuality(int quality, float sizeRatio) {
this->JPEGQuality = quality;
this->sizeRatio = sizeRatio;
return 0;
}
/**
@brief set capture purpose
@param GenCamCapturePurpose camPurpose: purpose, for streaming or recording
@return int
*/
int GenCamera::setCapturePurpose(GenCamCapturePurpose camPurpose) {
this->camPurpose = camPurpose;
return 0;
}
/**
@brief get capture purpose
@param GenCamCapturePurpose camPurpose: purpose, for streaming or recording
@return int
*/
GenCamCapturePurpose GenCamera::getCapturePurpose() {
return this->camPurpose;
}
/*************************************************************/
/* function to save capture images to files */
/*************************************************************/
/**
@brief save captured images to dir
@param std::string dir: input dir to save images
@return int
*/
int GenCamera::saveImages(std::string dir) {
if (this->bufferType == GenCamBufferType::JPEG) {
std::vector<cam::GenCamInfo> _camInfos;
this->getCamInfos(_camInfos);
SysUtil::mkdir(dir);
for (size_t i = 0; i < this->cameraNum; i++) {
// init npp jpeg coder
npp::NPPJpegCoder coder;
coder.init(camInfos[i].width, camInfos[i].height, JPEGQuality);
cv::cuda::GpuMat img_d(camInfos[i].height, camInfos[i].width, CV_8UC3);
cv::Mat img(camInfos[i].height, camInfos[i].width, CV_8UC3);
for (size_t j = 0; j < this->bufferSize; j++) {
char outname[256];
coder.decode(reinterpret_cast<uchar*>(this->bufferImgs[j][i].data),
this->bufferImgs[j][i].length,
img_d, 0);
img_d.download(img);
sprintf(outname, "%s/%s_%02d_%05d.png", dir.c_str(), _camInfos[i].sn.c_str(), i, j);
cv::imwrite(outname, img);
}
}
}
else {
std::vector<cam::GenCamInfo> _camInfos;
this->getCamInfos(_camInfos);
SysUtil::mkdir(dir);
for (size_t i = 0; i < this->cameraNum; i++) {
for (size_t j = 0; j < this->bufferSize; j++) {
cv::cuda::GpuMat bayer_img_d(camInfos[i].height, camInfos[i].width, CV_8U);
cv::cuda::GpuMat rgb_img_mat_d(camInfos[i].height, camInfos[i].width, CV_8UC3);
cv::Mat bayer_img(camInfos[i].height, camInfos[i].width, CV_8U,
reinterpret_cast<uchar*>(this->bufferImgs[j][i].data));
cv::Mat rgb_img_mat;
bayer_img_d.upload(bayer_img);
cv::cuda::demosaicing(bayer_img_d, rgb_img_mat_d,
npp::bayerPatternNPP2CVBGR(static_cast<NppiBayerGridPosition>(
_camInfos[i].bayerPattern)), -1);
rgb_img_mat_d.download(rgb_img_mat);
char outname[256];
sprintf(outname, "%s/%s_%02d_%05d.png", dir.c_str(), _camInfos[i].sn.c_str(), i, j);
cv::imwrite(outname, rgb_img_mat);
}
}
}
return 0;
}
/**
@brief save captured videos to dir
@param std::string dir: input dir to save videos
@return int
*/
int GenCamera::saveVideos(std::string dir) {
if (this->bufferType == GenCamBufferType::JPEG) {
SysUtil::mkdir(dir);
for (size_t i = 0; i < this->cameraNum; i++) {
// init npp jpeg coder
npp::NPPJpegCoder coder;
coder.init(camInfos[i].width, camInfos[i].height, JPEGQuality);
// init video parameter
std::string videoname = cv::format("%s/cam_%02d.avi", dir.c_str(), i);
cv::VideoWriter writer(videoname, cv::VideoWriter::fourcc('D', 'I', 'V', 'X'),
camInfos[i].fps, cv::Size(camInfos[i].width, camInfos[i].height), true);
cv::cuda::GpuMat img_d(camInfos[i].height, camInfos[i].width, CV_8UC3);
cv::Mat img(camInfos[i].height, camInfos[i].width, CV_8UC3);
for (size_t j = 0; j < this->bufferSize; j++) {
coder.decode(reinterpret_cast<uchar*>(this->bufferImgs[j][i].data),
this->bufferImgs[j][i].length,
img_d, 0);
img_d.download(img);
writer << img;
//char outname[256];
//sprintf(outname, "%s/%02d_%05d.jpg", dir.c_str(), i, j);
//cv::imwrite(outname, img);
}
writer.release();
// release npp jpeg coder
coder.release();
}
}
else {
std::vector<cam::GenCamInfo> _camInfos;
this->getCamInfos(_camInfos);
SysUtil::mkdir(dir);
for (size_t i = 0; i < this->cameraNum; i++) {
// init video parameter
std::string videoname = cv::format("%s/cam_%02d.avi", dir.c_str(), i);
cv::VideoWriter writer(videoname, cv::VideoWriter::fourcc('D', 'I', 'V', 'X'),
camInfos[i].fps, cv::Size(camInfos[i].width, camInfos[i].height), true);
for (size_t j = 0; j < this->bufferSize; j++) {
cv::cuda::GpuMat bayer_img_d(camInfos[i].height, camInfos[i].width, CV_8U);
cv::cuda::GpuMat rgb_img_mat_d(camInfos[i].height, camInfos[i].width, CV_8UC3);
cv::Mat bayer_img(camInfos[i].height, camInfos[i].width, CV_8U,
reinterpret_cast<uchar*>(this->bufferImgs[j][i].data));
cv::Mat rgb_img_mat;
bayer_img_d.upload(bayer_img);
cv::cuda::demosaicing(bayer_img_d, rgb_img_mat_d,
npp::bayerPatternNPP2CVBGR(static_cast<NppiBayerGridPosition>(
_camInfos[i].bayerPattern)), -1);
rgb_img_mat_d.download(rgb_img_mat);
writer << rgb_img_mat;
}
writer.release();
}
}
return 0;
}
/*************************************************************/
/* function to set mapping vector of capture function */
/* and function to capture images */
/* old function will be deprecated in the future */
/*************************************************************/
/**
@brief set mapping vector of capture function
@param std::vector<size_t> mappingVector: input mapping vector
@return int
*/
int GenCamera::setMappingVector(std::vector<size_t> mappingVector) {
this->mappingVector = mappingVector;
return 0;
}
/**
@brief capture one frame
@param std::vector<Imagedata> & imgs: output captured images
if in single mode, memory of image mats should be malloced
before using this function
@return int
*/
int GenCamera::captureFrame(std::vector<Imagedata> & imgs) {
if (captureMode == GenCamCaptureMode::Continous ||
captureMode == GenCamCaptureMode::ContinousTrigger) {
// get images from buffer
for (size_t camInd = 0; camInd < this->cameraNum; camInd++) {
int index = (thBufferInds[camInd] - 1 + bufferSize) % bufferSize;
imgs[camInd] = bufferImgs[index][camInd];
}
// increase buffer indices for file camera
if (this->camModel == cam::CameraModel::File) {
for (size_t camInd = 0; camInd < this->cameraNum; camInd++) {
thBufferInds[camInd] = (thBufferInds[camInd] + 1) % bufferSize;
}
}
}
else if (captureMode == GenCamCaptureMode::Single ||
captureMode == GenCamCaptureMode::SingleTrigger) {
SysUtil::errorOutput("Single mode is not implemented yet !");
exit(-1);
}
return 0;
}
/**
@brief capture one frame with Mapping
@param std::vector<Imagedata> & imgs: output captured images
if in single mode, memory of image mats should be malloced
before using this function
@return int
*/
int GenCamera::captureFrameWithMapping(std::vector<Imagedata> & imgs) {
size_t camInd;
if (captureMode == GenCamCaptureMode::Continous ||
captureMode == GenCamCaptureMode::ContinousTrigger) {
// get images from buffer
for (size_t i = 0; i < this->cameraNum; i++) {
camInd = mappingVector[i];
int index = (thBufferInds[camInd] - 1 + bufferSize) % bufferSize;
imgs[i] = bufferImgs[index][camInd];
}
}
else if (captureMode == GenCamCaptureMode::Single ||
captureMode == GenCamCaptureMode::SingleTrigger) {
SysUtil::errorOutput("Single mode is not implemented yet !");
exit(-1);
}
return 0;
}
/**
@brief get camera infos list with mapping
@param std::vector<cam::GenCamInfo> & camInfos: output camera info list
@return int
*/
int GenCamera::getCameraInfoListsWithMapping(std::vector<cam::GenCamInfo> & camInfos) {
size_t camInd;
// get camera infos without mapping
std::vector<cam::GenCamInfo> camInfosWoMapping;
this->getCamInfos(camInfosWoMapping);
camInfos.clear();
camInfos.resize(this->cameraNum);
for (size_t i = 0; i < this->cameraNum; i++) {
camInd = mappingVector[i];
camInfos[i] = camInfosWoMapping[camInd];
}
return 0;
}
/*************************************************************/
/* function to capture images */
/*************************************************************/
/**
@brief capture one frame
@param std::vector<Imagedata> & refImgs: output reference images
@param std::vector<Imagedata> & localImgs: output localview images
@param std::vector<int> refInds: input reference indices
@param std::vector<int> localInds: input local indices
@return int
*/
int GenCamera::captureFrame(std::vector<Imagedata> & refImgs,
std::vector<Imagedata> & localImgs,
std::vector<int> refInds,
std::vector<int> localInds) {
size_t camInd;
if (captureMode == GenCamCaptureMode::Continous ||
captureMode == GenCamCaptureMode::ContinousTrigger) {
// get refernce images from buffer
for (size_t i = 0; i < refInds.size(); i++) {
camInd = refInds[i];
int index = (thBufferInds[camInd] - 1 + bufferSize) % bufferSize;
refImgs[i] = bufferImgs[index][camInd];
}
// get local images from buffer
for (size_t i = 0; i < localInds.size(); i++) {
camInd = localInds[i];
int index = (thBufferInds[camInd] - 1 + bufferSize) % bufferSize;
localImgs[i] = bufferImgs[index][camInd];
}
// increase buffer indices for file camera
if (this->camModel == cam::CameraModel::File) {
for (size_t camInd = 0; camInd < this->cameraNum; camInd++) {
thBufferInds[camInd] = (thBufferInds[camInd] + 1) % bufferSize;
}
}
}
else if (captureMode == GenCamCaptureMode::Single ||
captureMode == GenCamCaptureMode::SingleTrigger) {
SysUtil::errorOutput("Single mode is not implemented yet !");
exit(-1);
}
return 0;
}
}