双边滤波算法的简易实现bilateralFilter

没怎么看过双边滤波的具体思路,动手写一写,看看能不能突破一下。

最后,感觉算法还是要分开 水平 与 垂直 方向进行分别处理,才能把速度提上去。

没耐性写下去了,发上来,给大伙做个参考好了。

先上几张效果图。

半径参数为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);
		}
	} 
}

 抛砖引玉一下,算法思路比较清晰。

懒得描述细节,代码很短,看代码吧。

原文地址:https://www.cnblogs.com/cpuimage/p/5052695.html