double gausssian5[25]={0.0000,0.0000,0.0002,0.0000,0.0000,0.0000,0.0113,0.0837,0.0113,0.0000,0.0002,0.0837,0.6187,0.0837,0.0002,0.0000,0.0113,0.0837,0.0113,0.0000,0.0000,0.0000,0.0002,0.0000,0.0000};
void Bilateralfilter(int* src,int* result,int Width,int Height)
{
//复制图片数据
int* src0=new int[Width*Height*3];
double* rst0=new double[Width*Height*3];
for(int i=0;i<Width*Height*3;++i)
{
rst0[i]=src0[i]=src[i];
}
//构造色差矩阵
double* secha=new double[256*256];
for(int i=0;i<256;++i)
{
for(int j=0;j<256;++j)
{
int index=j*256+i;
int distance=(i-j)*(i-j);
secha[index]=exp(-0.5L*distance/200);
}
}
//5*5 双边 算子
double suanzi[25];
//RGB 分别计算
for(int z=0;z<3;++z)
{
for(int i=2;i<Width-2;++i)
{
for(int j=2;j<Height-2;++j)
{
rst0[(j*Width+i)*3+z]=0;
double total=0.0L;
for(int m=-2,m0=0;m<3;++m,++m0)
{
for(int n=-2,n0=0;n<3;++n,++n0)
{
int color1=src0[(j*Width+i)*3+z];
int color2=src0[((j+n)*Width+i+m)*3+z];
double dr=secha[color1*256+color2];
dr=dr*dr;
suanzi[m0+n0*5]=dr*gausssian5[m0+n0*5];
total+=suanzi[m0+n0*5];
rst0[(j*Width+i)*3+z]+=src0[((j+n)*Width+i+m)*3+z]*suanzi[m0+n0*5];
}
}
if(total!=0)
{
rst0[(j*Width+i)*3+z]=rst0[(j*Width+i)*3+z]/total;
}
else
{
rst0[(j*Width+i)*3+z]=src0[(j*Width+i)*3+z];
}
}
}
}
int err=0;
for(int i=0;i<Width*Height*3;++i)
{
//四舍五入,转型,拷贝
result[i]=(int)(rst0[i]+0.5);
}
//删除临时数据
delete[] secha;
delete[] src0;
}
//另附一个收集的5阶高斯算子
1/273*[[1,4,7,4,1],[4,16,26,16,4],[7,26,41,26,7],[4,16,26,16,4],[1,4,7,4,1]]
//另附两个LoG算子
-2 -4 -4 -4 -2
-4 0 8 0 -4
-4 8 24 8 -4
-4 0 8 0 -4
-2 -4 -4 -4 -2
0 0 -1 0 0
0 -1 -2 -1 0
-1 -2 16 -2 -1
0 -1 -2 -1 0
0 0 -1 0 0
susan算子:
OffSetX[37] = { -1, 0, 1,
-2,-1, 0, 1, 2,-3,-2,-1, 0, 1, 2, 3,-3,-2,-1, 0, 1, 2, 3,-3,-2,-1, 0, 1, 2, 3,-2,-1, 0, 1, 2,-1, 0, 1 }; OffSetY[37] = { -3,-3,-3,
-2,-2,-2,-2,-2,-1,-1,-1,-1,-1,-1,-1,0, 0, 0, 0, 0, 0, 0,1, 1, 1, 1, 1, 1, 1,2, 2, 2, 2, 2,3, 3, 3 };