一、 实验目的与要求
实验目的:
1、进一步了解MatLab软件/语言,学会使用MatLab对图像作滤波处理,使学生有机会掌握滤波算法,体会滤波效果。
2、了解几种不同滤波方式的使用和使用的场合,培养处理实际图像的能力,并为课堂教学提供配套的实践机会。
3.利用MATLAB程序进行简单滤波器的实现
实验要求:
1.叙述实验过程;
2.按步骤保留实验结果图像。
3. 针对实验内容自编程部分补充完整代码备注和运行结果。
二、实验内容及步骤
1、空间域平滑滤波器
1.1利用matlab库函数实现实验内容:
a) 调入并显示原始图像electric.tif。
uiopen(‘C:\Users\Administrator\Desktop\electric.tif.png’,1)
I=imread(‘C:\Users\Administrator\Desktop\electric.tif.png’);
b) 利用imnoise 命令在图像electric.tif 上加入高斯(gaussian) 噪声
J = imnoise(I,'gauss',0.02);%利用imnoise 命令添加高斯噪声
c)利用预定义函数fspecial 命令产生均值(average)滤波器
ave1=fspecial(‘average’,3); %3×3模板
ave2=fspecial(‘average’,5); %5×5模板
d)分别采用3x3和5x5的模板,分别用均值滤波器以及中值滤波器,对加入噪声的图像进行处理并观察不同噪声水平下,上述滤波器处理的结果;
K = filter2(ave1,J)/255; %均值滤波器
A = filter2(ave2,J)/255; %5×5均值滤波器
M = medfilt2(J,[3 3]); %3×3中值滤波
B = medfilt2(J,[5 5]); %5×5中值滤波
e)选择不同大小的模板,对加入某一固定噪声水平噪声的图像进行处理,观察上述滤波器处理的结果。
figure(1), subplot(2,2,1),imshow(I),title(‘原图像’); %以子图的形式呈现方便对比分析
figure(1), subplot(2,2,2),imshow(J),title(‘添加高斯噪声’);
figure(1), subplot(2,2,3),imshow(K),title(‘3X3均值滤波器’);
figure(1), subplot(2,2,4),imshow(M),title(‘3X3中值滤波器’);
figure(1), subplot(2,2,3),imshow(A),title(‘5×5均值滤波器’);
figure(1), subplot(2,2,4),imshow(B),title(‘5×5中值滤波器’);
f)利用imnoise 命令在图像electric.tif 上加入椒盐噪声(salt & pepper)
R = imnoise(I,‘salt & pepper’,0.02);%利用imnoise 命令添加椒盐噪声
figure(1), subplot(2,2,2),imshow®,title(‘添加椒盐噪声’)
g)另外的方法
uiopen(‘C:\Users\Administrator\Desktop\electric.tif.png’,1)
I=imread(‘C:\Users\Administrator\Desktop\electric.tif.png’);J = imnoise(I,‘gauss’,0.02);
n= 3;%模板阶数
m = (n-1)/2; [p,q] = size(I);
PI = zeros(p+2m,q+2m);%待处理矩阵
for i = 1:p
for j = 1:q
PI(i+m,j+m) = I(i,j);
end
endfor i = 1:p
for ii = 1:m PI(i+m,ii) = I(i,1);
PI(i+m,q+m+ii) = I(i,q);
end
endfor j = 1:q
for jj = 1:m PI(jj,j+m) = I(1,j);
PI(p+m+jj,j+m) = I(p,j); end
endfor ii = 1:m
for jj = 1:m
PI(ii,jj) = I(1,1);
PI(p+m+ii,jj) = I(p,1);
PI(ii,q+m+jj) = I(1,q); PI(p+m+ii,q+m+jj) = I(p,q);
end
endderta = zeros(n,n);%n 阶模板矩阵
PImid = PI;
PImean = PI;
for i = m+1:p+m
for j = m+1:q+m for k = 1:m
for h = 1:m
derta(k,h) = PI(i+k-m-1,j+h-m-1);
derta(k,m+1) = PI(i+k-m-1,j);
derta(k,n-h+1) = PI(i+k-m-1,j+m+1-h);
derta(m+1,h) = PI(i,j+h-m-1);
derta(m+1,m+1) = PI(i,j); derta(m+1,n-h+1) = PI(i,j+m+1-h);
derta(n-k+1,h) = PI(i+m+1-k,j+h-m-1); derta(n-k+1,m+1) = PI(i+m+1-k,j);
derta(n-k+1,n-h+1) = PI(i+m+1-k,j+m+1-h); PImid(i,j) = median(median(derta));%中值滤波PImean(i,j) = round(mean(mean(derta)));%均值滤波
end
end
end
endImid = zeros(p,q);
Imean = zeros(p,q);
for i = 1:p
for j = 1:q
Imid(i,j) = PImid(i+m,j+m);
Imean(i,j) = PImean(i+m,j+m);
end
endfigure()
subplot(2,2,1);subplot(2,2,2);
imshow(I);title(‘加入椒盐噪声的图’); subplot(2,2,3);
imshow(Imid,[0,255]);title(‘中值滤波处理后的图像’);
subplot(2,2,4);
imshow(Imean,[0,255]);title(‘均值滤波处理后的图像’);
2、空间域锐化滤波
采用Roberts算子,Laplace算子分别进行图像锐化。
1、 Roberts算子锐化滤波
uiopen(‘C:\Users\Administrator\Desktop\room.tif.png’,1)
pic = imread(‘C:\Users\Administrator\Desktop\room.tif.png’);
edge = uint8(zeros(size(pic)));
h = size(pic, 1);
w = size(pic, 2);
for i = 1 : h - 1
for j = 1 : w - 1
edge(i, j) = uint8(abs(pic(i, j) - pic(i + 1, j + 1)) + abs(pic(i, j + 1) - pic(i + 1, j)));
end
end
imshow(pic); title(‘原始图像’);
A = uint8(edge);
figure; imshow(A); title(‘Roberts锐化并叠加原图的结果图像’);
2.Laplace算子锐化滤波
uiopen(‘C:\Users\Administrator\Desktop\room.tif.png’,1)
pic = imread(‘C:\Users\Administrator\Desktop\room.tif.png’);
edge = uint8(zeros(size(pic)));
h = size(pic, 1);
w = size(pic, 2);
l = [0, 1, 0; 1, -4, 1; 0, 1, 0];
for i = 2 : h - 1
for j = 2 : w - 1
sub = double(pic(i - 1 : i + 1, j - 1 : j + 1));
d = sum(sum(sub .* l));
edge(i, j) = uint8(d);
end
endimshow(pic); title(‘原始图像’);
A = uint8(edge);figure; imshow(A); title(‘Laplace锐化并叠加原图的结果图像’);