我正在用OpenCV捕捉网络摄像头的图像。这很好用。但是,如果我想在按下按钮时关闭OpenCV,它就不能工作(我同时尝试了cvDestroyWindow("NameOfWindow")和cvDestroyAllWindows())。窗口保持打开状态,应用程序仍在运行。
OpenCV是在独立于主图形用户界面的线程上初始化的。
我在我的苹果电脑上使用Juce框架和C++。但是,当OpenCV窗口有自己的cvNamedWindow时,同样的问题也会发生在带有Qt和Windows Forms的Windows上。
下面是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) {
} 发布于 2011-08-22 04:35:00
您可能遗漏的部分是对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_highguihttps://stackoverflow.com/questions/7139968
复制相似问题