这是我用来在不同线程上生成带有一些内容的 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();
// ...
问题是窗口最终总是黑色的(在这种情况下它应该是紫色的)。我显然在这里遗漏了一些东西....