Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Camera 實時濾鏡實現方法探討(九):基於局部均方差的磨皮算法

編輯:關於 Android 程式設計

前一篇提到過幾種可用於磨皮的去噪算法,這次把它們實際寫出來並觀察效果——暫時先不糾結實時性,把效果做對再談效能。

本文首先實作的是《基於局部均方差相關信息的圖像去噪及其在實時磨皮美容算法中的應用》一文中提到的方法。它的核心思想是:利用圖像的局部統計特性進行濾波,對每個像素,先取以其為中心、尺寸為 (2n+1)×(2m+1) 的窗口,計算窗口內的灰度均值 m(i,j):

以及均方差:

最終的加性去噪結果為:

其中權重係數 k 的計算方式為:

這個 k 值很有意思:當窗口方差 v 很大(代表該區域是邊緣或紋理)時,k 趨近於 1,結果幾乎保留原像素;當方差很小(平坦區域,例如皮膚)時,k 趨近於 0,結果趨近於窗口均值,也就是被平滑掉了。這正是「磨皮但保留五官輪廓」的數學表達。

優化一:用積分圖把窗口統計降到 O(1)

朴素做法下,每個像素都要重新遍歷窗口求均值與方差,計算量與半徑平方成正比。改用積分圖(Integral Image)後,任意矩形區域的和可以通過四次查詢得到,與半徑無關。

以半徑 r 的窗口為例,均值分子為:

Integral(i+r, j+r) + Integral(i-r-1, j-r-1) − Integral(i+r, j-r-1) − Integral(i-r-1, j+r)

實作時建立兩張積分圖:一張存灰度值,一張存灰度平方值,後者用來算方差。為了避免整數溢位,累加值使用 uint64_t。

void LocalStatisticsSkinSmooth::initIntegral(uint32_t* inputMatrix, int width, int height, int mode) {
    LOGE("initIntegral start");

    uint64_t* outputMartix = new uint64_t[width * height];
    uint64_t* outputMartixSqr = new uint64_t[width * height];
    uint64_t* columnSum = new uint64_t[width];
    uint64_t* columnSumSqr = new uint64_t[width];

    // 初始化第一列
    int color = ((inputMatrix[0] >> mode) & 0xff);
    columnSum[0] = color;
    columnSumSqr[0] = color * color;
    outputMartix[0] = columnSum[0];
    outputMartixSqr[0] = columnSumSqr[0];

    for (int i = 1; i < width; i++) {
        int color = ((inputMatrix[i] >> mode) & 0xff);
        columnSum[i] = color;
        columnSumSqr[i] = color * color;
        outputMartix[i] = columnSum[i];
        outputMartix[i] += outputMartix[i - 1];
        outputMartixSqr[i] = columnSumSqr[i];
        outputMartixSqr[i] += outputMartixSqr[i - 1];
    }

    // 逐行累加
    for (int i = 1; i < height; i++) {
        int offset = i * width;
        int color = ((inputMatrix[offset] >> mode) & 0xff);
        columnSum[0] += color;
        columnSumSqr[0] += color * color;
        outputMartix[offset] = columnSum[0];
        outputMartixSqr[offset] = columnSumSqr[0];

        for (int j = 1; j < width; j++) {
            int color = ((inputMatrix[offset + j] >> mode) & 0xff);
            columnSum[j] += color;
            columnSumSqr[j] += color * color;
            outputMartix[offset + j] = outputMartix[offset + j - 1] + columnSum[j];
            outputMartixSqr[offset + j] = outputMartixSqr[offset + j - 1] + columnSumSqr[j];
        }
    }

    delete[] columnSum;
    delete[] columnSumSqr;
    // ...略
    LOGE("initIntegral end");
}

優化二:逐像素套用公式

有了兩張積分圖,主循環只需查表計算 m 與 v,再算出 k 並混合即可。這裡額外做了一層判斷:只對亮度達到一定門檻的像素處理,避免把頭髮、深色背景之類的區域一併磨平。

for (int i = 0; i < height; i++) {
    for (int j = 0; j < width; j++) {
        // x(i,j) = (1 - k) * m(i,j) + k * x(i,j)
        ARGB RGB;
        BitmapOperation::convertIntToArgb(
                jniBitmap->_storedBitmapPixels[i * width + j], &RGB);

        // 只處理較亮的區域,避免誤傷暗部細節
        if (RGB.red > 20 && RGB.green > 40 && RGB.blue > 50) {
            color = ((jniBitmap->_storedBitmapPixels[i * width + j] >> mode) & 0xff);

            // 邊界裁剪,防止越界
            int iMax = i + radius >= height ? height - 1 : i + radius;
            int jMax = j + radius >= width  ? width  - 1 : j + radius;
            int iMin = i - radius <= 0      ? 0           : i - radius;
            int jMin = j - radius <= 0      ? 0           : j - radius;

            int square = (iMax - iMin + 1) * (jMax - jMin + 1);

            // 積分圖查詢:窗口總和 = 右下 - 左下 - 右上 + 左上
            float m = (mMartix[iMax * width + jMax]
                    + (jMin > 0 && iMin > 0 ? mMartix[(iMin - 1) * width + (jMin - 1)] : 0)
                    - (jMin > 0 ? mMartix[iMax * width + (jMin - 1)] : 0)
                    - (iMin > 0 ? mMartix[(iMin - 1) * width + jMax] : 0)) / square;

            float v = (mMartixSqr[iMax * width + jMax]
                    + (jMin > 0 && iMin > 0 ? mMartixSqr[(iMin - 1) * width + (jMin - 1)] : 0)
                    - (jMin > 0 ? mMartixSqr[iMax * width + (jMin - 1)] : 0)
                    - (iMin > 0 ? mMartixSqr[(iMin - 1) * width + jMax] : 0)) / square - m * m;

            float k = v / (v + sigema);
            color = (uint8_t) ((1.0f - k) * m + k * color);

            // 將 color 寫回目標資料
        }
    }
}

這裡的 sigema 是控制平滑強度的參數:值越小,k 越容易被拉離 1,磨皮越強;值越大則越保守。實測把磨皮強度調到最大 10 時,效果如下:

可以看到皮膚的瑕疵被明顯弱化,同時眼睛、嘴唇、髮絲的邊緣仍然清晰——這要歸功於方差判斷讓濾波在邊緣處自動收手。

下一步

磨皮完成後,通常還需要對圖像做二次處理:調整對比度、對皮膚區域進行美白、或者疊加一層柔光。這些可以在同一個 pipeline 中串接,但要注意順序——美白放在磨皮之後,可以避免噪點被同步放大。

至於實時性,積分圖的引入已經把單像素的計算降到常數級,剩下的瓶頸通常在記憶體頻寬與每幀重建積分圖的開銷上,後續可以考慮改用移動窗口增量更新的方式進一步優化。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved