-
Notifications
You must be signed in to change notification settings - Fork 35
/
zed_oc_rectify_example.cpp
140 lines (116 loc) · 5.01 KB
/
zed_oc_rectify_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
///////////////////////////////////////////////////////////////////////////
//
// 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
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;
}
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
cv::Mat frameBGR, left_raw, left_rect, right_raw, right_rect;
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
sl_oc::tools::showImage("left RAW", left_raw, params.res);
sl_oc::tools::showImage("right RAW", right_raw, params.res);
// <---- 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 );
sl_oc::tools::showImage("right RECT", right_rect, params.res);
sl_oc::tools::showImage("left RECT", left_rect, params.res);
// <---- Apply rectification
}
// ----> Keyboard handling
int key = cv::waitKey( 5 );
if(key=='q' || key=='Q') // Quit
break;
// <---- Keyboard handling
}
return EXIT_SUCCESS;
}