forked from IntelRealSense/librealsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rs-rotate-pc.cpp
174 lines (127 loc) · 4.76 KB
/
rs-rotate-pc.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <librealsense2/rs.hpp> // Include RealSense Cross Platform API
#include <opencv2/opencv.hpp> // Include OpenCV API
using namespace cv;
bool is_yaw{false};
bool is_roll{false};
cv::RotateFlags angle(cv::RotateFlags::ROTATE_90_CLOCKWISE);
void changeMode() {
static int i = 0;
i++;
if (i==5) i=0;
switch (i) {
case 0:
is_yaw = false;
is_roll = false;
break;
case 1:
is_yaw = true;
is_roll = false;
break;
case 2:
is_yaw = false;
is_roll = true;
angle = cv::RotateFlags::ROTATE_90_CLOCKWISE;
break;
case 3:
is_yaw = false;
is_roll = true;
angle = cv::RotateFlags::ROTATE_180;
break;
case 4:
is_yaw = false;
is_roll = true;
angle = cv::RotateFlags::ROTATE_90_COUNTERCLOCKWISE;
break;
default:
break;
}
}
int main(int argc, char * argv[]) try
{
// Declare depth colorizer for pretty visualization of depth data
rs2::colorizer color_map;
// Aligning frames to color size
rs2::align depthToColorAlignment(RS2_STREAM_COLOR);
// Declare threshold filter for work with dots in range
rs2::threshold_filter threshold;
float threshold_min = 0.3f;
float threshold_max = 1.5f;
// Keep dots on the depth frame in range
threshold.set_option(RS2_OPTION_MIN_DISTANCE, threshold_min);
threshold.set_option(RS2_OPTION_MAX_DISTANCE, threshold_max);
// Declare RealSense pipeline, encapsulating the actual device and sensors
rs2::pipeline pipe;
// Start streaming with default recommended configuration
pipe.start();
rs2::processing_block procBlock( [&](rs2::frame f, rs2::frame_source& src )
{
const int origWidth = f.as<rs2::video_frame>().get_width();
const int origHeight = f.as<rs2::video_frame>().get_height();
cv::Mat image(cv::Size(origWidth, origHeight), CV_16UC1, (void*)f.get_data(), cv::Mat::AUTO_STEP);
cv::Mat rotated;
if ( !is_yaw && !is_roll )
rotated = image;
if ( is_yaw ) {
int rotWidth(static_cast<int>(threshold_max * 1000));
rotated = cv::Mat::zeros(cv::Size(rotWidth, image.size().height), CV_16UC1 );
for(int h = 0; h < rotated.size().height; h++) {
for(int w = 0; w < rotated.size().width; w++) {
if ( ( h >= image.size().height ) || ( w >= image.size().width ) )
continue;
unsigned short value = image.at<unsigned short>(h, w);
if ( value == 0 )
continue;
rotated.at<unsigned short>( h, value ) = w;
}
}
}
if (is_roll) {
cv::rotate( image, rotated, angle );
}
auto res = src.allocate_video_frame(f.get_profile(), f, 0, rotated.size().width, rotated.size().height);
memcpy((void*)res.get_data(), rotated.data, rotated.size().width * rotated.size().height * 2);
src.frame_ready(res);
});
rs2::frame_queue frame_queue;
procBlock.start(frame_queue);
while ( true )
{
// get set of frames
rs2::frameset frames = pipe.wait_for_frames(); // Wait for next set of frames from the camera
// align images
frames = depthToColorAlignment.process(frames);
// get depth frames
rs2::frame depthFrame = frames.get_depth_frame();
// keep points in range from threshold_min to threshold_max
depthFrame = threshold.process(depthFrame);
// call processing block for handle cloud point
procBlock.invoke( depthFrame );
depthFrame = frame_queue.wait_for_frame();
// Query frame size (width and height)
const int w = depthFrame.as<rs2::video_frame>().get_width();
const int h = depthFrame.as<rs2::video_frame>().get_height();
// Create OpenCV matrix of size (w,h) from the colorized depth data
cv::Mat image(cv::Size(w, h), CV_8UC3, (void*)depthFrame.apply_filter(color_map).get_data());
// Rescale image for convenience
if ( ( image.size().width > 1000 ) || (image.size().height > 1000) )
resize( image, image, Size(), 0.5, 0.5);
// Update the window with new data
imshow("window_name", image);
int k = waitKey(1);
if ( k == 'q' )
break;
if ( k == 'c' )
changeMode();
}
return EXIT_SUCCESS;
}
catch (const rs2::error & e)
{
std::cerr << "RealSense error calling " << e.get_failed_function() << "(" << e.get_failed_args() << "):\n " << e.what() << std::endl;
return EXIT_FAILURE;
}
catch (const std::exception& e)
{
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}