Ask Your Question
1

C++ openCV waitKey(0) not working?

asked 2017-10-13 08:35:42 -0600

hsw2201 gravatar image
 #include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int isSquare(String fileName);
int main() {
    String imgName = "C:/A.jpg";
    isSquare(imgName);
}
int isSquare(String fileName) {
    Mat img;
    img = cv::imread(fileName, IMREAD_COLOR);
    if (img.empty()) {
        cout << "Could not open or find the image" << endl;
        return -1;
    }
    //namedWindow("display", WINDOW_AUTOSIZE);
    imshow("display", img);
    waitKey(0);
    cout << "hi";
    destroyWindow("display");
    return 0;
}

Hi, I'm currently messing around with openCV 3.30, C++, VS2015. Now I'm trying to open an image, but the display window just kept disappear when I execute above code. I commented namedWindow("display", WINDOW_AUTOSIZE); because openCV document says cv:imshow() will automatically create one, and if I un-comment that line I got one gray window, one image window like this. enter image description here

I don't want to got that gray window, and key input for waitKey(0) works only when I focus on gray window, not on image window. So I made that line as a comment. but if I do that, when I execute that code the image window disappears instantly as if I don't have waitKey(0) code. Clearly waitKey(0) is not working because cout<<"hi"; after waitKey(0) was executed.

Am I missing something? Is the document wrong and using namedWindow necessary? All I wanted was to get rid of that gray window... any words of wisdom is appreciated, thanks.

plus, I got these responses from stackoverflow:

image description

edit retag flag offensive close merge delete

Comments

there is no rpoblem in your code....(tested in both cases winodws 10 VS 2015 opencv 3.3-dev

LBerger gravatar imageLBerger ( 2017-10-13 09:44:41 -0600 )edit

thanks, I really hate this kind of situation :(

hsw2201 gravatar imagehsw2201 ( 2017-10-13 09:56:43 -0600 )edit

whty have you got a weird char in the name of cmd window?

LBerger gravatar imageLBerger ( 2017-10-13 10:30:24 -0600 )edit

you mean that W-like char? it's just same as '/' in korean language Windows.

hsw2201 gravatar imagehsw2201 ( 2017-10-13 11:44:45 -0600 )edit

Same problem, VS2017, openCV3.4

Interestingly, the name of the 2nd window (as defined in imshow("...this name...")) does not matter, i.e.:

namedWindow("D1", WINDOW_AUTOSIZE);

imshow("D2", image);

waitKey(0);

does exactly the same as:

namedWindow("D1", WINDOW_AUTOSIZE);

imshow("D1", image);

waitKey(0);

which is: creating 2 windows, the 'namedWindow' being gray. In both cases, waitKey(0) responds to the gray window only.

rdg65 gravatar imagerdg65 ( 2018-02-21 09:28:32 -0600 )edit

@rdg65 please do not post answers here, if you have a question or comment, thank you.

berak gravatar imageberak ( 2018-02-21 09:46:51 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
0

answered 2017-10-14 02:45:16 -0600

opencvcraze gravatar image

updated 2017-10-14 07:01:32 -0600

Please try this :

using namespace std;
using namespace cv;

int main(){
    int a;
    Mat image;
     image = imread("C:/Users/lapi/Desktop/a.png", CV_LOAD_IMAGE_COLOR);
     if(!image.data)
     {
        cout <<  "Could not open or find the image" << std::endl ;
         //imshow("Original Image",image);
     }
     namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display.
    imshow( "Display window", image );                   // Show our image inside it.


      waitKey(0);



}

if that doest't work check article here opencvcraze.com

edit flag offensive delete link more
0

answered 2017-10-13 14:27:31 -0600

LBerger gravatar image

updated 2017-10-13 14:41:15 -0600

Ok I think you have past an unicode char in imshow("display", img); or namedWindow("display", WINDOW_AUTOSIZE); Just before d of display which is invisible

Finally I think this code is better :

namedWindow(fileName, WINDOW_AUTOSIZE);
imshow(fileName, img);
edit flag offensive delete link more

Comments

I just tried this but no difference. thing is, if I execute namedWindow, then imshow with even different window name also works well, and they terminates when namedWindow's gray window terminates. maybe I should downgrade opencv and test it...

hsw2201 gravatar imagehsw2201 ( 2017-10-13 20:40:02 -0600 )edit

edit:nevermind, I downgraded to 3.0 and it works find.

hsw2201 gravatar imagehsw2201 ( 2017-10-13 21:16:24 -0600 )edit

That's not a good way to solve your problem : read bug list since opencv 3.0

LBerger gravatar imageLBerger ( 2017-10-14 02:11:26 -0600 )edit
-1

answered 2020-04-27 12:15:19 -0600

//This is just a version of opencv that treats differently the waitKey function. So the answer is to change it fro cvWaitKey. // check this out, as y'all can notice I am just continuously taking pictures from the webcam and stoping as soon as the user presses anykey.

Mat frame;

VideoCapture cap(0);

// check if we succeeded
if (!cap.isOpened()) {
    cerr << "ERROR! Unable to open camera\n";
    return -1;
}

for (;;)
{
    //  storing the camera capture into 'frame'
    cap>>frame;
    // check if we succeeded
    if (frame.empty()) {
        cerr << "ERROR! \n";
        break;
    }
    // show live and wait for a key with timeout long enough to show images
    imshow("Live", frame);
    if (cvWaitKey(30) >= 0)
        break;
}  
return 0;
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-13 08:35:42 -0600

Seen: 17,639 times

Last updated: Feb 21 '18