Sid411
Sid411

Reputation: 703

Drawing over video in Qt

I am showing a video on QGraphicsView. I want to add a transparent image on it. I am using the following code

There is a thread "video" from where I emit the processed image and catch it in MainWindow.

In header file

public:

video m_objVideo;      //Object of class video

QGraphicsScene *m_graphicSceneCamera;

QGraphicsPixmapItem *m_pixItemCamera;
QGraphicsPixmapItem *m_pixItemVideo;
QGraphicsProxyWidget *m_proxyWidgetVideoLabel;

public slots:
void ImageRecieved(QImage);

Main Window.cpp:

MainWindow::MainWindow(QWidget *parent) :
 QMainWindow(parent)
{
  setupUi(this);
  m_graphicSceneCamera = new QGraphicsScene(this);
  m_objVideo.play();
  connect(&m_objVideo,SIGNAL(signalProcessedImage(QImage)),this,SLOT(ImageRecieved(QImage)));
}

 void MainWindow::ImageRecieved(QImage l_processedImage)
 {
   m_graphicSceneCamera->addPixmap(QPixmap::fromImage(l_processedImage));
   m_graphicViewCamera->setScene(m_graphicSceneCamera);
   QImage l_image("/root/Desktop/FICV/images for demo/transparentImage.png");
   QPixmap l_pixmapImage(QPixmap::fromImage(l_image));
   m_pixItemVideo = m_graphicSceneGunCamera->addPixmap(l_pixmapImage);
 }

The problem is that the video slows down and crashes after sometime if I do this. Initially it does show the effect. Can someone suggest me how to solve this?

Upvotes: 0

Views: 1160

Answers (1)

TheDarkKnight
TheDarkKnight

Reputation: 27631

I assume that ImageReceived is being called for each frame of video sent to the main window. If this is the case, you're then creating a new pixmap for each frame and adding it to the scene. Eventually, you're going to run out of memory. It will slow down because you're constantly accumulating QPixmap objects, so the scene is having more and more objects to deal with.

Also, you're loading the transparent image and creating a pixmap from that every time a frame is received in ImageReceived.

In the constructor, add just one pixmap to the scene and create a second pixmap that will be used to composite the frame and transparent image. Load and create another pixmap for the transparent image.

In ImageReceived, instead of adding a pixmap per frame. The ImageReceived should use a QPainter to copy the frame onto the composite image and then copy the transparent image over that. Finally, copy the composite pixmap to the pixmap in the scene.

Do not add the composite pixmap to the scene.

Upvotes: 2

Related Questions