c语言实现QT YUV420P格式的QVideoFrame流转化为QImage

QImage frameToImage(const QVideoFrame& frame)
{
    QVideoFrame cloneFrame(frame);
    cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
 
    // 获取帧的大小和像素格式
    QSize frameSize = frame.size();
    QVideoFrame::PixelFormat frameFormat = cloneFrame.pixelFormat();
 
    // 根据像素格式选择转换方法
    if (frameFormat == QVideoFrame::Format_YUV420P)
    {
        // 将YUV420P格式的数据转换为RGB格式
         int width = frameSize.width();
         int height = frameSize.height();
        
         int uIndex = width * height;
         int vIndex = uIndex + ((width * height) >> 2);
        
         const uchar* yData = cloneFrame.bits();
         
         QImage image(frameSize, QImage::Format_RGB888);
         for (int y = 0; y < height; y++) {
               for (int x = 0; x < width; x++) {
                  
                // Y分量        
                double Y = (double)yData[y * width + x];
                // U分量
                double U = (double)yData[uIndex + (y / 2) * (width / 2) + (x / 2)] - 128;
                // V分量        
                double V = (double)yData[vIndex + (y / 2) * (width / 2) + (x / 2)] - 128;
            
            
                 // 转换公式
                int R = (int)(Y + 1.13983 * V);
                int G = (int)(Y - 0.39466 * U - 0.58060 * V);
                int B = (int)(Y + 2.03211 * U);
                
             
                 R = qBound(0, R, 255);
                 G = qBound(0, G, 255);
                 B = qBound(0, B, 255);
             
                 QRgb rgbValue = qRgb(R, G, B);
                 image.setPixel(x, y, rgbValue);
             }
         }
        
        return image;
    }
 
    // 如果帧格式不是YUV420P,则返回空图像
    return QImage();
}

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值