Ask Your Question
0

destroyWindow closes all windows and stops c++ program

asked 2014-02-16 11:31:45 -0500

Tduck gravatar image

updated 2014-02-16 11:56:59 -0500

I'm writing an live video processing program in c++, and want to be able to toggle three windows with the same mjpeg stream, in color, grayscale, and monochrome. I have all the image feeds running, but, since my screen is small, I want to be able to toggle them on and off individually. To do this, I have written the code below, but calling destroyWindow("[windowname]"); stops the whole program, instead. I've already read the documentation, and putting void in front of it doesn't help. Can anybody tell me what I'm doing wrong? I already tried asking on stackoverflow. There wasn't much help there. Here's the code (it's in an infinite loop, until the break you see below is called):

imshow("Color", imageColor);
imshow("Monochrome", imageMonochrome);
imshow("Grayscale", imageGrayscale);

int keyPressed = waitKey(0);
if (keyPressed > 0)
{
    cout << keyPressed;
    cout << "key was pressed\n";
    // Press C to toggle color window
    if (99 == keyPressed)
    {
        if (colorOpen)
        {
            cout << "Color window closed\n";
            destroyWindow("Color");
            colorOpen = false;
        }
        if (!colorOpen)
        {   
            cout << "Color window opened\n";
            imshow("Color", imageColor);
            colorOpen = true;
        }
    }

    // Press M to toggle monochrome window
    if (109 == keyPressed)
    {
        if (monochromeOpen)
        {
            cout << "Monochrome window closed\n";
            destroyWindow("Monochrome");
            monochromeOpen = false;
        }
        if (!monochromeOpen)
        {
            cout << "Monochrome window opened\n";
            imshow("Monochrome", imagebw);
            monochromeOpen = true;
        }
    }

    // Press G to toggle grayscale window
    if (103 == keyPressed)
    {
        if (grayscaleOpen)
        {
            cout << "Grayscale window closed\n";
            destroyWindow("Grayscale");
            grayscaleOpen = false;
        }
        if (!grayscaleOpen)
        {
            cout << "Grayscale window opened\n";
            imshow("Grayscale", image);
            grayscaleOpen = true;
        }
    }
    // Break out of infinite loop when [ESC] is pressed:   
    if (27 == keyPressed)
    {
        cout << "Escape Pressed\n";
        break;
    }
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-02-16 13:44:00 -0500

updated 2014-02-16 13:48:44 -0500

Hello, the problem here is that your code was wrong, when user presses 'G' for example, if flag grayscaleOpen==true (I guest you set this value as the beginning of the program) then the first if clause is executed and grayscaleOpen will be false, which causes the second if clause executes and grayscaleOpen will be true again and the program can not destroy the window as you wish. Another problem is the calling to waitKey() function, if you call waitKey(0), your prgram will wait for a keypress event infinitely and unless you press some key, it can not display the next image, so you should change that statement to waitKey(10) ect. I provide you with a small code below, which is run, but I think since your program works with video, you should try with thread based approach for more efficient.

if (grayscaleOpen)
    imshow("Grayscale", imageGrayscale);

keyPressed = waitKey(10);
if(keyPressed==27)
    break;
if (103 == keyPressed)
{
    switch(grayscaleOpen)
    {
        case true:
            std::cout << "Grayscale window closed\n";
            destroyWindow("Grayscale");
            grayscaleOpen = false;
        break;
        case false:
            std::cout << "Grayscale window opened\n";
            grayscaleOpen = true;
        }
    }
}

Hope this help.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-02-16 11:31:45 -0500

Seen: 2,680 times

Last updated: Feb 16 '14