沒怎么看過雙邊濾波的具體思路,動手寫一寫,看看能不能突破一下。
最后,感覺算法還是要分開 水平 與 垂直 方向進行分別處理,才能把速度提上去。
沒耐性寫下去了,發上來,給大伙做個參考好了。
先上幾張效果圖。
半徑參數為10.




見圖,磨皮降噪效果還不錯。
具體代碼如下:
void bilateralFilter(unsigned char* pSrc, unsigned char* pDest, int width, int height, int radius)
{
float delta = 0.1f;
float eDelta = 1.0f / (2 * delta * delta);
int colorDistTable[256 * 256] = { 0 };
for (int x = 0; x < 256; x++)
{
int * colorDistTablePtr = colorDistTable + (x * 256);
for (int y = 0; y < 256; y++)
{
int mod = ((x - y) * (x - y))*(1.0f / 256.0f);
colorDistTablePtr[y] = 256 * exp(-mod * eDelta);
}
}
for (int Y = 0; Y < height; Y++)
{
int Py = Y * width;
unsigned char* LinePD = pDest + Py;
unsigned char* LinePS = pSrc + Py;
for (int X = 0; X < width; X++)
{
int sumPix = 0;
int sum = 0;
int factor = 0;
for (int i = -radius; i <= radius; i++)
{
unsigned char* pLine = pSrc + ((Y + i + height) % height)* width;
int cPix = 0;
int * colorDistPtr = colorDistTable + LinePS[X] * 256;
for (int j = -radius; j <= radius; j++)
{
cPix = pLine[ (X + j+width)%width];
factor = colorDistPtr[cPix];
sum += factor;
sumPix += (factor *cPix);
}
}
LinePD[X] = (sumPix / sum);
}
}
}
拋磚引玉一下,算法思路比較清晰。
懶得描述細節,代碼很短,看代碼吧。
