Matlab中fspecial的用法


來源:https://blog.csdn.net/hustrains/article/details/9153553

 

Fspecial函數用於創建預定義的濾波算子,會與imfilter搭配使用,其語法格式為:
h = fspecial(type)h = fspecial(type,parameters,sigma)

參數type制定算子類型,parameters指定相應的參數,具體格式為:
type='average',為均值濾波,參數為n,代表模版尺寸,用向量表示,默認值為[3,3]。
type= 'gaussian',為高斯低通濾波器,參數有兩個,n表示模版尺寸,默認值為[3,3],sigma表示濾波器的標准差,單位為像素,默認值為
0.5。
When used with the 'average' filter type, the default filter size is [3 3]. When used with the Laplacian of Gaussian ('log') filter type, the default filter size is [5 5].
type= 'laplacian',為拉普拉斯算子,參數為alpha,用於控制拉普拉斯算子的形狀,取值范圍為[0,1],默認值為0.2。
type= 'log',為拉普拉斯高斯算子,參數有兩個,n表示模版尺寸,默認值為[3,3],sigma為濾波器的標准差,單位為像素,默認值為0.5
type= 'prewitt',為prewitt算子,用於邊緣增強,無參數。
type= 'sobel',為著名的sobel算子,用於邊緣提取,無參數。
type= 'unsharp',為對比度增強濾波器,參數alpha用於控制濾波器的形狀,范圍為[0,1],默認值為0.2。
例子:
>> G=fspecial('gaussian',5)%參數為5,表示產生5*5的gaussian矩陣,如果沒有,默認為3*3的矩陣。
G =     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
>> G=fspecial('gaussian',5,1.5)%1.5為濾波器的標准差。
G =     0.0144    0.0281    0.0351    0.0281    0.0144    0.0281    0.0547    0.0683    0.0547    0.0281    0.0351    0.0683    0.0853    0.0683    0.0351    0.0281    0.0547    0.0683    0.0547    0.0281    0.0144    0.0281    0.0351    0.0281    0.0144
>>
>> G=fspecial('average')%默認為3*3的矩陣。均值濾波
G =     0.1111    0.1111    0.1111    0.1111    0.1111    0.1111    0.1111    0.1111    0.1111
>> G=fspecial('average',5)%會產生5*5的矩陣。
————————————————

 
 
來源參考:https://ww2.mathworks.cn/help/images/ref/fspecial.html?searchHighlight=fspecial&s_tid=doc_srchtitle#d117e81606
 

fspecial

Create predefined 2-D filter

collapse all in page
 

Description

example

h = fspecial(type) creates a two-dimensional filter h of the specified type. Some of the filter types have optional additional parameters, shown in the following syntaxes. fspecial returns h as a correlation kernel, which is the appropriate form to use with imfilter.

h = fspecial('average',hsize) returns an averaging filter h of size hsize.

h = fspecial('disk',radius) returns a circular averaging filter (pillbox) within the square matrix of size 2*radius+1.

h = fspecial('gaussian',hsize,sigma) returns a rotationally symmetric Gaussian lowpass filter of size hsize with standard deviation sigma. Not recommended. Use imgaussfilt or imgaussfilt3 instead.

h = fspecial('laplacian',alpha) returns a 3-by-3 filter approximating the shape of the two-dimensional Laplacian operator, alpha controls the shape of the Laplacian.

h = fspecial('log',hsize,sigma) returns a rotationally symmetric Laplacian of Gaussian filter of size hsize with standard deviation sigma.

h = fspecial('motion',len,theta) returns a filter to approximate, once convolved with an image, the linear motion of a camera. len specifies the length of the motion and theta specifies the angle of motion in degrees in a counter-clockwise direction. The filter becomes a vector for horizontal and vertical motions. The default len is 9 and the default theta is 0, which corresponds to a horizontal motion of nine pixels.

h = fspecial('prewitt') returns a 3-by-3 filter that emphasizes horizontal edges by approximating a vertical gradient. To emphasize vertical edges, transpose the filter h'.

 

[ 1  1  1 
  0  0  0 
 -1 -1 -1 ]

 

