试图关闭OpenCV窗口无效

时间:2011-08-21 17:45:42

标签: 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) {   
}   

1 个答案:

答案 0 :(得分: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;
}

如果cvStartWindowThread无效,请在cvWaitKey来电后再尝试拨打cvDestroy

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

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