【图像去噪】基于中值+均值+维纳+小波滤波图像去噪matlab源码含 GUI

一、简介

1.均值滤波

          图片中一个方块区域(一般为3*3)内,中心点的像素为全部点像素值的平均值。均值滤波就是对于整张图片进行以上操作。

我们可以看下图的矩阵进行理解

                                                                      

                         

缺陷:均值滤波本身存在着固有的缺陷,即它不能很好地保护图像细节,在图像去噪的同时也破坏了图像的细节部分,从而使图像变得模糊,不能很好地去除噪声点。特别是椒盐噪声

2.中值滤波

       首先,我们复习中值。在一连串数字{1,4,6,8,9}中,数字6就是这串数字的中值。由此我们可以应用到图像处理中。依然我们在图像中去3*3的矩阵,里面有9个像素点,我们将9个像素进行排序,最后将这个矩阵的中心点赋值为这九个像素的中值。

                                     

3、小波去噪的原理

 

Donoho提出的小波阀值去噪的基本思想是将信号通过小波变换(采用Mallat算法)后,信号产生的小波系数含有信号的重要信息,将信号经小波分解后小波系数较大,噪声的小波系数较小,并且噪声的小波系数要小于信号的小波系数,通过选取一个合适的阀值,大于阀值的小波系数被认为是有信号产生的,应予以保留,小于阀值的则认为是噪声产生的,置为零从而达到去噪的目的。

     从信号学的角度看 ,小波去噪是一个信号滤波的问题。尽管在很大程度上小波去噪可以看成是低通滤波 ,但由于在去噪后 ,还能成功地保留信号特征 ,所以在这一点上又优于传统的低通滤波器。由此可见 ,小波去噪实际上是特征提取和低通滤波的综合 ,其流程图如下所示:

     

      一个含噪的模型可以表示如下:

     

     其中 ,f( k)为有用信号,s(k)为含噪声信号,e(k)为噪声,ε为噪声系数的标准偏差。

     假设,e(k)为高斯白噪声,通常情况下有用信号表现为低频部分或是一些比较平稳的信号,而噪声信号则表现为高频的信号,我们对 s(k)信号进行小波分解的时候,则噪声部分通常包含在HL、LH、HH中,如下图所示,只要对HL、LH、HH作相应的小波系数处理,然后对信号进行重构即可以达到消噪的目的。

      

     我们可以看到,小波去噪的原理是比较简单类,类似以往我们常见的低通滤波器的方法,但是由于小波去找保留了特征提取的部分,所以性能上是优于传统的去噪方法的。

4.维纳滤波

一.实验理论基础``
维纳滤波(wiener filtering) 一种基于最小均方误差准则、对平稳过程的最优估计器。这种滤波器的输出与期望输出之间的均方误差为最小,因此,它是一个最佳滤波系统。可用于提取被平稳噪声所污染的信号。
  从连续的(或离散的)输入数据中滤除噪声和干扰以提取有用信息的过程称为滤波,这是信号处理中经常采用的主要方法之一,具有十分重要的应用价值,而相应的装置称为滤波器。根据滤波器的输出是否为输入的线性函数,可将它分为线性滤波器和非线性滤波器两种。维纳滤波器是一种线性滤波器。
维纳滤波的基本原理是:设观察信号y(t)含有彼此统计独立的期望信号x(t)和白噪声ω(t)可用维纳滤波从观察信号y(t)中恢复期望信号x(t)。设线性滤波器的冲击响应为h(t),此时其输入y(t)为y(t)=x(t)+w(t),输出为

从而,可以得到输出对x(t)期望信号的误差为

其均方误差为:

E[ ]表示数学期望。应用数学方法求最小均方误差时的线性滤波器的冲击响应hopt(t)可得方程:

式中,Ryx(t)为y(t)与x(t)的互相关函数,Ryy(τ-σ)为y(t)的自相关函数。上述方程称为维纳-霍夫(Wiener-Hopf)方程。求解维纳-霍夫方程可以得到最佳滤波器的冲击响应hopt(t)。

二、源代码

