matlab实现平滑滤波

clc;clear;close all;
 
im=imread('p1.jpg');
im = rgb2gray(im);
im=double(im);
im=im/max(im(:));
figure('Name','原图','NumberTitle','off');imshow(im,[0,1]);

P1 = imnoise(im,'gaussian',0,0.005);  % adding gaussian noise 这里im是原始图像,添加了均值是0,方差是0.005的高斯噪声。
figure('Name','高斯噪声','NumberTitle','off');imshow(P1,[0,1]); 
P2 = imnoise(im,'salt & pepper',0.02);  % adding impulse noise  添加盐和胡椒噪声,默认噪声密度为0.05。这会影响大约5%的像素。
figure('Name','椒盐噪声','NumberTitle','off');imshow(P2,[0,1]);

a=[1 1 1;1 1 1;1 1 1];  %定义一个3X3的全为1的模板
template1=(1/9)*a;

%对高斯噪声进行滤波操作
imAve=conv2(double(P1),double(template1));% 返回矩阵 a 和 template1 的二维卷积。
figure('Name','高斯噪声:均值滤波','NumberTitle','off');imshow(imAve,[0,1]);
 
imMed=medfilt2(P1,[3,3],'symmetric'); %执行中值滤波,其中每个输出像素包含输入图像中相应像素周围的m按n邻域的中值。
figure('Name','高斯噪声:中值滤波','NumberTitle','off');imshow(imMed,[0,1]);
 
psf=fspecial('gaussian',3,1);    %返回大小为 hsize 的旋转对称高斯低通滤波器,标准差为 sigma。
imGau=imfilter(P1,psf,'conv','symmetric'); %根据一个或多个返回大小为 hsize 的旋转对称高斯低通滤波器,标准差为 sigma。
figure('Name','高斯噪声:高斯滤波','NumberTitle','off');imshow(imGau,[0,1]);

%对椒盐噪声进行滤波操作
imAve=conv2(double(P2),double(template1));% 返回矩阵 a 和 template1 的二维卷积。
figure('Name','椒盐噪声:均值滤波','NumberTitle','off');imshow(imAve,[0,1]); title('average filter');
 
imMed=medfilt2(P2,[3,3],'symmetric'); %执行中值滤波,其中每个输出像素包含输入图像中相应像素周围的m按n邻域的中值。
figure('Name','椒盐噪声:中值滤波','NumberTitle','off');imshow(imMed,[0,1]);
 
psf=fspecial('gaussian',3,1);    %返回大小为 hsize 的旋转对称高斯低通滤波器,标准差为 sigma。
imGau=imfilter(P2,psf,'conv','symmetric'); %根据一个或多个返回大小为 hsize 的旋转对称高斯低通滤波器,标准差为 sigma。
figure('Name','椒盐噪声:高斯滤波','NumberTitle','off');imshow(imGau,[0,1]);


原图

image-20201204224515986

椒盐噪声

image-20201204224606433

高斯噪声

image-20201204224553121

椒盐噪声:高斯滤波

image-20201204224628459

椒盐噪声:中值滤波

image-20201204224642931

椒盐噪声:均值滤波

image-20201204224659752

高斯噪声:高斯滤波

image-20201204224714899

高斯噪声:中值滤波

image-20201204224732514

高斯噪声:均值滤波

image-20201204224747977

posted @ 2020-12-04 22:52  碎了的冰  阅读(2947)  评论(0编辑  收藏  举报