Example of how to use the VideoCapture class to get raw video frames and control the camera parameters.
#include <iostream>
#include <iomanip>
#include <opencv2/opencv.hpp>
{
static void handleMouse(int event, int x, int y, int, void*);
int main(
int argc,
char *argv[])
{
(void)argc;
(void)argv;
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;
cv::setMouseCallback(
win_name, handleMouse);
resetControls(cap);
updateAllCtrlValues(cap);
uint64_t last_ts=0;
uint16_t not_a_new_frame = 0;
int frame_timeout_msec = 100;
while (1)
{
{
}
{
}
{
not_a_new_frame=0;
#if 0
std::cout << std::setprecision(9) <<
"[" << frame.
frame_id <<
"] Ts: " <<
static_cast<double>(frame.
timestamp)/1e9 <<
" sec" << std::endl;
if( last_ts!=0 )
{
std::cout << std::setprecision(9) << " * dT: " << dt << " sec - FPS: " << 1./dt << std::endl;
}
#endif
}
{
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))
{
std::cout << "Camera connection lost. Closing..." << std::endl;
break;
}
}
int key = cv::waitKey( 5 );
if( key != -1 )
{
if(key=='q' || key=='Q')
break;
else
handleKeyboard( cap, key );
}
}
return EXIT_SUCCESS;
}
{
if(key >= '0' && key <= '9')
{
int value = key - '0';
setControlValue( cap, value );
return;
}
switch(key)
{
#ifdef SENSOR_LOG_AVAILABLE
case 'L':
{
std::cout << std::string(
"*** AEC/AGC registers logging: ") << (
logging?std::string(
"ENABLED"):std::string(
"DISABLED")) << std::endl;
}
break;
case 'f':
case 'F':
{
std::cout << std::string("*** AEC/AGC registers reset: ") << (res?std::string("OK"):std::string("KO")) << std::endl;
}
break;
#endif
case 'l':
{
bool led;
std::cout << std::string(" * LED STATUS: ") << (led?std::string("ON"):std::string("OFF")) << std::endl;
}
break;
case 'b':
break;
case 'S':
break;
case 'c':
break;
case 'h':
setActiveControl( cap,
Hue );
break;
case 's':
break;
case 'w':
break;
case 'g':
setActiveControl( cap,
Gamma );
break;
case 'e':
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
}
}
{
uint16_t x,y,w,h;
}
void handleMouse(int event, int x, int y, int, void*)
{
switch (event)
{
case cv::EVENT_LBUTTONDOWN:
{
{
}
break;
}
case cv::EVENT_LBUTTONUP:
{
{
{
}
{
}
}
break;
}
case cv::EVENT_RBUTTONDOWN:
{
break;
}
}
{
y = MAX(y,0);
{
x = MAX(x,0);
}
else
{
x = MAX(x,0);
}
}
}
{
std::string ctrlStr;
{
ctrlStr = "Brightness";
break;
ctrlStr = "Contrast";
break;
ctrlStr = "Hue";
break;
ctrlStr = "Saturation";
break;
ctrlStr = "Gain";
break;
ctrlStr = "White Balance";
break;
ctrlStr = "Sharpness";
break;
ctrlStr = "Gamma";
break;
ctrlStr = "Exposure";
break;
}
std::cout << "Active camera control: " << ctrlStr << std::endl;
}
{
int newValue;
{
std::cout << "New Brightness value: ";
break;
std::cout << "New Contrast value: ";
break;
std::cout << "New Hue value: ";
break;
std::cout << "New Saturation value: ";
break;
std::cout << "New White Balance value: ";
break;
std::cout << "New Sharpness value: ";
break;
std::cout << "New Gamma value: ";
break;
default:
return;
}
std::cout << newValue << std::endl;
}
{
int curValue=0;
{
break;
break;
break;
break;
{
if(increase)
{
}
else
{
}
std::cout <<
"New Left Gain value: " << (int)
gain_val_left << std::endl;
std::cout <<
"New Right Gain value: " << (int)
gain_val_right << std::endl;
}
break;
{
if(increase)
{
}
else
{
}
}
break;
break;
break;
break;
}
{
if(increase)
curValue += 100;
else
curValue -= 100;
}
{
if(increase)
setControlValue( cap, ++curValue);
else
setControlValue( cap, --curValue);
}
}
{
{
std::cout << "Automatic White Balance control: " << ((!curValue)?"ENABLED":"DISABLED") << std::endl;
}
{
std::cout << "Automatic Exposure and Gain control: " << ((!curValue)?"ENABLED":"DISABLED") << std::endl;
}
}
{
cv::Mat resized;
switch(res)
{
default:
resized = img;
break;
name += " [Resize factor 0.6]";
break;
name += " [Resize factor 0.4]";
break;
}
{
{
cv::Rect rescaled_roi;
cv::rectangle(resized, rescaled_roi, cv::Scalar(220, 180, 20), 2);
}
{
cv::Rect rescaled_roi;
cv::rectangle(resized, rescaled_roi, cv::Scalar(20, 180, 220), 2);
}
}
std::string info;
{
info = "Brightness: ";
break;
info = "Contrast: ";
break;
info = "Hue: ";
break;
info = "Saturation: ";
break;
info = "Gain: ";
{
info += "AUTO";
}
else
{
info += " - ";
}
break;
info = "Exposure: ";
{
info += "AUTO";
}
else
{
info += " - ";
}
break;
info = "WhiteBalance: ";
{
info += "AUTO";
}
else
{
}
break;
info = "Sharpness: ";
break;
info = "Gamma: ";
break;
}
cv::putText( resized, info, cv::Point(20,40),cv::FONT_HERSHEY_SIMPLEX, 0.75,
cv::Scalar(241,240,236), 2);
}
{
}
The VideoCapture class provides image grabbing functions and settings control for all the Stereolabs ...
void setHue(int hue)
Set the Hue value.
int getBrightness()
Get the Brightness value.
void resetBrightness()
Reset the Brightness value to default value.
int getSharpness()
Get the Sharpness value.
void resetGamma()
Reset the Gamma value to default value.
void resetHue()
Reset the Hue value to default value.
void setSaturation(int saturation)
Set the Saturation value.
int getGain(CAM_SENS_POS cam)
Get the current Gain value.
void resetAutoWhiteBalance()
Reset the automatic White Balance control value to default value.
int getSaturation()
Get the Saturation value.
int getHue()
Get the Hue value.
void resetSharpness()
Reset the Sharpness value to default value.
bool getAutoWhiteBalance()
Get the status of the automatic White Balance control.
int getGamma()
Get the Gamma value.
int getWhiteBalance()
Get the White Balance value.
void setSharpness(int sharpness)
Set the Sharpness value.
void setWhiteBalance(int wb)
Set the White Balance value (disable auto White Balance if active)
void setExposure(CAM_SENS_POS cam, int exposure)
Set the Exposure value (disable Exposure and Gain control if active)
void setContrast(int contrast)
Set the Contrast value.
int setAECAGC(bool active)
Enable/Disable the automatic Exposure and Gain control.
int getContrast()
Get the Contrast value.
bool enableAecAgcSensLogging(bool enable, int frame_skip=10)
Start logging to file of AEG/AGC camera registers.
void resetSaturation()
Reset the Saturation value to default value.
void resetAECAGC()
Reset the automatic Exposure and Gain control value to default value.
int getExposure(CAM_SENS_POS cam)
Get the current Exposure value.
bool getROIforAECAGC(CAM_SENS_POS side, uint16_t &x, uint16_t &y, uint16_t &w, uint16_t &h)
Get the coordinates of the current ROI for AECAGC control.
bool resetAGCAECregisters()
void setGamma(int gamma)
Set the Gamma value.
void setBrightness(int brightness)
Set the Brightness value.
bool getAECAGC()
Get the status of the automatic Exposure and Gain control.
void setAutoWhiteBalance(bool active)
Enable/Disable the automatic White Balance control.
void setGain(CAM_SENS_POS cam, int gain)
Set the Gain value (disable Exposure and Gain control if active)
bool resetROIforAECAGC(CAM_SENS_POS side)
Reset the ROI for AECAGC control.
void resetContrast()
Reset the Contrast value to default value.
int toggleLED(bool *value)
Toggle the status of the camera led.
RESOLUTION
Available resolutions.
@ FPS_15
15 Frames per second. Available for all the resolutions.
The Frame struct containing the acquired video frames.
uint64_t timestamp
Timestamp in nanoseconds.
uint16_t height
Frame height.
uint64_t frame_id
Increasing index of frames.
uint16_t width
Frame width.
uint8_t * data
Frame data in YUV 4:2:2 format.
The camera configuration parameters.
RESOLUTION res
Camera resolution.
FPS fps
Frames per second.
int main(int argc, char *argv[])
cv::Rect aecagc_roi_right
uint8_t exposure_val_left
uint8_t exposure_val_right
enum _cam_control CamControl
bool applyAECAGCrectRight
sl_oc::video::VideoParams params