編輯:關於 Android 程式設計
前一篇提到過幾種可用於磨皮的去噪算法,這次把它們實際寫出來並觀察效果——暫時先不糾結實時性,把效果做對再談效能。
本文首先實作的是《基於局部均方差相關信息的圖像去噪及其在實時磨皮美容算法中的應用》一文中提到的方法。它的核心思想是:利用圖像的局部統計特性進行濾波,對每個像素,先取以其為中心、尺寸為 (2n+1)×(2m+1) 的窗口,計算窗口內的灰度均值 m(i,j):

以及均方差:

最終的加性去噪結果為:

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

這個 k 值很有意思:當窗口方差 v 很大(代表該區域是邊緣或紋理)時,k 趨近於 1,結果幾乎保留原像素;當方差很小(平坦區域,例如皮膚)時,k 趨近於 0,結果趨近於窗口均值,也就是被平滑掉了。這正是「磨皮但保留五官輪廓」的數學表達。
朴素做法下,每個像素都要重新遍歷窗口求均值與方差,計算量與半徑平方成正比。改用積分圖(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 中串接,但要注意順序——美白放在磨皮之後,可以避免噪點被同步放大。
至於實時性,積分圖的引入已經把單像素的計算降到常數級,剩下的瓶頸通常在記憶體頻寬與每幀重建積分圖的開銷上,後續可以考慮改用移動窗口增量更新的方式進一步優化。
Android 護眼燈與高相容性手電筒實作
這個範例包含兩個實用功能:護眼濾光片與手電筒。護眼燈透過懸浮視窗覆蓋螢幕,手電筒則利用 Camera 的閃光燈模式。 護眼燈 使用 WindowManager 建立全螢幕
Windows 下 Android 開發環境搭建與配置圖解教學
網上關於 Windows 下搭建 Android 開發環境的文章不勝枚舉,為方便日後參考,這裡做個記錄,重點關注安裝過程中的注意事項。本文介紹的是面向 Java 的 SD
深入理解 Android 之 AOP:從 JoinPoint 到 AspectJ 落地實踐
一、閒談 AOP 大家都熟悉 OOP(Object Oriented Programming,物件導向)。本文要談的是 AOP(Aspect Oriented Progr
Android性能優化之SQLite數據庫:索引與事務
本系列博文想圍繞Android中的一些優化細節與大家分享。Android上的優化可謂重中之重——它不像PC端有充足的內存執行空間供我們重量級使用,有限的內存資源限制了我們