尝试关闭OpenCV窗口没有任何效果 [英] Trying to close OpenCV window has no effect
问题描述
我使用OpenCV捕获网络摄像头图像。这工作正常。但是如果我想关闭OpenCV时按下按钮,它不工作(尝试 cvDestroyWindow(NameOfWindow)
和 cvDestroyAllWindows()
)。窗口保持打开,应用程序仍在运行。
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.
OpenCV在与主GUI分开的线程上初始化。
The OpenCV was initialized on separate thread from the main GUI.
我在Mac上使用Juce框架与C ++。但同样的问题也发生在Windows与Qt和Windows窗体,当OpenCV窗口有它自己的cvNamedWindow。
I'm using the Juce Framework with C++ on my Mac. But the same problem occurs also on Windows with Qt and Windows Forms, when the OpenCV Window has it's own cvNamedWindow.
以下是VST插件编辑器类的基本代码:
Here is the basic code of the VST plugin editor class:
PluginEditor.cpp
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
函数。
The piece you may be missing is a call to the cvStartWindowThread
function.
在Linux上,使用GTK HighGUI,此示例转换了您的问题,直到我调用 cvStartWindowThread
。
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;
}
如果 cvStartWindowThread
在您的 cvDestroy
调用后,请尝试对 cvWaitKey
执行额外的调用。
If cvStartWindowThread
doesn't help, try doing an extra call to cvWaitKey
after your cvDestroy
call.
运行示例,使用GCC编译它:
To run the example, compile it with GCC:
g++ destroy_window.cpp -o destroy_window -lopencv_core -lopencv_highgui
这篇关于尝试关闭OpenCV窗口没有任何效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!