【圖像去噪】基于均值+中值算法實現(xiàn)圖像去噪含Matlab源碼
1 簡介
噪聲在圖像上常表現(xiàn)為一引起較強視覺效果的孤立像素點或像素塊。一般,噪聲信號與要研究的對象不相關,它以無用的信息形式出現(xiàn),擾亂圖像的可觀測信息。通俗的說就是噪聲讓圖像不清楚?!?】噪聲主要來源于圖像獲取過程和圖像信號傳輸過程,常見的噪聲有高斯噪聲、椒鹽噪聲、乘性噪聲等。
高斯噪聲:
高斯噪聲是指它的概率密度函數(shù)服從高斯分布(即正態(tài)分布)的一類噪聲。如果一個噪聲,它的幅度分布服從高斯分布,而它的功率譜密度又是均勻分布的,則稱它為高斯白噪聲。高斯白噪聲的二階矩不相關,一階矩為常數(shù),是指先后信號在時間上的相關性。【1】在matlab軟件中有專門的高斯噪聲產生函數(shù),所以不需要用到太復雜的計算步驟??梢允褂胕mnoise函數(shù)并選擇gaussian即可在圖片中加入高斯噪聲。
椒鹽噪聲:
椒鹽噪聲也稱為脈沖噪聲,是圖像中經常見到的一種噪聲,它是一種隨機出現(xiàn)的白點或者黑點,可能是亮的區(qū)域有黑色像素或是在暗的區(qū)域有白色像素(或是兩者皆有)?!?】椒鹽噪聲通常是來源于圖像切割。同樣的,椒鹽噪聲的產生在matlab中也有相應的產生函數(shù)。與高斯噪聲加入的方式類似,在matlab中使用imnoise選擇salt & pepper即可在圖片中加入椒鹽噪聲。
均值濾波:
均值濾波是一種線性濾波算法,它是指在圖像上對目標像素給一個模板,該模板包括了其周圍的臨近像素(以目標像素為中心的周圍8個像素,構成一個濾波模板,即包括目標像素本身),再用模板中的全體像素的平均值來代替原來像素值。
在matlab函數(shù)庫中有多種實現(xiàn)均值濾波的方法,有filter2函數(shù)、conv2函數(shù)以及fspecial?和?imfilter?函數(shù)配合使用。此次的均值濾波我選擇的是fspecial和imfilter聯(lián)用的方法。
中值濾波:
與均值濾波不同,中值濾波法是一種非線性平滑技術,它將每一像素點的灰度值設置為該點某鄰域窗口內的所有像素點灰度值的中值。在matlab中實現(xiàn)中值濾波的方法也較為簡單,直接使用medfilt2函數(shù)即可實現(xiàn)。
GUI:
GUI全稱是Graphical User Interface,即圖形用戶界面。在matlab中的GUI可以在界面上添加按鍵、滑動條、可編輯文本框、靜態(tài)文本框、坐標區(qū)等等。其中,坐標區(qū)還可以用來作為圖像的顯示區(qū)域,配合按鍵可以實現(xiàn)一個按鍵對應一張圖的操作。在GUI界面上添加好自己所需的部件之后可以對每個部件進行編程,以此來達到自己預期的功能。此次的實踐中,我采用的是按鍵和坐標區(qū)相結合的方式來實現(xiàn)特定的按鍵對應特定操作,并且在實現(xiàn)完按鍵所具有的功能之后將所產生的圖象顯示在特定的坐標區(qū),以此來實現(xiàn)一個按鍵對應一個操作之后的圖像顯示。
2 部分代碼
function varargout = work(varargin)
% WORK MATLAB code for work.fig
% ? ? ?WORK, by itself, creates a new WORK or raises the existing
% ? ? ?singleton*.
%
% ? ? ?H = WORK returns the handle to a new WORK or the handle to
% ? ? ?the existing singleton*.
%
% ? ? ?WORK('CALLBACK',hObject,eventData,handles,...) calls the local
% ? ? ?function named CALLBACK in WORK.M with the given input arguments.
%
% ? ? ?WORK('Property','Value',...) creates a new WORK or raises the
% ? ? ?existing singleton*. ?Starting from the left, property value pairs are
% ? ? ?applied to the GUI before work_OpeningFcn gets called. ?An
% ? ? ?unrecognized property name or invalid value makes property application
% ? ? ?stop. ?All inputs are passed to work_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 work
% Last Modified by GUIDE v2.5 14-Mar-2022 12:52:52
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', ? ? ? mfilename, ...
? ? ? ? ? ? ? ? ? 'gui_Singleton', ?gui_Singleton, ...
? ? ? ? ? ? ? ? ? 'gui_OpeningFcn', @work_OpeningFcn, ...
? ? ? ? ? ? ? ? ? 'gui_OutputFcn', ?@work_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 work is made visible.
function work_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 work (see VARARGIN)
% Choose default command line output for work
handles.output = hObject;
handles.work1=[];
handles.work2=[];
handles.work3=[];
handles.work4=[];
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes work wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = work_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)
[imgfilename, imgpathname] = uigetfile({'*. jpg;*. png'},'選擇一張圖片' ) ;
if imgfilename
I = imread([imgpathname '\' imgfilename]);
axes (handles. axes1);
imshow (I)
handles.work1=I;
end
guidata (hObject,handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton2 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
M=rgb2gray(handles.work1);
axes (handles. axes2);
imshow (M)
handles.work2=M;
guidata (hObject,handles);
% --- 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)
GS=imnoise(handles.work2,'gaussian',0,0.06); %添加高斯噪聲,均值為0,方差為0.06
axes (handles. axes3);
imshow (GS)
handles.work3=GS;
guidata (hObject,handles);
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton4 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
H1=fspecial('average',[9,9]);
JZ1=imfilter(handles.work3,H1); %均值濾波
axes (handles. axes4);
imshow (JZ1);
guidata (hObject,handles);
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton5 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
ZZ1=medfilt2(handles.work3); %中值濾波
axes (handles. axes5);
imshow (ZZ1);
guidata (hObject,handles);
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton6 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
JY=imnoise(handles.work2,'salt & pepper',0.33); %添加椒鹽噪聲
axes (handles. axes6);
imshow (JY);
handles.work4=JY;
guidata (hObject,handles);
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton7 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
H1=fspecial('average',[9,9]);
JZ2=imfilter(handles.work4,H1);
axes (handles. axes7);
imshow (JZ2);
guidata (hObject,handles);
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject ? ?handle to pushbutton8 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
ZZ2=medfilt2(handles.work4);
axes (handles. axes8);
imshow (ZZ2);
guidata (hObject,handles);
% --- Executes on mouse press over figure background.
function figure1_ButtonDownFcn(hObject, eventdata, handles)
% hObject ? ?handle to figure1 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
% --- Executes on mouse press over figure background, over a disabled or
% --- inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(hObject, eventdata, handles)
% hObject ? ?handle to figure1 (see GCBO)
% eventdata ?reserved - to be defined in a future version of MATLAB
% handles ? ?structure with handles and user data (see GUIDATA)
3 仿真結果

4 參考文獻
[1]胡鵬, 徐會艷. 基于Matlab的圖像去噪算法的研究與實現(xiàn)[J]. 福建電腦, 2009(12):2.
博主簡介:擅長智能優(yōu)化算法、神經網絡預測、信號處理、元胞自動機、圖像處理、路徑規(guī)劃、無人機等多種領域的Matlab仿真,相關matlab代碼問題可私信交流。
部分理論引用網絡文獻,若有侵權聯(lián)系博主刪除。
