首先关于fspecial函数的定义,fspecial函数用于建立预定义的滤波算子。
其语法格式为:
h = fspecial(type)
h = fspecial(type,para)
其中type指定算子的类型,para指定相应的参数;
函数type的类型有:
1、'average'averaging filter为均值滤波,参数为hsize代表模板尺寸,默认值为[3,3]。
函数格式:H = fspecial('average',hsize)
2、'disk'circular averaging filter为圆形区域均值滤波,参数为radius代表区域半径,默认值为5。
函数格式:H = fspecial('disk',radius)
3、'gaussian'Gaussian lowpass filter为高斯低通滤波,有两个参数,hsize表示模板尺寸,默认值为[3 3],sigma为滤波器的标准值,单位为像素,默认值为0.5。
函数格式:H = fspecial('gaussian',hsize,sigma)
4、'laplacian' filter approximating the 2-D Laplacian operatorlaplacian filter为拉普拉斯算子,参数alpha用于控制算子形状,取值范围为[0,1],默认值为0.2.
函数格式:H = fspecial('laplacian',alpha)
5、'log'Laplacian of Gaussian filter为拉普拉斯高斯算子,有两个参数,hsize表示模板尺寸,默认值为[3 3],sigma为滤波器的标准差,单位为像素,默认值为0.5。
函数格式:H = fspecial('log',hsize,sigma)
6、'motion'motion filter运动模糊算子,有两个参数,表示摄像物体逆时针方向以theta角度运动了len个像素,len的默认值为9,theta的默认值为0。
函数格式:H = fspecial('motion',len,theta)
7、'prewitt'Prewitt horizontal edge-emphasizing filter用于边缘增强,大小为[3 3],无参数。
函数格式:H = fspecial('prewitt')
8、'sobel'Sobel horizontal edge-emphasizing filter用于边缘提取,无参数
函数格式:H = fspecial('sobel')the filter H: H'.9、'unsharp'unsharp contrast enhancement filter为对比度增强滤波器。参数alpha用于控制滤波器的形状,范围为[0,1],默认值为0.2.函数格式:H = fspecial('unsharp',alpha)
下面是几个应用的例子,另外还有一个中值滤波没必要用fspecial函数,直接有对应的函数:
1、均值滤波器:
A=fspecial('average',n);%生成系统预定义的3X3滤波器
Y=filter2(A,g)/255;%用生成的滤波器进行滤波,并归一化
其中n为设定的模板大小,g为等待滤波的图像数据;
2、中值滤波器:
Y3=medfilt2(g,n);
其中n为设定的模板大小,g为等待滤波的图像数据;
3、高斯滤波器:
n3=input('请输入高斯滤波器的均值/n');
k=input('请输入高斯滤波器的方差/n');
A2=fspecial('gaussian',k,n3); %生成高斯序列
Y5=filter2(A2,g)/255; %用生成的高斯序列进行滤波
g为等待滤波的图像数据;