cv::imshow 在不同的线程上不显示 cv::mat 颜色
cv::imshow does not display cv::mat color when on different thread
这是我用来生成一个HighGui窗口的类,其中包含不同线程上的一些内容。
class Capture {
private:
bool running;
std::thread thread;
cv::Mat background;
void loop() {
while (running) {
cv::imshow("sth",background);
cv::waitKey(settings::capture_wait_time);
}
}
public:
Capture() :
running {false},
thread {},
background { 800, 800, CV_8UC3, cv::Scalar{255,0,255}} {
cv::namedWindow("sth");
}
inline ~Capture() {
if (running) stop(); // stop and join the thread
cv::destroyWindow("sth");
}
void run() {
if (!running) {
running = true;
thread = std::thread{[this]{loop();}};
}
}
inline void join() { if (thread.joinable()) thread.join(); };
inline void stop() {
running = false;
if (thread.joinable()) thread.join();
}
};
// main
Capture cap;
cap.run();
// ...
问题是窗口最终总是黑色的(在这种情况下应该是紫色的)。我显然在这里错过了一些东西....
您似乎无法在另一个线程中创建窗口。此外,您在另一个线程上调用成员函数的方式似乎是错误的。
看看这段代码。它显示一个在不同线程中每秒更改的图像,并在 5 秒后返回。
#include <opencv2/opencv.hpp>
#include <thread>
using namespace std;
using namespace cv;
class Capture {
private:
bool running;
std::thread thread;
cv::Mat background;
void loop() {
while (running) {
cv::imshow("sth", background);
cv::waitKey(1000);
Scalar color(rand()&255, rand()&255, rand()&255);
background.setTo(color);
}
}
public:
Capture() :
running{ false },
thread{},
background{ 800, 800, CV_8UC3, cv::Scalar{ 255, 0, 255 } } {
}
inline ~Capture() {
if (running) stop(); // stop and join the thread
}
void run() {
if (!running) {
running = true;
thread = std::thread{ &Capture::loop, this };
}
}
inline void join() { if (thread.joinable()) thread.join(); };
inline void stop() {
running = false;
if (thread.joinable()) {
thread.join();
}
}
};
int main()
{
Capture cap;
cap.run();
std::this_thread::sleep_for(std::chrono::milliseconds(5000));
cap.stop();
return 0;
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 程序崩溃并显示"std::out_of_range"错误
- 在 QML VideoOutput 中将 cv::mat 显示为 QVideoFrame
- C++/openCV - 为什么显示图像会产生 Linux 分割错误?(打开CV错误?
- 在qpixMap中显示一个CV :: MAT(键入CV_32F)
- 在图片框中显示cv::Mat格式的网络摄像头提要
- 如何显示窗口大小不同的输出图像?通过使用 cv::Mat
- OpenCV 2.4:在MFC中显示cv::Mat
- 将我的阵列移动到 Mat 并使用打开的 CV 显示图像
- 使用直接显示控制相机,并使用打开的CV捕获图像
- 为什么我无法在 Open CV 中正确显示矩形?
- cv::imshow 在不同的线程上不显示 cv::mat 颜色
- 如何在Windows窗体应用程序中显示cv::Mat
- 显示带有cv::viz模块的WCloud
- 调试断言失败!在人脸识别打开Cv的检测和显示功能中