=
fspecial('gaussian',
hsize,
sigma),其中hsize是滤波器尺寸,sigma是标准差。
若要自己实现的话:
function
h
=
gau_fil(
hsize,
sigma)
siz
=
(hsize-1)/2
[x,y]
=
meshgrid(-siz(2):siz(2),-siz(1):siz(1))
arg
=
-(x.*x
+
y.*y)/(2*sigma*sigma)
h
=
exp(arg)
h(h<eps*max(h(:)))
=
0
sumh
=
sum(h(:))
if
sumh
~=
0,
h
=
h/sumh
end
end
调用时在command
window输入:(例如大小为3*3,标准差1)
gau_fil([3
3],
1)
得到结果:
ans
=
0.0751
0.1238
0.0751
0.1238
0.2042
0.1238
0.0751
0.1238
0.0751
望采纳,谢谢!
可以的。int*** SmoothImage(int ***XImage ,int width, int height, int channel)
{
double sigma = 1.85//(n/2 -1)*0.3 +0.8 { n = 9 ,no. of elements}
double conv[3][3]
double hg = 0
//Convolution kernel
for(i=0i<3i++)
{
for(j=0j<3j++)
{
int u=i-1//subtract from centre element index in 3*3 (1,1)
int v=j-1
conv[i][j] =exp ( - ((u*u) + (v*v)) / (2*sigma*sigma) )
hg += conv[i][j]
}
}
for(int i=0i<3i++)
{
for(int j=0j<3j++)
{
conv[i][j] = conv[i][j] / hg
}
}
int*** sXImage = 0
sXImage = CreateImageMatrix( sXImage , width , height , channel )// allocating a 3d array
//Assigning weights
for(int i =0i <height i++)
{
for(int j =0j <width j++)
{
for(int k =0k <3 k++)
{
double val = 0
double valw =0
if(j-1 >0 &&i-1 >0)
{
val += conv[0][0] * XImage[i-1][j-1][k]
valw += conv[0][0]
}
if(i-1 >0 )
{
val += conv[0][1] * XImage[i-1][j][k]
valw += conv[0][1]
}
if(i-1 >0 &&j+1 <width)
{
val += conv[0][2] * XImage[i-1][j+1][k]
valw += conv[0][2]
}
if(j-1 >0 )
{
val += conv[1][0] * XImage[i][j-1][k]
valw += conv[1][0]
}
val += conv[1][1] * XImage[i][j][k]
valw += conv[1][1]
if(j+1 <width)
{
val += conv[1][2] * XImage[i][j+1][k]
valw += conv[1][2]
}
if(j-1 >0 &&i+1 <height)
{
val += conv[2][0] * XImage[i+1][j-1][k]
valw += conv[2][0]
}
if(i+1 <height)
{
val += conv[2][1] * XImage[i+1][j][k]
valw += conv[2][1]
}
if(j+1 <width &&i+1 <height)
{
val += conv[2][2] * XImage[i+1][j+1][k]
valw += conv[2][2]
}
sXImage[i][j][k] = val / valw
}
}
}
return( sXImage)
}
高斯滤波器是一类根据高斯函数的形状来选择权值的线性平滑滤波器。高斯平滑滤波器对于抑制服从正态分布 的噪声非常有效。一维零均值高斯函数为:g(x)=exp( -x^2/(2 sigma^2)
其中,高斯分布参数Sigma决定了高斯函数的宽度。对于图像处理来说,常用二维零均值离散高斯函数作平滑滤波器。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)