Trying to close OpenCV window has no effect
I'm capturing the webcam image with OpenCV. That works fine. But if I want to close the OpenCV when a button is pressed, it does not work (tried both cvDestroyWindow("NameOfWindow")
and cvDestroyAllWindows()
). The window stays open and the application is still running.
The OpenCV was initialized on separate thread from the main GUI.
I'm using the Juce Framework with C++ on my Mac开发者_Go百科. But the same problem occurs also on Windows with Qt and Windows Forms, when the OpenCV Window has it's own cvNamedWindow.
Here is the basic code of the VST plugin editor class:
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) {
}
The piece you may be missing is a call to the cvStartWindowThread
function.
On Linux, using the GTK HighGUI, this example reproduced your problem, until I put in the call to 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;
}
If cvStartWindowThread
doesn't help, try doing an extra call to cvWaitKey
after your cvDestroy
call.
To run the example, compile it with GCC:
g++ destroy_window.cpp -o destroy_window -lopencv_core -lopencv_highgui
精彩评论