-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathzed_oc_multi_video_example.cpp
150 lines (124 loc) · 5.35 KB
/
zed_oc_multi_video_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
140
141
142
143
144
145
146
147
148
////////////////////////////////////////////////////////////////////////////
////
//// 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 "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
sl_oc::video::VideoParams params;
params.res = sl_oc::video::RESOLUTION::HD720;
params.fps = sl_oc::video::FPS::FPS_60;
// ----> Create Video Capture 0
sl_oc::video::VideoCapture cap_0(params);
if( !cap_0.initializeVideo(0) )
{
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 0
// ----> Create Video Capture 1
sl_oc::video::VideoCapture cap_1(params);
if( !cap_1.initializeVideo(2) )
{
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_1.getSerialNumber() << " [" << cap_1.getDeviceName() << "]" << std::endl;
// <---- Create Video Capture 1
// Set video parameters
bool autoSettingEnable = true;
cap_0.setAutoWhiteBalance(autoSettingEnable);
cap_0.setAECAGC(autoSettingEnable);
cap_1.setAutoWhiteBalance(autoSettingEnable);
cap_1.setAECAGC(autoSettingEnable);
#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_0 = cap_0.getLastFrame();
const sl_oc::video::Frame frame_1 = cap_1.getLastFrame();
// ----> If the frame is valid we can display it
if(frame_0.data!=nullptr && frame_1.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_0 = cv::Mat( frame_0.height, frame_0.width, CV_8UC2, frame_0.data );
cv::Mat frameBGR_0;
cv::Mat frameYUV_1 = cv::Mat( frame_1.height, frame_1.width, CV_8UC2, frame_1.data );
cv::Mat frameBGR_1;
cv::cvtColor(frameYUV_0,frameBGR_0,cv::COLOR_YUV2BGR_YUYV);
cv::cvtColor(frameYUV_1,frameBGR_1,cv::COLOR_YUV2BGR_YUYV);
// <---- Conversion from YUV 4:2:2 to BGR for visualization
// Show frame
sl_oc::tools::showImage( "Stream RGB #0", frameBGR_0, params.res );
sl_oc::tools::showImage( "Stream RGB #1", frameBGR_1, params.res );
}
// <---- If the frame is valid we can display it
// ----> Keyboard handling
int key = cv::waitKey( 5 );
if(key=='q' || key=='Q') // Quit
break;
if(key=='a' || key=='A')
{
autoSettingEnable = !autoSettingEnable;
cap_0.setAutoWhiteBalance(autoSettingEnable);
cap_0.setAECAGC(autoSettingEnable);
cap_1.setAutoWhiteBalance(autoSettingEnable);
cap_1.setAECAGC(autoSettingEnable);
std::cout << "Auto GAIN/EXPOSURE and Auto White Balance: " << (autoSettingEnable?"ENABLED":"DISABLED") << std::endl;
}
// <---- Keyboard handling
}
return EXIT_SUCCESS;
}