Вы находитесь на странице: 1из 6

Linear Spatial Filters

IPT supports a number of predefined 2-D linear spatial filters, obtained by using function fspecial, which generates a filter mask,
W, using the syntax

h=fspecial(type)
h=fspecial(type,parameters)
where 'type' specifies the filter type, and parameters further define the specified filter. The spatial filters supported by fspecial are
summarized in Table 1, including applicable parameters for each filter.

h=fspecial('average',hsize)returnsanaveragingfilterhofsizehsize.Theargumenthsizecanbea
vectorspecifyingthenumberofrowsandcolumnsinh,oritcanbeascalar,inwhichcasehisasquare
matrix.Thedefaultvalueforhsizeis[33].

Ex.>>h=fspecial('average')
h=

0.11110.11110.1111
0.11110.11110.1111
0.11110.11110.1111

h=fspecial('disk',radius)returnsacircularaveragingfilter(pillbox)withinthesquarematrixofside
2*radius+1.Thedefaultradiusis5.
Ex.>>h=fspecial('disk',4)

h=

000.00100.00720.00970.00720.001000
00.00410.01790.01990.01990.01990.01790.00410
0.00100.01790.01990.01990.01990.01990.01990.01790.0010
0.00720.01990.01990.01990.01990.01990.01990.01990.0072
0.00970.01990.01990.01990.01990.01990.01990.01990.0097
0.00720.01990.01990.01990.01990.01990.01990.01990.0072
0.00100.01790.01990.01990.01990.01990.01990.01790.0010
00.00410.01790.01990.01990.01990.01790.00410
000.00100.00720.00970.00720.001000

0.02

0.015

0.01

0.005

0
10
10
8

6
4
0

2
0

h=fspecial('gaussian',hsize,sigma)returnsarotationallysymmetricGaussianlowpassfilterofsizehsize
withstandarddeviationsigma(positive).hsizecanbeavectorspecifyingthenumberofrowsand
columnsinh,oritcanbeascalar,inwhichcasehisasquarematrix.Thedefaultvalueforhsizeis[33];
thedefaultvalueforsigmais0.5.
Algorithm:

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

n12 + n22

2 2

hg (n1 , n2 )
hg
n1

n2

Ex.>>h=fspecial('gaussian',5,1.5)

h=

0.01440.02810.03510.02810.0144
0.02810.05470.06830.05470.0281
0.03510.06830.08530.06830.0351
0.02810.05470.06830.05470.0281
0.01440.02810.03510.02810.0144

0.1
0.08
0.06
0.04
0.02
0
5
4

5
4

2
1

h=fspecial('laplacian',alpha)returnsa3by3filterapproximatingtheshapeofthetwodimensional
Laplacianoperator.TheparameteralphacontrolstheshapeoftheLaplacianandmustbeintherange
0.0to1.0.Thedefaultvalueforalphais0.2.
Algorithm:

2 =

2
2
+
x 2 y 2

4 1
2
=
( + 1) 4

1
4

1
4

4

1
1
4

Ex.>>h=fspecial('laplacian',0.5)

h=

0.33330.33330.3333
0.33332.66670.3333
0.33330.33330.3333

h=fspecial('log',hsize,sigma)returnsarotationallysymmetricLaplacianofGaussianfilterofsizehsize
withstandarddeviationsigma(positive).hsizecanbeavectorspecifyingthenumberofrowsand
columnsinh,oritcanbeascalar,inwhichcasehisasquarematrix.Thedefaultvalueforhsizeis[55]
and0.5forsigma.
Algorithm:

hg (n1 , n2 ) = e

n12 + n22

(n
h (n , n ) =

2
1

2 2

+ n12 2 2 ) hg (n1 , n2 )
2 6 hg
n1

n2

Ex.>>h=fspecial('log')

h=

0.04480.04680.05640.04680.0448
0.04680.31670.71460.31670.0468
0.05640.71464.90480.71460.0564
0.04680.31670.71460.31670.0468
0.04480.04680.05640.04680.0448

h=fspecial('motion',len,theta)returnsafiltertoapproximate,onceconvolvedwithanimage,the
linearmotionofacamerabylenpixels,withanangleofthetadegreesinacounterclockwisedirection.
Thefilterbecomesavectorforhorizontalandverticalmotions.Thedefaultlenis9andthedefaulttheta
is0,whichcorrespondstoahorizontalmotionofninepixels.

Tocomputethefiltercoefficients,h,for'motion':

1. Constructanideallinesegmentwiththedesiredlengthandangle,centeredatthecenter
coefficientofh.
2. Foreachcoefficientlocation(i,j),computethenearestdistancebetweenthatlocationandthe
ideallinesegment.
3. h=max(1nearest_distance,0);
4. Normalizeh:h=h/(sum(h(:))
Ex.>>h=fspecial('motion',3,45)

h=

00.07540.1883
0.07540.32150.0754
0.18830.07540

h=fspecial('prewitt')returnsthe3by3filterh(shownbelow)thatemphasizeshorizontaledgesby
approximatingaverticalgradient.Ifyouneedtoemphasizeverticaledges,transposethefilterh'.
Ex.>>h=fspecial('prewitt')

h=

111
000
111

Tofindverticaledges,orforxderivatives,useh'.

h=fspecial('sobel')returnsa3by3filterh(shownbelow)thatemphasizeshorizontaledgesusingthe
smoothingeffectbyapproximatingaverticalgradient.Ifyouneedtoemphasizeverticaledges,
transposethefilterh'.
Ex.>>h=fspecial('sobel')

h=

121
000
121

h=fspecial('unsharp',alpha)returnsa3by3unsharpcontrastenhancementfilter.fspecialcreatesthe
unsharpfilterfromthenegativeoftheLaplacianfilterwithparameteralpha.alphacontrolstheshapeof
theLaplacianandmustbeintherange0.0to1.0.Thedefaultvalueforalphais0.2.
Algorithm:

1
1
1 + 5 1
h=

+1
1
NoteDonotbeconfusedbythenameofthisfilter:anunsharpfilterisanoperatorusedtosharpen
images.Thenamecomesfromapublishingindustryprocessinwhichanimageissharpenedby
subtractingablurred(unsharp)versionoftheimagefromitself.

Examples

I=imread('cameraman.tif');
subplot(2,2,1);
imshow(I);title('OriginalImage');

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

H=fspecial('disk',10);
blurred=imfilter(I,H,'replicate');
subplot(2,2,3);
imshow(blurred);title('BlurredImage');

H=fspecial('unsharp');
sharpened=imfilter(I,H,'replicate');
subplot(2,2,4);
imshow(sharpened);title('SharpenedImage');

fspecial

'cameraman.tif' f
imfilter

Syntax
B=imfilter(f,h)

B=imfilter(f,h,option1,option2,...)

Description
B=imfilter(A,H)filtersthemultidimensionalarrayAwiththemultidimensionalfilterH.The
arrayAcanbelogicaloranonsparsenumericarrayofanyclassanddimension.TheresultBhasthe
samesizeandclassasA.

EachelementoftheoutputBiscomputedusingdoubleprecisionfloatingpoint.IfAisanintegeror
logicalarray,thenoutputelementsthatexceedtherangeoftheintegertypearetruncated,and
fractionalvaluesarerounded.

Вам также может понравиться