title: QT, C++: fast way to draw live image stream from camera on QGraphicsview
body: <p>I'm writing a QT GUI application in wich a live stream of a connected camera is shown on a QGraphicsview. Therefore an openCV image is first converted to a QImage and than to a QPixmap. This is added to the QGraphicsScene of the QGraphicsView.</p>
  
  <p>The bandwidth is not a problem, the cameras are connected via ethernet or USB.   </p>
  
  <p>I am testing the performance with the Analyze Toole build in Visual Studio 2012 and it shows that the conversion to the QPixmap is very slow and takes 60% of the computation time (of displaying the image), so that I end up with 1 FPS or so. The images are 2560 by 1920 or even bigger. Scaling the cv::Ptr stream_image befor converting it to a QImage improves the performance significantly but I need all the image detail in the image. </p>
  
  <p><strong>EDIT</strong>
  Here is some code how I do the conversion:</p>
  
  <pre><code>cv::Ptr&lt;IplImage&gt; color_image;
  // stream_image is a cv::Ptr&lt;IplImage&gt; and holds the current image from the camera
  if (stream_image-&gt;nChannels != 3) {
      color_image = cvCreateImage(cvGetSize(stream_image), IPL_DEPTH_8U, 3);
      cv::Mat gr(stream_image);
      cv::Mat col(color_image);
      cv::cvtColor(gr, col, CV_GRAY2BGR);
  }
  else {
      color_image = stream_image;
  }
  
  QImage *tmp = new QImage(color_image-&gt;width, color_image-&gt;height, QImage::Format_RGB888);
  memcpy(tmp-&gt;bits(), color_image-&gt;imageData, color_image-&gt;width * color_image-&gt;height * 3);
  
  
  // update Scene
  m_pixmap = QPixmap::fromImage(*tmp); // this line takes the most time!!!
  m_scene-&gt;clear();
  QGraphicsPixmapItem *item = m_scene-&gt;addPixmap(m_pixmap);
  m_scene-&gt;setSceneRect(0,0, m_pixmap.width(), m_pixmap.height());
  
  
  delete tmp;
  m_ui-&gt;graphicsView-&gt;fitInView(m_scene.sceneRect(),Qt::KeepAspectRatio);
  
  
  m_ui-&gt;graphicsView-&gt;update();
  </code></pre>
  
  <p><strong>EDIT 2</strong>
  I tested the method from from Thomas answer, but it is as slow as my method.</p>
  
  <pre><code>QPixmap m_pixmap = QPixmap::fromImage(QImage(reinterpret_cast&lt;uchar const*&gt;(color_image-&gt;imageData), 
  color_image-&gt;width, 
  color_image-&gt;height, 
  QImage::Format_RGB888));
  </code></pre>
  
  <p><strong>EDIT 3</strong>
  I tried to incorporate Thomas second suggestion:</p>
  
  <pre><code>color_image = cvCreateImage(cvGetSize(resized_image), IPL_DEPTH_32F, 3);
  //[...]   
  QPixmap m_pixmap = QPixmap::fromImage(QImage( 
  reinterpret_cast&lt;uchar const*&gt;( color_image-&gt;imageData), 
  color_image-&gt;width, 
  color_image-&gt;height, 
  QImage::Format_RGB32));
  </code></pre>
  
  <p>But that crashes when the drawEvent of the Widget is called.</p>
  
  <p>Q: Is there a way to display the image stream in a QGraphicsView without converting it to a QPixmap first or any other fast/performant way? The QGraphicsView is importent since I want to add overlays to the image. </p>