一、图像去噪及滤波简介

1 图像去噪

1.1 图像噪声定义

噪声是干扰图像视觉效果的重要因素,图像去噪是指减少图像中噪声的过程。噪声分类有三种:加性噪声,乘性噪声和量化噪声。我们用f(x,y)表示图像,g(x,y)表示图像信号,n(x,y)表示噪声。

图像去噪是指减少数字图像中噪声的过程。现实中的数字图像在数字化和传输过程中常受到成像设备与外部环境噪声干扰等影响,称为含噪图像或噪声图像。去噪是图像处理研究中的一个重点内容。在图像的获取、传输、发送、接收、复制、输出等过程中,往往都会产生噪声,其中的椒盐噪声是比较常见的一种噪声,它属于加性噪声。

1.2 图像噪声来源

(1)图像获取过程中

图像传感器CCD和CMOS采集图像过程中受传感器材料属性、工作环境、电子元器件和电路结构等影响,会引入各种噪声。

(2)图像信号传输过程中

传输介质和记录设备等的不完善,数字图像在其传输记录过程中往往会受到多种噪声的污染。

1.3 噪声分类

噪声按照不同的分类标准可以有不同的分类形式:

基于产生原因:内部噪声,外部噪声。

基于噪声与信号的关系:

加性噪声:加性噪声和图像信号强度是不相关的,这类带有噪声的图像g可看成为理想无噪声图像f与噪声n之和:

g = f + n;

乘性嗓声:乘性噪声和图像信号是相关的,往往随图像信号的变化而变化,载送每一个象素信息的载体的变化而产生的噪声受信息本身调制。在某些情况下,如信号变化很小,噪声也不大。为了分析处理方便,常常将乘性噪声近似认为是加性噪声,而且总是假定信号和噪声是互相统计独立。

g = f + f*n

按照基于统计后的概率密度函数:

是比较重要的,主要因为引入数学模型这就有助于运用数学手段去除噪声。在不同场景下噪声的施加方式都不同,由于在外界的某种条件下,噪声下图像-原图像(没有噪声时)的概率密度函数(统计结果)服从某种分布函数,那么就把它归类为相应的噪声。下面将具体说明基于统计后的概率密度函数的噪声分类及其消除方式。

1.4 图像去噪算法的分类

(1)空间域滤波

空域滤波是在原图像上直接进行数据运算,对像素的灰度值进行处理。常见的空间域图像去噪算法有邻域平均法、中值滤波、低通滤波等。

(2)变换域滤波

图像变换域去噪方法是对图像进行某种变换,将图像从空间域转换到变换域,再对变换域中的变换系数进行处理,再进行反变换将图像从变换域转换到空间域来达到去除图像嗓声的目的。将图像从空间域转换到变换域的变换方法很多,如傅立叶变换、沃尔什-哈达玛变换、余弦变换、K-L变换以及小波变换等。而傅立叶变换和小波变换则是常见的用于图像去噪的变换方法。

(3)偏微分方程

偏微分方程是近年来兴起的一种图像处理方法,主要针对低层图像处理并取得了很好的效果。偏微分方程具有各向异性的特点,应用在图像去噪中,可以在去除噪声的同时,很好的保持边缘。偏微分方程的应用主要可以分为两类:一种是基本的迭代格式,通过随时间变化的更新,使得图像向所要得到的效果逐渐逼近,这种算法的代表为Perona和Malik的方程,以及对其改进后的后续工作。该方法在确定扩散系数时有很大的选择空间,在前向扩散的同时具有后向扩散的功能,所以,具有平滑图像和将边缘尖锐化的能力。偏微分方程在低噪声密度的图像处理中取得了较好的效果,但是在处理高噪声密度图像时去噪效果不好,而且处理时间明显高出许多。

(4)变分法

另一种利用数学进行图像去噪方法是基于变分法的思想,确定图像的能量函数,通过对能量函数的最小化工作,使得图像达到平滑状态,现在得到广泛应用的全变分TV模型就是这一类。这类方法的关键是找到合适的能量方程,保证演化的稳定性,获得理想的结果。

形态学噪声滤除器将开与闭结合可用来滤除噪声,首先对有噪声图像进行开运算,可选择结构要素矩阵比噪声尺寸大,因而开运算的结果是将背景噪声去除;再对前一步得到的图像进行闭运算,将图像上的噪声去掉。据此可知,此方法适用的图像类型是图像中的对象尺寸都比较大,且没有微小细节,对这类图像除噪效果会较好。

