<p>双循环语句,移动平均法</p><p>%均值滤波</p>
<p>clc,clear</p>
<p>f=imread('lena.bmp')</p>
<p>subplot(121),imshow(f),title('原图')</p>
<裂巧p>f1=imnoise(f,'gaussian',0.002,0.0008)</p>
<p>%subplot(222),imshow(f1),title('添加高斯噪声图')</p>
<p>k1=floor(3/2)+1</p>
<p>k2=floor(3/2)+1</p><p>X=f1</p>
<p>[M,N]=size(X)</p><p>uint8 Y=zeros(M,N)</p>
<p>funBox=zeros(3,3)</p><p>for i=1:M-3</p>
<p> for j=1:N-3</p><p> funBox=X(i:i+3,j:j+3) </p>模源洞
<p> s=sum(funBox(:))</p><p> h=s/9 </p>
<p> Y(i+k1,j+k2)=h </p><p> end</p>
<p>end</p><p>Y=Y/255</p>
<p>subplot(122),imshow(Y),title('均值滤波')</p>
<p>实现图:</p>
2、filter2。
<p>filter2</p>
<p>clear all</p>
<p>I=imread('lena.bmp')</p>
<p>%读入预处理图像</p>
<p>imshow(I)</p><p>%显示预处理图像</p>
<p>K1=filter2(fspecial('average',3),I)/255</p><p>%进行3*3均值滤旦枯波</p>
<p>K2=filter2(fspecial('average',5),I)/255</p><p>%进行5*5均值滤波</p>
<p>K3=filter2(fspecial('average',7),I)/255</p><p>%进行7*7均值滤波</p>
<p>figure,imshow(K1)</p><p>figure,imshow(K2)</p>
<p>figure,imshow(K3)</p>
以灰度图像eight.tif为例,向原始图像中昌闷加入高斯首含噪声,再对噪声图像调用均值滤波函耐芹弯数avefilt进行去噪。I=imread('eight.tif')
G=imnoise(I,'gaussian')
after=avefilt(G,3)
subplot(1,3,1)
imshow(I)
subplot(1,3,2)
imshow(G)
subplot(1,3,3)
imshow(after)
function d=avefilt(x,n)
a=ones(n)
[M,N]=size(x)
x1=double(x)
x2=x1
for i=1:M-n+1
for j=N-n+1
c=x1(i:i+n-1,j:j+n-1).*a
s=sum(sum(c))
x2(i+fix((n-1)/2),j+fix((n-1)/2))=s/(n*n)
end
end
d=uint8(x2)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)