深入了解 libyuv 库

一、什么是libyuv?

libyuv 是一个 Google 维护的开源库,主要提供了一些方便的函数来处理视频图像相关的逻辑,例如:缩放(scaling)、旋转(rotating)、颜色空间转化(color space conversion)、镜像(mirroring) 等等。

libyuv 库支持一些常见的图像格式,例如:jpeg、bmp、png、yuv,以及 Google 的 Webp 格式等等。同时,它支持多种平台,例如:Windows、macOS、Android 以及 iOS 等等。

二、基本使用方法

使用 libyuv 库进行图像处理是相对简单的,你只需要按照官方文档所示,通过 include 关键字将 libyuv 头文件包含进你的项目,同时将 libyuv 库文件链接到你的代码中,即可轻松使用。

#include "libyuv.h"

libyuv 的主要函数都在 libyuv.h 这个头文件中定义了,因此你可以直接通过这个头文件来使用它的函数。

三、图像旋转

旋转图像是一种非常常见的操作,libyuv 内置了一些函数来完成这个操作,例如:

int libyuv::RotatePlane(const uint8* src_plane,
                        int src_stride_plane,
                        uint8* dst_plane,
                        int dst_stride_plane,
                        int width,
                        int height,
                        enum RotationMode mode);

上面的代码是 libyuv 库中提供的旋转函数之一。其中,src_plane 表示源图像数据地址,src_stride_plane 表示源图像数据每行的字节数,dst_plane 表示目标图像数据地址,dst_stride_plane 表示目标图像数据每行的字节数,width 和 height 表示图像宽度和高度,mode 表示旋转的模式。

通过调用上述函数,你可以轻松地对输入的图像进行旋转操作。下面是一个完整的代码示例:

    uint8* src_y = (uint8*)malloc(width * height);
    uint8* dst_y = (uint8*)malloc(width * height);
    uint8* src_u = (uint8*)malloc(width * height / 4);
    uint8* src_v = (uint8*)malloc(width * height / 4);
    uint8* dst_u = (uint8*)malloc(width * height / 4);
    uint8* dst_v = (uint8*)malloc(width * height / 4);

    // fill in src_y/src_u/src_v with image data

    libyuv::I420Rotate(src_y, width,
                       src_u, width / 2,
                       src_v, width / 2,
                       dst_y, height,
                       dst_u, height / 2,
                       dst_v, height / 2,
                       width, height,
                       libyuv::kRotate90);

四、图像缩放

除了旋转之外,libyuv 还支持图像缩放操作。缩放图像是一个比较耗时的操作,但是多数情况下都是必须的,这也是 libyuv 库设计的初衷之一。libyuv 支持多种缩放算法,例如:双线性插值、双三次插值等等。

int libyuv::I420Scale(const uint8* src_y,
                      int src_stride_y,
                      const uint8* src_u,
                      int src_stride_u,
                      const uint8* src_v,
                      int src_stride_v,
                      int src_width,
                      int src_height,
                      uint8* dst_y,
                      int dst_stride_y,
                      uint8* dst_u,
                      int dst_stride_u,
                      uint8* dst_v,
                      int dst_stride_v,
                      int dst_width,
                      int dst_height,
                      interp_type_ interpolation = kFilterBox);

上述代码是 libyuv 中提供的缩放函数之一。其中,src_y、src_u 和 src_v 分别表示源图像的 YUV 数据,src_stride_y、src_stride_u 和 src_stride_v 分别表示三个分量的行字节数,dst_y、dst_u 和 dst_v 表示目标图像的 YUV 数据,dst_stride_y、dst_stride_u 和 dst_stride_v 分别表示三个分量的行字节数,src_width 和 src_height 表示源图像的宽度和高度,dst_width 和 dst_height 表示目标图像的宽度和高度,interpolation 表示缩放算法的类型。

完整的缩放代码示例如下:

uint8* src_y = ...; // input data
uint8* src_u = ...; // input data
uint8* src_v = ...; // input data
uint8* dst_y = ...; // output data
uint8* dst_u = ...; // output data
uint8* dst_v = ...; // output data

int src_w = ...; // input width
int src_h = ...; // input height
int dst_w = ...; // output width
int dst_h = ...; // output height

int src_stride_y = src_w;
int src_stride_u = src_w >> 1;
int src_stride_v = src_w >> 1;

int dst_stride_y = dst_w;
int dst_stride_u = dst_w >> 1;
int dst_stride_v = dst_w >> 1;

libyuv::I420Scale(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, src_w, src_h, dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v, dst_w, dst_h);

五、图像颜色空间转换

在视频处理过程中,经常需要将图像从一种颜色空间转换到另一种颜色空间。libyuv 提供了非常方便的函数来完成这个任务。

int libyuv::I420ToABGR(const uint8* src_y, int src_stride_y,
                       const uint8* src_u, int src_stride_u,
                       const uint8* src_v, int src_stride_v,
                       uint8* dst_abgr, int dst_stride_abgr,
                       int width, int height);

上述代码是 libyuv 中提供的颜色空间转换函数之一。其中,src_y、src_u 和 src_v 分别表示源图像的 YUV 数据,src_stride_y、src_stride_u 和 src_stride_v 分别表示三个分量的行字节数,dst_abgr 表示目标图像的 ABGR 数据,dst_stride_abgr 表示 ABGR 数据的行字节数,width 和 height 表示图像的宽度和高度。

完整的颜色空间转换示例如下:

