Skip to content

Commit 6b8f84e

Browse files
author
yujianbin
committed
feat(imee): add mutex fo queue
1 parent 2c0d2ac commit 6b8f84e

11 files changed

+118
-49
lines changed

demo/get_depth.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "times.h"
1818
#include "types.h"
1919
#include <queue>
20-
20+
#include <mutex>
2121
using namespace indem;
2222

2323
template <typename T> void clear(std::queue<T> &q) {
@@ -35,20 +35,25 @@ int main(int argc, char **argv) {
3535

3636
m_pSDK->Init(config);
3737
std::queue<cv::Mat> depth_queue;
38+
std::mutex mutex_depth;
3839
int depth_count = 0;
3940
if (m_pSDK->EnableDepthProcessor()) {
4041
m_pSDK->RegistDepthCallback(
41-
[&depth_count, &depth_queue](double time, cv::Mat depth) {
42+
[&depth_count, &depth_queue, &mutex_depth](double time, cv::Mat depth) {
4243
if (!depth.empty()) {
4344
depth.convertTo(depth, CV_16U, 1000.0);
44-
depth_queue.push(depth);
45+
{
46+
std::unique_lock<std::mutex> u_lock(mutex_depth);
47+
depth_queue.push(depth);
48+
}
4549
++depth_count;
4650
}
4751
});
4852
}
4953
auto &&time_beg = times::now();
5054
while (true) {
5155
if (!depth_queue.empty()) {
56+
std::unique_lock<std::mutex> u_lock(mutex_depth);
5257
cv::imshow("depth", depth_queue.front());
5358
clear(depth_queue);
5459
}

demo/get_depth_with_region.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <opencv2/imgproc.hpp>
2020
#include <opencv2/opencv.hpp>
2121
#include <queue>
22-
22+
#include <mutex>
2323
using namespace indem;
2424

2525
static cv::Mat cv_in_left, cv_in_left_inv;
@@ -186,44 +186,58 @@ int main(int argc, char **argv) {
186186
return os.str();
187187
};
188188
std::queue<cv::Mat> disparity_queue, depth_queue;
189+
std::mutex mutex_depth;
190+
std::mutex mutex_disparity;
189191
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) {
191193
if (!depth.empty()) {
192194
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+
}
194199
}
195200
});
196201
}
197202
if (m_pSDK->EnableDisparityProcessor()) {
198203
m_pSDK->RegistDisparityCallback(
199-
[&disparity_queue](double time, cv::Mat disparity) {
204+
[&disparity_queue, &mutex_disparity](double time, cv::Mat disparity) {
200205
if (!disparity.empty()) {
201206
disparity.convertTo(disparity, CV_8U, 255. / (16 * 64));
202207
cv::applyColorMap(disparity, disparity, cv::COLORMAP_JET);
203208
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+
}
205213
}
206214
});
207215
}
208216

