-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathpost_processing.cpp
209 lines (169 loc) · 7.39 KB
/
post_processing.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
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
#include "window.hpp"
#include <iostream>
extern "C" {
#include <stdlib.h>
#include <libobsensor/h/Error.h>
#include <libobsensor/h/Frame.h>
#include <libobsensor/h/ObTypes.h>
#include <libobsensor/h/Pipeline.h>
#include <libobsensor/h/StreamProfile.h>
#include <libobsensor/h/Device.h>
}
/*
*This sample is written in C++ code, based on the C language version API of OrbbecSDK.
*/
void check_error(ob_error *error) {
if(error) {
printf("ob_error was raised: \n\tcall: %s(%s)\n", ob_error_function(error), ob_error_args(error));
printf("\tmessage: %s\n", ob_error_message(error));
printf("\terror type: %d\n", ob_error_exception_type(error));
ob_delete_error(error);
exit(EXIT_FAILURE);
}
}
int main(int argc, char **args) {
Window *win = nullptr; // render window, based on opencv
ob_error *error = NULL; // Used to return SDK interface error information
ob_pipeline *pipeline = nullptr; // pipeline, used to open the depth stream after connecting the device
// Create a pipeline to open the depth stream after connecting the device
pipeline = ob_create_pipeline(&error);
check_error(error);
// Create config to configure the resolution, frame rate, and format of the depth stream
ob_config *config = ob_create_config(&error);
check_error(error);
// Configure the depth stream
ob_stream_profile *depth_profile = NULL;
ob_stream_profile_list *profiles = ob_pipeline_get_stream_profile_list(pipeline, OB_SENSOR_DEPTH, &error);
check_error(error);
// Find the corresponding profile according to the specified format, first look for the y16 format
depth_profile = ob_stream_profile_list_get_video_stream_profile(profiles, 640, OB_HEIGHT_ANY, OB_FORMAT_Y16, 30, &error);
// If the specified format is not found, search for the default profile to open the stream
if(error) {
depth_profile = ob_stream_profile_list_get_profile(profiles, OB_PROFILE_DEFAULT, &error);
ob_delete_error(error);
error = nullptr;
}
// enable stream
ob_config_enable_stream(config, depth_profile, &error);
check_error(error);
// Get device
ob_device *device = ob_pipeline_get_device(pipeline, &error);
check_error(error);
// Get depth sensor
ob_sensor *depthSensor = ob_device_get_sensor(device, OB_SENSOR_DEPTH, &error);
check_error(error);
// Get recommended post processor filter list
ob_filter_list *filterList = ob_sensor_get_recommended_filter_list(depthSensor, &error);
check_error(error);
uint32_t count = ob_filter_list_get_count(filterList, &error);
check_error(error);
std::vector<ob_filter *> filters;
ob_filter *dec_filter = nullptr;
for(uint32_t i = 0; i < count; i++) {
ob_filter *obFilter = ob_get_filter(filterList, i, &error);
check_error(error);
const char *flterName = ob_get_filter_name(obFilter, &error);
check_error(error);
printf(" Depth recommended post processor filter name: %s\n", flterName);
if(std::string(flterName) == "DecimationFilter") {
dec_filter = obFilter;
}
filters.push_back(obFilter);
}
// Start the pipeline with config
ob_pipeline_start_with_config(pipeline, config, &error);
check_error(error);
// Create a window for rendering, and set the resolution of the window
uint32_t width = ob_video_stream_profile_width(depth_profile, &error);
check_error(error);
uint32_t height = ob_video_stream_profile_height(depth_profile, &error);
check_error(error);
win = new Window("DepthViewer", width, height);
check_error(error);
bool resize_win = ob_filter_is_enable(dec_filter, &error);
check_error(error);
// Wait in a loop, exit after the window receives the "esc" key
while(*win) {
// Wait for up to 100ms for a frameset in blocking mode.
ob_frame *frameset = ob_pipeline_wait_for_frameset(pipeline, 100, &error);
check_error(error);
if(frameset == nullptr) {
continue;
}
ob_frame *depth_frame = ob_frameset_depth_frame(frameset, &error);
check_error(error);
if(depth_frame != nullptr) {
for(uint32_t i = 0; i < count; i++) {
ob_frame * new_depth_frame = nullptr;
ob_filter *obFilter = filters[i];
bool enable = ob_filter_is_enable(obFilter, &error);
check_error(error);
if(enable) {
new_depth_frame = ob_filter_process(obFilter, depth_frame, &error);
check_error(error);
ob_delete_frame(depth_frame, &error);
check_error(error);
depth_frame = new_depth_frame;
}
}
}
uint32_t width, height;
if(depth_frame != nullptr) {
// for Y16 format depth frame, print the distance of the center pixel every 30 frames
uint32_t index = ob_frame_index(depth_frame, &error);
check_error(error);
ob_format format = ob_frame_format(depth_frame, &error);
check_error(error);
if(index % 30 == 0 && format == OB_FORMAT_Y16) {
uint32_t width = ob_video_frame_width(depth_frame, &error);
check_error(error);
uint32_t height = ob_video_frame_height(depth_frame, &error);
check_error(error);
float scale = ob_depth_frame_get_value_scale(depth_frame, &error);
check_error(error);
uint16_t *data = (uint16_t *)ob_frame_data(depth_frame, &error);
check_error(error);
// pixel value multiplied by scale is the actual distance value in millimeters
float center_distance = data[width * height / 2 + width / 2] * scale;
// attention: if the distance is 0, it means that the depth camera cannot detect the object(may be out of detection range)
printf("Facing an object %.2f mm away.\n", center_distance);
}
if(resize_win) {
uint32_t width = ob_video_frame_width(depth_frame, &error);
check_error(error);
uint32_t height = ob_video_frame_height(depth_frame, &error);
check_error(error);
win->resize(width, height);
resize_win = false;
}
// add frame to render
// attention: the frame will be released inside the window,for user's code should release it by call ob_delete_frame()
win->addToRender(depth_frame);
}
ob_delete_frame(frameset, &error);
check_error(error);
};
for(uint32_t i = 0; i < count; i++) {
ob_filter *obFilter = filters[i];
ob_delete_filter(obFilter, &error);
check_error(error);
}
// destroy profile filterList
ob_delete_filter_list(filterList, &error);
check_error(error);
// stop the pipeline
ob_pipeline_stop(pipeline, &error);
check_error(error);
// destroy the window
delete win;
// destroy profile
ob_delete_stream_profile(depth_profile, &error);
check_error(error);
// destroy profile list
ob_delete_stream_profile_list(profiles, &error);
check_error(error);
// destroy the pipeline
ob_delete_pipeline(pipeline, &error);
check_error(error);
return 0;
}