-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathzed_oc_control_example.cpp
860 lines (740 loc) · 25 KB
/
zed_oc_control_example.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
///////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2021, STEREOLABS.
//
// All rights reserved.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
///////////////////////////////////////////////////////////////////////////
// ----> Includes
#include "videocapture.hpp"
#include <iostream>
#include <iomanip>
#include <opencv2/opencv.hpp>
// <---- Includes
// ----> Camera settings control
typedef enum _cam_control
{
Brightness,
Contrast,
Hue,
Saturation,
Gain,
Exposure,
WhiteBalance,
Sharpness,
Gamma
} CamControl;
CamControl activeControl = Brightness;
// <---- Camera settings control
// ----> Global functions to control settings
// Rescale the images according to the selected resolution to better display them on screen
static void showImage( std::string name, cv::Mat& img, sl_oc::video::RESOLUTION res );
// Handle Keyboard
static void handleKeyboard( sl_oc::video::VideoCapture &cap, int key );
// Handle Mouse events
static void handleMouse(int event, int x, int y, int, void*);
// Change active control
static void setActiveControl(sl_oc::video::VideoCapture &cap, CamControl control );
// Set new value for the active control
static void setControlValue( sl_oc::video::VideoCapture &cap, int value );
// '+' or '-' pressed
static void changeControlValue( sl_oc::video::VideoCapture &cap, bool increase );
// 'a' or 'A' pressed to enable automatic WhiteBalanse or Gain/Exposure
static void toggleAutomaticControl( sl_oc::video::VideoCapture &cap );
// Retrieve all the control values from the camera
static void updateAllCtrlValues(sl_oc::video::VideoCapture &cap);
// Reset all the control values to default
static void resetControls( sl_oc::video::VideoCapture &cap );
// <---- Global functions to control settings
// ----> Global variables
cv::String win_name = "Stream RGB"; // Name of the stream window
bool selectInProgress = false; // Indicates that an AECAGC ROI is being drawn
bool selectLeft = false;
bool selectRight = false;
cv::Rect aecagc_roi_left = {0,0,0,0}; // The current agcaec ROI rectangle
cv::Rect aecagc_roi_right = {0,0,0,0}; // The current agcaec ROI rectangle
cv::Point origin_roi = {0,0}; // Click point for AECAGC ROI
double img_resize_factor = 1.0; // Image resize factor for drawing
int img_w = 0; // Camera image width
int img_h = 0; // Camera image height
bool logging = false; // Indicates if AEG/AGC registers logging is enabled
uint8_t brightness_val;
uint8_t contrast_val;
uint8_t hue_val;
uint8_t saturation_val;
uint8_t gain_val_left;
uint8_t gain_val_right;
uint8_t exposure_val_left;
uint8_t exposure_val_right;
int whiteBalance_val;
uint8_t sharpness_val;
uint8_t gamma_val;
bool autoAECAGC=false;
bool autoWB=false;
bool applyAECAGCrectLeft=false;
bool applyAECAGCrectRight=false;
// <---- Global variables
// The main function
int main(int argc, char *argv[])
{
// ----> Silence unused warning
(void)argc;
(void)argv;
// <---- Silence unused warning
sl_oc::VERBOSITY verbose = sl_oc::VERBOSITY::INFO;
// ----> Set Video parameters
sl_oc::video::VideoParams params;
params.res = sl_oc::video::RESOLUTION::HD2K;
params.fps = sl_oc::video::FPS::FPS_15;
params.verbose = verbose;
// <---- Set Video parameters
// ----> Create Video Capture
sl_oc::video::VideoCapture cap(params);
if( !cap.initializeVideo(-1) )
{
std::cerr << "Cannot open camera video capture" << std::endl;
std::cerr << "See verbosity level for more details." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Connected to camera sn: " << cap.getSerialNumber() << std::endl;
// <---- Create Video Capture
// ----> Create rendering window
cv::namedWindow(win_name);
cv::setMouseCallback(win_name, handleMouse);
// <---- Create rendering window
// Reset all the controls to default values
resetControls(cap);
// Set the default camera control setting
setActiveControl(cap, Brightness );
// Update the values for all the controls
updateAllCtrlValues(cap);
uint64_t last_ts=0;
uint16_t not_a_new_frame = 0;
int frame_timeout_msec = 100;
// Infinite video grabbing loop
while (1)
{
// 3) Get last available frame
const sl_oc::video::Frame frame = cap.getLastFrame(frame_timeout_msec);
img_w = frame.width;
img_h = frame.height;
// 3a) Apply AEC AGC ROI if necessary
if(applyAECAGCrectLeft)
{
applyAECAGCrectLeft = false;
cap.setROIforAECAGC( sl_oc::video::CAM_SENS_POS::LEFT,
aecagc_roi_left.x, aecagc_roi_left.y,
aecagc_roi_left.width, aecagc_roi_left.height);
selectLeft=false;
selectRight=false;
}
if(applyAECAGCrectRight)
{
applyAECAGCrectRight = false;
cap.setROIforAECAGC( sl_oc::video::CAM_SENS_POS::RIGHT,
aecagc_roi_right.x, aecagc_roi_right.y,
aecagc_roi_right.width, aecagc_roi_right.height);
selectLeft=false;
selectRight=false;
}
// ----> If the frame is valid we can display it
if(frame.data!=nullptr && frame.timestamp!=last_ts)
{
not_a_new_frame=0;
#if 0
// ----> Video Debug information
std::cout << std::setprecision(9) << "[" << frame.frame_id << "] Ts: " << static_cast<double>(frame.timestamp)/1e9 << " sec" << std::endl;
if( last_ts!=0 )
{
double dt = (frame.timestamp - last_ts)/1e9;
std::cout << std::setprecision(9) << " * dT: " << dt << " sec - FPS: " << 1./dt << std::endl;
}
// <---- Video Debug information
#endif
last_ts = frame.timestamp;
// ----> Conversion from YUV 4:2:2 to BGR for visualization
cv::Mat frameYUV = cv::Mat( frame.height, frame.width, CV_8UC2, frame.data );
cv::Mat frameBGR;
cv::cvtColor(frameYUV,frameBGR,cv::COLOR_YUV2BGR_YUYV);
// <---- Conversion from YUV 4:2:2 to BGR for visualization
// 4.c) Show frame
showImage( win_name, frameBGR, params.res );
}
else if(frame.timestamp==last_ts)
{
not_a_new_frame++;
std::cout << "Not a new frame #" << not_a_new_frame << std::endl;
if( not_a_new_frame>=(3000/frame_timeout_msec)) // Lost connection for 5 seconds
{
std::cout << "Camera connection lost. Closing..." << std::endl;
break;
}
}
// <---- If the frame is valid we can display it
// ----> Keyboard handling
int key = cv::waitKey( 5 );
if( key != -1 )
{
if(key=='q' || key=='Q') // Quit
break;
else
handleKeyboard( cap, key );
}
// <---- Keyboard handling
}
return EXIT_SUCCESS;
}
// Handle Keyboard
void handleKeyboard( sl_oc::video::VideoCapture &cap, int key )
{
if(key >= '0' && key <= '9')
{
int value = key - '0';
setControlValue( cap, value );
return;
}
switch(key)
{
#ifdef SENSOR_LOG_AVAILABLE
case 'L':
{
logging = !logging;
cap.enableAecAgcSensLogging( logging, 5 );
std::cout << std::string("*** AEC/AGC registers logging: ") << (logging?std::string("ENABLED"):std::string("DISABLED")) << std::endl;
}
break;
case 'f':
case 'F':
{
bool res = cap.resetAGCAECregisters();
std::cout << std::string("*** AEC/AGC registers reset: ") << (res?std::string("OK"):std::string("KO")) << std::endl;
}
break;
#endif
case 'l':
{
bool led;
cap.toggleLED( &led );
std::cout << std::string(" * LED STATUS: ") << (led?std::string("ON"):std::string("OFF")) << std::endl;
}
break;
case 'b':
setActiveControl( cap, Brightness );
break;
case 'S':
setActiveControl( cap, Sharpness );
break;
case 'c':
setActiveControl( cap, Contrast );
break;
case 'h':
setActiveControl( cap, Hue );
break;
case 's':
setActiveControl( cap, Saturation );
break;
case 'w':
setActiveControl( cap, WhiteBalance );
break;
case 'g':
setActiveControl( cap, Gamma );
break;
case 'e':
setActiveControl( cap, Exposure );
break;
case 'G':
setActiveControl( cap, Gain );
break;
case 'a':
toggleAutomaticControl( cap );
break;
case 'r':
case 'R':
resetControls(cap);
updateAllCtrlValues(cap);
std::cout << "All control settings are reset to default values" << std::endl;
break;
case '+':
case 171:
changeControlValue(cap,true);
break;
case '-':
case 173:
changeControlValue(cap,false);
break;
case '?':
std::cout << "COMMANDS HELP" << std::endl;
std::cout << " * 'q' or 'Q' -> Quit the example" << std::endl;
std::cout << " * '?' -> This help menu" << std::endl;
std::cout << " * 'l' -> Toggle camera led" << std::endl;
std::cout << " * 'b' -> Brightness control" << std::endl;
std::cout << " * 's' -> Saturation control" << std::endl;
std::cout << " * 'c' -> Contrast control" << std::endl;
std::cout << " * 'h' -> Hue control" << std::endl;
std::cout << " * 'S' -> Sharpness control" << std::endl;
std::cout << " * 'w' -> White Balance control" << std::endl;
std::cout << " * 'g' -> Gamma control" << std::endl;
std::cout << " * 'e' -> Exposure control" << std::endl;
std::cout << " * 'G' -> Gain control" << std::endl;
std::cout << " * 'a' -> Toggle automatic for White Balance or Exposure and Gain" << std::endl;
std::cout << " * 'r' or 'R' -> Reset to default configuration" << std::endl;
std::cout << " * '+' -> Increase the current control value" << std::endl;
std::cout << " * '-' -> Decrease the current control value" << std::endl;
std::cout << " * '0' .. '9' -> Set the current control value" << std::endl;
#ifdef SENSOR_LOG_AVAILABLE
std::cout << " * 'L' -> Toggle AGC/AEC registers logging" << std::endl;
std::cout << " * 'f' -> Fix AGC/AEC registers" << std::endl;
#endif
}
}
void updateAllCtrlValues(sl_oc::video::VideoCapture &cap)
{
brightness_val = cap.getBrightness();
sharpness_val = cap.getSharpness();
contrast_val = cap.getContrast();
hue_val = cap.getHue();
saturation_val = cap.getSaturation();
gamma_val = cap.getGamma();
autoAECAGC = cap.getAECAGC();
autoWB = cap.getAutoWhiteBalance();
gain_val_left = cap.getGain(sl_oc::video::CAM_SENS_POS::LEFT);
gain_val_right = cap.getGain(sl_oc::video::CAM_SENS_POS::RIGHT);
whiteBalance_val = cap.getWhiteBalance();
uint16_t x,y,w,h;
cap.getROIforAECAGC(sl_oc::video::CAM_SENS_POS::LEFT, x,y,w,h);
aecagc_roi_left.x = x;
aecagc_roi_left.y = y;
aecagc_roi_left.width = w;
aecagc_roi_left.height = h;
cap.getROIforAECAGC(sl_oc::video::CAM_SENS_POS::RIGHT, x,y,w,h);
aecagc_roi_right.x = x;
aecagc_roi_right.y = y;
aecagc_roi_right.width = w;
aecagc_roi_right.height = h;
}
void handleMouse(int event, int x, int y, int, void*)
{
switch (event)
{
case cv::EVENT_LBUTTONDOWN: // AEC AGC ROI drawing started
{
if(autoAECAGC && (activeControl==Gain || activeControl==Exposure))
{
origin_roi = cv::Point(x, y);
selectInProgress = true;
}
break;
}
case cv::EVENT_LBUTTONUP: // AECAGC ROI drawing completed
{
selectInProgress = false;
if(autoAECAGC && (activeControl==Gain || activeControl==Exposure))
{
if(selectLeft)
{
selectLeft=false;
applyAECAGCrectLeft = true;
}
if(selectRight)
{
selectRight=false;
applyAECAGCrectRight = true;
}
}
break;
}
case cv::EVENT_RBUTTONDOWN: // Reset AECAGC ROI with right button
{
//Reset selection
selectInProgress = false;
aecagc_roi_left = cv::Rect(0,0,img_w/2,img_h);
aecagc_roi_right = cv::Rect(0,0,img_w/2,img_h);
applyAECAGCrectLeft = true;
applyAECAGCrectRight = true;
break;
}
}
if(selectInProgress) // AECAGC ROI drawing in progress
{
x /= img_resize_factor;
y /= img_resize_factor;
int or_x = origin_roi.x/img_resize_factor;
int or_y = origin_roi.y/img_resize_factor;
y = MAX(y,0);
y = MIN(y,img_h);
if(or_x<img_w/2) // AECAGC ROI for the left image
{
x = MAX(x,0);
x = MIN(x,img_w/2-1);
selectLeft = true;
aecagc_roi_left.x = MIN(x, or_x);
aecagc_roi_left.y = MIN(y, or_y);
aecagc_roi_left.width = abs(x - or_x) + 1;
aecagc_roi_left.height = abs(y - or_y) + 1;
}
else // AECAGC ROI for the right image
{
or_x -= img_w/2;
x -= img_w/2;
x = MAX(x,0);
x = MIN(x,img_w/2-1);
selectRight = true;
aecagc_roi_right.x = MIN(x, or_x);
aecagc_roi_right.y = MIN(y, or_y);
aecagc_roi_right.width = abs(x - or_x) + 1;
aecagc_roi_right.height = abs(y - or_y) + 1;
}
}
}
// Change active control
void setActiveControl(sl_oc::video::VideoCapture &cap, CamControl control )
{
activeControl = control;
std::string ctrlStr;
switch( activeControl )
{
case Brightness:
ctrlStr = "Brightness";
break;
case Contrast:
ctrlStr = "Contrast";
break;
case Hue:
ctrlStr = "Hue";
break;
case Saturation:
ctrlStr = "Saturation";
break;
case Gain:
ctrlStr = "Gain";
break;
case WhiteBalance:
ctrlStr = "White Balance";
break;
case Sharpness:
ctrlStr = "Sharpness";
break;
case Gamma:
ctrlStr = "Gamma";
break;
case Exposure:
ctrlStr = "Exposure";
break;
}
std::cout << "Active camera control: " << ctrlStr << std::endl;
}
// Set new value for the active control
void setControlValue(sl_oc::video::VideoCapture &cap, int value )
{
int newValue;
switch( activeControl )
{
case Brightness:
cap.setBrightness( value );
newValue = cap.getBrightness();
brightness_val = newValue;
std::cout << "New Brightness value: ";
break;
case Contrast:
cap.setContrast( value );
newValue = cap.getContrast();
contrast_val = newValue;
std::cout << "New Contrast value: ";
break;
case Hue:
cap.setHue( value );
newValue = cap.getHue();
hue_val = newValue;
std::cout << "New Hue value: ";
break;
case Saturation:
cap.setSaturation( value );
newValue = cap.getSaturation();
saturation_val = newValue;
std::cout << "New Saturation value: ";
break;
case WhiteBalance:
cap.setWhiteBalance( 2800+value*411 );
newValue = cap.getWhiteBalance();
whiteBalance_val = newValue;
std::cout << "New White Balance value: ";
break;
case Sharpness:
cap.setSharpness( value );
newValue = cap.getSharpness();
sharpness_val = newValue;
std::cout << "New Sharpness value: ";
break;
case Gamma:
cap.setGamma( value);
newValue = cap.getGamma();
gamma_val = newValue;
std::cout << "New Gamma value: ";
break;
case Gain:
case Exposure:
default:
// Nothing to do here
return;
}
std::cout << newValue << std::endl;
}
// '+' or '-' pressed
void changeControlValue( sl_oc::video::VideoCapture &cap, bool increase )
{
int curValue=0;
switch( activeControl )
{
case Brightness:
curValue = cap.getBrightness();
brightness_val = curValue;
break;
case Contrast:
curValue = cap.getContrast();
contrast_val = curValue;
break;
case Hue:
curValue = cap.getHue();
hue_val = curValue;
break;
case Saturation:
curValue = cap.getSaturation();
saturation_val = curValue;
break;
case Gain:
{
int curValueLeft = cap.getGain(sl_oc::video::CAM_SENS_POS::LEFT);
int curValueRight = cap.getGain(sl_oc::video::CAM_SENS_POS::RIGHT);
if(increase)
{
cap.setGain(sl_oc::video::CAM_SENS_POS::LEFT,++curValueLeft);
cap.setGain(sl_oc::video::CAM_SENS_POS::RIGHT,++curValueRight);
}
else
{
cap.setGain(sl_oc::video::CAM_SENS_POS::LEFT,--curValueLeft);
cap.setGain(sl_oc::video::CAM_SENS_POS::RIGHT,--curValueRight);;
}
gain_val_left = cap.getGain(sl_oc::video::CAM_SENS_POS::LEFT);
gain_val_right = cap.getGain(sl_oc::video::CAM_SENS_POS::RIGHT);
std::cout << "New Left Gain value: " << (int)gain_val_left << std::endl;
std::cout << "New Right Gain value: " << (int)gain_val_right << std::endl;
autoAECAGC = cap.getAECAGC();
}
break;
case Exposure:
{
int curValueLeft = cap.getExposure(sl_oc::video::CAM_SENS_POS::LEFT);
int curValueRight = cap.getExposure(sl_oc::video::CAM_SENS_POS::RIGHT);
if(increase)
{
cap.setExposure(sl_oc::video::CAM_SENS_POS::LEFT,++curValueLeft);
cap.setExposure(sl_oc::video::CAM_SENS_POS::RIGHT,++curValueRight);
}
else
{
cap.setExposure(sl_oc::video::CAM_SENS_POS::LEFT,--curValueLeft);
cap.setExposure(sl_oc::video::CAM_SENS_POS::RIGHT,--curValueRight);;
}
exposure_val_left = cap.getExposure(sl_oc::video::CAM_SENS_POS::LEFT);
exposure_val_right = cap.getExposure(sl_oc::video::CAM_SENS_POS::RIGHT);
std::cout << "New Left Exposure value: " << (int)exposure_val_left << std::endl;
std::cout << "New Right Exposure value: " << (int)exposure_val_right << std::endl;
autoAECAGC = cap.getAECAGC();
}
break;
case WhiteBalance:
cap.setAutoWhiteBalance(false);
curValue = cap.getWhiteBalance();
whiteBalance_val = curValue;
autoWB = cap.getAutoWhiteBalance();
break;
case Sharpness:
curValue = cap.getSharpness();
sharpness_val = curValue;
break;
case Gamma:
curValue = cap.getGamma();
gamma_val = curValue;
break;
}
if(activeControl==WhiteBalance)
{
if(increase)
curValue += 100;
else
curValue -= 100;
cap.setWhiteBalance( curValue );
whiteBalance_val = cap.getWhiteBalance();
std::cout << "New White Balance value: " << whiteBalance_val << std::endl;
}
else if(activeControl != Gain && activeControl != Exposure)
{
if(increase)
setControlValue( cap, ++curValue);
else
setControlValue( cap, --curValue);
}
}
// 'a' or 'A' pressed to enable automatic WhiteBalanse or Gain/Exposure
void toggleAutomaticControl( sl_oc::video::VideoCapture &cap )
{
if(activeControl == WhiteBalance)
{
bool curValue = cap.getAutoWhiteBalance();
cap.setAutoWhiteBalance( !curValue );
autoWB = cap.getAutoWhiteBalance();
std::cout << "Automatic White Balance control: " << ((!curValue)?"ENABLED":"DISABLED") << std::endl;
}
if(activeControl == Exposure || activeControl == Gain )
{
bool curValue = cap.getAECAGC();
cap.setAECAGC( !curValue );
autoAECAGC = cap.getAECAGC();
std::cout << "Automatic Exposure and Gain control: " << ((!curValue)?"ENABLED":"DISABLED") << std::endl;
}
}
// Rescale the images according to the selected resolution to better display them on screen
void showImage( std::string name, cv::Mat& img, sl_oc::video::RESOLUTION res )
{
cv::Mat resized;
switch(res)
{
default:
case sl_oc::video::RESOLUTION::VGA:
img_resize_factor = 1.0;
resized = img;
break;
case sl_oc::video::RESOLUTION::HD720:
name += " [Resize factor 0.6]";
img_resize_factor = 0.6;
cv::resize( img, resized, cv::Size(), img_resize_factor, img_resize_factor );
break;
case sl_oc::video::RESOLUTION::HD1080:
case sl_oc::video::RESOLUTION::HD2K:
name += " [Resize factor 0.4]";
img_resize_factor = 0.4;
cv::resize( img, resized, cv::Size(), img_resize_factor, img_resize_factor );
break;
}
if(autoAECAGC && (activeControl==Gain || activeControl==Exposure))
{
// Check that left selection rectangle is valid and draw it on the image
if ( (aecagc_roi_left.area()>0) &&
(aecagc_roi_left.width-aecagc_roi_left.x)<=img_w/2 &&
(aecagc_roi_left.height-aecagc_roi_left.y)<=img_h)
{
cv::Rect rescaled_roi;
rescaled_roi.x = aecagc_roi_left.x*img_resize_factor;
rescaled_roi.y = aecagc_roi_left.y*img_resize_factor;
rescaled_roi.width = aecagc_roi_left.width*img_resize_factor;
rescaled_roi.height = aecagc_roi_left.height*img_resize_factor;
cv::rectangle(resized, rescaled_roi, cv::Scalar(220, 180, 20), 2);
}
// Check that right selection rectangle is valid and draw it on the image
if ( (aecagc_roi_right.area()>0) &&
(aecagc_roi_right.width-aecagc_roi_right.x)<=img_w/2 &&
(aecagc_roi_right.height-aecagc_roi_right.y)<=img_h)
{
cv::Rect rescaled_roi;
rescaled_roi.x = (img_w/2+aecagc_roi_right.x)*img_resize_factor;
rescaled_roi.y = aecagc_roi_right.y*img_resize_factor;
rescaled_roi.width = aecagc_roi_right.width*img_resize_factor;
rescaled_roi.height = aecagc_roi_right.height*img_resize_factor;
cv::rectangle(resized, rescaled_roi, cv::Scalar(20, 180, 220), 2);
}
}
std::string info;
switch (activeControl)
{
case Brightness:
info = "Brightness: ";
info += std::to_string(brightness_val);
break;
case Contrast:
info = "Contrast: ";
info += std::to_string(contrast_val);
break;
case Hue:
info = "Hue: ";
info += std::to_string(hue_val);
break;
case Saturation:
info = "Saturation: ";
info += std::to_string(saturation_val);
break;
case Gain:
info = "Gain: ";
if(autoAECAGC)
{
info += "AUTO";
}
else
{
info += std::to_string(gain_val_left);
info += " - ";
info += std::to_string(gain_val_right);
}
break;
case Exposure:
info = "Exposure: ";
if(autoAECAGC)
{
info += "AUTO";
}
else
{
info += std::to_string(exposure_val_left);
info += " - ";
info += std::to_string(exposure_val_right);
}
break;
case WhiteBalance:
info = "WhiteBalance: ";
if(autoWB)
{
info += "AUTO";
}
else
{
info += std::to_string(whiteBalance_val);
}
break;
case Sharpness:
info = "Sharpness: ";
info += std::to_string(sharpness_val);
break;
case Gamma:
info = "Gamma: ";
info += std::to_string(gamma_val);
break;
}
cv::putText( resized, info, cv::Point(20,40),cv::FONT_HERSHEY_SIMPLEX, 0.75,
cv::Scalar(241,240,236), 2);
cv::imshow( win_name, resized );
}
void resetControls( sl_oc::video::VideoCapture &cap )
{
cap.resetBrightness();
cap.resetSharpness();
cap.resetContrast();
cap.resetHue();
cap.resetSaturation();
cap.resetGamma();
cap.resetAECAGC();
cap.resetAutoWhiteBalance();
cap.resetROIforAECAGC(sl_oc::video::CAM_SENS_POS::LEFT);
cap.resetROIforAECAGC(sl_oc::video::CAM_SENS_POS::RIGHT);
}