试图关闭OpenCV窗口没有效果

Trying to close OpenCV window has no effect

本文关键字:窗口 有效果 OpenCV      更新时间:2023-10-16

我正在用OpenCV捕捉网络摄像头图像。这很好。但是,如果我想在按下按钮时关闭OpenCV,它不起作用(尝试了cvDestroyWindow("NameOfWindow")cvDestroyAllWindows())。窗口保持打开状态,应用程序仍在运行。

OpenCV在独立于主GUI的线程上初始化。

我在Mac上使用c++的Juce框架。但同样的问题也发生在Windows上,当OpenCV窗口有它自己的cvNamedWindow时,Qt和Windows Forms。

下面是VST插件编辑器类的基本代码:

PluginEditor.cpp

#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include "PluginProcessor.h"
#include "PluginEditor.h"
//
TestAudioProcessorEditor::TestAudioProcessorEditor (TestAudioProcessor* ownerFilter)
: AudioProcessorEditor (ownerFilter)
{   
    // This is where our plugin's editor size is set.
    setSize (500, 500);
    // open the tracker
    openTracker();
}   
// code for opencv handling
TestAudioProcessorEditor::openTracker() {
    // KEY LINE: Start the window thread
    cvStartWindowThread();
    // Create a window in which the captured images will be presented
    cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE );  
    cvWaitKey(0);
    cvDestroyWindow( "Webcam" );
    // window should disappear!
}

TestAudioProcessorEditor::~TestAudioProcessorEditor()
{
}
// paint stuff on the vst plugin surface
void TestAudioProcessorEditor::paint (Graphics& g) {   
}   

您可能缺少的部分是对cvStartWindowThread函数的调用。

在Linux上,使用GTK HighGUI,这个例子再现了你的问题,直到我把cvStartWindowThread的调用。

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include <iostream>
using namespace std;
int main( int argc, char** argv )
{
    // KEY LINE: Start the window thread
    cvStartWindowThread();
    // Open a window
    cvNamedWindow("original", 0);
    // Wait until a key gets pressed inside the window
    cvWaitKey(0);
    // Close the window
    cvDestroyWindow("original");
    // Verify that the window is closed
    cout<<"The window should be closed now. (Press ENTER to continue.)"<<endl;
    string line;
    getline(cin, line);
    cout<<"Exiting..."<<endl;
}

如果cvStartWindowThread没有帮助,尝试在cvDestroy调用之后对cvWaitKey进行额外调用。

要运行该示例,请使用GCC编译它:

g++ destroy_window.cpp -o destroy_window -lopencv_core -lopencv_highgui