当前位置: 首页 > news >正文

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 RGB24uint8_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 fileFILE *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/libsudo ldconfig

http://www.lryc.cn/news/348972.html

相关文章:

  • 怎么给视频加水印?2招轻松搞定
  • SpringBootWeb 篇-深入了解请求响应(服务端接收不同类型的请求参数的方式)
  • 实验十 智能手机互联网程序设计(微信程序方向)实验报告
  • Python图形复刻——绘制母亲节花束
  • 【算法优选】 动态规划之子数组、子串系列——壹
  • PXE+Kickstart无人值守安装安装Centos7.9
  • C语言实现通讯录,包括增删改查以及动态开辟内存,写入文件等功能
  • flowable多对并发网关跳转的分析
  • 【嵌入式——QT】QT集成Ymodem协议使用UDP进行传输
  • python笔记(17)输入输出
  • 408数据结构总结复习笔记一:线性表
  • Docker——目录迁移
  • SpringAMQP-消息转换器
  • 轻松拿下指针(5)
  • Nginx反向代理配置
  • 突破编程界限:探索AI编程新境界
  • C语言(指针)2
  • go学习笔记
  • MacApp自动化测试之Automator初体验
  • Vue学习v-html
  • C++并发:锁
  • Git | git log 和 git status 的区别
  • Django 4.x 智能分页get_elided_page_range
  • java-spring 09 下.populateBean (方法成员变量的注入@Autowird,@Resource)
  • 赛氪网携手众机构助力第七届京津冀生态修复实践论坛圆满落幕
  • Naive RAG 、Advanced RAG 和 Modular RAG 简介
  • Python高级编程-DJango2
  • bash脚本 报错:/bin/bash^M:解释器错误: 没有那个文件或目录
  • win10专业版远程桌面连接不上,win10专业版远程桌面连接不上常见原因与解决方法
  • 前端 日期 new Date 少0 转换成 yyyy-MM-dd js vue