一、什麼是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/zh-hk/n/361584.html