-
Notifications
You must be signed in to change notification settings - Fork 23
/
CameraV4l2.cpp
1942 lines (1356 loc) · 53.3 KB
/
CameraV4l2.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
CameraV4l2.cpp
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
* This file is part of: freeture
*
* Copyright: (C) 2014-2015 Yoan Audureau
* FRIPON-GEOPS-UPSUD-CNRS
*
* License: GNU General Public License
*
* FreeTure 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.
* FreeTure 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 FreeTure. If not, see <http://www.gnu.org/licenses/>.
*
* Last modified: 17/08/2015
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
/**
* \file CameraV4l2.cpp
* \author Yoan Audureau -- FRIPON-GEOPS-UPSUD
* \version 1.0
* \date 17/08/2015
*/
#include "CameraV4l2.h"
#ifdef LINUX
enum io_method {
IO_METHOD_READ,
IO_METHOD_MMAP,
IO_METHOD_USERPTR,
};
struct buffer
{
void *start;
size_t length;
};
struct v4l2_buffer buf;
enum io_method io = IO_METHOD_MMAP;
struct buffer *buffers = NULL;
unsigned int n_buffers;
int out_buf = 1;
int frame_count = 10;
int frame_number = 0;
boost::log::sources::severity_logger< LogSeverityLevel > CameraV4l2::logger;
CameraV4l2::Init CameraV4l2::initializer;
CameraV4l2::CameraV4l2(){
io_method io = IO_METHOD_MMAP;
fd = -1;
out_buf = 1;
frame_count = 10;
frame_number = 0;
expMin = 0;
expMax = 0;
exp =0;
gain = 0;
gainMin = 0;
gainMax = 0;
mFrameCounter = 0;
mWidth = 640;
mHeight = 480;
n_buffers = 3;
mExposureAvailable = true;
mGainAvailable = true;
mCustomSize = false;
mInputDeviceType = CAMERA;
}
CameraV4l2::~CameraV4l2(){
}
bool CameraV4l2::getInfos() {
struct v4l2_capability caps = {};
// http://linuxtv.org/downloads/v4l-dvb-apis/vidioc-querycap.html
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &caps)) {
perror("Querying Capabilities");
return false;
}
cout << "Driver name : " << caps.driver << endl;
cout << "Device name : " << caps.card << endl;
cout << "Device location : " << caps.bus_info << endl;
printf ("Driver version : %u.%u.%u\n",(caps.version >> 16) & 0xFF, (caps.version >> 8) & 0xFF, caps.version & 0xFF);
cout << "Capabilities : " << caps.capabilities << endl;
struct v4l2_cropcap cropcap;
memset(&cropcap, 0, sizeof(cropcap));
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
perror("Querying Cropping Capabilities");
return false;
}
printf( "Camera Cropping :\n"
" Bounds : %dx%d+%d+%d\n"
" Default : %dx%d+%d+%d\n"
" Aspect : %d/%d\n",
cropcap.bounds.width, cropcap.bounds.height, cropcap.bounds.left, cropcap.bounds.top,
cropcap.defrect.width, cropcap.defrect.height, cropcap.defrect.left, cropcap.defrect.top,
cropcap.pixelaspect.numerator, cropcap.pixelaspect.denominator);
int support_grbg10 = 0;
struct v4l2_fmtdesc fmtdesc = {0};
fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
char fourcc[5] = {0};
char c, e;
printf( " FORMAT : CE Desc\n");
while (0 == xioctl(fd, VIDIOC_ENUM_FMT, &fmtdesc)) {
strncpy(fourcc, (char *)&fmtdesc.pixelformat, 4);
if (fmtdesc.pixelformat == V4L2_PIX_FMT_SGRBG10)
support_grbg10 = 1;
c = fmtdesc.flags & 1? 'C' : ' ';
e = fmtdesc.flags & 2? 'E' : ' ';
printf(" %s : %c%c %s\n", fourcc, c, e, fmtdesc.description);
fmtdesc.index++;
}
/*struct v4l2_format fmt = {0};
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
fmt.fmt.pix.width = 640;
fmt.fmt.pix.height = 480;
//fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24;
//fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_GREY;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_MJPEG;
fmt.fmt.pix.field = V4L2_FIELD_NONE;
if (-1 == xioctl(fd, VIDIOC_S_FMT, &fmt)) {
perror("Setting Pixel Format");
return false;
}
strncpy(fourcc, (char *)&fmt.fmt.pix.pixelformat, 4);
printf( "Selected mode :\n"
" Width : %d\n"
" Height : %d\n"
" PixFmt : %s\n"
" Field : %d\n",
fmt.fmt.pix.width,
fmt.fmt.pix.height,
fourcc,
fmt.fmt.pix.field);*/
double eMin, eMax; int gMin, gMax;
getExposureBounds(eMin, eMax);
cout << "Min exposure : " << eMin << endl;
cout << "Max exposure : " << eMax << endl;
getGainBounds(gMin, gMax);
cout << "Min gain : " << gMin << endl;
cout << "Max gain : " << gMax << endl;
return true;
};
vector<pair<int,string>> CameraV4l2::getCamerasList() {
vector<pair<int,string>> camerasList;
bool loop = true;
bool res = true;
int deviceNumber = 0;
do {
string devicePathStr = "/dev/video" + Conversion::intToString(deviceNumber);
// http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform
if(access(devicePathStr.c_str(), F_OK) != -1 ) {
// file exists
// http://stackoverflow.com/questions/4290834/how-to-get-a-list-of-video-capture-devices-web-cameras-on-linux-ubuntu-c
int fd;
if((fd = open(devicePathStr.c_str(), O_RDONLY)) == -1){
perror("Can't open device");
res = false;
}else {
struct v4l2_capability caps = {};
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &caps)) {
cout << "Fail Querying Capabilities." << endl;
perror("Querying Capabilities");
res = false;
}else {
pair<int,string> c;
c.first = deviceNumber;
std::string s( reinterpret_cast< char const* >(caps.card) ) ;
c.second = "NAME[" + s + "] SDK[V4L2]";
camerasList.push_back(c);
}
}
close(fd);
deviceNumber++;
} else {
loop = false;
}
}while(loop);
return camerasList;
}
bool CameraV4l2::listCameras() {
bool loop = true;
bool res = true;
int deviceNumber = 0;
cout << endl << "------------ USB2 CAMERAS WITH V4L2 ----------" << endl << endl;
do {
string devicePathStr = "/dev/video" + Conversion::intToString(deviceNumber);
// http://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform
if(access(devicePathStr.c_str(), F_OK) != -1 ) {
// file exists
// http://stackoverflow.com/questions/4290834/how-to-get-a-list-of-video-capture-devices-web-cameras-on-linux-ubuntu-c
int fd;
if((fd = open(devicePathStr.c_str(), O_RDONLY)) == -1){
perror("Can't open device");
res = false;
}else {
struct v4l2_capability caps = {};
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &caps)) {
perror("Querying Capabilities");
res = false;
}else {
cout << "-> [" << deviceNumber << "] " << caps.card << endl;
}
}
close(fd);
deviceNumber++;
} else {
// file doesn't exist
if(deviceNumber == 0)
cout << "-> No cameras detected ..." << endl;
loop = false;
}
}while(loop);
cout << endl << "------------------------------------------------" << endl << endl;
return res;
}
bool CameraV4l2::createDevice(int id){
string deviceNameStr = "/dev/video" + Conversion::intToString(id);
mDeviceName = deviceNameStr.c_str();
struct stat st;
if (-1 == stat(mDeviceName, &st)) {
fprintf(stderr, "Cannot identify '%s': %d, %s\n", mDeviceName, errno, strerror(errno));
return false;
}
if (!S_ISCHR(st.st_mode)) {
fprintf(stderr, "%s is no device\n", mDeviceName);
return false;
}
fd = open(mDeviceName, O_RDWR /* required */ | O_NONBLOCK, 0);
if (-1 == fd) {
fprintf(stderr, "Cannot open '%s': %d, %s\n", mDeviceName, errno, strerror(errno));
return false;
}
getExposureBounds(expMin, expMax);
getGainBounds(gainMin, gainMax);
memset(&mFormat, 0, sizeof(mFormat));
mFormat.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
// Preserve original settings as set by v4l2-ctl for example
if (-1 == xioctl(fd, VIDIOC_G_FMT, &mFormat)){
return false;
}
return true;
}
bool CameraV4l2::setSize(int width, int height, bool customSize) {
mWidth = width;
mHeight = height;
mCustomSize = customSize;
return true;
}
// if customSize = true --> set width and height values passed in argument
// if customSize = false --> set maximum size
bool CameraV4l2::setSize() {
int chooseWidth = 0;
int chooseHeight = 0;
bool discreteSize = false;
bool res = false;
struct v4l2_frmsizeenum frmsize;
memset(&frmsize, 0, sizeof(frmsize));
frmsize.pixel_format = mFormat.fmt.pix.pixelformat; // Necessary to set size.
while(ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &frmsize) == 0) {
switch(frmsize.type) {
case V4L2_FRMSIZE_TYPE_DISCRETE :
if(chooseHeight == 0 && chooseWidth == 0) {
chooseHeight = frmsize.discrete.height;
chooseWidth = frmsize.discrete.width;
}else {
if((abs(mWidth - chooseWidth) > abs(mWidth - frmsize.discrete.width)) && (abs(mHeight - chooseHeight) > abs(mHeight - frmsize.discrete.height))) {
chooseWidth = frmsize.discrete.width;
chooseHeight = frmsize.discrete.height;
}
}
discreteSize = true;
res = true;
break;
case V4L2_FRMSIZE_TYPE_CONTINUOUS :
break;
case V4L2_FRMSIZE_TYPE_STEPWISE :
if(mCustomSize) {
if(mWidth >= frmsize.stepwise.min_width && mWidth <=frmsize.stepwise.max_width) {
mFormat.fmt.pix.width = mWidth;
}else {
mFormat.fmt.pix.width = frmsize.stepwise.max_width;
}
if(mHeight >= frmsize.stepwise.min_height && mHeight <=frmsize.stepwise.max_height) {
mFormat.fmt.pix.height = mHeight;
}else {
mFormat.fmt.pix.height = frmsize.stepwise.max_height;
}
}else {
mFormat.fmt.pix.height = frmsize.stepwise.max_height;
mFormat.fmt.pix.width = frmsize.stepwise.max_width;
}
res = true;
break;
}
frmsize.index++;
}
if(discreteSize && res) {
mFormat.fmt.pix.height = chooseHeight;
mFormat.fmt.pix.width = chooseWidth;
}
return res;
}
bool CameraV4l2::getDeviceNameById(int id, string &device){
return false;
}
bool CameraV4l2::getCameraName() {
if(fd != -1) {
struct v4l2_capability caps = {};
// http://linuxtv.org/downloads/v4l-dvb-apis/vidioc-querycap.html
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &caps)) {
perror("Querying Capabilities");
return false;
}
cout << "Driver name : " << caps.driver << endl;
cout << "Device name : " << caps.card << endl;
cout << "Device location : " << caps.bus_info << endl;
printf ("Driver version : %u.%u.%u\n",(caps.version >> 16) & 0xFF, (caps.version >> 8) & 0xFF, caps.version & 0xFF);
cout << "Capabilities : " << caps.capabilities << endl;
return true;
}
return false;
}
bool CameraV4l2::grabInitialization(){
struct v4l2_capability cap;
struct v4l2_cropcap cropcap;
struct v4l2_crop crop;
unsigned int min;
if (-1 == xioctl(fd, VIDIOC_QUERYCAP, &cap))
{
if (EINVAL == errno)
{
fprintf(stderr,
"%s is no V4L2 device\n",
mDeviceName);
exit(EXIT_FAILURE);
}
else
{
errno_exit("VIDIOC_QUERYCAP");
}
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE))
{
fprintf(stderr,
"%s is no video capture device\n",
mDeviceName);
exit(EXIT_FAILURE);
}
switch (io)
{
case IO_METHOD_READ:
{
if (!(cap.capabilities & V4L2_CAP_READWRITE))
{
fprintf(stderr,
"%s does not support read i/o\n",
mDeviceName);
exit(EXIT_FAILURE);
}
break;
}
case IO_METHOD_MMAP:
case IO_METHOD_USERPTR:
{
if (!(cap.capabilities & V4L2_CAP_STREAMING))
{
fprintf(stderr, "%s does not support streaming i/o\n",
mDeviceName);
exit(EXIT_FAILURE);
}
break;
}
}
// Select video input, video standard and tune here.
memset(&cropcap, 0, sizeof(cropcap));
cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (0 == xioctl(fd, VIDIOC_CROPCAP, &cropcap)) {
crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
crop.c = cropcap.defrect; // reset to default
if (-1 == xioctl(fd, VIDIOC_S_CROP, &crop)) {
switch (errno) {
case EINVAL:
// Cropping not supported.
break;
default:
// Errors ignored.
break;
}
}
} else {
// Errors ignored.
}
// Set some parameters...SIZE
if(!setSize())
return false;
if(-1 == xioctl(fd, VIDIOC_S_FMT, &mFormat)) {
cout << "Fail to set fmt." << endl;
return false;
}
/* Buggy driver paranoia. */
min = mFormat.fmt.pix.width * 2;
if (mFormat.fmt.pix.bytesperline < min)
mFormat.fmt.pix.bytesperline = min;
min = mFormat.fmt.pix.bytesperline * mFormat.fmt.pix.height;
if (mFormat.fmt.pix.sizeimage < min)
mFormat.fmt.pix.sizeimage = min;
return true;
}
void CameraV4l2::grabCleanse(){
// Uninit device
unsigned int i;
if(buffers != NULL) {
switch (io) {
case IO_METHOD_READ:
free(buffers[0].start);
break;
case IO_METHOD_MMAP:
for (i = 0; i < n_buffers; ++i)
if (-1 == munmap(buffers[i].start, buffers[i].length))
errno_exit("munmap");
break;
case IO_METHOD_USERPTR:
for (i = 0; i < n_buffers; ++i)
free(buffers[i].start);
break;
}
free(buffers);
}
// Close device
if (-1 == close(fd))
errno_exit("close");
fd = -1;
}
bool CameraV4l2::acqStart(){
// INIT DEVICE
unsigned int i;
enum v4l2_buf_type type;
switch (io) {
case IO_METHOD_READ:
init_read(mFormat.fmt.pix.sizeimage);
break;
case IO_METHOD_MMAP:
init_mmap();
break;
case IO_METHOD_USERPTR:
init_userp(mFormat.fmt.pix.sizeimage);
break;
}
// START CAPTURING
switch (io) {
case IO_METHOD_READ:
{
/* Nothing to do. */
break;
}
case IO_METHOD_MMAP:
{
for (i = 0; i < n_buffers; ++i)
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_MMAP;
buf.index = i;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
{
errno_exit("VIDIOC_QBUF");
return false;
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
{
errno_exit("VIDIOC_STREAMON");
return false;
}
break;
}
case IO_METHOD_USERPTR:
{
for (i = 0; i < n_buffers; ++i)
{
struct v4l2_buffer buf;
memset(&buf, 0, sizeof(buf));
buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
buf.memory = V4L2_MEMORY_USERPTR;
buf.index = i;
buf.m.userptr = (unsigned long)buffers[i].start;
buf.length = buffers[i].length;
if (-1 == xioctl(fd, VIDIOC_QBUF, &buf))
{
errno_exit("VIDIOC_QBUF");
return false;
}
}
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMON, &type))
{
errno_exit("VIDIOC_STREAMON");
return false;
}
break;
}
}
return true;
}
void CameraV4l2::acqStop(){
enum v4l2_buf_type type;
switch (io)
{
case IO_METHOD_READ:
{
/* Nothing to do. */
break;
}
case IO_METHOD_MMAP:
case IO_METHOD_USERPTR:
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
if (-1 == xioctl(fd, VIDIOC_STREAMOFF, &type))
{
errno_exit("VIDIOC_STREAMOFF");
}
break;
}
}
bool CameraV4l2::grabImage(Frame &newFrame) {
unsigned char* ImageBuffer = NULL;
Mat img = Mat(mFormat.fmt.pix.height,mFormat.fmt.pix.width,CV_8UC1, Scalar(0));
size_t s = mFormat.fmt.pix.width*mFormat.fmt.pix.height;
bool grabSuccess = false;
for(;;) {
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
tv.tv_sec = 2;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if(-1 == r) {
if (EINTR == errno)
continue;
errno_exit("select");
}
if(0 == r) {
fprintf(stderr, "select timeout\n");
BOOST_LOG_SEV(logger, warning) << "Select timeout !";
//exit(EXIT_FAILURE);
}
if(read_frame()) {
grabSuccess = true;
break;
}
/* EAGAIN - continue select loop. */
}
if(grabSuccess) {
ImageBuffer = (unsigned char*)buffers[buf.index].start;
boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time();
newFrame.mDate = TimeDate::splitIsoExtendedDate(to_iso_extended_string(time));
double fps = 0;
if(getFPS(fps))
newFrame.mFps = fps;
newFrame.mFormat = MONO8;
newFrame.mSaturatedValue = 255;
newFrame.mFrameNumber = mFrameCounter;
newFrame.mExposure = exp;
newFrame.mGain = gain;
mFrameCounter++;
if(!convertImage(ImageBuffer, newFrame.mImg))
grabSuccess = false;
}
return grabSuccess;
}
bool CameraV4l2::grabSingleImage(Frame &frame, int camID){
createDevice(camID);
if(frame.mHeight > 0 && frame.mWidth > 0) {
cout << "Setting size to : " << frame.mWidth << "x" << frame.mHeight << endl;
mWidth = frame.mWidth;
mHeight = frame.mHeight;
mCustomSize = true;
}
grabInitialization();
acqStart();
cout << ">> Height : " << mFormat.fmt.pix.height << endl;
cout << ">> Width : " << mFormat.fmt.pix.width << endl;
if(!setPixelFormat(frame.mFormat))
return false;
if(expMin != -1 && expMax != -1)
setExposureTime(frame.mExposure);
if(expMin != -1 && expMax != -1)
setGain(frame.mGain);
unsigned char* ImageBuffer = NULL;
Mat img = Mat(mFormat.fmt.pix.height,mFormat.fmt.pix.width,CV_8UC1, Scalar(0));
size_t s = mFormat.fmt.pix.width*mFormat.fmt.pix.height;
bool grabSuccess = false;
for(int i = 0; i< n_buffers; i++) {
for(;;) {
fd_set fds;
struct timeval tv;
int r;
FD_ZERO(&fds);
FD_SET(fd, &fds);
/* Timeout. */
int timeout = 2;
if(frame.mExposure/1000000 > 1)
timeout = timeout + (int)(frame.mExposure/1000000);
tv.tv_sec = timeout;
tv.tv_usec = 0;
r = select(fd + 1, &fds, NULL, NULL, &tv);
if(-1 == r) {
if (EINTR == errno)
continue;
errno_exit("select");
}
if(0 == r) {
fprintf(stderr, "select timeout\n");
exit(EXIT_FAILURE);
}
if(read_frame()) {
grabSuccess = true;
break;
}
/* EAGAIN - continue select loop. */
}
}
if(grabSuccess) {
ImageBuffer = (unsigned char*)buffers[buf.index].start;
boost::posix_time::ptime time = boost::posix_time::microsec_clock::universal_time();
frame.mDate = TimeDate::splitIsoExtendedDate(to_iso_extended_string(time));
double fps = 0;
if(getFPS(fps))
frame.mFps = fps;
frame.mSaturatedValue = 255;
frame.mFrameNumber = mFrameCounter;
cout << "size image buffer : " << sizeof(buffers[buf.index].start) << endl;
if(!convertImage(ImageBuffer, frame.mImg))
grabSuccess = false;
}
acqStop();
grabCleanse();
return grabSuccess;
}
bool CameraV4l2::convertImage(unsigned char* buffer, Mat &image) {
bool res = false;
if(buffer != NULL) {
switch(mFormat.fmt.pix.pixelformat) {
case V4L2_PIX_FMT_GREY :
{
image = Mat(mFormat.fmt.pix.height, mFormat.fmt.pix.width, CV_8UC1, Scalar(0));
memcpy(image.ptr(), buffer, mFormat.fmt.pix.width*mFormat.fmt.pix.height);
res = true;
}
break;
case V4L2_PIX_FMT_YUYV :
{
unsigned char* bigbuffer = (unsigned char*)malloc(mFormat.fmt.pix.height * mFormat.fmt.pix.width*3*sizeof(char));
Mat dispimg(mFormat.fmt.pix.height, mFormat.fmt.pix.width, CV_8UC3, bigbuffer);
PixFmtConv::YUYV_to_BGR24(buffer, bigbuffer, mFormat.fmt.pix.width, mFormat.fmt.pix.height, mFormat.fmt.pix.bytesperline);
cvtColor(dispimg,image,CV_BGR2GRAY);
res = true;
free(bigbuffer);
}
break;
case V4L2_PIX_FMT_UYVY :
{
unsigned char bigbuffer[mFormat.fmt.pix.height * mFormat.fmt.pix.width*3];
PixFmtConv::UYVY_to_BGR24(buffer, bigbuffer, mFormat.fmt.pix.width, mFormat.fmt.pix.height, mFormat.fmt.pix.bytesperline);
Mat dispimg(mFormat.fmt.pix.height, mFormat.fmt.pix.width, CV_8UC3, bigbuffer);
cvtColor(dispimg,image,CV_BGR2GRAY);
res = true;
}
break;
case V4L2_PIX_FMT_RGB565 :
{
unsigned char bigbuffer[mFormat.fmt.pix.height * mFormat.fmt.pix.width*3];
PixFmtConv::RGB565_to_BGR24(buffer, bigbuffer, mFormat.fmt.pix.width, mFormat.fmt.pix.height);
Mat dispimg(mFormat.fmt.pix.height, mFormat.fmt.pix.width, CV_8UC3, bigbuffer);
cvtColor(dispimg,image,CV_BGR2GRAY);
res = true;
}
break;
case V4L2_PIX_FMT_BGR24 :
{
Mat dispimg = Mat(mFormat.fmt.pix.height, mFormat.fmt.pix.width, CV_8UC3, buffer);
cvtColor(dispimg,image,CV_BGR2GRAY);
res = true;
}
break;
case V4L2_PIX_FMT_RGB24 :
{
Mat dispimg = Mat(mFormat.fmt.pix.height, mFormat.fmt.pix.width, CV_8UC3, buffer);
cvtColor(dispimg,image,CV_BGR2GRAY);
res = true;
}
break;
}
}
return res;
}
void CameraV4l2::getExposureBounds(double &eMin, double &eMax){
struct v4l2_queryctrl queryctrl;
memset(&queryctrl, 0, sizeof(queryctrl));
queryctrl.id = V4L2_CID_EXPOSURE_ABSOLUTE;
if (-1 == ioctl(fd, VIDIOC_QUERYCTRL, &queryctrl)) {
if (errno != EINVAL) {