209217
while (true) {
210218
if (!depth_queue.empty() && !disparity_queue.empty()) {
211219
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+
}
213225
cv::setMouseCallback("depth", OnDepthMouseCallback, &depth_region);
214226
// 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) {
219233
if (elem >= 10000) {
220-
return std::string("invalid");
234+
return std::string("invalid");
221235
}
222236
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+
}
227241
}
228242
char key = static_cast<char>(cv::waitKey(1));
229243
if (key == 27 || key == 'q' || key == 'Q') { // ESC/Q

demo/get_detector.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "times.h"
1818
#include "types.h"
1919
#include <queue>
20-
20+
#include <mutex>
2121
using namespace indem;
2222

2323
template <typename T> void clear(std::queue<T> &q) {
@@ -35,16 +35,20 @@ int main(int argc, char **argv) {
3535

3636
m_pSDK->Init(config);
3737
std::queue<cv::Mat> detector_queue;
38+
std::mutex mutex_detector;
3839
int detector_count = 0;
3940
std::string name[10] = {
4041
"BG", "PERSON", "PET_CAT", "PET_DOG", "SOFA",
4142
"TABLE", "BED", "EXCREMENT", "WIRE", "KEY",
4243
};
4344
m_pSDK->EnableDetectorProcessor();
4445
m_pSDK->RegistDetectorCallback(
45-
[&detector_count, &detector_queue, &name](DetectorInfo info) {
46+
[&detector_count, &detector_queue, &name, &mutex_detector](DetectorInfo info) {
4647
if (!info.img.empty()) {
47-
detector_queue.push(info.img);
48+
{
49+
std::unique_lock<std::mutex> lock(mutex_detector);
50+
detector_queue.push(info.img);
51+
}
4852
++detector_count;
4953
for (int i = 0; i < info.finalBoxInfo.size(); ++i) {
5054
BoxInfo &obj = info.finalBoxInfo[i];
@@ -59,6 +63,7 @@ int main(int argc, char **argv) {
5963
auto &&time_beg = times::now();
6064
while (true) {
6165
if (!detector_queue.empty()) {
66+
std::unique_lock<std::mutex> lock(mutex_detector);
6267
cv::imshow("detector", detector_queue.front());
6368
clear(detector_queue);
6469
}

demo/get_disparity.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <opencv2/imgproc.hpp>
2121
#include <opencv2/opencv.hpp>
2222
#include <queue>
23-
23+
#include <mutex>
2424
using namespace indem;
2525

2626
template <typename T> void clear(std::queue<T> &q) {
@@ -38,24 +38,29 @@ int main(int argc, char **argv) {
3838

3939
m_pSDK->Init(config);
4040
std::queue<cv::Mat> disparity_queue;
41+
std::mutex mutex_disparity;
4142
int disparity_count = 0;
4243
if (m_pSDK->EnableDisparityProcessor()) {
4344
// m_pSDK->EnableLRConsistencyCheck();
4445
// m_pSDK->SetDepthCalMode(DepthCalMode::HIGH_ACCURACY);
4546
m_pSDK->RegistDisparityCallback(
46-
[&disparity_count, &disparity_queue](double time, cv::Mat disparity) {
47+
[&disparity_count, &disparity_queue, &mutex_disparity](double time, cv::Mat disparity) {
4748
if (!disparity.empty()) {
4849
disparity.convertTo(disparity, CV_8U, 255. / (16 * 64));
4950
cv::applyColorMap(disparity, disparity, cv::COLORMAP_JET);
5051
disparity.setTo(0, disparity == -16);
51-
disparity_queue.push(disparity);
52+
{
53+
std::unique_lock<std::mutex> lock(mutex_disparity);
54+
disparity_queue.push(disparity);
55+
}
5256
++disparity_count;
5357
}
5458
});
5559
}
5660
auto &&time_beg = times::now();
5761
while (true) {
5862
if (!disparity_queue.empty()) {
63+
std::unique_lock<std::mutex> lock(mutex_disparity);
5964
cv::imshow("disparity", disparity_queue.front());
6065
clear(disparity_queue);
6166
}

demo/get_disparity_with_high_accuracy.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <opencv2/imgproc.hpp>
2121
#include <opencv2/opencv.hpp>
2222
#include <queue>
23-
23+
#include <mutex>
2424
using namespace indem;
2525

2626
template <typename T> void clear(std::queue<T> &q) {
@@ -38,24 +38,29 @@ int main(int argc, char **argv) {
3838

3939
m_pSDK->Init(config);
4040
std::queue<cv::Mat> disparity_queue;
41+
std::mutex mutex_disparity;
4142
int disparity_count = 0;
4243
if (m_pSDK->EnableDisparityProcessor()) {
4344
m_pSDK->EnableLRConsistencyCheck();
4445
m_pSDK->SetDepthCalMode(DepthCalMode::HIGH_ACCURACY);
4546
m_pSDK->RegistDisparityCallback(
46-
[&disparity_count, &disparity_queue](double time, cv::Mat disparity) {
47+
[&disparity_count, &disparity_queue, &mutex_disparity](double time, cv::Mat disparity) {
4748
if (!disparity.empty()) {
4849
disparity.convertTo(disparity, CV_8U, 255. / (16 * 64));
4950
cv::applyColorMap(disparity, disparity, cv::COLORMAP_JET);
5051
disparity.setTo(0, disparity == -16);
51-
disparity_queue.push(disparity);
52+
{
53+
std::unique_lock<std::mutex> lock(mutex_disparity);
54+
disparity_queue.push(disparity);
55+
}
5256
++disparity_count;
5357
}
5458
});
5559
}
5660
auto &&time_beg = times::now();
5761
while (true) {
5862
if (!disparity_queue.empty()) {
63+
std::unique_lock<std::mutex> lock(mutex_disparity);
5964
cv::imshow("disparity", disparity_queue.front());
6065
clear(disparity_queue);
6166
}

demo/get_disparity_with_lr_check.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include <opencv2/imgproc.hpp>
2121
#include <opencv2/opencv.hpp>
2222
#include <queue>
23-
23+
#include <mutex>
2424
using namespace indem;
2525

2626
template <typename T> void clear(std::queue<T> &q) {
@@ -38,24 +38,29 @@ int main(int argc, char **argv) {
3838

3939
m_pSDK->Init(config);
4040
std::queue<cv::Mat> disparity_queue;
41+
std::mutex mutex_disparity;
4142
int disparity_count = 0;
4243
if (m_pSDK->EnableDisparityProcessor()) {
4344
m_pSDK->EnableLRConsistencyCheck();
4445
// m_pSDK->SetDepthCalMode(DepthCalMode::HIGH_ACCURACY);
4546
m_pSDK->RegistDisparityCallback(
46-
[&disparity_count, &disparity_queue](double time, cv::Mat disparity) {
47+
[&disparity_count, &disparity_queue, &mutex_disparity](double time, cv::Mat disparity) {
4748
if (!disparity.empty()) {
4849
disparity.convertTo(disparity, CV_8U, 255. / (16 * 64));
4950
cv::applyColorMap(disparity, disparity, cv::COLORMAP_JET);
5051
disparity.setTo(0, disparity == -16);
51-
disparity_queue.push(disparity);
52+
{
53+
std::unique_lock<std::mutex> lock(mutex_disparity);
54+
disparity_queue.push(disparity);
55+
}
5256
++disparity_count;
5357
}
5458
});
5559
}
5660
auto &&time_beg = times::now();
5761
while (true) {
5862
if (!disparity_queue.empty()) {
63+
std::unique_lock<std::mutex> lock(mutex_disparity);
5964
cv::imshow("disparity", disparity_queue.front());
6065
clear(disparity_queue);
6166
}

demo/get_image.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "types.h"
2020
#include <iostream>
2121
#include <queue>
22+
#include <mutex>
2223

2324
#define FONT_FACE cv::FONT_HERSHEY_PLAIN
2425
#define FONT_SCALE 1
@@ -43,9 +44,11 @@ int main(int argc, char **argv) {
4344
m_pSDK->Init(config);
4445

4546
std::queue<cv::Mat> image_queue;
47+
std::mutex mutex_image;
4648
int img_count = 0;
4749
double last_img_time = -1.0;
48-
m_pSDK->RegistImgCallback([&last_img_time, &img_count, &image_queue](
50+
m_pSDK->RegistImgCallback([&last_img_time, &img_count, &image_queue,
51+
&mutex_image](
4952
double time, cv::Mat left, cv::Mat right) {
5053
if (!left.empty() && !right.empty()) {
5154
cv::Mat img;
@@ -59,13 +62,17 @@ int main(int argc, char **argv) {
5962
FONT_COLOR, THICKNESS);
6063
}
6164
last_img_time = time;
62-
image_queue.push(img);
65+
{
66+
std::unique_lock<std::mutex> lock(mutex_image);
67+
image_queue.push(img);
68+
}
6369
++img_count;
6470
}
6571
});
6672
auto &&time_beg = times::now();
6773
while (true) {
6874
if (!image_queue.empty()) {
75+
std::unique_lock<std::mutex> lock(mutex_image);
6976
cv::imshow("image", image_queue.front());
7077
clear(image_queue);
7178
}

demo/get_imu.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "times.h"
1818
#include "types.h"
1919
#include <queue>
20-
20+
#include <mutex>
2121
using namespace indem;
2222

2323
template <typename T> void clear(std::queue<T> &q) {
@@ -36,14 +36,17 @@ int main(int argc, char **argv) {
3636
m_pSDK->Init(config);
3737

3838
std::queue<cv::Mat> image_queue;
39-
39+
std::mutex mutex_image;
4040
int img_count = 0;
4141
m_pSDK->RegistImgCallback(
42-
[&img_count, &image_queue](double time, cv::Mat left, cv::Mat right) {
42+
[&img_count, &image_queue, &mutex_image](double time, cv::Mat left, cv::Mat right) {
4343
if (!left.empty() && !right.empty()) {
4444
cv::Mat img;
4545
cv::hconcat(left, right, img);
46-
image_queue.push(img);
46+
{
47+
std::unique_lock<std::mutex> lock(mutex_image);
48+
image_queue.push(img);
49+
}
4750
++img_count;
4851
}
4952
});
@@ -63,6 +66,7 @@ int main(int argc, char **argv) {
6366
auto &&time_beg = times::now();
6467
while (true) {
6568
if (!image_queue.empty()) {
69+
std::unique_lock<std::mutex> lock(mutex_image);
6670
cv::imshow("image", image_queue.front());
6771
clear(image_queue);
6872
}

demo/get_points.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "types.h"
1919
#include "util_pcl.h"
2020
#include <queue>
21-
21+
#include <mutex>
2222
using namespace indem;
2323

2424
template <typename T> void clear(std::queue<T> &q) {
@@ -36,14 +36,18 @@ int main(int argc, char **argv) {
3636

3737
m_pSDK->Init(config);
3838
std::queue<cv::Mat> points_queue;
39+
std::mutex mutex_points;
3940
int points_count = 0;
4041
if (m_pSDK->EnablePointProcessor()) {
4142
// m_pSDK->EnableLRConsistencyCheck();
4243
// m_pSDK->SetDepthCalMode(DepthCalMode::HIGH_ACCURACY);
4344
m_pSDK->RegistPointCloudCallback(
44-
[&points_count, &points_queue](double time, cv::Mat points) {
45+
[&points_count, &points_queue, &mutex_points](double time, cv::Mat points) {
4546
if (!points.empty()) {
46-
points_queue.push(points);
47+
{
48+
std::unique_lock<std::mutex> lock(mutex_points);
49+
points_queue.push(points);
50+
}
4751
++points_count;
4852
}
4953
});
@@ -52,6 +56,7 @@ int main(int argc, char **argv) {
5256
auto &&time_beg = times::now();
5357
while (true) {
5458
if (!points_queue.empty()) {
59+
std::unique_lock<std::mutex> lock(mutex_points);
5560
pcviewer.Update(points_queue.front());
5661
clear(points_queue);
5762
}

0 commit comments

Comments
 (0)