去除图片高斯噪声和椒盐噪声(图像处理)

最小值最大值中值均值滤波去除高斯噪声和椒盐噪声

#代码如下:

clear;clc;
f=imread(‘rice.png’);
f1=imnoise(f,‘gaussian’);
f2=imnoise(f,‘salt & pepper’,0.02);
[m,n]=size(f1);
g1=f1;
g2=f2;
mask=[1 1 1;1 1 1;1 1 1]/9;
for x=2:m-1
for y=2:n-1
subimage=double(f1(x-1:x+1,y-1:y+1));
temp=subimage.*mask;
g1(x,y)=round(sum(sum(temp)));
subimage2=double(f2(x-1:x+1,y-1:y+1));
temp2=subimage2.*mask;
g2(x,y)=round(sum(sum(temp2)));
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1);
subplot(222),imshow(f2);
subplot(223),imshow(g1);
subplot(224),imshow(g2);

clear;clc;
I=imread(‘rice.png’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n]=size(f1);
g1=f1;
g2=f2;
mask=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1]/25;
for x=3:m-2
for y=3:n-2
subimage=double(f1(x-2:x+2,y-2:y+2));
temp=subimage.*mask;
g1(x,y)=round(sum(sum(temp)));
subimage2=double(f2(x-2:x+2,y-2:y+2));
temp2=subimage2.*mask;
g2(x,y)=round(sum(sum(temp2)));
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1);
subplot(222),imshow(f2);
subplot(223),imshow(g1);
subplot(224),imshow(g2);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1 ]/25;
for x=3:m-2
for y=3:n-2
subimage = double(f1(x-2:x+2,y-2:y+2));
a = reshape(subimage,1,25);
a = sort(a);
g1(x,y) = a(1);
subimage2 = double(f2(x-2:x+2,y-2:y+2));
a = reshape(subimage2,1,25);
a = sort(a);
g2(x,y) = a(1);
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘55最小值去除高斯噪声’);
subplot(224),imshow(g2),title('5
5最小值去除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1 ]/25;
for x=3:m-2
for y=3:n-2
subimage = double(f1(x-2:x+2,y-2:y+2));
a = reshape(subimage,1,25);
a = sort(a);
g1(x,y) = a(25);
subimage2 = double(f2(x-2:x+2,y-2:y+2));
a = reshape(subimage2,1,25);
a = sort(a);
g2(x,y) = a(25);
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘55最大值去除高斯噪声’);
subplot(224),imshow(g2),title('5
5最大值去除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1 ]/25;
for x=3:m-2
for y=3:n-2
subimage = double(f1(x-2:x+2,y-2:y+2));
a = reshape(subimage,1,25);
a = sort(a);
g1(x,y) = (a(1)+a(25))/2;
subimage2 = double(f2(x-2:x+2,y-2:y+2));
a = reshape(subimage2,1,25);
a = sort(a);
g2(x,y) = (a(1)+a(25))/2;
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘55中点去除高斯噪声’);
subplot(224),imshow(g2),title('5
5中点去除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1;1 1 1;1 1 1]/9;
for x=2:m-1
for y=2:n-1
subimage = double(f1(x-1:x+1,y-1:y+1));
a = reshape(subimage,1,9);
a = sort(a);
g1(x,y) = a(1);
subimage2 = double(f2(x-1:x+1,y-1:y+1));
a = reshape(subimage2,1,9);
a = sort(a);
g2(x,y) = a(1);
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘33模板最小值除高斯噪声’);
subplot(224),imshow(g2),title('3
3模板最小值去除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1;1 1 1;1 1 1]/9;
for x=2:m-1
for y=2:n-1
subimage = double(f1(x-1:x+1,y-1:y+1));
a = reshape(subimage,1,9);
a = sort(a);
g1(x,y) = a(5);
subimage2 = double(f2(x-1:x+1,y-1:y+1));
a = reshape(subimage2,1,9);
a = sort(a);
g2(x,y) = a(5);
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘33模板中值除高斯噪声’);
subplot(224),imshow(g2),title('3
3模板中值去除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1;1 1 1;1 1 1]/9;
for x=2:m-1
for y=2:n-1
subimage = double(f1(x-1:x+1,y-1:y+1));
a = reshape(subimage,1,9);
a = sort(a);
g1(x,y) = (a(1)+a(9))/2;
subimage2 = double(f2(x-1:x+1,y-1:y+1));
a = reshape(subimage2,1,9);
a = sort(a);
g2(x,y) = (a(1)+a(9))/2;
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘33中点去除高斯噪声’);
subplot(224),imshow(g2),title('3
3中点去除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1;1 1 1;1 1 1]/9;
for x=2:m-1
for y=2:n-1
subimage = double(f1(x-1:x+1,y-1:y+1));
a = reshape(subimage,1,9);
a = sort(a);
g1(x,y) = a(9);
subimage2 = double(f2(x-1:x+1,y-1:y+1));
a = reshape(subimage2,1,9);
a = sort(a);
g2(x,y) = a(9);
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘33模板去除高斯噪声’);
subplot(224),imshow(g2),title('3
3模板除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1;1 1 1;1 1 1]/9;
for x=2:m-1
for y=2:n-1
subimage = double(f1(x-1:x+1,y-1:y+1));
a = reshape(subimage,1,9);
a = sort(a);
g1(x,y) = a(9);
subimage2 = double(f2(x-1:x+1,y-1:y+1));
a = reshape(subimage2,1,9);
a = sort(a);
g2(x,y) = a(9);
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title(‘33模板去除高斯噪声’);
subplot(224),imshow(g2),title('3
3模板除椒盐噪声’);

I = imread(‘pout.tif’);
f1=imnoise(I,‘gaussian’);
f2=imnoise(I,‘salt & pepper’,0.02);
[m,n] = size(I);
g1=f1;
g2=f2;
mask=[1 1 1 1 1;1 1 1 1 1;1 1 1 1 1;1 1 1 1 1; 1 1 1 1 1]/25;
for x=3:m-2
for y=3:n-2
subimage = double(f1(x-2:x+2,y-2:y+2));
temp = subimage.mask;
g1(x,y) = round(sum(sum(temp)));
subimage2 = double(f2(x-2:x+2,y-2:y+2));
temp2 = subimage2.mask;
g2(x,y) = round(sum(sum(temp2)));
end
end
g1=uint8(g1);
g2=uint8(g2);
subplot(221),imshow(f1),title(‘高斯噪声’);
subplot(222),imshow(f2),title(‘椒盐噪声’);
subplot(223),imshow(g1),title('5
5均值滤波去除高斯噪声’);
subplot(224),imshow(g2),title('5
5均值滤波效果去除椒盐噪声’);

你可能感兴趣的:(去除图片高斯噪声和椒盐噪声(图像处理))