Program doesn't update the image for the first time

asked 2015-11-03 15:14:53 -0500

vitruvius gravatar image

Hi,

I would like to capture an image whenever my program runs. Then I ask the user if the image is okay, and if he says "no", I capture another one. But, if he says "no" for the first time, I get to see the same old first image. If he says "no" for the second time, then I see the new image. What is going on here?

#include <iostream>
#include <stdio.h>
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

Mat mySrc;

char window_frame[] = "Input Frame";

int main()
{
    VideoCapture cap(0);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);     // Set width in pixels
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 720);     // Set height in pixels

    string s_isPicOkay;
    int int_isPicOkay = 0;

    if (!cap.isOpened())    // Check if we are connected to the camera
    {
        cout << "Failed to connect to the camera.\n";
        return -1;
    }

    while (!int_isPicOkay)
    {
        cap >> mySrc;

        if (mySrc.empty())  // Check if we were able to capture an image
        {
            cout << "Failed to capture an image.\n";
            return -1;
        }

        else
        {
            imshow(window_frame, mySrc);
            waitKey(1);

            cout << "Is the picture okay? (Y/N): ";
            getline(cin, s_isPicOkay);

            if ((s_isPicOkay == "Y") || (s_isPicOkay == "y"))
            {
                destroyWindow(window_frame);
                int_isPicOkay = 1;
            }

            else if ((s_isPicOkay == "N") || (s_isPicOkay == "n"))
            {
                destroyWindow(window_frame);
            }
        }
    }

    cvWaitKey(0);
    return 0;
}

If I capture the image twice, it works okay, but I just want to know what the problem is here.

Thank you.

edit retag flag offensive close merge delete

Comments

Seems to me that cap >> mySrc captures two frames and buffers the second one. So, when you try to display the second image, it picks it up from the camera buffer. There is a lag of one frame as long as you keep hitting n.

unxnut gravatar imageunxnut ( 2015-11-03 19:49:34 -0500 )edit