title: Qt OpenCV Webcam Stream Opening and Closing
body: <p>I have created a very simple UI using <code>Qt</code> which consists of a simple button and a label. When the button's <code>clicked()</code> signal is emitted, a function which captures a frame from a webcam using <code>OpenCV</code> is called. The code I am currently using to achieve this is:</p>
  
  <pre><code>cv::Mat MainWindow::captureFrame(int width, int height)
  {
  
      //sets the width and height of the frame to be captured
      webcam.set(CV_CAP_PROP_FRAME_WIDTH, width);
      webcam.set(CV_CAP_PROP_FRAME_HEIGHT, height);
  
      //determine whether or not the webcam video stream was successfully initialized
      if(!webcam.isOpened())
      {
          qDebug() &lt;&lt; "Camera initialization failed.";
      }
  
      //attempts to grab a frame from the webcam
      if (!webcam.grab()) {
          qDebug() &lt;&lt; "Failed to capture frame.";
      }
  
      //attempts to read the grabbed frame and stores it in frame
      if (!webcam.read(frame)) {
          qDebug() &lt;&lt; "Failed to read data from captured frame.";
      }
  
      return frame;
  }
  </code></pre>
  
  <p>After a frame has been captured, it must be converted into a <code>QImage</code> in order to be displayed in the label. In order to achieve this, I use the following method:</p>
  
  <pre><code>QImage MainWindow::getQImageFromFrame(cv::Mat frame) {
      //converts the color model of the image from RGB to BGR because OpenCV uses BGR
      cv::cvtColor(frame, frame, CV_RGB2BGR);
      return QImage((uchar*) (frame.data), frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
  }
  </code></pre>
  
  <p>The constructor for my <code>MainWaindow</code> class looks like this: </p>
  
  <pre><code>MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      resize(1280, 720);
      move(QPoint(200, 200));
      webcam.open(0);
      fps = 1000/25;
      qTimer = new QTimer(this);
      qTimer-&gt;setInterval(fps);
      connect(qTimer, SIGNAL(timeout()), this, SLOT(displayFrame()));
  }
  </code></pre>
  
  <p>The <code>QTimer</code> is supposed to display a frame by calling <code>dislayFrame()</code></p>
  
  <pre><code>void MainWindow::displayFrame() {
      //capture a frame from the webcam
      frame = captureFrame(640, 360);
      image = getQImageFromFrame(frame);
  
      //set the image of the label to be the captured frame and resize the label appropriately
      ui-&gt;label-&gt;setPixmap(QPixmap::fromImage(image));
      ui-&gt;label-&gt;resize(ui-&gt;label-&gt;pixmap()-&gt;size());
  }
  </code></pre>
  
  <p>each time its <code>timeout()</code> signal is emitted. 
  However, while this appears to work to a degree, what actually happens is that the video capture stream from my webcam (a <strong>Logitech Quickcam Pro 9000</strong>) repeatedly opens and closes. This is evidenced by the fact that the blue ring, which indicates that the webcam is on, repeatedly flashes on and off. This leads to the refresh rate for the webcam video stream label to be very low, and is not desirable. Is there some way to make it so that the webcam stream remains open in order to prevent this "flickering" from occurring?</p>