2024-05-10 Ubuntu上面使用libyuv,用于转换、缩放、旋转和其他操作YUV图像数据,测试实例使用I420ToRGB24

一、简介:libyuv 最初是由Google开发的,主要是为了支持WebRTC项目中的视频处理需求。用于处理YUV格式图像数据的开源库。它提供了一系列的函数,用于转换、缩放、旋转和其他操作YUV图像数据。

二、执行下面的命令下载和安装libyuv。

复制代码
git clone https://github.com/lemenkov/libyuv.git
cd libyuv
mkdir build && cd build
cmake ..
make
sudo make install

三、测试实例convert_yuv_to_rgb.cpp,使用c编译的时候,I420ToRGB24前面就不要有libyuv::。

复制代码
#include <stdio.h>
#include <stdlib.h>
#include "libyuv/convert_from.h"
#include "libyuv/convert.h"

int main() {
    FILE *input_file = fopen("cowboy_girl_1024X1280_yuv420p_i420.yuv", "rb");
    if (!input_file) {
        printf("Error opening input file.\n");
        return 1;
    }

    int width = 1024;
    int height = 1280;
    size_t uv_size = (width * height) / 2;

    uint8_t *yuv_data = (uint8_t *)malloc(width * height * 3 / 2);
    if (!yuv_data) {
        printf("Memory allocation error.\n");
        fclose(input_file);
        return 1;
    }

    fread(yuv_data, sizeof(uint8_t), width * height * 3 / 2, input_file);
    fclose(input_file);

    // Convert YUV to RGB24
    uint8_t *rgb_data = (uint8_t *)malloc(width * height * 3);
    if (!rgb_data) {
        printf("Memory allocation error.\n");
        free(yuv_data);
        return 1;
    }

    libyuv::I420ToRGB24(yuv_data, width, yuv_data + width * height, width / 2,
                        yuv_data + width * height * 5 / 4, width / 2,
                        rgb_data, width * 3, width, height);
/*
    libyuv::I420ToRAW(yuv_data, width, yuv_data + width * height, width / 2,
                        yuv_data + width * height * 5 / 4, width / 2,
                        rgb_data, width * 3, width, height);
*/
    // Save RGB image to file
    FILE *output_file = fopen("output.rgb", "wb");
    if (!output_file) {
        printf("Error opening output file.\n");
        free(yuv_data);
        free(rgb_data);
        return 1;
    }

    fwrite(rgb_data, sizeof(uint8_t), width * height * 3, output_file);
    fclose(output_file);

    free(yuv_data);
    free(rgb_data);

    printf("Conversion complete.\n");

    return 0;
}

四、测试运行结果

复制代码
g++ -o convert_yuv_to_rgb convert_yuv_to_rgb.cpp -lyuv
./convert_yuv_to_rgb

五、上面的测试得出的yuv文件显示出来的效果有点异常,R和B对换了,为啥呢?这个问题困扰了我许久。直到我看到我看到libyuv/include/libyuv/convert.h里面有这一段才豁然开朗,因为RGB24ToI420也是存在这个问题,解决方法是使用I420ToRAW、RAWToI420对换。

复制代码
// RGB little endian (bgr in memory) to I420.
LIBYUV_API
int RGB24ToI420(const uint8_t* src_rgb24,
                int src_stride_rgb24,
                uint8_t* dst_y,
                int dst_stride_y,
                uint8_t* dst_u,
                int dst_stride_u,
                uint8_t* dst_v,
                int dst_stride_v,
                int width,
                int height);

六、如果运行的时候提示找不到libyuv.so库,按照下面的方法运行sudo ldconfig更新动态链接库缓存。也可以直接用gcc -o yuv yuv.c /usr/local/lib/libyuv.so这种编译形式。

复制代码
编辑配置文件并使新安装的库生效:
sudo vi /etc/ld.so.conf
在末尾加入如下行:
include /usr/local/lib

sudo ldconfig
相关推荐
大侠锅锅6 小时前
第 19 篇:远程运维体系——指标、SSH、日志与 OTA 升级
运维·ssh
TARDIS_20207 小时前
VMware配置ubuntu26.04虚拟机桥接
ubuntu
公众号:fuwuqiBMC8 小时前
(转自“服务器BMC”)服务器BMC芯片——多Die(芯片)封装
运维·服务器·人工智能
jsons18 小时前
autofs挂载
linux·服务器·网络
liwulin05068 小时前
【ollama】自定义结构化输出
linux·前端·数据库·ollama
gs801409 小时前
【实战】记一次 Linux 服务器入侵排查:grok-agent 后门木马深度剖析与彻底清理
linux·运维·服务器
时空无限9 小时前
vllm 大模型启动缓存相关环境变量 export
linux·缓存·vllm
Echo_cy_10 小时前
基于ZYNQ-7000的Ethernet驱动配置
linux·驱动开发·嵌入式硬件·fpga开发·ethernet·zynq
ShineWinsu10 小时前
对于Linux:模版方法类的解析以及socket、TcpSocket的封装
linux·c++·面试·socket·模板方法模式·封装·tcpsocket
D2aZXN3FhrDa7e21210 小时前
佛山乐从低预算实体店如何选择?看美诚AI自动化获客方案
运维·人工智能·自动化·佛山美诚科技有限公司