Qt 中如何将图片转化为yuv420p

使用 QImage 存储的图片,如果想转成yuv420p 发送出去,该怎么办呢?

QImage 存储图片有多种格式,可以通过image.format() 查看当前的格式;

建议通过将格式转换为mage.convertToFormat(QImage::Format_RGB888),这样rgb的存储每个八字节,按照rgb rgb 的格式存储;image.bitPlaneCount() 可以查看一个像素点需要用的bit数;

默认是 QImage::Format_ARGB32_Premultiplied格式,此时一个像素用四个字节存储,按照argb argb的格式,不同的格式转换为yuv420p的时候,由于rgb的数据排列方式不同,需要采用不同的计算方式,因此建议转化为QImage::Format_RGB888;

这里给出计算方式:

复制代码
#include <QtGui/QImage>
#include <QtCore/QByteArray>

/*
origin_image  QImage图片
width /height  图片的宽度/高度
yuvData   yuv420p数据
*/ 
void convertRGBToYUV420P1(const QImage& origin_image, int width, int height, QByteArray& yuvData)
{
     QImage image ;
     // 将图片转换为指定大小
     image = origin_image.scaled(width, height, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
     qDebug() << "Format:" << image.format();

     image = image.convertToFormat(QImage::Format_RGB888);
    qDebug() << "Format:" << image.format();
     // 提取RGB数据
     QByteArray rgbData;
     const int bytesPerLine = image.width() * image.bitPlaneCount()/8;
     qDebug() << "image:" << "width= " << image.width() << ";height:" << image.height()

              << "bytesPerLine:" <<bytesPerLine;

     for (int y = 0; y < image.height(); ++y) {
         const uchar* scanline = image.constScanLine(y);
         rgbData.append(reinterpret_cast<const char*>(scanline), bytesPerLine);
     }

     qDebug() << "image:bitPlaneCount"   << image.bitPlaneCount()/8;
     // 将RGB数据转换为YUV420P


     FILE* fpyuv = fopen("d:/test/out_200x200_yuv420p.yuv", "wb");
    const uchar* rgbPtr = reinterpret_cast<const uchar*>(rgbData.constData());
     int w = image.width(), h = image.height();
     unsigned char b, g, r;
     unsigned char* ybuf = new unsigned char[w * h];
     unsigned char* ubuf = new unsigned char[w * h / 4];
     unsigned char* vbuf = new unsigned char[w * h / 4];


     unsigned char* y = ybuf;
     unsigned char* u = ubuf;
     unsigned char* v = vbuf;
     // //图像是以RGB排序的,图像数据是左上角为图像第一个像素,从左往右,从上往下排列的
    for (int i = 0; i < h; i++) {

         for (int j = 0; j < w; j++) {
           //    rgbPtr++;
               r = *rgbPtr++;
               g = *rgbPtr++;
               b = *rgbPtr++;
             unsigned char Y = (unsigned char)((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;

             *y = Y;
             y++;

             /*
            yuv420的uv采样规则:
            一行一行的扫描采样,每一行都会采集U或V
            在偶数行的偶数列位置采集U,奇数行的奇数列位置采集V
            整体采样比例y:u:v=4:1:1
            YUV 4:2:0 并不是说不采样V分量,这个0是因为每次都是隔行采集U和V,
            如果第一行是 4:2:0,下一行就是 4:0:2,再下一行又是 4:2:0,以此类推
            如图:每一个2x2的子块中,左上角采集u,右小角采集v
            */

             if (i % 2 == 0&& j % 2 == 0) {
                 unsigned char  U = (unsigned char)((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;

                 *(u++) = U;
             }
             else if (i % 2 != 0 && j % 2 != 0) {
                 unsigned char V = (unsigned char)((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;

                 *(v++) = V;
             }

         }
     }

    yuvData.append(reinterpret_cast<const char*>(ybuf), w * h);
    yuvData.append(reinterpret_cast<const char*>(ubuf), w * h/ 4);
    yuvData.append(reinterpret_cast<const char*>(vbuf), w * h/ 4);

     //yuv420p,先写y,再写u,在写v
     fwrite(ybuf, 1, w * h, fpyuv);
     fwrite(ubuf, 1, w * h / 4, fpyuv);
     fwrite(vbuf, 1, w * h / 4, fpyuv);
     fclose(fpyuv);

    delete[] ybuf;
    delete[] ubuf;
    delete[] vbuf;
}
相关推荐
小徐不徐说6 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang6 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
Wang's Blog6 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象3276 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
Dxy12393102167 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷7 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
云和数据.ChenGuang8 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
nice_lcj5208 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试
樱花树下的数学喵10 小时前
手写简易 STL string|C++ 字符串底层实现
开发语言·c++
jimy111 小时前
C++ 报错的 qualifiers 的意思
开发语言·c++