h = fspecial('sobel') returns a 3-by-3 filter that emphasizes horizontal edges using the smoothing effect by approximating a vertical gradient. To emphasize vertical edges, transpose the filter h'.

 

[ 1  2  1 
  0  0  0 
 -1 -2 -1 ]

 

 

Examples

collapse all

Create Various Filters and Filter an Image

Read image and display it.

I = imread('cameraman.tif');
imshow(I);

Create a motion filter and use it to blur the image. Display the blurred image.

H = fspecial('motion',20,45);
MotionBlur = imfilter(I,H,'replicate');
imshow(MotionBlur);

Create a disk filter and use it to blur the image. Display the blurred image.

H = fspecial('disk',10);
blurred = imfilter(I,H,'replicate'); 
imshow(blurred);

 
 

Input Arguments

collapse all

typeType of filter
'average' | 'disk' | 'gaussian' | 'laplacian' | 'log' | 'motion' | 'prewitt' | 'sobel'

Type of filter, specified as one of the following values:

Value

Description

'average'

Averaging filter

'disk'

Circular averaging filter (pillbox)

'gaussian'

Gaussian lowpass filter. Not recommended. Use imgaussfilt or imgaussfilt3 instead.

'laplacian'

Approximates the two-dimensional Laplacian operator

'log'

Laplacian of Gaussian filter

'motion'

Approximates the linear motion of a camera

'prewitt'

Prewitt horizontal edge-emphasizing filter

'sobel'

Sobel horizontal edge-emphasizing filter

Data Types: char | string

hsizeSize of the filter
positive integer | 2-element vector of positive integers

Size of the filter, specified as a positive integer or 2-element vector of positive integers. Use a vector to specify the number of rows and columns in h. If you specify a scalar, then h is a square matrix.

When used with the 'average' filter type, the default filter size is [3 3]. When used with the Laplacian of Gaussian ('log') filter type, the default filter size is [5 5].

Data Types: double

radiusRadius of a disk-shaped filter
5 (default) | positive number

Radius of a disk-shaped filter, specified as a positive number.

Data Types: double

sigmaStandard deviation
0.5 (default) | positive number

Standard deviation, specified as a positive number.

Data Types: double

alphaShape of the Laplacian
0.2 (default) | scalar in the range [0 1]

Shape of the Laplacian, specified as a scalar in the range [0 1].

Data Types: double

lenLinear motion of camera
9 (default) | numeric scalar

Linear motion of camera, specified as a numeric scalar, measured in pixels.

Data Types: double

thetaAngle of camera motion
0 (default) | numeric scalar

Angle of camera motion, specified as a numeric scalar, measured in degrees, in a counter-clockwise direction.

Data Types: double

Output Arguments

collapse all

h — Correlation kernel
matrix

Correlation kernel, returned as a matrix.

Data Types: double

Algorithms

Averaging filters:

ones(n(1),n(2))/(n(1)*n(2))

Gaussian filters:

hg(n1,n2)=e(n21+n22)2σ2

 

h(n1,n2)=hg(n1,n2)n1n2hg

 

Laplacian filters:

2=2x2+2y2

2=4(α+1)α41α4α41α411α4α41α4α4

Laplacian of Gaussian (LoG) filters:

hg(n1,n2)=e(n21+n22)2σ2

h(n1,n2)=(n21+n222σ2)hg(n1,n2)σ4n1n2hg

Note that fspecial shifts the equation to ensure that the sum of all elements of the kernel is zero (similar to the Laplace kernel) so that the convolution result of homogeneous regions is always zero.

Motion filters:

  1. Construct an ideal line segment with the length and angle specified by the arguments len and theta, centered at the center coefficient of h.

  2. For each coefficient location (i,j), compute the nearest distance between that location and the ideal line segment.

  3. h = max(1 - nearest_distance, 0);

  4. Normalize h: h = h/(sum(h(:)))

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

 

  • fspecial supports the generation of C code (requires MATLAB® Coder™). For more information, see Code Generation for Image Processing.

  • When generating code, all inputs must be constants at compilation time.

 

Usage notes and limitations:

 

  • When generating code, all inputs must be constants at compilation time.

 

 
 
 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM