cv::imshow 在不同的线程上不显示 cv::mat 颜色
cv::imshow does not display cv::mat color when on different thread
这是一个 class 我用来生成 HighGui window 的不同线程上的一些内容。
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();
// ...
问题是 window 最终总是黑色的(在本例中它应该是紫色的)。我显然在这里遗漏了一些东西....
您似乎无法在另一个线程中创建 window。另外,你在另一个线程上调用成员函数的方式似乎是错误的。
看看这段代码。它在不同的线程中显示每秒变化的图像,5 秒后 returns。
#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;
}
这是一个 class 我用来生成 HighGui window 的不同线程上的一些内容。
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();
// ...
问题是 window 最终总是黑色的(在本例中它应该是紫色的)。我显然在这里遗漏了一些东西....
您似乎无法在另一个线程中创建 window。另外,你在另一个线程上调用成员函数的方式似乎是错误的。
看看这段代码。它在不同的线程中显示每秒变化的图像,5 秒后 returns。
#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;
}