Trying to close OpenCV window has no effect - c++

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. 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

Related

Qt crashes when running a basic OpenCV command VideoCapture

I have an interesting problem when i switched to OpenCV 4.3.0 version.
I built OpenCV 4.3.0 with Qt using Cmake 3.16.0 and mingw730_64 and build done in Release mode on Windows 10 machine
When i compile a simple code including VideoCapture, i can build it without errors but when i run it in Release mode exe simply crashes..
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
..
void MainWindow::on_pushButton_2_clicked()
{
..
VideoCapture capWebcam(0);
..
}
There is no problem when reading and displaying images..!
void MainWindow::on_pushButton_clicked()
{
Mat Test_Image;
QString fileName = QFileDialog::getOpenFileName(this, "Open Image File", QDir::currentPath(), "Image (*.*)");
if(QFile::exists(fileName))
{
Test_Image = imread(fileName.toStdString(), IMREAD_COLOR);
if(Test_Image.empty()){
QMessageBox msg;
msg.setText("File couldn't load!");
msg.exec();
}
else {
namedWindow("Test Window", WINDOW_AUTOSIZE);
flip(Test_Image, Test_Image,0);
cvtColor(Test_Image, Test_Image, COLOR_BGR2GRAY);
imshow("Test Window", Test_Image);
waitKey(1);
}
}
Can anyone help me understand why the program crashes immediately when i instantiate VideoCapture object?
C:\Qt_Projects\build-Deneme1-Desktop_Qt_5_14_2_MinGW_64_bit-Release\release\Deneme1.exe crashed.
Thank you in advance.

GTK+ moving window based on face detection coordinates

I have a program here that performs face detection and I would like to use these coordinates to move a window created with GTK+ 3.22 using GTK's
gtk_window_move function. I would like the window to remain open the entire time while it is moving similar to OpenCV's moveWindow function.
I just downloaded the GTK+ packages yesterday so I am not all too familiar.
The program will perform a loop 100 times, tracking a face the entire time. Currently, the face tracking works, but the window does not appear until the loop is complete. Why is this? I believe the gtk_move_window function is working, but the window does not stay open. I have tried reopening the window each time in the loop, or just opening once before the loop. If you are familiar with OpenCV's moveWindow function that is exactly what I am looking for. Here is the sample code.
By the way, if you know how a GTK+ function that will bring the window to the very top layer on top of all the other windows open when called, that is helpful information for me as well.
#include "FlyCapture2.h"
#include <opencv2/core/core.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudaobjdetect.hpp>
#include <math.h>
#include <thread>
#include <iostream>
#include <vector>
#include <gtk-3.0/gtk/gtk.h>
using namespace FlyCapture2;
cv::Ptr<cv::cuda::CascadeClassifier> face_detect;
void detect_faces(cv::Mat img, cv::cuda::GpuMat buf,GtkWidget *win)
{
std::vector<cv::Rect>faces;
cv::cuda::GpuMat image_gpu(img);
//Face detection here
...
if (faces.size() > 0)
{
float x = faces[0].x;
float y = faces[0].y;
int new_x = roundf(x*40/51);
int new_y = roundf(y*135/256);
gtk_window_move(GTK_WINDOW (win),new_x,new_y);
gtk_widget_show (win); //Should this go here?
std::cout<<faces[0]<<std::endl;
}
}
int main( int argc, char *argv[])
{
//Camera connect here
...
//face detect variables
face_detect = cv::cuda::CascadeClassifier::create("/home/nvidia/opencv/data/haarcascades_cuda/haarcascade_frontalface_default.xml");
cv::cuda::GpuMat objbuf;
//GTK+ Params
GtkWidget *window;
GdkRGBA *color;
gtk_init (&argc, &argv);
gdk_rgba_parse(color,"(0,0,0)");
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_decorated(GTK_WINDOW (window),FALSE);
gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_CENTER);
gtk_widget_override_background_color(window, GTK_STATE_FLAG_NORMAL, color);
gtk_widget_show (win); //Should this go here?
// capture loop
for (int i=0;i<100;i++)
{
// Get the image
Image rawImage;
camera.RetrieveBuffer( &rawImage );
// convert to rgb
Image rgbImage;
rawImage.Convert( FlyCapture2::PIXEL_FORMAT_MONO8, &rgbImage );
// convert to OpenCV Mat
unsigned int rowBytes = (double)rgbImage.GetReceivedDataSize()/(double)rgbImage.GetRows();
cv::Mat image = cv::Mat(rgbImage.GetRows(), rgbImage.GetCols(), CV_8UC1, rgbImage.GetData(),rowBytes);
//Detect Faces
detect_faces(image,objbuf,window);
}
//Disconnect Camera
camera.StopCapture();
camera.Disconnect();
gtk_main();
return 0;
}
The code in your capture loop should be in an event handler callback.
You first need to call g_timeout_add or g_idle_add to register your callback.
The callback you registered is a GSourceFunc that will be called after gtk_main is run. The return value (G_SOURCE_CONTINUE or G_SOURCE_REMOVE) controls if you want to have the callback be called again.

