首先關於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為等待濾波的圖像數據;