|
19 | 19 | #include <opencv2/imgproc.hpp>
|
20 | 20 | #include <opencv2/opencv.hpp>
|
21 | 21 | #include <queue>
|
22 |
| - |
| 22 | +#include <mutex> |
23 | 23 | using namespace indem;
|
24 | 24 |
|
25 | 25 | static cv::Mat cv_in_left, cv_in_left_inv;
|
@@ -186,44 +186,58 @@ int main(int argc, char **argv) {
|
186 | 186 | return os.str();
|
187 | 187 | };
|
188 | 188 | std::queue<cv::Mat> disparity_queue, depth_queue;
|
| 189 | + std::mutex mutex_depth; |
| 190 | + std::mutex mutex_disparity; |
189 | 191 | if (m_pSDK->EnableDepthProcessor()) {
|
190 |
| - m_pSDK->RegistDepthCallback([&depth_queue](double time, cv::Mat depth) { |
| 192 | + m_pSDK->RegistDepthCallback([&depth_queue, &mutex_depth](double time, cv::Mat depth) { |
191 | 193 | if (!depth.empty()) {
|
192 | 194 | depth.convertTo(depth, CV_16U, 1000.0);
|
193 |
| - depth_queue.push(depth); |
| 195 | + { |
| 196 | + std::unique_lock<std::mutex> lock(mutex_depth); |
| 197 | + depth_queue.push(depth); |
| 198 | + } |
194 | 199 | }
|
195 | 200 | });
|
196 | 201 | }
|
197 | 202 | if (m_pSDK->EnableDisparityProcessor()) {
|
198 | 203 | m_pSDK->RegistDisparityCallback(
|
199 |
| - [&disparity_queue](double time, cv::Mat disparity) { |
| 204 | + [&disparity_queue, &mutex_disparity](double time, cv::Mat disparity) { |
200 | 205 | if (!disparity.empty()) {
|
201 | 206 | disparity.convertTo(disparity, CV_8U, 255. / (16 * 64));
|
202 | 207 | cv::applyColorMap(disparity, disparity, cv::COLORMAP_JET);
|
203 | 208 | disparity.setTo(0, disparity == -16);
|
204 |
| - disparity_queue.push(disparity); |
| 209 | + { |
| 210 | + std::unique_lock<std::mutex> lock(mutex_disparity); |
| 211 | + disparity_queue.push(disparity); |
| 212 | + } |
205 | 213 | }
|
206 | 214 | });
|
207 | 215 | }
|
208 | 216 |
|
209 | 217 | while (true) {
|
210 | 218 | if (!depth_queue.empty() && !disparity_queue.empty()) {
|
211 | 219 | cv::namedWindow("depth");
|
212 |
| - cv::imshow("depth", disparity_queue.front()); |
| 220 | + { |
| 221 | + std::unique_lock<std::mutex> lock(mutex_disparity); |
| 222 | + cv::imshow("depth", disparity_queue.front()); |
| 223 | + clear(disparity_queue); |
| 224 | + } |
213 | 225 | cv::setMouseCallback("depth", OnDepthMouseCallback, &depth_region);
|
214 | 226 | // Note: DrawRect will change some depth values to show the rect.
|
215 |
| - depth_region.DrawRect(depth_queue.front()); |
216 |
| - depth_region.ShowElems<ushort>( |
217 |
| - depth_queue.front(), |
218 |
| - [](const ushort &elem) { |
| 227 | + { |
| 228 | + std::unique_lock<std::mutex> lock(mutex_depth); |
| 229 | + depth_region.DrawRect(depth_queue.front()); |
| 230 | + depth_region.ShowElems<ushort>( |
| 231 | + depth_queue.front(), |
| 232 | + [](const ushort &elem) { |
219 | 233 | if (elem >= 10000) {
|
220 |
| - return std::string("invalid"); |
| 234 | + return std::string("invalid"); |
221 | 235 | }
|
222 | 236 | return std::to_string(elem);
|
223 |
| - }, |
224 |
| - 90, depth_info); |
225 |
| - clear(disparity_queue); |
226 |
| - clear(depth_queue); |
| 237 | + }, |
| 238 | + 90, depth_info); |
| 239 | + clear(depth_queue); |
| 240 | + } |
227 | 241 | }
|
228 | 242 | char key = static_cast<char>(cv::waitKey(1));
|
229 | 243 | if (key == 27 || key == 'q' || key == 'Q') { // ESC/Q
|
|
0 commit comments