Ask Your Question
0

codes after waitKey(0) not running

asked 2019-01-19 10:11:33 -0600

Allaye gravatar image

updated 2019-01-20 09:20:36 -0600

code:

  while(1) {
double alpha = -1; // initially invalid
while ( alpha < 0.0 || alpha > 1.0) {
    cout << endl;
    cout << "Enter Your Blend Value from 0.0 to 1.0" << endl;
    cin >> alpha; // while your console window has the focus !
}

addWeighted(img, alpha, img2, 1.0 - alpha, 0, des);
imshow("img", img);
imshow("ime1", img2);
imshow("Blended", des);
waitKey(0); 
cout<<"This is just a sample<<endl;

}

after the three images has been displayed the code beneath it wont run, like the cout, except i close the images or terminate the program all together.. Thanks in advance

edit retag flag offensive close merge delete

Comments

as a rule of thumb: you only need 1 waitKey(0) call, ever.

this simply looks like broken programming logic, to me ;)

why do you need a cin AND a waitKey() there ? (they also work on different windows)

berak gravatar imageberak ( 2019-01-19 10:15:26 -0600 )edit

i need it to be able to close the program, then i put the waitKey(0) before the two imshow it, am able to go down the code but the cin>> isn't working..

Allaye gravatar imageAllaye ( 2019-01-19 10:32:05 -0600 )edit

again, see answer below.

you'll also need to switch the focus between the console window for cin to work, and the highgui one for waitKey() !

berak gravatar imageberak ( 2019-01-19 10:50:49 -0600 )edit

@Allaye you changed your question : comments and answer are unintelligible. Please go back and post a new question with a full error message

LBerger gravatar imageLBerger ( 2019-01-20 07:35:06 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-01-19 10:29:16 -0600

berak gravatar image

updated 2019-01-20 10:31:55 -0600

imho, all you need is this:

while(1) {
    double alpha = -1; // initially invalid
    while ( alpha < 0.0 || alpha > 1.0) {
        cout << endl;
        cout << "Enter Your Blend Value from 0.0 to 1.0" << endl;
        cin >> alpha; // while your console window has the focus !
    }

    addWeighted(img, alpha, img2, 1.0 - alpha, 0, des);
    imshow("img", img);
    imshow("ime1", img2);
    imshow("Blended", des);

    char  key = cv::waitKey(0); // while the highgui window has the focus !
    if (key == 'Q' || key =='q' || key == 'E' || key =='e' )
         return EXIT_SUCCESS;
}

edit

to ease your pain, ditch the cin, and use a trackbar, like this:

Mat img(300,300,CV_8UC3, Scalar(200,0,0));
Mat img2(300,300,CV_8UC3, Scalar(0,0,200));
Mat des;

int blend=50;
// opencv maintains an internal, hidden list of windows, so make it a "known" one"
namedWindow("Blended");
// now we can add a trackbar, range [0..100]
createTrackbar("blend", "Blended", &blend, 100);

while(1) {
    double alpha = (double)blend / 100;

    addWeighted(img, alpha, img2, 1.0 - alpha, 0, des);
    imshow("blue", img);
    imshow("red", img2);
    imshow("Blended", des);

    char  key = cv::waitKey(10);
    if (key == 'Q' || key =='q' || key == 'E' || key =='e' )
         return EXIT_SUCCESS;
}
edit flag offensive delete link more

Comments

also, just saying, you probably want another / better range for your alpha. something in [0..1] is usually used here !

berak gravatar imageberak ( 2019-01-19 10:36:23 -0600 )edit

everything worked well except capturing the key input, which is needed to quit the program ..

while(1) {
    double alpha = -1; // initially invalid
    while ( alpha < 0.0 || alpha > 1.0) {
        cout << endl;
        cout << "Enter Your Blend Value from 0.0 to 1.0" << endl;
        cin >> alpha; // while your console window has the focus !
    }

    addWeighted(img, alpha, img2, 1.0 - alpha, 0, des);
    imshow("img", img);
    imshow("ime1", img2);
    imshow("Blended", des);
    waitKey(0); 
    cout<<"This is just a sample<<endl;
}

The cout show sometimes but more than often it does not show

Allaye gravatar imageAllaye ( 2019-01-19 15:55:07 -0600 )edit

@Allaye please try the code from the answer, instead of your own broken attempt.

again, you have to check the return value from waitKey(), and use that to decide, if you want to finish your program.

berak gravatar imageberak ( 2019-01-20 07:28:27 -0600 )edit

That was the first thing i did, comment the old code and run yours the image displayed well, but the cmd is not accepting input, sometimes i have to click on the image but the input shows on the terminal..., just like before

Allaye gravatar imageAllaye ( 2019-01-20 09:18:09 -0600 )edit

@Allaye, you probably should get rid of the cin, entirely, and use a trackbar for the blend value. make it from 0 to 100, and divide by 100.

berak gravatar imageberak ( 2019-01-20 09:34:01 -0600 )edit

@berak okay, it seems that what i have to do, concerning the previous code, i think waitKey(0) is the one blocking the console from capturing the user input, and when it does it usually very slow, thank for your time though, and i think making use of the mouse call back function might work fairly enough...

Allaye gravatar imageAllaye ( 2019-01-20 09:40:49 -0600 )edit

i think waitKey(0) is the one blocking the console from capturing the user input

nope, wrong.

berak gravatar imageberak ( 2019-01-20 09:41:55 -0600 )edit

then if i may ask why is it behaving like that..?

Allaye gravatar imageAllaye ( 2019-01-20 09:48:11 -0600 )edit

well i tried it, and found it terribly confusing, that you have to track, which of the windows is currently active, and where you have to do something. you're probably just doing something in the "wrong" window.

berak gravatar imageberak ( 2019-01-20 09:53:04 -0600 )edit

see edited answer, please !

berak gravatar imageberak ( 2019-01-20 09:59:17 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2019-01-19 10:11:33 -0600

Seen: 826 times

Last updated: Jan 20 '19