如何在 Qt 中将带有 YUV 数据的“QVideoFrame"转换为带有 RGBA32 数据的“QVideoframe"?
我从网络摄像头收到 QVideoFrames
,它们包含 YUV 格式 (QVideoFrame::Format_YUV420P
) 的图像数据.如何使用 QVideoFrame::Format_ARGB32
或 QVideoFrame::Format_RGBA32
将一帧转换为一帧?
我可以不使用低级别,只使用 Qt5 中现有的功能吗?
例子:
QVideoFrame convertFormat(const QVideoFrame &inputframe, QVideoFrame::PixelFormat outputFormat){//什么来了?}//用法QVideoFrame 转换 = convertFormat(mySourceFrame, QVideoFrame::Format_RGB32);
解决方案 我找到了一个Qt5内置的解决方案,但是不支持Qt.
具体方法如下:
- 将
QT += media-private
放入您的 qmake .pro 文件中 - 将
#include "private/qvideoframe_p.h"
放入您的代码中以使该功能可用. - 您现在可以访问具有以下签名的函数:
QImage qt_imageFromVideoFrame(const QVideoFrame &frame);
- 使用该函数将
QVideoFrame
转换为临时QImage
,然后从该图像创建输出QVideoFrame
.
这是我的示例用法:
QVideoFrame convertFormat(const QVideoFrame &inputframe, QVideoFrame::PixelFormat outputFormat){inputframe->map(QAbstractVideoBuffer::ReadOnly);QImage tempImage=qt_imageFromVideoFrame(inputframe);inputframe->unmap();QVideoFrame outputFrame=QVideoFrame(tempImage);返回输出帧;}
同样,从标头复制的警告内容如下:
<块引用><代码>//// 警告//-------------////这个文件不是 Qt API 的一部分.它纯粹作为一个存在//实现细节.此头文件可能会从版本更改为//没有通知的版本,甚至被删除.////我们是认真的.//
这在我的项目中并不重要,因为它是个人玩具产品.如果它变得严重,我会追踪该功能的实现并将其复制到我的项目或其他东西中.
I receive QVideoFrames
from webcam, and they contain image data in YUV format (QVideoFrame::Format_YUV420P
). How can I convert one such frame to one with QVideoFrame::Format_ARGB32
or QVideoFrame::Format_RGBA32
?
Can I do it without going low level, using just existing functionality in Qt5?
Example:
QVideoFrame convertFormat(const QVideoFrame &inputframe, QVideoFrame::PixelFormat outputFormat)
{
// What comes here?
}
//Usage
QVideoFrame converted = convertFormat(mySourceFrame, QVideoFrame::Format_RGB32);
解决方案
I found a solution that is built into Qt5, but UNSUPPORTED BY Qt.
Here is how to go about:
- Put
QT += multimedia-private
into your qmake .pro file - Put
#include "private/qvideoframe_p.h"
into your code to make the function available. - You now have access to a function with the following signature:
QImage qt_imageFromVideoFrame(const QVideoFrame &frame);
- Use the function to convert the
QVideoFrame
to a temporayQImage
and then create the outputQVideoFrame
from that image.
Here is my example usage:
QVideoFrame convertFormat(const QVideoFrame &inputframe, QVideoFrame::PixelFormat outputFormat)
{
inputframe->map(QAbstractVideoBuffer::ReadOnly);
QImage tempImage=qt_imageFromVideoFrame(inputframe);
inputframe->unmap();
QVideoFrame outputFrame=QVideoFrame(tempImage);
return outputFrame;
}
Again, the warning copied from the header reads as follows:
// // W A R N I N G // ------------- // // This file is not part of the Qt API. It exists purely as an // implementation detail. This header file may change from version to // version without notice, or even be removed. // // We mean it. //
This does not matter in my project since it is a personal toy product. If it ever gets serious I will just track down the implementation of that function and copy it into my project or something.
相关文章