天天看點

【圖像修複】基于matlab GUI空域濾波圖像複原【含Matlab源碼 849期】

二、簡介

空域濾波是一種鄰域處理方法,通過直接在圖像空間中對鄰域内像素進行處理,達到平滑或銳化圖像的作用。此外,在圖像識别中,通過濾波還可以抽出圖像的特征作為圖像識别的特征模式。

空域濾波是圖像處理領域中廣泛使用的主要工具。空域濾波主要可以分為線性濾波和非線性濾波,其中,線性濾波和頻域濾波存在一一對應的關系。但是,空域濾波可以用于非線性濾波,但是頻域濾波不能用于非線性濾波。

從根源上講,濾波這一詞語來自于頻域,信号進行中頻域濾波指的是允許或者限制一定的頻率成分通過。但空域濾波直接在圖像空間中增強圖像的某些特征或者減弱圖像的某些特征。

空域濾波的作用域是像素及其鄰域,通常使用空域模闆對鄰域内的像素進行處理進而産生該像素的輸出值。

線性空域濾波指的是像素的輸出值是計算該像素鄰域内像素值的線性組合,系數矩陣在這裡我們稱之為模闆。由信号處理的原理可以得出,線性濾波可以用卷積來實作。是以,在數字圖像進行中,線性濾波通常是利用濾波模闆與圖像的空域進行卷積來實作的,線上性濾波中濾波模闆也稱為卷積模闆。

根據空域卷積的定義可以知道,卷積首先需要将模闆進行反褶,也就是将模闆繞模闆中心旋轉180°,但是在數字圖像進行中,卷積模闆通常是關于原點對稱的,是以通常不需要考慮反褶過程。模闆卷積的主要步驟包括如下幾個步驟,

(a.将模闆在圖像中進行周遊,将模闆中心和各個像素位置重合;

(b.将模闆的各個系數與模闆對應像素值進行相乘;

(c.将所有的乘積相加,并将求和結果指派于模闆中心對應的像素

對于尺寸為m*n的模闆,線性濾波在圖像中像素(x,y)處的相應輸出g(x,y)的一般表達式如下:

【圖像修複】基于matlab GUI空域濾波圖像複原【含Matlab源碼 849期】

上式中,w(s,t)和f(x+s,y+t)分别為模闆系數和模闆對應像素,其中,為了保證唯一坐标s和t是整數,要求m和n是奇數。在一般情況下,線性濾波選取尺寸為奇數的正方形模闆。

為了友善進行描述,設模闆的尺寸為mxn,線性濾波在像素(x,y)處的相應輸出R的另一種簡單化表示為:

【圖像修複】基于matlab GUI空域濾波圖像複原【含Matlab源碼 849期】

上式中wi表示的是模闆系數,zi表示與模闆系數對應的像素值,這裡i = 1,2,3,…,mn,mn這裡指的是模闆對應的像素總數。

在非線性空域濾波中也是采用基于鄰域的處理,而且模闆滑過一幅圖像的機理和線性空域濾波是一緻的。非線性濾波處理也取決于模闆對應的鄰域内的像素,然而,因為不能直接利用上面g(x,y)的表達式計算乘積求和。例如非常有用的中值濾波,它是将模闆對應的鄰域内的像素值進行排序,然後查找中間值。利用這種方法可以有效地去除椒鹽噪聲,但是因為非線性濾波涉及到了像素值的排序操作,是以它的時間開銷要比線性濾波的大。

對于實作空域濾波的最後一個問題是對于圖像邊界像素鄰域的處理。對于尺寸為n*n的空域模闆,當模闆中心距離圖像邊界為(n-1)/2像素時,該模闆正好在圖像的内部。如果模闆的中心繼續像圖像外邊界靠近,那麼模闆的行或列就會超出圖像之外。是以常常采用延拓的方式解決外邊界問題。常用的方法有四種,分别是補零、重複、對稱和循環方式,補零是指通過補零來擴充圖像。重複是指通過複制外邊界的值來擴充圖像,對稱是指通過鏡像反射外邊界的值來擴充圖像,循環是指将圖像看成二維周期函數的一個周期來擴充。在空域濾波完成後,從處理後的圖像裁剪出與源圖像對應的部分,使處理後的圖像與源圖像的尺寸相等。

三、部分源代碼

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

% Last Modified by GUIDE v2.5 31-May-2014 09:03:34

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @spatial_filter_OpeningFcn, ...
                   'gui_OutputFcn',  @spatial_filter_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(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 spatial_filter is made visible.
function spatial_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 spatial_filter (see VARARGIN)
axes(handles.axes1);
img = imread('lena.bmp');
imshow(img);
[M,N] = size(img);
g = imnoise(img,'gaussian',0.05,0.01);
axes(handles.axes2);
imshow(g);
w = fspecial('average',[3,3]);
f = imfilter(g,w);
axes(handles.axes3);
imshow(f);
set(handles.m_edit,'string',0.05);
set(handles.v_edit,'string',0.01);
set(handles.c_edit,'string',3);
% Choose default command line output for spatial_filter
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = spatial_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 during object creation, after setting all properties.
function m_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to m_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function m_edit_Callback(hObject, eventdata, handles)
% hObject    handle to m_edit (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 m_edit as text
%        str2double(get(hObject,'String')) returns contents of m_edit as a double


% --- Executes during object creation, after setting all properties.
function v_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to v_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function v_edit_Callback(hObject, eventdata, handles)
% hObject    handle to v_edit (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 v_edit as text
%        str2double(get(hObject,'String')) returns contents of v_edit as a double


% --- Executes during object creation, after setting all properties.
function c_edit_CreateFcn(hObject, eventdata, handles)
% hObject    handle to c_edit (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
    set(hObject,'BackgroundColor','white');
else
    set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
end



function c_edit_Callback(hObject, eventdata, handles)
% hObject    handle to c_edit (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 c_edit as text
%        str2double(get(hObject,'String')) returns contents of c_edit as a double


% --- Executes on button press in apply_button.
function apply_button_Callback(hObject, eventdata, handles)
% hObject    handle to apply_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% m = str2num(get(handles.m_edit,'string'));
% v = str2num(get(handles.v_edit,'string'));
c = str2num(get(handles.c_edit,'string'));
if c>0&&c==floor(c)&&size(c,1)*size(c,2)==1%c為整數且輸入是‘一個’整數
    
img = getimage(handles.axes1);
val1 = get(handles.filter_pop_menu,'value');
str1 = get(handles.filter_pop_menu,'string');
g = getimage(handles.axes2);
switch str1{val1}
    case '均值濾波器'
         w = fspecial('average',[c,c]);
         f = imfilter(g,w);
     case '中值濾波器'
         f = medfilt2(g,[c,c]);
     case '最大濾波器'
         f = ordfilt2(g,9,ones(c,c));
     case '最小濾波器'
         f = ordfilt2(g,1,ones(c,c));
     end      

四、運作結果

【圖像修複】基于matlab GUI空域濾波圖像複原【含Matlab源碼 849期】

四、matlab版本及參考文獻

​1 matlab版本​

2014a

​2 參考文獻​

[1] 蔡利梅.MATLAB圖像處理——理論、算法與執行個體分析[M].清華大學出版社,2020.

[2]楊丹,趙海濱,龍哲.MATLAB圖像處理執行個體詳解[M].清華大學出版社,2013.

[3]周品.MATLAB圖像處理與圖形使用者界面設計[M].清華大學出版社,2013.

繼續閱讀