title: OpenCV VideoCapture returns an empty frame only in first call to glutDisplayFunc callback
body: <p>I have been trying to work with OpenCV and freeglut.</p>
  
  <p>The program involves capturing an image from a WebCam, processing the image with OpenCV, and drawing 3D objects with OpenGL according to the processed image.</p>
  
  <p>It works perfectly fine when I only use OpenCV routines.</p>
  
  <p>The problem arises when the main loop becomes controlled by GLUT. When I try to grab a frame from within a callback I registered with <em>glutDisplayFunc()</em> the image returned is empty.</p>
  
  <p>Strangely, however, when I grab a frame from a callback I registered with <em>glutIdleFunc()</em> it successfully returns a frame.</p>
  
  <p>And after doodling around I figured out that somehow a frame cannot be captured in the first call of <em>display()</em> and works after the second call.</p>
  
  <p>Currently my code is querying a frame inside the <em>idle()</em> function.</p>
  
  <p>Regarding such background I have several questions.</p>
  
  <ol>
  <li><strong>Why does this happen?</strong> Is it because the program stalls inside <em>display()</em> before VideoCapture gains full access to the webcam? Or is this purely a hardware problem?</li>
  <li><strong>Is this safe?</strong> I'm perfectly fine about grabbing a frame from within <em>idle()</em>, but is this method safe to use?</li>
  <li><strong>If not so, is there a workaround?</strong> If this approach is not safe may somebody please notify me with another way of dealing with this issue?</li>
  </ol>
  
  <p>The program is built on OS X Version 10.9.1 and libraries being use are</p>
  
  <ul>
  <li>OpenCV 2.4.7.0</li>
  <li>freeglut 2.0.1</li>
  </ul>
  
  <p>Here is the simplified version of my code:</p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;GL/freeglut.h&gt;
  #include &lt;iostream&gt;
  
  cv::VideoCapture capture;
  cv::Mat render;
  
  void display()
  {
    std::cerr &lt;&lt; "Grabbing frame in display()" &lt;&lt; std::endl;
  
    capture &gt;&gt; render; // This does not work on first call
  
    if(render.empty()) {
      std::cerr &lt;&lt; "Error: Grabbing empty frame in display()" &lt;&lt; std::endl;
    }
  }
  
  void idle()
  {
    std::cerr &lt;&lt; "Grabbing frame in idle()" &lt;&lt; std::endl;
  
    capture &gt;&gt; render; // This always works
  
    if(render.empty()) {
      std::cerr &lt;&lt; "Error: Grabbing empty frame in idle()" &lt;&lt; std::endl;
    }
  
    glutPostRedisplay();
  }
  
  int main(int argc, char* argv[])
  {
    glutInit(&amp;argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(640, 480);
    int debug_window = glutCreateWindow("Debug");
    glutDisplayFunc(display);
    glutIdleFunc(idle);
  
    capture.open(0);
  
    if(!capture.isOpened()) {
      std::cerr &lt;&lt; "Error: Failed to open camera" &lt;&lt; std::endl;
      exit(1);
    }
  
    glutMainLoop();
  
    return 0;
  }
  </code></pre>