Question

So my code captures a frame upon a certain action using my hands, only problem is I need to wait for my hands to move out the way before the frame is stored.

I've tried using Sleep(3000), all the video outputs freeze for 3 seconds but it captures the frame before the video outputs freeze not after them, so essentially while my hands are still in the way.

Is there any way I can stop capture >> saveImage; from running for a few seconds?

//bools for gestures
bool bSavePic = false;
bool bSingleFrame = true;

int main(int argc, char* argv[])
{
//Following for measuring FPS
time_t startFPS, endFPS;
double fps;
int counter = 0;
double sec;
time(&startFPS);

//if we would like to calibrate our filter values, set to true.
bool calibrationMode = true;

//Matrix to store each frame of the webcam feed
Mat cameraFeed;
Mat threshold;
Mat HSV;

//Initalise count at 0 for image saving
int c = 0;

if(calibrationMode)
{
    //create slider bars for HSV filtering
    createTrackbars();
    cout << "The program is currently in calibration mode" << endl;
    cout << "this is so that HSV values can be recorded for" << endl;
    cout << "each marker." << endl;
}//if calibrationMode

//video capture object to acquire webcam feed
VideoCapture capture;
//open capture object at location zero (default location for webcam)
capture.open(2);
//When using video files.
//capture.open("My Recording1.avi");

//set height and width of capture frame
capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);

//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all of our operations will be performed within this loop
while(capture.isOpened())
{
    //store image to matrix
    capture.read(cameraFeed);

    //After each frame in captured measure FPS
    time(&endFPS);
    ++counter;        
    sec = difftime (endFPS, startFPS);      
    fps = counter / sec;
    printf("FPS = %.2f\n", fps);

    //convert frame from BGR to HSV colorspace
    cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);

    if(calibrationMode==true)
    {
    //if in calibration mode, we track objects based on the HSV slider values.

    inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold);
    morphOps(threshold);
    //Show thresholded image
    imshow(windowName2,threshold);
    trackFilteredObject(threshold,HSV,cameraFeed);

    }else{

    }

    //Following function checks if distance is <100
    //Uses another one time bool flag, bSingleFrame, which is initially set to true
    //Checks if bSingleFrame && bSavePic are true, if so captures a frame
    //bSingleFrame is then changed to false if the distance is still <100
    //Once the distance is >100, bSingleFrame is changed back to true, and another frame can be captured
    //Means only one frame is captured when the conditions are met

    //If distance<100
    if (bSavePic == true)
    {
        if (bSingleFrame == true)
        {
            //Display that the frame captured is being saved
            putText(cameraFeed,"Saving Image",Point(50,70),1,1,Scalar(0,0,255),1);
                            //*****SLEEP FUNCTION*****
                            Sleep(3000);
            //Matrix to store image for saving
            Mat saveImage;

            capture >> saveImage;
            //string stream initialised through each passing of while loop
            stringstream ssFileName;
            //Directory to save image & File name based on frame captured 0++
            ssFileName << "gestureimages/Image-" << c << ".png";        
            ssFileName >> sFileName;

            //If file exists function       ***NEEDS MODIFIED***
            if(!fileExists == 0)
            //{
            //  //need file name search function, find number after image
            //  //start c at this number
            //  c++;
            //  stringstream ssFileName;                    //string stream initialised through each passing of while loop
            //  ssFileName << "Image-" << c << ".jpg";      //File name based on frame captured 0++
            //  ssFileName >> sFileName;
            //}

        //check if frame captured is stored in matrix
        if(!saveImage.empty())                          
        {
            cout << "Frame captured." << endl;
            //Save frame
            imwrite(sFileName.c_str(), saveImage);
            saveImage.release();                        
            c++;
            //If distance is still <100 switch bSingleFrame
            if (bSavePic == true)   
            {
                bSingleFrame = false;
            }

            Mat readImage;
            //Load saved frame form file
            readImage = imread(sFileName.c_str(),CV_LOAD_IMAGE_COLOR);  
            //show captured frame in new window
            imshow(windowName4, readImage);             
        }else{
            cout << "Error, could not capture frame to save." << endl;
        }//if(!saveImage.empty())
        }//bSingleFrame
    }//ifbSavePic
    if (bSavePic == false)
        {
            //if distance back >100 can capture another frame
            bSingleFrame = true;
        }

    //Display webcam output 
    imshow(windowName,cameraFeed);
    //Display thresholded output
    imshow(windowName2,threshold);

    //delay 30ms so that screen can refresh.
    //image will not appear without this waitKey() command
    waitKey(30);
}//while

return 0;
}//main

This is only a snippet of my main function my full code is available here

Was it helpful?

Solution

Try using sleep first and manually call grab. This should ensure that an Image is Captured only after a certain time.

If this should fail, try reading in two images and only keep the second one. (This is not nice, but you aren't time dependend anyway).

Should that also fail, check your sleep function for strange behaviour ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top