ZED Open Capture  v0.6.0
Low level camera driver for the ZED stereo camera family
zed_oc_video_reg_log.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 "videocapture.hpp"
23 
24 #include <iostream>
25 #include <iomanip>
26 
27 #include <opencv2/opencv.hpp>
28 
29 #include "ocv_display.hpp"
30 // <---- Includes
31 
32 // #define TEST_FPS 1
33 
34 // The main function
35 int main(int argc, char *argv[])
36 {
37  // ----> Create Video Capture
41 
43  if( !cap.initializeVideo() )
44  {
45  std::cerr << "Cannot open camera video capture" << std::endl;
46  std::cerr << "See verbosity level for more details." << std::endl;
47 
48  return EXIT_FAILURE;
49  }
50  std::cout << "Connected to camera sn: " << cap.getSerialNumber() << std::endl;
51  // <---- Create Video Capture
52 
53 #ifdef TEST_FPS
54  // Timestamp to check FPS
55  double lastTime = static_cast<double>(getSteadyTimestamp())/1e9;
56  // Frame timestamp to check FPS
57  uint64_t lastFrameTs = 0;
58 #endif
59 
60  // Enable AGC/AEG registers logging
61  cap.enableAecAgcSensLogging(false);
62 
63  uint64_t last_ts=0;
64  uint16_t not_a_new_frame = 0;
65  int frame_timeout_msec = 100;
66 
67  int f_count = 0;
68  // Infinite video grabbing loop
69  while (1)
70  {
71  // Get last available frame
72  const sl_oc::video::Frame frame = cap.getLastFrame(frame_timeout_msec);
73 
74  // ----> If the frame is valid we can display it
75  if(frame.data!=nullptr && frame.timestamp!=last_ts)
76  {
77  last_ts = frame.timestamp;
78  not_a_new_frame=0;
79 #ifdef TEST_FPS
80  if(lastFrameTs!=0)
81  {
82  // ----> System time
83  double now = static_cast<double>(getSteadyTimestamp())/1e9;
84  double elapsed_sec = now - lastTime;
85  lastTime = now;
86  std::cout << "[System] Frame period: " << elapsed_sec << "sec - Freq: " << 1./elapsed_sec << " Hz" << std::endl;
87  // <---- System time
88 
89  // ----> Frame time
90  double frame_dT = static_cast<double>(frame.timestamp-lastFrameTs)/1e9;
91  std::cout << "[Camera] Frame period: " << frame_dT << "sec - Freq: " << 1./frame_dT << " Hz" << std::endl;
92  // <---- Frame time
93  }
94  lastFrameTs = frame.timestamp;
95 #endif
96 
97 
98 
99  if (f_count%10==0)
100  {
101  /* cap.
102  cap.saveAllISPRegisters("ov580_lr_"+std::to_string(f_count)+".csv");
103  cap.saveAllSensorsRegisters("ov4689_lr_"+std::to_string(f_count)+".csv");
104  std::cout<<" Save Data for f_count "<<f_count<<std::endl;*/
105  if (f_count%20==0)
106  cap.setColorBars(0,true);
107  else
108  cap.setColorBars(0,false);
109  }
110 
111  // if (f_count!=0 && f_count%10==0)
112 // {
113 // cap.saveAllISPRegisters("ov580_lr_"+std::to_string(f_count)+".csv");
114 // cap.saveAllSensorsRegisters("ov4689_lr_"+std::to_string(f_count)+".csv");
115 // std::cout<<" Save Data for f_count "<<f_count<<std::endl;
116 // }
117 
118 
119 
120  // ----> Conversion from YUV 4:2:2 to BGR for visualization
121  cv::Mat frameYUV = cv::Mat( frame.height, frame.width, CV_8UC2, frame.data );
122  cv::Mat frameBGR;
123  cv::cvtColor(frameYUV,frameBGR,cv::COLOR_YUV2BGR_YUYV);
124  // <---- Conversion from YUV 4:2:2 to BGR for visualization
125 
126  f_count++;
127  // Show frame
128  showImage( "Stream RGB", frameBGR, params.res );
129  }
130  else
131  {
132  not_a_new_frame++;
133  std::cout << "Not a new frame #" << not_a_new_frame << std::endl;
134 
135  if( not_a_new_frame>=(1000/frame_timeout_msec)) // Lost connection for 1 seconds
136  {
137  cap.saveAllISPRegisters("ov580_lr_"+std::to_string(f_count)+"_not_a_new _frame.csv");
138  cap.saveAllSensorsRegisters("ov4689_lr_"+std::to_string(f_count)+"_not_a_new _frame.csv");
139  std::cout<<" Save Data for f_count "<<f_count<<std::endl;
140 
141  std::cout << "Camera connection lost. Closing..." << std::endl;
142  break;
143  }
144  }
145  // <---- If the frame is valid we can display it
146 
147  // ----> Keyboard handling
148  int key = cv::waitKey( 5 );
149  if(key=='q' || key=='Q') // Quit
150  break;
151  // <---- Keyboard handling
152  }
153 
154  // Disable AGC/AEG registers logging
155  cap.enableAecAgcSensLogging(true);
156 
157  return EXIT_SUCCESS;
158 }
159 
160 
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 saveAllSensorsRegisters(std::string filename)
Save all sensors ctrl register.
void saveAllISPRegisters(std::string filename)
Save all ISP camera registers into a file.
bool initializeVideo(int devId=-1)
Open a ZED camera using the specified ID or searching for the first available.
bool enableAecAgcSensLogging(bool enable, int frame_skip=10)
Start logging to file of AEG/AGC camera registers.
int getSerialNumber()
Retrieve the serial number of the connected camera.
void setColorBars(int side, bool c)
Utils fct to set Color Bars on Image.
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[])