-
Notifications
You must be signed in to change notification settings - Fork 18
/
SyncAlignViewer.cpp
189 lines (174 loc) · 6.4 KB
/
SyncAlignViewer.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
/**
*Synchronous alignment example
*
* In this example, the mirror status of the depth and the color may be inconsistent because the depth or color sensor does not support mirroring.
* As a result, the images displayed by the depth and the color are reversed. If this happens, just set the mirror interface to keep the two mirror states
* consistent.
* In addition, the resolution obtained by some devices may not support the D2C function, so the resolutions that support D2C, please refer to the
* product manual.
* For example: the D2C resolution supported by DaBai DCW is 640x360, but the actual resolution obtained in this example may be 640x480, at this time the user
* must refer to the product manual to select 640x360 resolution.
*/
#include "window.hpp"
#include "libobsensor/hpp/Pipeline.hpp"
#include "libobsensor/hpp/Error.hpp"
#include <mutex>
#include <thread>
static bool sync = false;
static bool started = true;
static bool hd2c = false;
static bool sd2c = true;
static float alpha = 0.5;
static int windowWidth = 0;
static int windowHeight = 0;
// key press event processing
void keyEventProcess(Window &app, ob::Pipeline &pipe, std::shared_ptr<ob::Config> config) {
////Get the key value
int key = app.waitKey(10);
if(key == '+' || key == '=') {
// Press the + key to increase alpha
alpha += 0.1f;
if(alpha >= 1.0f) {
alpha = 1.0f;
}
app.setAlpha(alpha);
}
else if(key == '-' || key == '_') {
// press - key to decrease alpha
alpha -= 0.1f;
if(alpha <= 0.0f) {
alpha = 0.0f;
}
app.setAlpha(alpha);
}
else if(key == 'D' || key == 'd') {
// Press the D key to switch the hardware D2C
try {
if(!hd2c) {
started = false;
pipe.stop();
hd2c = true;
sd2c = false;
config->setAlignMode(ALIGN_D2C_HW_MODE);
pipe.start(config);
started = true;
}
else {
started = false;
pipe.stop();
hd2c = false;
sd2c = false;
config->setAlignMode(ALIGN_DISABLE);
pipe.start(config);
started = true;
}
}
catch(std::exception &e) {
std::cerr << "Property not support" << std::endl;
}
}
else if(key == 'S' || key == 's') {
// Press the S key to switch the software D2C
try {
if(!sd2c) {
started = false;
pipe.stop();
sd2c = true;
hd2c = false;
config->setAlignMode(ALIGN_D2C_SW_MODE);
pipe.start(config);
started = true;
}
else {
started = false;
pipe.stop();
hd2c = false;
sd2c = false;
config->setAlignMode(ALIGN_DISABLE);
pipe.start(config);
started = true;
}
}
catch(std::exception &e) {
std::cerr << "Property not support" << std::endl;
}
}
else if(key == 'F' || key == 'f') {
// Press the F key to switch synchronization
sync = !sync;
if(sync) {
try {
// enable synchronization
pipe.enableFrameSync();
}
catch(...) {
std::cerr << "Sync not support" << std::endl;
}
}
else {
try {
// turn off sync
pipe.disableFrameSync();
}
catch(...) {
std::cerr << "Sync not support" << std::endl;
}
}
}
}
int main(int argc, char **argv) try {
// Create a pipeline with default device
ob::Pipeline pipe;
// Configure which streams to enable or disable for the Pipeline by creating a Config
std::shared_ptr<ob::Config> config = std::make_shared<ob::Config>();
std::shared_ptr<ob::VideoStreamProfile> colorProfile = nullptr;
try {
// Get all stream profiles of the color camera, including stream resolution, frame rate, and frame format
auto colorProfiles = pipe.getStreamProfileList(OB_SENSOR_COLOR);
if(colorProfiles) {
colorProfile = std::const_pointer_cast<ob::StreamProfile>(colorProfiles->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
}
config->enableStream(colorProfile);
}
catch(...) {
std::cerr << "Current device is not support color sensor!" << std::endl;
exit(EXIT_FAILURE);
}
// Get all stream profiles of the depth camera, including stream resolution, frame rate, and frame format
auto depthProfiles = pipe.getStreamProfileList(OB_SENSOR_DEPTH);
std::shared_ptr<ob::VideoStreamProfile> depthProfile = nullptr;
if(depthProfiles) {
depthProfile = std::const_pointer_cast<ob::StreamProfile>(depthProfiles->getProfile(OB_PROFILE_DEFAULT))->as<ob::VideoStreamProfile>();
}
config->enableStream(depthProfile);
// Configure the alignment mode as hardware D2C alignment
config->setAlignMode(ALIGN_D2C_HW_MODE);
// Start the pipeline with config
try {
pipe.start(config);
}
catch(ob::Error &e) {
std::cerr << "function:" << e.getName() << "\nargs:" << e.getArgs() << "\nmessage:" << e.getMessage() << "\ntype:" << e.getExceptionType() << std::endl;
}
// Create a window for rendering and set the resolution of the window
Window app("SyncAlignViewer", colorProfile->width(), colorProfile->height(), RENDER_OVERLAY);
while(app) {
keyEventProcess(app, pipe, config);
auto frameSet = pipe.waitForFrames(100);
if(frameSet == nullptr) {
continue;
}
auto colorFrame = frameSet->colorFrame();
auto depthFrame = frameSet->depthFrame();
if(colorFrame != nullptr && depthFrame != nullptr) {
app.addToRender({ colorFrame, depthFrame });
}
}
// Stop the Pipeline, no frame data will be generated
pipe.stop();
return 0;
}
catch(ob::Error &e) {
std::cerr << "function:" << e.getName() << "\nargs:" << e.getArgs() << "\nmessage:" << e.getMessage() << "\ntype:" << e.getExceptionType() << std::endl;
exit(EXIT_FAILURE);
}