2 均值滤波

均值滤波是指任意一点的像素值,都是周围 N \times M 个像素值的均值。例如下图中,红色点的像素值是其周围蓝色背景区域像素值之和除25,25=5\times5 是蓝色区域的大小。

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_均值滤波

均值滤波详细的计算方法如下图所示:

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_图像去噪_02

其中5\times5的矩阵称为核,针对原始图像内的像素点,采用核进行处理,得到结果图像,如下图所示:

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_matlab图像处理_03

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_图像去噪_04

提取 1/25 可以将核转换为如下形式:

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_matlab图像处理_05

3 中值滤波

(1)概念:

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_均值滤波_06

(2)原理解释:

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_图像处理_07

4 高斯滤波

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_偏微分方程_08

(1)核大小为 33
【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_均值滤波_09
(2)核大小为 5
5

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_图像处理_10

高斯滤波让临近的像素具有更高的重要度,对周围像素计算加权平均值,较近的像素具有较大的权重值。如下图所示,中心位置权重最高为0.4。

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_偏微分方程_11

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_matlab图像处理_12

二、部分源代码

function varargout = noise_filter(varargin)
% NOISE_FILTER MATLAB code for noise_filter.fig
% NOISE_FILTER, by itself, creates a new NOISE_FILTER or raises the existing
% singleton*.
%
% H = NOISE_FILTER returns the handle to a new NOISE_FILTER or the handle to
% the existing singleton*.
%
% NOISE_FILTER('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NOISE_FILTER.M with the given input arguments.
%
% NOISE_FILTER('Property','Value',...) creates a new NOISE_FILTER or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before noise_filter_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to noise_filter_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 noise_filter

% Last Modified by GUIDE v2.5 12-Apr-2021 09:31:49

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @noise_filter_OpeningFcn, ...
'gui_OutputFcn', @noise_filter_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 noise_filter is made visible.
function noise_filter_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 noise_filter (see VARARGIN)

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

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = noise_filter_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 button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (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';'*.tif';'*.jpeg'},'选择图像');
%将im定义成全局变量,这样在代码的任何位置都可以使用im了。
global im;
%如果没有输入路径,则弹出错误对话框
if isequal(filename,0)||isequal(pathname,0)
errordlg('您还没有选取图片!!','程序员哥哥告诉你');
return;
else
%合成路径+文件名
image=[pathname,filename];
%读取图像
im=imread(image);
%打开坐标,方便操作
set(handles.axes1,'HandleVisibility','ON');
%使用图像,操作在坐标axes1里
axes(handles.axes1);
%在坐标图axes1里显示原图像
imshow(im);
title('原始图像');
end


% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (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 listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1


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

% Hint: listbox 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


% --- Executes on button press in radiobutton1.

function radiobutton1_Callback(hObject, eventdata, handles)
global gs;
global jy;
global zs;
global ps;
jy=0;
gs=1;
zs=0;
ps=0;


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton1


% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
global gs;
global jy;
global zs;
global ps;
jy=1;
gs=0;
zs=0;
ps=0;


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton2


% --- Executes on button press in radiobutton3.
function radiobutton3_Callback(hObject, eventdata, handles)
global gs;
global jy;
global zs;
global ps;
jy=0;
gs=0;
zs=1;
ps=0;


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

% Hint: get(hObject,'Value') returns toggle state of radiobutton3


% --- Executes on selection change in listbox2.
function listbox2_Callback(hObject, eventdata, handles)
% hObject handle to listbox2 (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 listbox2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox2


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

% Hint: listbox 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


% --- Executes on button press in radiobutton4.
function radiobutton4_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global zzl;
global jzl;
global kjl;
global gsl;
jzl=1;
zzl=0;
kjl=0;
gsl=0;

% Hint: get(hObject,'Value') returns toggle state of radiobutton4


% --- Executes on button press in radiobutton5.
function radiobutton5_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global zzl;
global jzl;
global kjl;
global gsl;
jzl=0;
zzl=1;
kjl=0;
gsl=0;
% Hint: get(hObject,'Value') returns toggle state of radiobutton5

三、运行结果

【图像去噪】基于matlab GUI均值+中值+空间+高斯滤波图像去噪【含Matlab源码 763期】_图像去噪_13

四、matlab版本及参考文献

1 matlab版本

2014a

2 参考文献

[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.

[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.

[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.

[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.

[5]基于matlab的传统算法图像去噪的实现原理