【图像去噪】基于matlab邻域+中值滤波图像去噪【含Matlab源码 961期】

240 阅读1分钟

一、简介

邻域平均法与中值滤波法都属于空间域图像平滑的范畴。
邻域平均法就是对含有噪声的原始图像f(x,y)的每一个像素点取一个邻域S,计算S中所有像素灰度级的平均值,作为处理后图像g(x,y)的像素值,即:
在这里插入图片描述
中值滤波法是一种非线性处理技术,实际上就是确定一个滑动的窗口,取该窗口像素点的中间值作为处理后图像的像素值。

二、源代码

%邻域平均法
close all;clear all;clc;
a=imread('lena.jpg');
subplot(231);imshow(a);title('原图');
 b1=imnoise(a,'salt & pepper');
  subplot(232);imshow(b1);
  title('加入椒盐噪声');
% b1=imnoise(a,'gaussian');
%  subplot(232);imshow(b1);
%title('加入高斯噪声');
[m1,n1]=size(a);
c1=b1;
for i=2:m1-1
    for j=2:n1-1
       s=b1(i-1:i+1,j-1:j+1);
   
    end
end
subplot(234);imshow(c1);title('4邻域滤波');
c2=b1;
for i=2:m1-1
    for j=2:n1-1
      
    end
end
subplot(235);imshow(c2);title('8邻域滤波');

c3=b1;
for i=3:m1-3
    for j=3:n1-3
       s=b1(i-2:i+2,j-2:j+2);
       
    end
end
subplot(236);imshow(c3);title('12邻域滤波');
%中值滤波
close all;clear all;clc
a=imread('lena.jpg');
subplot(221);imshow(a);title('原图');
b1=imnoise(a,'salt & pepper');
%b1=imnoise(a,'gaussian');title('加入高斯噪声');
subplot(222);imshow(b1);title('加入椒盐噪声');
[m1,n1]=size(a);
d1=b1;
for i=2:m1-1
    for j=2:n1-1
       s=b1(i-1:i+1,j-1:j+1);
       s1=s(:);

三、运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a