ZED Open Capture  v0.6.0
Low level camera driver for the ZED stereo camera family
zed_oc_video_example.cpp

Example of how to use the VideoCapture class to get raw video frames and show the stream on screen using the OpenCV library.

#include "videocapture.hpp"
#include "ocv_display.hpp"
#include <iostream>
#include <iomanip>
#include <opencv2/opencv.hpp>
// <---- Includes
// #define TEST_FPS 1
// The main function
int main(int argc, char *argv[])
{
// ----> Silence unused warning
(void)argc;
(void)argv;
// <---- Silence unused warning
// ----> Create Video Capture
if( !cap_0.initializeVideo() )
{
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_0.getSerialNumber() << "[" << cap_0.getDeviceName() << "]" << std::endl;
// <---- Create Video Capture
#ifdef TEST_FPS
// Timestamp to check FPS
double lastTime = static_cast<double>(getSteadyTimestamp())/1e9;
// Frame timestamp to check FPS
uint64_t lastFrameTs = 0;
#endif
// Infinite video grabbing loop
while (1)
{
// Get last available frame
const sl_oc::video::Frame frame = cap_0.getLastFrame();
// ----> If the frame is valid we can display it
if(frame.data!=nullptr)
{
#ifdef TEST_FPS
if(lastFrameTs!=0)
{
// ----> System time
double now = static_cast<double>(getSteadyTimestamp())/1e9;
double elapsed_sec = now - lastTime;
lastTime = now;
std::cout << "[System] Frame period: " << elapsed_sec << "sec - Freq: " << 1./elapsed_sec << " Hz" << std::endl;
// <---- System time
// ----> Frame time
double frame_dT = static_cast<double>(frame.timestamp-lastFrameTs)/1e9;
std::cout << "[Camera] Frame period: " << frame_dT << "sec - Freq: " << 1./frame_dT << " Hz" << std::endl;
// <---- Frame time
}
lastFrameTs = frame.timestamp;
#endif
// ----> 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
// Show frame
}
// <---- If the frame is valid we can display it
// ----> Keyboard handling
int key = cv::waitKey( 5 );
if(key=='q' || key=='Q') // Quit
break;
// <---- Keyboard handling
}
return EXIT_SUCCESS;
}
The VideoCapture class provides image grabbing functions and settings control for all the Stereolabs ...
const Frame & getLastFrame(uint64_t timeout_msec=100)
Get the last received camera image.
bool initializeVideo(int devId=-1)
Open a ZED camera using the specified ID or searching for the first available.
std::string getDeviceName()
Retrieve the OS device name.
int getSerialNumber()
Retrieve the serial number of the connected camera.
uint64_t getSteadyTimestamp()
Get the current system clock as steady clock, so with no jumps even if the system time changes.
Definition: defines.hpp:63
void showImage(std::string name, cv::UMat &img, sl_oc::video::RESOLUTION res, bool change_name=true, std::string info="")
Rescale the OpenCV T-API images [cv::UMat] according to the selected resolution to better display the...
Definition: ocv_display.hpp:27
@ FPS_60
60 Frames per second. Not available for RESOLUTION::HD2K and RESOLUTION::HD1080.
The Frame struct containing the acquired video frames.
uint64_t timestamp
Timestamp in nanoseconds.
uint16_t height
Frame height.
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.
cv::Mat frameBGR
sl_oc::video::VideoParams params
cv::Mat frameYUV
int main(int argc, char *argv[])