将QVideoFrame转换为QImage

LxL*_*LxL 7 c++ qt qimage

我想从a获取每个帧QMediaPlayer并将其转换为QImage(或cv::Mat)

所以我使用了 videoFrameProbed来自的信号QVideoProbe:

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

但我没有发现任何方式获得QImage来自QVideoFrame!

我怎样才能转换QVideoFrameQImage?!

lar*_*red 11

您可以使用QImage的构造函数:

 QImage img( currentFrame.bits(),
             currentFrame.width(),
             currentFrame.height(),
             currentFrame.bytesPerLine(),
             imageFormat);
Run Code Online (Sandbox Code Playgroud)

在那里你可以得到imageFormatpixelFormatQVideoFrame:

 QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(currentFrame.pixelFormat());
Run Code Online (Sandbox Code Playgroud)

  • 另请阅读此处的要求:http://doc.qt.io/qt-5/qvideoframe.html#details,以便QVideoFrame在调用bits()之前调用map() (2认同)

小智 5

对于QCamera输出,该方法并不总是有效.特别是,QVideoFrame :: imageFormatFromPixelFormat()在给定QVideoFrame :: Format_Jpeg时返回QImage :: Format_Invalid,这是我的QCamera中出现的内容.但这有效:

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 {
        // e.g. JPEG
        int nbytes = frame.mappedBytes();
        img = QImage::fromData(frame.bits(), nbytes);
    }
    frame.unmap();
    return img;
}
Run Code Online (Sandbox Code Playgroud)