-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrealsense.cpp
More file actions
319 lines (227 loc) · 6.11 KB
/
Copy pathrealsense.cpp
File metadata and controls
319 lines (227 loc) · 6.11 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#include "realsense.h"
// Constructor
RealSense::RealSense()
{
// Initialize
initialize();
}
// Destructor
RealSense::~RealSense()
{
// Finalize
finalize();
}
// Processing
void RealSense::run()
{
// Main Loop
while( true ){
// Update Data
update();
// Draw Data
draw();
// Show Data
show();
// Key Check
const int32_t key = cv::waitKey( 10 );
if( key == 'q' ){
break;
}
}
}
// Initialize
void RealSense::initialize()
{
cv::setUseOptimized( true );
// Initialize Sensor
initializeSensor();
}
// Initialize Sensor
inline void RealSense::initializeSensor()
{
// Set Device Config
rs2::config config;
config.enable_stream( rs2_stream::RS2_STREAM_COLOR, color_width, color_height, rs2_format::RS2_FORMAT_BGR8, color_fps );
config.enable_stream( rs2_stream::RS2_STREAM_DEPTH, depth_width, depth_height, rs2_format::RS2_FORMAT_Z16, depth_fps );
// Start Pipeline
pipeline_profile = pipeline.start( config );
}
// Finalize
void RealSense::finalize()
{
// Close Windows
cv::destroyAllWindows();
// Stop Pipline
pipeline.stop();
}
// Update Data
void RealSense::update()
{
// Update Frame
updateFrame();
// Update Color
updateColor();
// Update Depth
updateDepth();
// Update DepthColor()
updateDepthColor();
}
// Update Frame
inline void RealSense::updateFrame()
{
// Update Frame
rs2::frameset frameset = pipeline.wait_for_frames();
dec.set_option(RS2_OPTION_FILTER_MAGNITUDE,2);
// Retrieve Aligned Frame
rs2::align align( rs2_stream::RS2_STREAM_COLOR );
aligned_frameset = align.process( frameset );
if( !aligned_frameset.size() ){
return;
}
// Option for holes filling
spat.set_option(RS2_OPTION_HOLES_FILL,4);
}
// Update Color
inline void RealSense::updateColor()
{
// Retrieve Color Frame
color_frame = aligned_frameset.get_color_frame();
// Retrive Frame Information
color_width = color_frame.as<rs2::video_frame>().get_width();
color_height = color_frame.as<rs2::video_frame>().get_height();
}
// Update Depth
inline void RealSense::updateDepth()
{
// Retrieve Depth Frame
depth_frame = aligned_frameset.get_depth_frame();
depth_frame = dec.process(depth_frame);
depth_frame = spat.process(depth_frame);
// Retrive Frame Information
depth_width = depth_frame.as<rs2::video_frame>().get_width();
depth_height = depth_frame.as<rs2::video_frame>().get_height();
}
// Update Depth Color
inline void RealSense::updateDepthColor()
{
// Retrieve Depth color Frame
depth_color_frame = rs2::colorizer().colorize(depth_frame);
}
// Draw Data
void RealSense::draw()
{
// Draw Color
drawColor();
// Draw Depth
drawDepth();
// Draw color Depth
drawDepthColor();
// Draw color Depth
drawDepthColor();
// initialize XYZ mat
drawXYZ();
}
// Draw Color
inline void RealSense::drawColor()
{
// Create cv::Mat form Color Frame
color_mat = cv::Mat( color_height, color_width, CV_8UC3, const_cast<void*>( color_frame.get_data() ) );
}
// Draw Depth
inline void RealSense::drawDepth()
{
// Create cv::Mat form Depth Frame
depth_mat = cv::Mat( depth_height, depth_width, CV_16UC1, const_cast<void*>( depth_frame.get_data() ) );
}
// Draw Depth
inline void RealSense::drawDepthColor()
{
// Create cv::Mat form Depth Frame
depth_color_mat = cv::Mat( depth_height, depth_width, CV_8UC3, const_cast<void*>( depth_color_frame.get_data() ) );
}
inline void RealSense::drawXYZ()
{
// Create cv::Mat form XYZ Frame
xyz_mat = cv::Mat( depth_mat.rows, depth_mat.cols, CV_32FC3 );
}
// Show Data
void RealSense::show()
{
// Show Color
showColor();
// Show Depth
showDepth();
// Show Depth Color
showDepthColor();
// Show XYZ Color and free memory
// for xyz mat
showXYZ();
}
// Show Color
inline void RealSense::showColor()
{
if( color_mat.empty() ){
return;
}
// Show Color Image
cv::imshow( "Color", color_mat );
}
// Show Depth
inline void RealSense::showDepth()
{
if( depth_mat.empty() ){
return;
}
// Scaling
depth_mat.convertTo( scale_mat, CV_8U, 1.0/10, 0. ); // 0-10000 -> 255(white)-0(black) alpha=-255/10000 beta->255
resize(scale_mat , scale_mat, color_mat.size());
// Show Depth Image
cv::imshow( "Depth", scale_mat );
}
// Show Depth Color
inline void RealSense::showDepthColor()
{
if( depth_color_mat.empty() ){
return;
}
resize(depth_color_mat , depth_color_mat, color_mat.size());
// Show Depth Image
cv::imshow( "Depth Color", depth_color_mat );
}
// Modification of the function dist_3d from librealsens
void dist_3d_mod(const rs2::depth_frame& frame, float*upixel, float* upoint)
{
auto udist = frame.get_distance(upixel[0], upixel[1]);
// Deproject from pixel to point in 3D
rs2_intrinsics intr = frame.get_profile().as<rs2::video_stream_profile>().get_intrinsics(); // Calibration data
rs2_deproject_pixel_to_point(upoint, &intr, upixel, udist);
}
// This will slow down the all program.
// It is not supposed to be used on the all frame.
// only for verification
inline void RealSense::showXYZ()
{
if( depth_mat.empty() ){
return;
}
rs2_intrinsics intr = depth_frame.get_profile().as<rs2::video_stream_profile>().get_intrinsics(); // Calibration data
float upixel[2]; // From pixel
float upoint[3]; // From point (in 3D)
int i=0;
int j=0;
for (i=0;i<depth_mat.rows ;i++) {
for(j=0;j<depth_mat.cols ;j++) {
upixel[0]=j;
upixel[1]=i;
dist_3d_mod(depth_frame,upixel,upoint);
xyz_mat.at<cv::Vec3f>(i,j)[0]=upoint[0];
xyz_mat.at<cv::Vec3f>(i,j)[1]=upoint[1];
xyz_mat.at<cv::Vec3f>(i,j)[2]=upoint[2];
}
}
// Resize to the RGB size and show XYZ Image
//(distance in each direction with respect to the camera center
resize(xyz_mat , xyz_mat, color_mat.size());
cv::imshow( "Depth Color xyz", xyz_mat );
xyz_mat.release();
}