c++对yuv图片缩放

这个博客介绍了如何使用C++和FFmpeg库实现图像转换。`ImageConvert`类负责处理转换过程,包括初始化、释放资源、转换图像以及保存转换后的图像到文件。通过调用`convert`方法,可以将源图像数据按指定的像素格式和尺寸转换为目标格式和尺寸。最终,转换后的图像可以使用`save`方法保存为文件,并可用ffplay查看结果。
摘要由CSDN通过智能技术生成

yuv图片简介

YUV图片是一种采用YUV颜色编码方法的图像格式‌。‌YUV将亮度信息(‌Y)‌与色彩信息(‌U和V)‌分离,‌其中Y代表明亮度(‌灰度值)‌,‌U和V代表色度,‌用于描述影像色彩及饱和度。‌这种编码方式基于人眼对亮度比色彩敏感度更高的特点,‌通过降低色度带宽来优化彩色视频信号的传输,‌占用存储空间少,‌数据传输效率高,‌且能兼容黑白与彩色显示。‌YUV图片广泛应用于电视系统、‌视频处理及计算机视觉领域‌

代码

image_convert.cpp, 使用ffmpeg库

extern "c"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
}
#include <stdint.h>

//g++ ./image_convert.cpp -lswscale -lavutil -lavformat -lavcodec
//ffplay -f rawvideo -pix_fmt bgra -video_size 480x320 ./480_320_rgb.img.rgb

struct AVFrame;
struct SwsContext;

class ImageConvert
{
    public:
        ImageConvert(int srcW, int srcH, int dstW, int dstH, int srcPixFmtType, int dstPixFmtType);
        void Init(int srcW, int srcH, int dstW, int dstH, int srcPixFmtType, int dstPixFmtType);
        void Release();
        ~ImageConvert();
        bool Convert(const uint8_t *src);
        bool Convert(const uint8_t *src, int srcW, int srcH, int dstW,
            int dstH, int srcPixFmtType, int dstPixFmtType);
        const uint8_t *GetData() const;
        void Save(const char *filename) const;

    private:
        AVFrame *m_pDstFrame;
        uint8_t *m_dstBuffer;
        struct SwsContext *m_pSwsContext;
        int m_srcW;
        int m_srcH;
        int m_dstW;
        int m_dstH;
        int m_srcPixFmtType;
        int m_dstPixFmtType;
};

imageconvert::imageconvert(int srcw, int srch, int dstw, int dsth, int srcpixfmttype, int dstpixfmttype)
{
    init(srcw, srch, dstw, dsth, srcpixfmttype, dstpixfmttype);
}

void imageconvert::init(int srcw, int srch, int dstw, int dsth, int srcpixfmttype, int dstpixfmttype)
{
    m_srcw = srcw;
    m_srch = srch;
    m_dstw = dstw;
    m_dsth = dsth;

    //av_pix_fmt_yuv420p, av_pix_fmt_rgb32
    m_srcpixfmttype = srcpixfmttype;
    m_dstpixfmttype = dstpixfmttype;

    m_pdstframe = av_frame_alloc();

    //int numbytes = avpicture_get_size(av_pix_fmt_rgb32, dstw, dsth);
    int numbytes = av_image_get_buffer_size((avpixelformat)m_dstpixfmttype, dstw, dsth, 1);
    m_dstbuffer = (uint8_t *) av_malloc(numbytes * sizeof(uint8_t));

    //avpicture_fill((avpicture *)m_pdstframe, m_dstbuffer, av_pix_fmt_rgb32, dstw, dsth);
    av_image_fill_arrays(m_pdstframe->data, m_pdstframe->linesize, m_dstbuffer, (avpixelformat)m_dstpixfmttype, dstw, dsth, 1);

    m_pswscontext = sws_getcontext(srcw, srch, (avpixelformat)m_srcpixfmttype,
                                   dstw, dsth, (avpixelformat)m_dstpixfmttype, sws_bicubic, null, null, null);
}

imageconvert::~imageconvert()
{
    release();
}

void imageconvert::release()
{
    av_frame_free(&m_pdstframe);
    av_free(m_dstbuffer);
    sws_freecontext(m_pswscontext);
}

