0

I have a window that I open just like the OpenCV tutorials show but I want to be able to close the window and do other things in my program. The image displays correctly but when the call to destroyWindow is made it seems to do nothing and just moves to the next line of code. I have also tried destroyAllWindow() as well with the same result. Most of the questions relating to this that I have found are for C, or they just say use destroyWindow() but that is not working for me.

#include <iostream>
#include <string>
#include "opencv2/core/core.hpp"
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>//FOR TESTING


void MyPause(std::string msg){
    if(msg != ""){
        std::cout << "[+] " << msg << std::endl;
    }
    std::cout << "Press enter to continue" << std::endl;
    std::cin.get();
}

void DisplayMat(cv::Mat img){
    cv::namedWindow( "Input", cv::WINDOW_AUTOSIZE );// Create a window for display.
    cv::imshow( "Input", img );
    cv::waitKey(0);
    cv::destroyWindow("Input");

    return;
}

std::string filename = "/home/nedwards/Code/projectFiles/testMedia/myYellow.jpg";


int main(){

    DisplayMat(som.inputImg);//just assume that som.input is a correctly opend Mat... it is!
    MyPause("END OF MAIN");
    return 0;
}
aaron burns
  • 267
  • 4
  • 15
  • 1
    cv::waitKey(0); wait for some key infinitely, so window should close after you pressing some key, if you want show window a few second and after this close it, try cv::waitKey(2000); (2sec). Try this,window still don't want close? – Jablonski Aug 17 '14 at 05:16
  • I do not want the window to pop up for x seconds and then close. I want the person to press a key with the window selected and then the window closes. It seems many people have had issues with destroying created windows and few have come up with a solution to the problem. – aaron burns Aug 18 '14 at 21:39

1 Answers1

-1

A possible solution to this is placing:

cvStartWindowThread();

near to the start of your code, (the start of main). This can help with the often times slow event exchange which happens when cvDestroyWindow() is called. You can look here for more details.

As a side note however this isn't perfect solution. It's worked well with some of my code but currently i've been having problems when other event listeners like waitKey() are involved, which i guess is understandable.

Community
  • 1
  • 1
aheigins
  • 2,574
  • 3
  • 19
  • 26
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](http://stackoverflow.com/questions/ask). You can also [add a bounty](http://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question once you have enough [reputation](http://stackoverflow.com/help/whats-reputation). – JKor Aug 12 '15 at 17:57
  • Hi i should have worded it more clearly as an answer, it wasn't meant as a question. I'm not that new :) – aheigins Aug 12 '15 at 20:02