接下来学习的是关于图像滤波方面的知识。
图像滤波是图像预处理操作中非常重要的一步,经过图像滤波操作处理后的滤波结果将会对之后的图像处理产生影响。 图像滤波常见的方法有中值滤波和自适应滤波两种。
- 中值滤波使用非线性算法,用像素点实心邻域中的全部所有像素点灰度值的算术平均值取代原始值。为确保可以比较方便的取得中值,实心邻域中的像素点数一定要为正奇数。中值滤波的好处是如果一幅图像中存在某个值相差偏大的像素点时,由于使用了输出像素值实心邻域中的中值,所以不会影响结果。中值滤波算法能够被用于去除图像上孤立的噪声点,消除模糊。
- 自适应滤波是近年来在维纳滤波和卡尔曼滤波的基础上成长起来的一种最优滤波方法,现如今它在实际现场当中的运用已经波及很多方面,自适应滤波的优点是能够在未知的情况下进行有效工作,它是一种针对性更强的滤波方法。
实例一: 通过中值滤波和自适应滤波两种方法分别对图像进行滤波处理,并得到滤波后的图像。
第一步:GUIDE画界面。
第二步:编辑代码。
function varargout = lvbo(varargin)
% LVBO MATLAB code for lvbo.fig
% LVBO, by itself, creates a new LVBO or raises the existing
% singleton*.
%
% H = LVBO returns the handle to a new LVBO or the handle to
% the existing singleton*.
%
% LVBO('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in LVBO.M with the given input arguments.
%
% LVBO('Property','Value',...) creates a new LVBO or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before lvbo_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to lvbo_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 lvbo
% Last Modified by GUIDE v2.5 08-Apr-2019 13:59:23
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @lvbo_OpeningFcn, ...
'gui_OutputFcn', @lvbo_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 lvbo is made visible.
function lvbo_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 lvbo (see VARARGIN)
% Choose default command line output for lvbo
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes lvbo wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = lvbo_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 open.
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)
[filename,pathname]=uigetfile({'*.bmp;*.jpg;*.png;*.jpeg;*.tif;*.gif;*.Image files'},'载入图像');%选择路径打开图像
str=[pathname filename]; %打开图像
x=imread(str); %打开axes1的句柄 进行axes1的操作
axes(handles.axes1); %在axes1中显示图像
imshow(x);
handles.img=x; %把图像发给handles.img
guidata(hObject,handles);%把handles句柄更新
% --- Executes on button press in save.
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)
[FileName,PathName] = uiputfile({'*.jpg','JPEG(*.jpg)';'*.bmp','Bitmap(*.bmp)';'*.gif','GIF(*.gif)';'*.*', 'All Files (*.*)'},'Save Picture','Untitled');
if FileName==0
return;
else
h=getframe(handles.axes2);
imwrite(h.cdata,[PathName,FileName]);
end
% --- Executes on button press in exit.
function exit_Callback(hObject, eventdata, handles)
% hObject handle to exit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc %清除指令窗
close all %关闭所有句柄可见的窗口
close(gcf) %关闭当前窗口
clear %清除内存变量和函数
% --- Executes on button press in salt.
function salt_Callback(hObject, eventdata, handles)
% hObject handle to salt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes2);
x=rgb2gray(handles.img);
y=imnoise(x,'salt & pepper',0.04);%加椒盐噪声
imshow(y);
title('加椒盐噪声');
% --- Executes on button press in gaussian.
function gaussian_Callback(hObject, eventdata, handles)
% hObject handle to gaussian (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes2);
x=rgb2gray(handles.img);
y=imnoise(x,'gaussian',0,0.05);%加高斯噪声
imshow(y);
title('加高斯噪声');
% --- Executes on button press in medfilt.
function medfilt_Callback(hObject, eventdata, handles)
% hObject handle to medfilt (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes2);
x=rgb2gray(handles.img);
y=imnoise(x,'salt & pepper',0.04);%加椒盐噪声
z=medfilt2(y,[5 5],'symmetric');%中值滤波
imshow(z);
title('中值滤波');
% --- Executes on button press in wiener.
function wiener_Callback(hObject, eventdata, handles)
% hObject handle to wiener (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
axes(handles.axes2);
x=rgb2gray(handles.img);
y=imnoise(x,'gaussian',0,0.05);%加高斯噪声
z=wiener2(y,[5 5]);%自适应滤波
imshow(z);
title('自适应滤波');
第三步:运行。
第四步:在完成GUI界面创建后,对所创建界面的效果进行试验。点击“打开图片”按钮在文件夹中选择一张需要处理的图像,点击“椒盐噪声”或“高斯噪声”按钮可以在图像上添加椒盐噪声或高斯噪声,点击“中值滤波”或“自适应滤波”按钮对图像进行相应的滤波处理,点击“保存图片”按钮可以保存右边画布中得到的图像,点击“退出”按钮可以退出界面。
对添加椒盐噪声的图像进行中值滤波处理。
对添加高斯噪声的图像进行自适应滤波处理。