一、什么是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