Designing OpenCV GUI with Qt

I want to customize Qt's GUI
Here is the Default Qt OpenCV GUI
I want to place createButton on Menu.
How can I do that?
Below is my code
#include "mainwindow.h"
#include <QApplication>
#include <opencv2/core/core.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
void callbackButton2(int state, void* userData){
}
int main(int argc, char *argv[])
{
//QApplication a(argc, argv);
//MainWindow w;
//w.show();
cv::VideoCapture vc(0);
if(!vc.isOpened()) perror("Can't Open WebCam");
const char* winname = "WebCam";
cv::namedWindow(winname,CV_GUI_EXPANDED);
cv::createButton("button6",callbackButton2,NULL,CV_PUSH_BUTTON,1);
for(;;){
cv::Mat frm;
vc>>frm;
if(!frm.empty()) imshow(winname,frm);
if(cv::waitKey(20)==27) break;
}
vc.release();
cv::destroyWindow(winname);
return 0;
// return a.exec();
}
The code above simply opens webcam and display it on the screen. But for my next application, I need to customize gui and place it on to the menu.
BTW I searched through the fllowing link, and I couldn't find how can I customize the GUI. http://docs.opencv.org/2.4/modules/highgui/doc/qt_new_functions.html
I'm not sure about if you can customize the OpenCv Qt windows but you can code your own window and then show the openCv image, using something similiar to this, you can retrive the cv:Mat immage and convert it to a Qimage object then show the object in a Qt graphicsview using a QPixmap.

Camera Interfacing with Opencv:

I have Windows 8 on "DELL Inspiron 5537" installed and i create a virtual machine from "Virtual box" and installed Ubuntu 13.04 in it. And then I installed Opencv Version=2.4.9 through commands in Ubuntu's terminal now I want to do camera interfacing in opencv which is very necessary for my project to move forward..my code contains no error still it is not working the way i want.
My code is given below:
#include <iostream>
#include <cv.h>
#include <highgui.h>
using namespace std;
char key;
int main()
{
cvNamedWindow("Camera_Output", 1); //Create window
CvCapture* capture = cvCaptureFromCAM(CV_CAP_ANY);
//Capture using any camera connected to your system
while(1){ //Create infinte loop for live streaming
IplImage* frame = cvQueryFrame(capture); //Create image
frames from capture
cvShowImage("Camera_Output", frame); //Show image
frames on created window
key = cvWaitKey(10); //Capture Keyboard stroke
if (char(key) == 27){
//cvSaveImage("webcam_frame.png",frame);
}
}
cvReleaseCapture(&capture); //Release capture.
cvDestroyWindow("Camera_Output"); //Destroy Window
return 0;
}
when I run this code in my terminal it is not capturing the image rather it shows a blank window. I have attached a photo of output of this code.

Allegro 5 al_create_display(x, y) not working

I have set up my allegro 5.0.7 project in MSVC 2010 properly and the code executes. I am able to compile and run programs that will display an error dialog or something. However, whenever I run a program that draws a window, the window is not shown on my screen. I see it minimized with a broken file icon. The code runs with no errors, however. Here is an example of some code that gives me this problem. Thanks!
#include <stdio.h>
#include <allegro5/allegro.h>
int main(int argc, char **argv){
ALLEGRO_DISPLAY *display = NULL;
if(!al_init()) {
fprintf(stderr, "failed to initialize allegro!\n");
return -1;
}
display = al_create_display(640, 480);
if(!display) {
fprintf(stderr, "failed to create display!\n");
return -1;
}
al_clear_to_color(al_map_rgb(0,0,0));
al_flip_display();
al_rest(10.0);
al_destroy_display(display);
return 0;
}
This code even exits after 10 seconds, as it should. The only problem is that the window is not drawn to the screen. It is only minimized, with a broken file icon. I have Windows 7 64-bit.
This is a known bug affecting certain configurations that may be fixed in a more recent version.
Use al_set_window_position() to move the window onscreen.