【发布时间】:2016-02-03 23:34:54
【问题描述】:
这是我用来在不同线程上生成带有一些内容的 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();
// ...
问题是窗口最终总是黑色的(在这种情况下它应该是紫色的)。我显然在这里遗漏了一些东西....
【问题讨论】:
-
尝试使用 cv::namedWindow 在主线程中创建窗口
-
窗口在主线程中创建:在我的主线程中调用 Capture 的构造函数(这是创建窗口的位置),然后 Capture::run 生成一个新线程。
-
CAPTURE_WINDOW_NAME 和“sth”有什么区别?
-
无,抱歉我已经编辑了原文
-
你能在 imshow 之前 adf 一个 cout,在 imshow 之后一个 cout,在 waitKey 之后一个 cout 来检查它是冻结在那里还是不显示?
标签: c++ multithreading opencv highgui