uint8* src_y = ...; // input data
uint8* src_u = ...; // input data
uint8* src_v = ...; // input data
uint8* dst_abgr = ...; // output data

int src_w = ...; // input width
int src_h = ...; // input height

int src_stride_y = src_w;
int src_stride_u = src_w >> 1;
int src_stride_v = src_w >> 1;

int dst_stride_abgr = src_w << 2;

libyuv::I420ToABGR(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, dst_abgr, dst_stride_abgr, src_w, src_h);

六、图像镜像

图像镜像也是一种非常常见的操作,同样也是 libyuv 内置的一个函数。

int libyuv::I420Mirror(const uint8* src_y, int src_stride_y,
                        const uint8* src_u, int src_stride_u,
                        const uint8* src_v, int src_stride_v,
                        uint8* dst_y, int dst_stride_y,
                        uint8* dst_u, int dst_stride_u,
                        uint8* dst_v, int dst_stride_v,
                        int width, int height);

上述代码是 libyuv 中提供的图像镜像函数之一。其中,src_y、src_u 和 src_v 分别表示源图像的 YUV 数据,src_stride_y、src_stride_u 和 src_stride_v 分别表示三个分量的行字节数,dst_y、dst_u 和 dst_v 表示目标图像的 YUV 数据,dst_stride_y、dst_stride_u 和 dst_stride_v 分别表示三个分量的行字节数,width 和 height 表示图像的宽度和高度。

完整的图像镜像示例如下:

uint8* src_y = ...; // input data
uint8* src_u = ...; // input data
uint8* src_v = ...; // input data
uint8* dst_y = ...; // output data
uint8* dst_u = ...; // output data
uint8* dst_v = ...; // output data

int src_w = ...; // input width
int src_h = ...; // input height

int src_stride_y = src_w;
int src_stride_u = src_w >> 1;
int src_stride_v = src_w >> 1;

int dst_stride_y = src_w;
int dst_stride_u = src_w >> 1;
int dst_stride_v = src_w >> 1;

libyuv::I420Mirror(src_y, src_stride_y, src_u, src_stride_u, src_v, src_stride_v, dst_y, dst_stride_y, dst_u, dst_stride_u, dst_v, dst_stride_v, src_w, src_h);

七、小结

libyuv 是一款非常实用的图像处理库,它提供了高效、稳定、跨平台的图像处理函数,且对许多视频应用开发者来说很方便地实现了图像处理。本文简单介绍了 libyuv 的几种基本使用方法,希望能帮助大家更好地使用 libyuv 进行图像处理操作。

原创文章,作者:XAGRQ,如若转载,请注明出处:https://www.506064.com/n/361584.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
XAGRQXAGRQ
上一篇 2025-02-25 18:17
下一篇 2025-02-25 18:17

相关推荐

  • 深入解析Vue3 defineExpose

    Vue 3在开发过程中引入了新的API `defineExpose`。在以前的版本中,我们经常使用 `$attrs` 和` $listeners` 实现父组件与子组件之间的通信,但…

    编程 2025-04-25
  • 深入理解byte转int

    一、字节与比特 在讨论byte转int之前,我们需要了解字节和比特的概念。字节是计算机存储单位的一种,通常表示8个比特(bit),即1字节=8比特。比特是计算机中最小的数据单位,是…

    编程 2025-04-25
  • 深入理解Flutter StreamBuilder

    一、什么是Flutter StreamBuilder? Flutter StreamBuilder是Flutter框架中的一个内置小部件,它可以监测数据流(Stream)中数据的变…

    编程 2025-04-25
  • 深入探讨OpenCV版本

    OpenCV是一个用于计算机视觉应用程序的开源库。它是由英特尔公司创建的,现已由Willow Garage管理。OpenCV旨在提供一个易于使用的计算机视觉和机器学习基础架构,以实…

    编程 2025-04-25
  • 深入了解scala-maven-plugin

    一、简介 Scala-maven-plugin 是一个创造和管理 Scala 项目的maven插件,它可以自动生成基本项目结构、依赖配置、Scala文件等。使用它可以使我们专注于代…

    编程 2025-04-25
  • 深入了解LaTeX的脚注(latexfootnote)

    一、基本介绍 LaTeX作为一种排版软件,具有各种各样的功能,其中脚注(footnote)是一个十分重要的功能之一。在LaTeX中,脚注是用命令latexfootnote来实现的。…

    编程 2025-04-25
  • 深入了解Python包

    一、包的概念 Python中一个程序就是一个模块,而一个模块可以引入另一个模块,这样就形成了包。包就是有多个模块组成的一个大模块,也可以看做是一个文件夹。包可以有效地组织代码和数据…

    编程 2025-04-25
  • 深入探讨冯诺依曼原理

    一、原理概述 冯诺依曼原理,又称“存储程序控制原理”,是指计算机的程序和数据都存储在同一个存储器中,并且通过一个统一的总线来传输数据。这个原理的提出,是计算机科学发展中的重大进展,…

    编程 2025-04-25
  • 深入理解Python字符串r

    一、r字符串的基本概念 r字符串(raw字符串)是指在Python中,以字母r为前缀的字符串。r字符串中的反斜杠(\)不会被转义,而是被当作普通字符处理,这使得r字符串可以非常方便…

    编程 2025-04-25
  • 深入剖析MapStruct未生成实现类问题

    一、MapStruct简介 MapStruct是一个Java bean映射器,它通过注解和代码生成来在Java bean之间转换成本类代码,实现类型安全,简单而不失灵活。 作为一个…

    编程 2025-04-25

发表回复

登录后才能评论