title: Delay frame capture opencv
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>Is there any way I can stop <code>capture &gt;&gt; saveImage;</code> from running for a few seconds?</p>
  
  <pre><code>//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(&amp;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 &lt;&lt; "The program is currently in calibration mode" &lt;&lt; endl;
      cout &lt;&lt; "this is so that HSV values can be recorded for" &lt;&lt; endl;
      cout &lt;&lt; "each marker." &lt;&lt; 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(&amp;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 &lt;100
      //Uses another one time bool flag, bSingleFrame, which is initially set to true
      //Checks if bSingleFrame &amp;&amp; bSavePic are true, if so captures a frame
      //bSingleFrame is then changed to false if the distance is still &lt;100
      //Once the distance is &gt;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&lt;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 &gt;&gt; saveImage;
              //string stream initialised through each passing of while loop
              stringstream ssFileName;
              //Directory to save image &amp; File name based on frame captured 0++
              ssFileName &lt;&lt; "gestureimages/Image-" &lt;&lt; c &lt;&lt; ".png";        
              ssFileName &gt;&gt; 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 &lt;&lt; "Image-" &lt;&lt; c &lt;&lt; ".jpg";      //File name based on frame captured 0++
              //  ssFileName &gt;&gt; sFileName;
              //}
  
          //check if frame captured is stored in matrix
          if(!saveImage.empty())                          
          {
              cout &lt;&lt; "Frame captured." &lt;&lt; endl;
              //Save frame
              imwrite(sFileName.c_str(), saveImage);
              saveImage.release();                        
              c++;
              //If distance is still &lt;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 &lt;&lt; "Error, could not capture frame to save." &lt;&lt; endl;
          }//if(!saveImage.empty())
          }//bSingleFrame
      }//ifbSavePic
      if (bSavePic == false)
          {
              //if distance back &gt;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
  </code></pre>
  
  <p>This is only a snippet of my main function my full code is available <a href="http://pastebin.com/dat3ghM0" rel="nofollow">here</a></p>