bool imageconvert::convert(const uint8_t *src)
{
    return convert(src, m_srcw, m_srch, m_dstw, m_dsth, m_srcpixfmttype, m_dstpixfmttype);
}

bool imageconvert::convert(const uint8_t *src, int srcw, int srch, int dstw,
    int dsth, int srcpixfmttype, int dstpixfmttype)
{
    if (srcw != m_srcw || srch != m_srch || dstw != m_dstw || dsth != m_dsth
        || srcpixfmttype != m_srcpixfmttype || dstpixfmttype != m_dstpixfmttype)
    {
        release();
        init(srcw, srch, dstw, dsth, m_srcpixfmttype, m_dstpixfmttype);
    }

    avframe *psrcframe = av_frame_alloc();
    //avpicture_fill((avpicture *)psrcframe, src, av_pix_fmt_yuv420p, srcw, srch);
    av_image_fill_arrays(psrcframe->data, psrcframe->linesize, src, (avpixelformat)m_srcpixfmttype, srcw, srch, 1);

    sws_scale(m_pswscontext,
            psrcframe->data,
            psrcframe->linesize,
            0,
            m_srch,
            m_pdstframe->data,
            m_pdstframe->linesize);
}

const uint8_t *imageconvert::getdata() const
{
    return m_dstbuffer;
}

void imageconvert::save(const char *filename) const
{
    file *pfile = fopen(filename, "wb");

    if (pfile == null)
        return;

    const uint8_t *data = getdata();
    if (m_dstpixfmttype == av_pix_fmt_rgb32)
    {
        //int size = avpicture_get_size(av_pix_fmt_rgb32, m_dstw, m_dsth);
        int size = av_image_get_buffer_size((avpixelformat)m_dstpixfmttype, m_dstw, m_dsth, 1);
        fwrite(data, 1, size, pfile);

        // close file
        fclose(pfile);
    }
    else
    {
        printf("not implement\n");
    }
}

int main(int argc, char **argv)
{
    int srcw = 700;
    int srch = 700;
    int dstw = 480;
    int dsth = 320;
    imageconvert c(srcw, srch, dstw, dsth, av_pix_fmt_yuv420p, av_pix_fmt_rgb32);
    file *fp = fopen(argv[1], "rb");
    if (fp == null)
    {
        return -1;
    }
    uint8_t yuv[srcw*srch*3/2];
    fread(yuv, sizeof(yuv), 1, fp);

    c.convert(yuv);
    c.save("480_320_rgb.img");

    return 0;
}

使用方法

编译:
g++ ./image_convert.cpp -lswscale -lavutil -lavformat -lavcodec
./a.out 770_770.img

查看结果:
ffplay -f rawvideo -pix_fmt bgra -video_size 480x320 ./480_320_rgb.img

作者:帅得不敢出门

