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

Example of how to use the VideoCapture class to get and rectify raw video frames downloading calibration parameters from Stereolabs servers.

//
// 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 <iostream>
#include <sstream>
#include <string>
#include "videocapture.hpp"
// OpenCV includes
#include <opencv2/opencv.hpp>
// Sample includes
#include "calibration.hpp"
#include "ocv_display.hpp"
// <---- Includes
// ----> Global functions
int main(int argc, char *argv[])
{
// ----> Silence unused warning
(void)argc;
(void)argv;
// <---- Silence unused warning
// ----> Set Video parameters
params.verbose = verbose;
// <---- Set Video parameters
// ----> Create Video Capture
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;
}
int sn = cap.getSerialNumber();
std::cout << "Connected to camera sn: " << sn << std::endl;
// <---- Create Video Capture
// ----> Retrieve calibration file from Stereolabs server
std::string calibration_file;
// ZED Calibration
unsigned int serial_number = sn;
// Download camera calibration file
if( !sl_oc::tools::downloadCalibrationFile(serial_number, calibration_file) )
{
std::cerr << "Could not load calibration file from Stereolabs servers" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Calibration file found. Loading..." << std::endl;
// ----> Frame size
int w,h;
cap.getFrameSize(w,h);
// <---- Frame size
// ----> Initialize calibration
cv::Mat map_left_x, map_left_y;
cv::Mat map_right_x, map_right_y;
cv::Mat cameraMatrix_left, cameraMatrix_right;
sl_oc::tools::initCalibration(calibration_file, cv::Size(w/2,h), map_left_x, map_left_y, map_right_x, map_right_y,
cameraMatrix_left, cameraMatrix_right);
std::cout << " Camera Matrix L: \n" << cameraMatrix_left << std::endl << std::endl;
std::cout << " Camera Matrix R: \n" << cameraMatrix_right << std::endl << std::endl;
// ----> Initialize calibration
uint64_t last_ts=0;
// Infinite video grabbing loop
while (1)
{
// Get a new frame from camera
const sl_oc::video::Frame frame = cap.getLastFrame();
// ----> If the frame is valid we can convert, rectify and display it
if(frame.data!=nullptr && frame.timestamp!=last_ts)
{
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::cvtColor(frameYUV,frameBGR,cv::COLOR_YUV2BGR_YUYV);
// <---- Conversion from YUV 4:2:2 to BGR for visualization
// ----> Extract left and right images from side-by-side
left_raw = frameBGR(cv::Rect(0, 0, frameBGR.cols / 2, frameBGR.rows));
right_raw = frameBGR(cv::Rect(frameBGR.cols / 2, 0, frameBGR.cols / 2, frameBGR.rows));
// Display images
// <---- Extract left and right images from side-by-side
// ----> Apply rectification
cv::remap(left_raw, left_rect, map_left_x, map_left_y, cv::INTER_LINEAR );
cv::remap(right_raw, right_rect, map_right_x, map_right_y, cv::INTER_LINEAR );
// <---- Apply rectification
}
// ----> 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.
void getFrameSize(int &width, int &height)
Get the size of the camera frame.
bool initializeVideo(int devId=-1)
Open a ZED camera using the specified ID or searching for the first available.
int getSerialNumber()
Retrieve the serial number of the connected camera.
bool initCalibration(std::string calibration_file, cv::Size2i image_size, cv::Mat &map_left_x, cv::Mat &map_left_y, cv::Mat &map_right_x, cv::Mat &map_right_y, cv::Mat &cameraMatrix_left, cv::Mat &cameraMatrix_right, double *baseline=nullptr)
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
bool downloadCalibrationFile(unsigned int serial_number, std::string &calibration_file)
@ FPS_15
15 Frames per second. Available for all the resolutions.
VERBOSITY
Definition: defines.hpp:85
@ INFO
Definition: defines.hpp:89
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.
int main(int argc, char *argv[])
cv::Mat left_raw
cv::Mat frameBGR
sl_oc::video::VideoParams params
cv::Mat right_raw
cv::Mat frameYUV
cv::Mat left_rect
cv::Mat right_rect