一、图像去噪及滤波简介
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 是蓝色区域的大小。
均值滤波详细的计算方法如下图所示:
其中5\times5的矩阵称为核,针对原始图像内的像素点,采用核进行处理,得到结果图像,如下图所示:
提取 1/25 可以将核转换为如下形式:
3 中值滤波
(1)概念:
(2)原理解释:
4 高斯滤波
(1)核大小为 33
(2)核大小为 55
高斯滤波让临近的像素具有更高的重要度,对周围像素计算加权平均值,较近的像素具有较大的权重值。如下图所示,中心位置权重最高为0.4。
5 小波滤波
随着小波理论的日益完善,其以自身良好的时频特性在图像去噪领域受到越来越多的关注,开辟了用非线性方法去噪的先河。具体来说,小波能够去噪主要得益于小波变换有如下特点:
(1)低熵性。小波系数的稀疏分布,使图像变换后的熵降低。 意思是对信号(即图像)进行分解后,有更多小波基系数趋于0(噪声),而信号主要部分多集中于某些小波基,采用阈值去噪可以更好的保留原始信号。
(2)多分辨率特性。由于采用了多分辨方法,所以可以非常好地刻画信号的非平稳性,如突变和断点等(例如0-1突变是傅里叶变化无法合理表示的),可以在不同分辨率下根据信号和噪声的分布来消除噪声。
(3)去相关性。小波变换可对信号去相关,且噪声在变换后有白化趋势,所以小波域比时域更利于去噪。
(4)基函数选择灵活。小波变换可灵活选择基函数,也可根据信号特点和去噪要求选择多带小波和小波包等(小波包对高频信号再次分解,可提高时频分辨率),对不同场合,选择不同小波基函数。
根据基于小波系数处理方式的不同,常见去噪方法可分为三类:
(1)基于小波变换模极大值去噪(信号与噪声模极大值在小波变换下会呈现不同变化趋势)
(2)基于相邻尺度小波系数相关性去噪(噪声在小波变换的各尺度间无明显相关性,信号则相反)
(3)基于小波变换阈值去噪
小波去噪实现步骤:
(1)二维信号的小波分解。选择一个小波和小波分解的层次N,然后计算信号s到第N层的分解。
(2)对高频系数进行阈值量化。对于从1~N的每一层,选择一个阈值,并对这一层的高频系数进行软阈值量化处理。
(3)二维小波重构。根据小波分解的第N层的低频系数和经过修改的从第一层到第N的各层高频系数,计算二维信号的小波重构。
二、部分源代码
function varargout = ImageProcess(varargin)
% IMAGEPROCESS MATLAB code for ImageProcess.fig
% IMAGEPROCESS, by itself, creates a new IMAGEPROCESS or raises the existing
% singleton*.
%
% H = IMAGEPROCESS returns the handle to a new IMAGEPROCESS or the handle to
% the existing singleton*.
%
% IMAGEPROCESS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in IMAGEPROCESS.M with the given input arguments.
%
% IMAGEPROCESS('Property','Value',...) creates a new IMAGEPROCESS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before ImageProcess_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to ImageProcess_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 ImageProcess
% Last Modified by GUIDE v2.5 28-May-2015 23:28:37
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @ImageProcess_OpeningFcn, ...
'gui_OutputFcn', @ImageProcess_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 ImageProcess is made visible.
function ImageProcess_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 ImageProcess (see VARARGIN)
% Choose default command line output for ImageProcess
global ImagesShownCnt;
ImagesShownCnt = 0;
set(handles.axes1,'visible','off')
set(handles.axes2,'visible','off')
set(handles.axes3,'visible','off')
set(handles.axes4,'visible','off')
set(handles.axes5,'visible','off')
set(handles.axes6,'visible','off')
set(handles.axesSrcImg,'visible','off')
setappdata(0,'thr',0.0);
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes ImageProcess wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = ImageProcess_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;
% --------------------------------------------------------------------
function File_Callback(hObject, eventdata, handles)
% hObject handle to File (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Open_Callback(hObject, eventdata, handles)
% hObject handle to Open (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Save_Callback(hObject, eventdata, handles)
% hObject handle to Save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function Save_As_Callback(hObject, eventdata, handles)
% hObject handle to Save_As (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global srcImg;
global dstImg;
global doubledstImg;
global doublesrcImg;
% IMGTYPE
% 0 Gray
% 1 Color
global IMGTYPE;
[filename, pathname] = uigetfile({'*.bmp;*.jpg;*.png;*.jpeg;*.tif'},'Pick an image');
if isequal(filename,0)||isequal(pathname,0)
return;
end
fpath=[pathname filename];
srcImg=imread(fpath);
axes(handles.axesSrcImg);
imshow(srcImg);
if length(size(srcImg))>2
IMGTYPE = 1;
else
IMGTYPE = 0;
end
dstImg = srcImg;
doubledstImg = im2double(dstImg);
doublesrcImg = im2double(srcImg);
%title('原始图像','fontsize',8);
% --- Executes on button press in pushbuttonSave.
function pushbuttonSave_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonSave (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function varargout = getthreshold(varargin)
% GETTHRESHOLD MATLAB code for getthreshold.fig
% GETTHRESHOLD, by itself, creates a new GETTHRESHOLD or raises the existing
% singleton*.
%
% H = GETTHRESHOLD returns the handle to a new GETTHRESHOLD or the handle to
% the existing singleton*.
%
% GETTHRESHOLD('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GETTHRESHOLD.M with the given input arguments.
%
% GETTHRESHOLD('Property','Value',...) creates a new GETTHRESHOLD or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before getthreshold_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to getthreshold_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 getthreshold
% Last Modified by GUIDE v2.5 25-May-2015 21:49:34
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @getthreshold_OpeningFcn, ...
'gui_OutputFcn', @getthreshold_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 getthreshold is made visible.
function getthreshold_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 getthreshold (see VARARGIN)
% Choose default command line output for getthreshold
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes getthreshold wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = getthreshold_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;
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit 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 pushbuttonDonoho.
function pushbuttonDonoho_Callback(hObject, eventdata, handles)
% hObject handle to pushbuttonDonoho (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global doubledstImg;
thresh = Donoho(doubledstImg);
set(handles.edit1,'string',num2str(thresh));
三、运行结果