天天看點

【圖像去噪】基于均值+中值算法實作圖像去噪含Matlab源碼

1 簡介

噪聲在圖像上常表現為一引起較強視覺效果的孤立像素點或像素塊。一般,噪聲信号與要研究的對象不相關,它以無用的資訊形式出現,擾亂圖像的可觀測資訊。通俗的說就是噪聲讓圖像不清楚。【1】噪聲主要來源于圖像擷取過程和圖像信号傳輸過程,常見的噪聲有高斯噪聲、椒鹽噪聲、乘性噪聲等。

高斯噪聲:

高斯噪聲是指它的機率密度函數服從高斯分布(即正态分布)的一類噪聲。如果一個噪聲,它的幅度分布服從高斯分布,而它的功率譜密度又是均勻分布的,則稱它為高斯白噪聲。高斯白噪聲的二階矩不相關,一階矩為常數,是指先後信号在時間上的相關性。【1】在matlab軟體中有專門的高斯噪聲産生函數,是以不需要用到太複雜的計算步驟。可以使用imnoise函數并選擇gaussian即可在圖檔中加入高斯噪聲。

椒鹽噪聲:

椒鹽噪聲也稱為脈沖噪聲,是圖像中經常見到的一種噪聲,它是一種随機出現的白點或者黑點,可能是亮的區域有黑色像素或是在暗的區域有白色像素(或是兩者皆有)。【1】椒鹽噪聲通常是來源于圖像切割。同樣的,椒鹽噪聲的産生在matlab中也有相應的産生函數。與高斯噪聲加入的方式類似,在matlab中使用imnoise選擇salt & pepper即可在圖檔中加入椒鹽噪聲。

均值濾波:

均值濾波是一種線性濾波算法,它是指在圖像上對目标像素給一個模闆,該模闆包括了其周圍的臨近像素(以目标像素為中心的周圍8個像素,構成一個濾波模闆,即包括目标像素本身),再用模闆中的全體像素的平均值來代替原來像素值。

在matlab函數庫中有多種實作均值濾波的方法,有filter2函數、conv2函數以及fspecial 和 imfilter 函數配合使用。此次的均值濾波我選擇的是fspecial和imfilter聯用的方法。

中值濾波:

與均值濾波不同,中值濾波法是一種非線性平滑技術,它将每一像素點的灰階值設定為該點某鄰域視窗内的所有像素點灰階值的中值。在matlab中實作中值濾波的方法也較為簡單,直接使用medfilt2函數即可實作。

GUI:

GUI全稱是Graphical User Interface,即圖形使用者界面。在matlab中的GUI可以在界面上添加按鍵、滑動條、可編輯文本框、靜态文本框、坐标區等等。其中,坐标區還可以用來作為圖像的顯示區域,配合按鍵可以實作一個按鍵對應一張圖的操作。在GUI界面上添加好自己所需的部件之後可以對每個部件進行程式設計,以此來達到自己預期的功能。此次的實踐中,我采用的是按鍵和坐标區相結合的方式來實作特定的按鍵對應特定操作,并且在實作完按鍵所具有的功能之後将所産生的圖象顯示在特定的坐标區,以此來實作一個按鍵對應一個操作之後的圖像顯示。

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 仿真結果

【圖像去噪】基于均值+中值算法實作圖像去噪含Matlab源碼

4 參考文獻

部落客簡介:擅長智能優化算法、神經網絡預測、信号處理、元胞自動機、圖像處理、路徑規劃、無人機等多種領域的Matlab仿真,相關matlab代碼問題可私信交流。

部分理論引用網絡文獻,若有侵權聯系部落客删除。