试图关闭OpenCV窗口没有任何效果

sn3*_*3ek 4 c++ multithreading opencv

我正在使用OpenCV捕获网络摄像头图像.这很好.但是,如果我想在按下一个按钮来关闭OpenCV的,这是行不通的(都尝试cvDestroyWindow("NameOfWindow")cvDestroyAllWindows()).窗口保持打开状态,应用程序仍在运行.

OpenCV在与主GUI的单独线程上初始化.

我在Mac上使用Juce Framework和C++.但是,当Windows上有Qt和Windows窗体时,当OpenCV窗口拥有自己的cvNamedWindow时,也会出现同样的问题.

以下是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) {   
}   
Run Code Online (Sandbox Code Playgroud)

Dan*_*ile 5

您可能缺少的部分是对该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;
}
Run Code Online (Sandbox Code Playgroud)

如果cvStartWindowThread没有帮助,请cvWaitKeycvDestroy通话后尝试拨打额外电话.

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

g++ destroy_window.cpp -o destroy_window -lopencv_core -lopencv_highgui
Run Code Online (Sandbox Code Playgroud)