rs-rotate-pc.cpp
Go to the documentation of this file.
1 
2 #include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
3 #include <opencv2/opencv.hpp> // Include OpenCV API
4 
5 using namespace cv;
6 
7 bool is_yaw{false};
8 bool is_roll{false};
9 
10 cv::RotateFlags angle(cv::RotateFlags::ROTATE_90_CLOCKWISE);
11 
12 void changeMode() {
13  static int i = 0;
14 
15  i++;
16  if (i==5) i=0;
17 
18  switch (i) {
19  case 0:
20  is_yaw = false;
21  is_roll = false;
22  break;
23  case 1:
24  is_yaw = true;
25  is_roll = false;
26  break;
27  case 2:
28  is_yaw = false;
29  is_roll = true;
30 
31  angle = cv::RotateFlags::ROTATE_90_CLOCKWISE;
32 
33  break;
34  case 3:
35  is_yaw = false;
36  is_roll = true;
37 
38  angle = cv::RotateFlags::ROTATE_180;
39 
40  break;
41  case 4:
42  is_yaw = false;
43  is_roll = true;
44 
45  angle = cv::RotateFlags::ROTATE_90_COUNTERCLOCKWISE;
46 
47  break;
48  default:
49  break;
50  }
51 }
52 
53 int main(int argc, char * argv[]) try
54 {
55  // Declare depth colorizer for pretty visualization of depth data
56  rs2::colorizer color_map;
57 
58  // Aligning frames to color size
59  rs2::align depthToColorAlignment(RS2_STREAM_COLOR);
60 
61  // Declare threshold filter for work with dots in range
62  rs2::threshold_filter threshold;
63  float threshold_min = 0.3f;
64  float threshold_max = 1.5f;
65 
66  // Keep dots on the depth frame in range
67  threshold.set_option(RS2_OPTION_MIN_DISTANCE, threshold_min);
68  threshold.set_option(RS2_OPTION_MAX_DISTANCE, threshold_max);
69 
70  // Declare RealSense pipeline, encapsulating the actual device and sensors
72  // Start streaming with default recommended configuration
73  pipe.start();
74 
76  {
77  const int origWidth = f.as<rs2::video_frame>().get_width();
78  const int origHeight = f.as<rs2::video_frame>().get_height();
79 
80  cv::Mat image(cv::Size(origWidth, origHeight), CV_16UC1, (void*)f.get_data(), cv::Mat::AUTO_STEP);
81  cv::Mat rotated;
82 
83  if ( !is_yaw && !is_roll )
84  rotated = image;
85 
86  if ( is_yaw ) {
87  int rotWidth(static_cast<int>(threshold_max * 1000));
88 
89  rotated = cv::Mat::zeros(cv::Size(rotWidth, image.size().height), CV_16UC1 );
90 
91  for(int h = 0; h < rotated.size().height; h++) {
92  for(int w = 0; w < rotated.size().width; w++) {
93 
94  if ( ( h >= image.size().height ) || ( w >= image.size().width ) )
95  continue;
96 
97  unsigned short value = image.at<unsigned short>(h, w);
98 
99  if ( value == 0 )
100  continue;
101 
102  rotated.at<unsigned short>( h, value ) = w;
103  }
104  }
105  }
106 
107  if (is_roll) {
108  cv::rotate( image, rotated, angle );
109  }
110 
111  auto res = src.allocate_video_frame(f.get_profile(), f, 0, rotated.size().width, rotated.size().height);
112  memcpy((void*)res.get_data(), rotated.data, rotated.size().width * rotated.size().height * 2);
113 
114  src.frame_ready(res);
115  });
116 
117  rs2::frame_queue frame_queue;
118  procBlock.start(frame_queue);
119 
120  while ( true )
121  {
122  // get set of frames
123  rs2::frameset frames = pipe.wait_for_frames(); // Wait for next set of frames from the camera
124 
125  // align images
126  frames = depthToColorAlignment.process(frames);
127 
128  // get depth frames
129  rs2::frame depthFrame = frames.get_depth_frame();
130 
131  // keep points in range from threshold_min to threshold_max
132  depthFrame = threshold.process(depthFrame);
133 
134  // call processing block for handle cloud point
135  procBlock.invoke( depthFrame );
136  depthFrame = frame_queue.wait_for_frame();
137 
138  // Query frame size (width and height)
139  const int w = depthFrame.as<rs2::video_frame>().get_width();
140  const int h = depthFrame.as<rs2::video_frame>().get_height();
141 
142  // Create OpenCV matrix of size (w,h) from the colorized depth data
143  cv::Mat image(cv::Size(w, h), CV_8UC3, (void*)depthFrame.apply_filter(color_map).get_data());
144 
145  // Rescale image for convenience
146  if ( ( image.size().width > 1000 ) || (image.size().height > 1000) )
147  resize( image, image, Size(), 0.5, 0.5);
148 
149  // Update the window with new data
150  imshow("window_name", image);
151  int k = waitKey(1);
152 
153  if ( k == 'q' )
154  break;
155 
156  if ( k == 'c' )
157  changeMode();
158  }
159 
160  return EXIT_SUCCESS;
161 }
162 catch (const rs2::error & e)
163 {
164  std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
165  return EXIT_FAILURE;
166 }
167 catch (const std::exception& e)
168 {
169  std::cerr << e.what() << std::endl;
170  return EXIT_FAILURE;
171 }
172 
173 
174 
frame apply_filter(filter_interface &filter)
Definition: rs_frame.hpp:593
frameset wait_for_frames(unsigned int timeout_ms=RS2_DEFAULT_TIMEOUT) const
GLfloat value
stream_profile get_profile() const
Definition: rs_frame.hpp:557
GLfloat angle
Definition: glext.h:6819
cv::RotateFlags angle(cv::RotateFlags::ROTATE_90_CLOCKWISE)
const void * get_data() const
Definition: rs_frame.hpp:545
GLdouble GLdouble GLdouble w
bool is_yaw
Definition: rs-rotate-pc.cpp:7
GLenum src
Definition: glext.h:1751
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:1960
e
Definition: rmse.py:177
void frame_ready(frame result) const
GLenum GLenum GLsizei void * image
depth_frame get_depth_frame() const
Definition: rs_frame.hpp:1006
frame allocate_video_frame(const stream_profile &profile, const frame &original, int new_bpp=0, int new_width=0, int new_height=0, int new_stride=0, rs2_extension frame_type=RS2_EXTENSION_VIDEO_FRAME) const
const std::string & get_failed_args() const
Definition: rs_types.hpp:117
int main(int argc, char *argv[])
GLdouble f
rs2::frame process(rs2::frame frame) const override
void changeMode()
frameset process(frameset frames)
bool is_roll
Definition: rs-rotate-pc.cpp:8
std::ostream & cerr()
int i
GLuint res
Definition: glext.h:8856
pipeline_profile start()
void set_option(rs2_option option, float value) const
Definition: rs_options.hpp:99
const std::string & get_failed_function() const
Definition: rs_types.hpp:112
T as() const
Definition: rs_frame.hpp:580


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