function varargout = dsp1(varargin)
% DSP1 MATLAB code for dsp1.fig
%      DSP1, by itself, creates a new DSP1 or raises the existing
%      singleton*.
%
%      H = DSP1 returns the handle to a new DSP1 or the handle to
%      the existing singleton*.
%
%      DSP1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DSP1.M with the given input arguments.
%
%      DSP1('Property','Value',...) creates a new DSP1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before dsp1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to dsp1_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help dsp1

% Last Modified by GUIDE v2.5 21-Mar-2014 21:34:03

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @dsp1_OpeningFcn, ...
                   'gui_OutputFcn',  @dsp1_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before dsp1 is made visible.
function dsp1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to dsp1 (see VARARGIN)
setappdata(handles.figure1,'img1',0);
setappdata(handles.figure1,'img2',0);
setappdata(handles.figure1,'img3',0);
%set(handles.myimage_compress,'Enable', 'off');
set(handles.pushbutton_compress,'Enable', 'off');
set(handles.menu_quzaodesc, 'Enable', 'off');
set(handles.save, 'Enable', 'off');


% Choose default command line output for dsp1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes dsp1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = dsp1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on selection change in popupmenu.
function popupmenu_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu
val = get(hObject,'Value');
str = get(hObject, 'String');
switch str{val};
case '无' % 
	img2=getappdata(handles.figure1,'img1');
    axes(handles.axes2);
    imshow(img2);
    setappdata(handles.figure1,'img2',img2);
    set(handles.pushbutton_listen, 'Enable', 'off');
case '高斯噪声' % 
	img2=getappdata(handles.figure1,'img1');
    img2 = imnoise(img2,'gaussian'); %加高斯噪声
    axes(handles.axes2);
    imshow(img2);
    setappdata(handles.figure1,'img2',img2);
    set(handles.menu_quzaodesc, 'Enable', 'on');
    set(handles.pushbutton_listen, 'Enable', 'on');
case '椒盐噪声' % 
	img2=getappdata(handles.figure1,'img1');
    img2 = imnoise(img2,'salt');
    axes(handles.axes2);
    imshow(img2);
    setappdata(handles.figure1,'img2',img2);
    set(handles.menu_quzaodesc, 'Enable', 'on');
    set(handles.pushbutton_listen, 'Enable', 'on');
case '泊松噪声' % 
	img2=getappdata(handles.figure1,'img1');
    img2 = imnoise(img2,'poisson');
    axes(handles.axes2);
    imshow(img2);
    setappdata(handles.figure1,'img2',img2);
    set(handles.menu_quzaodesc, 'Enable', 'on');
    set(handles.pushbutton_listen, 'Enable', 'on');
case '斑点噪声speckle' % 
	img2=getappdata(handles.figure1,'img1');
    img2 = imnoise(img2,'speckle');
    axes(handles.axes2);
    imshow(img2);
    setappdata(handles.figure1,'img2',img2);
    set(handles.menu_quzaodesc, 'Enable', 'on');
    set(handles.pushbutton_listen, 'Enable', 'on');
end

% --- Executes during object creation, after setting all properties.
function popupmenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --------------------------------------------------------------------
function myfile_Callback(hObject, eventdata, handles)
% hObject    handle to myfile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --------------------------------------------------------------------
function myfile_open_Callback(hObject, eventdata, handles)
% hObject    handle to myfile_open (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename, pathname] = uigetfile(...
    {'*.bmp;*.jpg; *.png; *.jpeg; ','Image Files(*.bmp,*.jpg,*.png,*.jpeg)';...
    '*.*',      'All Files(*.*)' }...
    , 'Pick an image');%打开文件 选择图像
if isequal(filename,0) || isequal(pathname,0)
    return;%如 果点了“ 取 消”
end
axes(handles.axes1);%重要,在哪个坐标轴显示图像
fpath = [pathname filename];%路径名和文件名
img1 = imread(fpath);%读取图像 
imshow(img1);%显示图像
setappdata(handles.figure1,'img1',img1);


% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu3
img2=getappdata(handles.figure1,'img2');%加噪图像
img1=getappdata(handles.figure1,'img1');%原始图像
val = get(hObject,'Value');
str = get(hObject, 'String');
switch str{val};
case '中值滤波'
    set(handles.uipanel_lowfilt, 'Visible', 'off');
    set(handles.uipanel_gausslow, 'Visible', 'off');
    set(handles.pushbutton_quzao, 'Visible', 'off');
	img3 = medfilt2(img2);%中值滤波
    setappdata(handles.figure1,'img3',img3);
    axes(handles.axes3);
    imshow(img3);
    PSNR = psnr2(img3, img1);
    str1 = strcat('psnr为: ' , num2str(PSNR) , 'db');
    set(handles.text3, 'string', str1); %显示PSNR值
    set(handles.save, 'Enable', 'on');
   % set(handles.myimage_compress,'Enable', 'on');
    set(handles.pushbutton_compress,'Enable', 'on');
case '维纳滤波' 
    set(handles.uipanel_lowfilt, 'Visible', 'off');
    set(handles.uipanel_gausslow, 'Visible', 'off');
    set(handles.pushbutton_quzao, 'Visible', 'off');
	img3 = wiener2(img2,[5,5]);
    setappdata(handles.figure1,'img3',img3);
    axes(handles.axes3);
    imshow(img3); 
    PSNR = psnr2(img3, img1);
    str1 = strcat('psnr为: ' , num2str(PSNR) , 'db');
    set(handles.text3, 'string', str1);
   % set(handles.myimage_compress,'Enable', 'on');
    set(handles.pushbutton_compress,'Enable', 'on');
    set(handles.save, 'Enable', 'on');
case '小波'
    set(handles.uipanel_lowfilt, 'Visible', 'off');
    set(handles.uipanel_gausslow, 'Visible', 'off');
    set(handles.pushbutton_quzao, 'Visible', 'off');
    [c,s]=wavedec2(img2,2,'sym4');%用小波函数sym4对img3进行两层小波分解
    %a1=wrcoef2('a',c,s,'sym4',1);%提取小波分解中第一层的低频图像,即实现了低通滤波消噪
    %subplot(2,2,3);imshow(uint8(a1));%画出第一次消噪后的图像
    img3=wrcoef2('a',c,s,'sym4',2);%提取小波分解中第二层的低频图像,即实现了低通滤波消噪
    setappdata(handles.figure1,'img3',uint8(img3));
    axes(handles.axes3);
    imshow(uint8(img3));%画出第二次消噪后的图像
    PSNR = psnr2(img3, img1);
    str1 = strcat('psnr为: ' , num2str(PSNR) , 'db');
    set(handles.text3, 'string', str1);
   % set(handles.myimage_compress,'Enable', 'on');
    set(handles.pushbutton_compress,'Enable', 'on');
    set(handles.save, 'Enable', 'on');
case '理想低通滤波' 
    set(handles.uipanel_gausslow, 'Visible', 'off');
    set(handles.uipanel_lowfilt, 'Visible', 'on');
    set(handles.pushbutton_quzao, 'Visible', 'on')  
case '高斯低通滤波' 
    set(handles.uipanel_lowfilt, 'Visible', 'off');
    set(handles.uipanel_gausslow, 'Visible', 'on' );
    set(handles.pushbutton_quzao, 'Visible', 'on');
end   

guidata(hObject,handles)

% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end

三、运行结果

在这里插入图片描述

四、备注

完整代码或者代写添加QQ1575304183

往期回顾>>>>>>

【图像去噪】基于自适应形态学的图像去噪matlab源码

【图像压缩】基于行程编码实现的图像压缩matlab源码

【图像去噪】基于全变分算法图像去噪matlab源码

【图像去噪】基于非局部均值(NLM)滤波图像去噪matlab源码

【图像去噪】基于小波变换(软阈值、硬阈值、半软阈值、改进阈值)的图像去噪matlab源码

【图像滤波】图像二维双边高斯滤波matlab源码​​​​​​​

【图像去雾】基于暗通道图像去雾matlab源码​​​​​​​

你可能感兴趣的:(matlab,图像处理)