均值滤波是
I=medfilt2(a,[3 3],'symmetric')
可以在matlab中查询medfilt函数的用法,本例是使用3*3的滤波器采用镜像边界法做均值滤波。
方法一:filter2
clear all
I=imread('lena.bmp')
%读入预处理图像
imshow(I)
%显示预处理图像
K1=filter2(fspecial('average',3),I)/255
%进行3*3均值滤波
K2=filter2(fspecial('average',5),I)/255
%进行5*5均值滤波
K3=filter2(fspecial('average',7),I)/255
%进行7*7均值滤波
figure,imshow(K1)
figure,imshow(K2)
figure,imshow(K3)
方法二:双循环语句,移动平均法
%均值滤波
clc,clear
f=imread('lena.bmp')
subplot(121),imshow(f),title('原图')
f1=imnoise(f,'gaussian',0.002,0.0008)
%subplot(222),imshow(f1),title('添加高斯噪声图')
k1=floor(3/2)+1
k2=floor(3/2)+1
X=f1
[M,N]=size(X)
uint8 Y=zeros(M,N)
funBox=zeros(3,3)
for i=1:M-3
for j=1:N-3
funBox=X(i:i+3,j:j+3)
s=sum(funBox(:))
h=s/9
Y(i+k1,j+k2)=h
end
end
Y=Y/255
subplot(122),imshow(Y),title('均值滤波')
实现图:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)