ZED Open Capture  v0.6.0
Low level camera driver for the ZED stereo camera family
zed_oc_rectify_example.cpp
Go to the documentation of this file.
1 //
3 // Copyright (c) 2021, STEREOLABS.
4 //
5 // All rights reserved.
6 //
7 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
8 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
9 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
10 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
11 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
12 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
13 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
14 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
15 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
16 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
17 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
18 //
20 
21 // ----> Includes
22 #include <iostream>
23 #include <sstream>
24 #include <string>
25 
26 #include "videocapture.hpp"
27 
28 // OpenCV includes
29 #include <opencv2/opencv.hpp>
30 
31 // Sample includes
32 #include "calibration.hpp"
33 #include "ocv_display.hpp"
34 // <---- Includes
35 
36 // ----> Global functions
37 int main(int argc, char *argv[])
38 {
39  // ----> Silence unused warning
40  (void)argc;
41  (void)argv;
42  // <---- Silence unused warning
43 
45 
46  // ----> Set Video parameters
50  params.verbose = verbose;
51  // <---- Set Video parameters
52 
53  // ----> Create Video Capture
55  if( !cap.initializeVideo(-1) )
56  {
57  std::cerr << "Cannot open camera video capture" << std::endl;
58  std::cerr << "See verbosity level for more details." << std::endl;
59 
60  return EXIT_FAILURE;
61  }
62  int sn = cap.getSerialNumber();
63  std::cout << "Connected to camera sn: " << sn << std::endl;
64  // <---- Create Video Capture
65 
66  // ----> Retrieve calibration file from Stereolabs server
67  std::string calibration_file;
68  // ZED Calibration
69  unsigned int serial_number = sn;
70  // Download camera calibration file
71  if( !sl_oc::tools::downloadCalibrationFile(serial_number, calibration_file) )
72  {
73  std::cerr << "Could not load calibration file from Stereolabs servers" << std::endl;
74  return EXIT_FAILURE;
75  }
76  std::cout << "Calibration file found. Loading..." << std::endl;
77 
78  // ----> Frame size
79  int w,h;
80  cap.getFrameSize(w,h);
81  // <---- Frame size
82 
83  // ----> Initialize calibration
84  cv::Mat map_left_x, map_left_y;
85  cv::Mat map_right_x, map_right_y;
86  cv::Mat cameraMatrix_left, cameraMatrix_right;
87  sl_oc::tools::initCalibration(calibration_file, cv::Size(w/2,h), map_left_x, map_left_y, map_right_x, map_right_y,
88  cameraMatrix_left, cameraMatrix_right);
89 
90  std::cout << " Camera Matrix L: \n" << cameraMatrix_left << std::endl << std::endl;
91  std::cout << " Camera Matrix R: \n" << cameraMatrix_right << std::endl << std::endl;
92  // ----> Initialize calibration
93 
95 
96  uint64_t last_ts=0;
97 
98  // Infinite video grabbing loop
99  while (1)
100  {
101  // Get a new frame from camera
102  const sl_oc::video::Frame frame = cap.getLastFrame();
103 
104  // ----> If the frame is valid we can convert, rectify and display it
105  if(frame.data!=nullptr && frame.timestamp!=last_ts)
106  {
107  last_ts = frame.timestamp;
108 
109  // ----> Conversion from YUV 4:2:2 to BGR for visualization
110  cv::Mat frameYUV = cv::Mat( frame.height, frame.width, CV_8UC2, frame.data );
111  cv::cvtColor(frameYUV,frameBGR,cv::COLOR_YUV2BGR_YUYV);
112  // <---- Conversion from YUV 4:2:2 to BGR for visualization
113 
114  // ----> Extract left and right images from side-by-side
115  left_raw = frameBGR(cv::Rect(0, 0, frameBGR.cols / 2, frameBGR.rows));
116  right_raw = frameBGR(cv::Rect(frameBGR.cols / 2, 0, frameBGR.cols / 2, frameBGR.rows));
117  // Display images
120  // <---- Extract left and right images from side-by-side
121 
122  // ----> Apply rectification
123  cv::remap(left_raw, left_rect, map_left_x, map_left_y, cv::INTER_LINEAR );
124  cv::remap(right_raw, right_rect, map_right_x, map_right_y, cv::INTER_LINEAR );
125 
128  // <---- Apply rectification
129  }
130 
131  // ----> Keyboard handling
132  int key = cv::waitKey( 5 );
133  if(key=='q' || key=='Q') // Quit
134  break;
135  // <---- Keyboard handling
136  }
137 
138  return EXIT_SUCCESS;
139 }
140 
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