rs-latency-tool.cpp
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 #include "latency-detector.h"
5 
6 // This demo is presenting one way to estimate latency without access to special equipment
7 // See ReadMe.md for more information
8 int main(int argc, char * argv[]) try
9 {
10  using namespace cv;
11  using namespace rs2;
12 
13  // Start RealSense camera
14  // Uncomment the configuration you wish to test
15  pipeline pipe;
16  config cfg;
17  //cfg.enable_stream(RS2_STREAM_COLOR, RS2_FORMAT_BGR8);
18  //cfg.enable_stream(RS2_STREAM_COLOR, 640, 480, RS2_FORMAT_BGR8, 30);
19  cfg.enable_stream(RS2_STREAM_COLOR, 1920, 1080, RS2_FORMAT_BGR8, 30);
20  //cfg.enable_stream(RS2_STREAM_INFRARED, 640, 480, RS2_FORMAT_Y8);
21  //cfg.enable_stream(RS2_STREAM_INFRARED, 1280, 720, RS2_FORMAT_Y8);
22  pipe.start(cfg);
23 
24  // To test with a regular webcam, comment-out the previous block
25  // And uncomment the following code:
26  //context ctx;
27  //auto dev = ctx.query_devices().front();
28  //auto sensor = dev.query_sensors().front();
29  //auto sps = sensor.get_stream_profiles();
30  //stream_profile selected;
31  //for (auto& sp : sps)
32  //{
33  // if (auto vid = sp.as<video_stream_profile>())
34  // {
35  // if (vid.format() == RS2_FORMAT_BGR8 &&
36  // vid.width() == 640 && vid.height() == 480 &&
37  // vid.fps() == 30)
38  // selected = vid;
39  // }
40  //}
41  //sensor.open(selected);
42  //syncer pipe;
43  //sensor.start(pipe);
44 
45  const auto window_name = "Display Image";
46  namedWindow(window_name, WINDOW_NORMAL);
47  setWindowProperty(window_name, WND_PROP_FULLSCREEN, WINDOW_FULLSCREEN);
48 
49  const int display_w = 1280;
50  const int digits = 16;
51  const int circle_w = (display_w - 120) / (digits + 2);
52  const int display_h = 720;
53 
54  bit_packer packer(digits);
55 
56  detector d(digits, display_w);
57 
58  while (getWindowProperty(window_name, WND_PROP_AUTOSIZE) >= 0)
59  {
60  // Wait for frameset from the camera
61  for (auto f : pipe.wait_for_frames())
62  {
63  // Unfortunately depth frames do not register well using this method
64  if (f.get_profile().stream_type() != RS2_STREAM_DEPTH)
65  {
66  d.submit_frame(f);
67  break;
68  }
69  }
70 
71  // Commit to the next clock value before starting rendering the frame
72  auto next_value = d.get_next_value();
73 
74  // Start measuring rendering time for the next frame
75  // We substract rendering time since during that time
76  // the clock is already captured, but there is zero chance
77  // for the clock to appear on screen (until done rendering)
78  d.begin_render();
79 
80  Mat display = Mat::zeros(display_h, display_w, CV_8UC1);
81 
82  // Copy preview image generated by the detector (to not waste time in the main thread)
83  d.copy_preview_to(display);
84 
85  // Pack the next value into binary form
86  packer.try_pack(next_value);
87 
88  // Render the clock encoded in circles
89  for (int i = 0; i < digits + 2; i++)
90  {
91  const int rad = circle_w / 2 - 6;
92  // Always render the corner two circles (as markers)
93  if (i == 0 || i == digits + 1 || packer.get()[i - 1])
94  {
95  circle(display, Point(50 + circle_w * i + rad, 70 + rad),
96  rad, Scalar(255, 255, 255), -1, 8);
97  }
98  }
99 
100  // Display current frame. WaitKey is doing the actual rendering
101  imshow(window_name, display);
102  if (waitKey(1) >= 0) break;
103 
104  // Report rendering of current frame is done
105  d.end_render();
106  }
107 
108  return EXIT_SUCCESS;
109 }
110 catch (const rs2::error & e)
111 {
112  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
113  return EXIT_FAILURE;
114 }
115 catch (const std::exception& e)
116 {
117  std::cerr << e.what() << std::endl;
118  return EXIT_FAILURE;
119 }
120 
121 
122 
frameset wait_for_frames(unsigned int timeout_ms=RS2_DEFAULT_TIMEOUT) const
static const textual_icon circle
Definition: model-views.h:243
bool try_pack(int number)
Definition: cah-model.h:10
d
Definition: rmse.py:171
void end_render()
e
Definition: rmse.py:177
void begin_render()
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
GLdouble f
void submit_frame(rs2::frame f)
void enable_stream(rs2_stream stream_type, int stream_index, int width, int height, rs2_format format=RS2_FORMAT_ANY, int framerate=0)
int get_next_value()
int main(int argc, char *argv[])
std::ostream & cerr()
void display(void)
Definition: boing.c:194
int i
pipeline_profile start()
std::vector< bool > & get()
const std::string & get_failed_function() const
Definition: rs_types.hpp:112
void copy_preview_to(cv::Mat &display)
::geometry_msgs::Point_< std::allocator< void > > Point
Definition: Point.h:57


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40