我想从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!
我怎样才能转换QVideoFrame成QImage?!
lar*_*red 11
您可以使用QImage的构造函数:
QImage img( currentFrame.bits(),
currentFrame.width(),
currentFrame.height(),
currentFrame.bytesPerLine(),
imageFormat);
Run Code Online (Sandbox Code Playgroud)
在那里你可以得到imageFormat从pixelFormat的QVideoFrame:
QImage::Format imageFormat = QVideoFrame::imageFormatFromPixelFormat(currentFrame.pixelFormat());
Run Code Online (Sandbox Code Playgroud)
小智 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)