一、简介
1 中值滤波
中值滤波是基于排序统计理论的一种能有效抑制噪声的非线性信号处理技术。它也是一种邻域运算,类似于卷积,但是计算的不是加权求和,而是把数字图像或数字序列中一点的值用该点的一个邻域中各点值的中值代替,让周围像素灰度值的差比较大的像素改取与周围的像素值接近的值,从而可以消除孤立的噪声点。它能减弱或消除傅立叶空间的高频分量,但影响低频分量。因为高频分量对应图像中的区域边缘的灰度值具有较大较快变化的部分,该滤波可将这些分量滤除,使图像平滑。值滤波技术在衰减噪声的同时能较好的保护图像的边缘。
2 均值滤波
均值滤波是指任意一点的像素值,都是周围 个像素值的均值。例如下图中,红色点的像素值是其周围蓝色背景区域像素值之和除25,25=55 是蓝色区域的大小。
均值滤波详细的计算方法如下图所示:
其中55的矩阵称为核,针对原始图像内的像素点,采用核进行处理,得到结果图像,如下图所示:
提取 1/25 可以将核转换为如下形式:
3 小波变换
小波滤波原理
小波变换就是把某一基函数做位移后,求不同尺度下的小波函数与原始信号的内积,表达式如下:
其中,a>0,是尺度因子,隐含信号的频率信息,是平移因子,这样信号就被分解成一系列小波函数的叠加,这就是小波分解的过程。基本原理是:对于信号的不同频率部分,改变尺度值,相当于在时间轴上对信号进行压缩和伸展。尺度越大,表示分析的信号区间越长,那么在频域的分辨率就越低,这样可以获取信号的低频成分;反之可以得到信号的高频成分。小波变换可以得到一系列的小波系数,从中分析有用信号和噪声各自对应的部分,对小波系数进行适当的处理。小波重构就是用处理得到的新的系数来重构信号。
小波进行滤波就是一个小波分解和重构的过程,其基本步骤如下:
u 选择合适的小波基函数
u 对信号进行指定层次的小波分解
u 对各分解层进行处理,得到新的小波系数
u 用新得到的系数进行小波重构(小波逆变换)
小波基函数的选择
对于同样的信号,不同的基函数会得到不同的结果。在小波基函数选择的时候要结合信号本身的特点,也要清楚小波基函数选取的原则:
u 正交性:可以使分析简便,有利于信号的精确重构
u 对称性:对称的基函数使得小波滤波呈线性相位,信号不会失真,也可以提高算法的运行速度
u 紧支性:紧支集的长度决定着信号局部特性的好坏,紧支集越短的小波基函数,局部时频特征就越好,越有利于信号的瞬时检测
u 正则性:决定信号重构后的平滑性,会影响频域的分辨率,支集长度越长,正则性越好;
u 消失矩:基函数的消失矩越高,在高频的衰减也就越快,变换后信号的的能量越集中,可以保持良好的频域定域性。
二、源代码
-
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');
三、运行结果
四、备注
完整代码添加QQ1575304183
转载:https://blog.csdn.net/weixin_50197058/article/details/116431251