Convert QVideoFrame to QImage

I want to get every frame from QMediaPlayer and convert it to QImage (or cv::Mat )

so I used the videoFrameProbed signal from QVideoProbe :

 connect(&video_probe_, &QVideoProbe::videoFrameProbed, [this](const QVideoFrame& currentFrame){ //QImage img = ?? } 

But I did not find a way to get QImage from QVideoFrame !

How to convert QVideoFrame to QImage ?!

+6
source share
2 answers

You can use the QImage constructor:

  QImage img( currentFrame.bits(), currentFrame.width(), currentFrame.height(), currentFrame.bytesPerLine(), imageFormat); 

Where you can get imageFormat from pixelFormat QVideoFrame :

  QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(currentFrame.pixelFormat()); 
+8
source

For QCamera output, this method does not always work. In particular, QVideoFrame :: imageFormatFromPixelFormat () returns QImage :: Format_Invalid when QVideoFrame :: Format_Jpeg is set, which is what comes out of my QCamera. But it works:

 QImage Camera::imageFromVideoFrame(const QVideoFrame& buffer) const { QImage img; QVideoFrame frame(buffer); // make a copy we can call map (non-const) on frame.map(QAbstractVideoBuffer::ReadOnly); QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat( frame.pixelFormat()); // BUT the frame.pixelFormat() is QVideoFrame::Format_Jpeg, and this is // mapped to QImage::Format_Invalid by // QVideoFrame::imageFormatFromPixelFormat if (imageFormat != QImage::Format_Invalid) { img = QImage(frame.bits(), frame.width(), frame.height(), // frame.bytesPerLine(), imageFormat); } else { // eg JPEG int nbytes = frame.mappedBytes(); img = QImage::fromData(frame.bits(), nbytes); } frame.unmap(); return img; } 
+4
source

Source: https://habr.com/ru/post/980675/


All Articles