I want to get every frames from a QMediaPlayer
and convert it to QImage
(or cv::Mat
)
so I used videoFrameProbed
signal from QVideoProbe
:
connect(&video_probe_, &QVideoProbe::videoFrameProbed,
[this](const QVideoFrame& currentFrame){
//QImage img = ??
}
But I didn't find any way for getting QImage
from QVideoFrame
!
How can I convert QVideoFrame
to QImage
?!
You can use QImage's constructor:
Where you can get
imageFormat
frompixelFormat
of theQVideoFrame
:Qt has a private function for converting
QVideoFrame
toQImage
. If you want to use it, you'll need to header like a forward declaration:The implementation uses QVideoFrame::map() and does low-level manipulation with the bits. The converters handle a variety of conditions including YUV.
The
qt_imageFromVideoFrame
works on almost all platforms. i.e. Windows, macOS, linux and iOS. The only platform it doesn't work reliably well is Android. There, you'll need to use OpenGL calls to extract the VideoFrame.I have an implementation of
QImage QVideoFrameToQImage( const QVideoFrame& videoFrame );
on GitHub:https://github.com/stephenquan/MyVideoFilterApp/blob/master/QVideoFrameToQImage.cpp
For QCamera output, that method doesn't always work. In particular, QVideoFrame::imageFormatFromPixelFormat() returns QImage::Format_Invalid when given QVideoFrame::Format_Jpeg, which is what's coming out of my QCamera. But this works: