Ask Your Question
0

imshow() shows the image after scanf_s()

asked 2015-10-14 05:58:51 -0600

vitruvius gravatar image

updated 2015-10-14 05:59:49 -0600

Hello,

I would like to get some user input after I show an image. But even though imshow() is being executed, it waits till the user input to show the image. I get an empty window until scanf_s() is done. Why is this happening?

I am using Visual Studio 2013, and OpenCV 2.4.11.

Here is a little demo:

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;
using namespace std;

int main()
{   
    char in[15];
    namedWindow("Source", 1);

    Mat src = imread("toy.png");

    imshow("Source", src);

    scanf_s("%s", in, sizeof in);

    waitKey();
    return 0;
}

toy.png: toy

edit retag flag offensive close merge delete

Comments

why not replacing scanf_s by waitKey?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-10-14 06:46:59 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
5

answered 2015-10-14 07:22:29 -0600

berak gravatar image

updated 2015-10-14 09:11:06 -0600

Why is this happening?

imshow() only sets a pointer to the image, the real drawing happens in waitKey()

(the window's internal event-loop is triggered from there)

so, it's just a matter of order:

Mat src = imread("toy.png");
imshow("Source", src);
waitKey(1); // wait only a very short time

scanf_s("%s", in, sizeof in); // get your input 

// some more processing ...

waitKey(); // wait forever now (don't let the prog close the window)
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-10-14 05:58:51 -0600

Seen: 546 times

Last updated: Oct 14 '15