# 灰度变换与空间滤波

g1 = imadjust(f, [0 1], [1 0]);

g = imcomplement(f)

》> g2 = imadjust(f, [0.5 0.75], [0 1]);

g3 = imadjust(f,[ ],[ ], 2);

2.2利用对数变换减小动态范围

》> g =im2uint8(mat2gray(1og(1 + double())));
》> imshow(g)

g = imcomplement(f)

》> g2 = imadjust(f, [0.5 0.75], [0 1]);

g3 = imadjust(f,[ ],[ ], 2);

2.3函数intrans的说明

9 =intrans(f,’ stretch’, mean2(tofloat(f)), 0.9);
figure, imshow(g)

2.4计算并绘制图像直方图****

imhist(f);

S

bar(horz, z; width)
T，4是1的云的行向量;horz是个与z同维数的向量它包含了水平刻度的增量; width

2.5直方图均衡。

》> imshow(f); % Fig. 2.8(a).
》> figure, imhist(f) % Fig. 2.8(b).

ylim(‘auto’)
》> figure, imhist(g) % Fig. 2.8(d).
》> ylim( ’ auto’ )

2.6直方图匹配。

f1 = histeq(f, 256);

function p = twomodegauss(m1, s1g1, m2, s1g2, A1, A2, k)
twomodegauss
&TWOMODEGAUSS Generates a two- mode Gaussian function.
%P-TWOMODEGAUSS(M1, SIG1, M2, SIG2, A1, A2, K) generates a
5中。, !四丫8)-likefunction inthe interval [o0,11.Pisa
r normalized so that SUM§ = 1. The mean and
standard deviation of the modes are (M1， SIG1) and (M2, SIG2) ,
respectively. A1 and A2 are the amplitude values of the two

》g2 = adapthisteq(f, ’ NumTiles’, [25 25]);

function p = twomodegauss(m1, s1g1, m2, s1g2, A1, A2, k)
twomodegauss
&TWOMODEGAUSS Generates a two- mode Gaussian function.
%P-TWOMODEGAUSS(M1, SIG1, M2, SIG2, A1, A2, K) generates a
5中。, !四丫8)-likefunction inthe interval [o0,11.Pisa
r normalized so that SUM§ = 1. The mean and
standard deviation of the modes are (M1， SIG1) and (M2, SIG2) ,
respectively. A1 and A2 are the amplitude values of the two

2.8函数imfilter的应用

W = ones(31);

gd = imfilter(f, w);
imshow(gd,[ ])

’ replicate ‘选项可解决这一问题:
》> gr - imfilter(f, w," replicate’);
》> figure, imshow(gr, [ ])

gs = imfilter(f, w，’ symmetric’);
figure, imshow(gs, [ ])

》> gc = imfilter(f, w,‘circular’);
》> figure, imshow(gc, [ 1)

》f8 = im2uint8(f);

2.9使用函数 colfilt实现非线性空间滤波

gmean = @(A) prod(A, 1)^1/size(A, 1));

g= colfilt(f, [m n],‘sliding’, @gmean) ;

colfilt传递的其他函数句柄)必须写成可处理可变列数的形式。

[M，N]= size(f);
g= g((1:M)+ m, (1:N) + n);

2.10使用函数 imfilter实现拉普拉斯滤波器。

f(x,y)的拉普拉斯算子定义为V2f(x,y):

》> w=fspecial(‘laplacian’, 0)
w=
0. 0000

1. 0000
2. 0000 -4 .0000
3. 0000
主意，该滤波器是double类的，其alpha - 0的形状是前面讨论过的拉普拉斯滤波器。我们可以很容
数字图像处理(MATLAB版) (第二版) (本科教学版)
》> w=[010;1-4，1;0 10];
下面对输人图像f [见图2.17(a) ]应用w，图像f是uint8类图像:

g1 = imfilter(f, w,’ replicate’);
》> imshow(g1, [ ])

》f2 = tofloat(f);
g2 = imfilter(f2, w, ‘replicate’);
imshow(g2, [ ])

g=f2-92;
imshow(g);

2.11手工指定滤波 器及增强技术的比较

W4 =fspecial(’ laplacian’, 0); %Same as w in Example 2.10.
》> w8=[111;1-81;111];
f= tofloat(f);
g4 =f - imfilter(f, W4, ‘replicate’
g8=f-imfilter(f, w8, ’ replicate’);
imshow(f)
figure, imshow(g4)
figure, imshow(g8)

》> w=[010;1-4，1;0 10];

g1 = imfilter(f, w,’ replicate’);
》> imshow(g1, [ ])

》f2 = tofloat(f);
g2 = imfilter(f2, w, ‘replicate’);
imshow(g2, [ ])

g=f2-92;
imshow(g);

2.12利用函数 medfilt2 进行中值滤波。

fn = imnoise(f, ‘salt & pepper’, 0.2);

Innoise
》> gm = medfilt2(fn);

‘symmetric’选项可降低这种效应:
》> gms = medfilt2(fn, ’ symmetric’);

figure, imshow(g8)

》> w=[010;1-4，1;0 10];

g1 = imfilter(f, w,’ replicate’);
》> imshow(g1, [ ])

》f2 = tofloat(f);
g2 = imfilter(f2, w, ‘replicate’);
imshow(g2, [ ])

g=f2-92;
imshow(g);

THE END