### 回答1: YUV是一种颜色编码格式,常用于表达视频图像。缩放YUV图像的代码可以通过以下步骤实现: 1. 读取源YUV图像数据:首先要读取源YUV图像的数据,并确定图像的宽度和高度。YUV图像的数据通常以二进制文件形式存储。 2. 创建目标YUV图像:根据缩放比例计算目标图像的宽度和高度,然后创建一个新的YUV图像。 3. 缩放Y分量:将源图像的Y分量按照缩放比例进行插值计算,得到目标图像的Y分量。插值算法可以选择简单的双线性插值或更复杂的卷积插值。 4. 缩放UV分量:由于YUV图像的UV分量采样比Y分量低,所以在缩放UV分量时需要考虑到采样率。可以选择将UV分量按照与Y分量相同的缩放比例进行插值,或者采用更复杂的算法进行处理。 5. 写入目标YUV图像数据:将目标图像的YUV数据写入到一个新的二进制文件中,以供后续使用。 需要注意的是,缩放YUV图像并不仅仅是对图像进行简单的拉伸或压缩,还需要考虑到YUV颜色空间的特殊性,以避免颜色失真或其他问题的出现。因此,在实际的代码实现中,还需要考虑色彩空间转换、边缘处理等问题,以保证缩放后的图像质量。 ### 回答2: 在RGB图像处理中,YUV是一种颜色编码系统,常用于视频信号的传输和显示。YUV图像缩放是指改变图像的大小,调整图像的宽度和高度。 在C语言中,可以使用图像处理库,如OpenCV来实现YUV图像的缩放。下面是一个简单的代码示例: ```c #include <opencv2/opencv.hpp> using namespace cv; int main() { // 读取YUV文件 Mat yuvImage = imread("input.yuv", IMREAD_UNCHANGED); // 设置缩放比例 double scale = 0.5; // 缩小为原来的一半,如果想放大,可以设置大于1的值 // 计算缩放后的图像尺寸 Size newSize(yuvImage.cols * scale, yuvImage.rows * scale); // 创建缩放后的图像 Mat resizedImage; // 缩放YUV图像 resize(yuvImage, resizedImage, newSize, 0, 0, INTER_LINEAR); // 保存缩放后的YUV图像 imwrite("output.yuv", resizedImage); return 0; } ``` 在上述代码中,首先使用OpenCV的`imread`函数读取YUV图像。然后,通过设置缩放比例来计算缩放后图像的尺寸。接下来,使用`resize`函数对YUV图像进行缩放操作,将结果保存到`resizedImage`中。最后,使用`imwrite`函数将缩放后的图像保存到指定的输出文件中。 需要注意的是,这只是一个简单的示例代码,实际的应用中可能还需要处理图像的颜色空间转换和数据格式转换等问题。 ### 回答3: c语言中实现yuv缩放的代码如下: ```c #include <stdio.h> #include <stdlib.h> void yuvScale(unsigned char* srcY, unsigned char* srcU, unsigned char* srcV, int srcWidth, int srcHeight, unsigned char* dstY, unsigned char* dstU, unsigned char* dstV, int dstWidth, int dstHeight) { int x, y; int ratio_w = (srcWidth << 16) / dstWidth + 1; int ratio_h = (srcHeight << 16) / dstHeight + 1; for (y = 0; y < dstHeight; y++) { for (x = 0; x < dstWidth; x++) { int px = x * ratio_w >> 16; // 计算源图像的坐标 int py = y * ratio_h >> 16; dstY[y * dstWidth + x] = srcY[py * srcWidth + px]; if (x % 2 == 0 && y % 2 == 0) { // 同时对U和V进行缩放计算 int uIndex = (y >> 1) * (dstWidth >> 1) + (x >> 1); int vIndex = (y >> 1) * (dstWidth >> 1) + (x >> 1); dstU[uIndex] = srcU[(py >> 1) * (srcWidth >> 1) + (px >> 1)]; dstV[vIndex] = srcV[(py >> 1) * (srcWidth >> 1) + (px >> 1)]; } } } } int main() { int srcWidth = 640; int srcHeight = 480; int dstWidth = 320; int dstHeight = 240; unsigned char* srcY = (unsigned char*)malloc(srcWidth * srcHeight * sizeof(unsigned char)); unsigned char* srcU = (unsigned char*)malloc(srcWidth * srcHeight / 4 * sizeof(unsigned char)); unsigned char* srcV = (unsigned char*)malloc(srcWidth * srcHeight / 4 * sizeof(unsigned char)); unsigned char* dstY = (unsigned char*)malloc(dstWidth * dstHeight * sizeof(unsigned char)); unsigned char* dstU = (unsigned char*)malloc(dstWidth * dstHeight / 4 * sizeof(unsigned char)); unsigned char* dstV = (unsigned char*)malloc(dstWidth * dstHeight / 4 * sizeof(unsigned char)); // 将原始YUV数据填充,此处仅为示例,实际应按实际需求填充数据 yuvScale(srcY, srcU, srcV, srcWidth, srcHeight, dstY, dstU, dstV, dstWidth, dstHeight); // 处理缩放后的YUV数据,此处仅为示例,实际应按实际需求处理数据 free(srcY); free(srcU); free(srcV); free(dstY); free(dstU); free(dstV); return 0; } ``` 该代码使用了双重循环,分别对Y、U、V进行缩放计算。首先根据目标图像的宽高和源图像的宽高计算出宽高缩放比,然后通过双重循环遍历目标图像的每个像素点,通过缩放比计算出源图像的坐标,然后将对应位置的Y、U、V分量值赋给目标图像的相应位置。在对U和V分量进行缩放计算时,由于U和V的像素数目是Y的四分之一,所以要对坐标进行相应的位移和缩放。最后,可以根据实际需求对缩放后的YUV数据进行处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值