深入了解 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/zh-hant/n/361584.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
XAGRQ的頭像XAGRQ
上一篇 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

發表回復

登錄後才能評論