【限速標誌識別】基於matlab GUI形態學限速標誌識別【含Matlab原始碼 1142期】
一、簡介
數學形態學操作可以分為二值形態學和灰度形態學,灰度形態學由二值形態學擴充套件而來。數學形態學有2個基本的運算,即腐蝕和膨脹,而腐蝕和膨脹通過結合又形成了開運算和閉運算。
開運算就是先腐蝕再膨脹,閉運算就是先膨脹再腐蝕。
1 二值形態學
粗略的說,腐蝕可以使目標區域範圍“變小”,其實質造成影象的邊界收縮,可以用來消除小且無意義的目標物。式子表達為:
該式子表示用結構B腐蝕A,需要注意的是B中需要定義一個原點,【而B的移動的過程與卷積核移動的過程一致,同卷積核與影象有重疊之後再計算一樣】當B的原點平移到影象A的像元(x,y)時,如果B在(x,y)處,完全被包含在影象A重疊的區域,(也就是B中為1的元素位置上對應的A影象值全部也為1)則將輸出影象對應的像元(x,y)賦值為1,否則賦值為0。
我們看一個演示圖。
B依順序在A上移動(和卷積核在影象上移動一樣,然後在B的覆蓋域上進行形態學運算),當其覆蓋A的區域為[1,1;1,1]或者[1,0;1,1]時,(也就是B中‘1’是覆蓋區域的子集)對應輸出影象的位置才會為1。
2 膨脹
粗略地說,膨脹會使目標區域範圍“變大”,將於目標區域接觸的背景點合併到該目標物中,使目標邊界向外部擴張。作用就是可以用來填補目標區域中某些空洞以及消除包含在目標區域中的小顆粒噪聲。
該式子表示用結構B膨脹A,將結構元素B的原點平移到影象像元(x,y)位置。如果B在影象像元(x,y)處與A的交集不為空(也就是B中為1的元素位置上對應A的影象值至少有一個為1),則輸出影象對應的像元(x,y)賦值為1,否則賦值為0。
演示圖為:
3 小結
也就是說無論腐蝕還是膨脹,都是把結構元素B像卷積操作那樣,在影象上平移,結構元素B中的原點就相當於卷積核的核中心,結果也是儲存在核中心對應位置的元素上。只不過腐蝕是B被完全包含在其所覆蓋的區域,膨脹時B與其所覆蓋的區域有交集即可。
4 灰度形態學
在講述灰度值形態學之前,我們進行一個約定,即將結構元素B覆蓋住的影象A的區域記為P(取Part之意)。
5 灰度形態學的腐蝕
那麼灰度形態學中的腐蝕就是類似卷積的一種操作,用P減去結構元素B形成的小矩形,取其中最小值賦到對應原點的位置即可。
我們來看一個例項,進行加深對灰度形態學的理解。
假設我們有如下的影象A和結構元素B:
進行灰度形態學腐蝕的過程如下:
我們對輸出影象的第一個元素的輸出結果進行具體的展示,也就是原點對應的4的位置。輸出影象其他的元素的值也都是這樣得到的。我們會看到,B首先覆蓋的區域就是被減數矩陣,然後在其差矩陣中求min(最小值)來作為原點對應位置的值。
灰度形態學的膨脹
根據上面對腐蝕的描述,我們對膨脹做出同樣的描述,灰度形態學中的膨脹就是類似卷積的一種操作,用P加上B,然後取這個區域中的最大值賦值給結構元素B的原點所對應的位置。
這裡也對輸出影象第一個元素值的來歷做個說明。
對上面矩陣的和求最大值就是6,所以把6賦值給結構元素原點所對應的位置。
6 小結
上面介紹了灰度形態學的概念,這裡來說一說各自的用處。相比較於原影象,因為腐蝕的結果要使得各像元比之前變得更小,所以適用於去除高峰噪聲。而灰度值膨脹的結果會使得各像元比之前的變得更大,所以適用於去除低谷噪聲。
二、原始碼
function varargout = Mat5_2(varargin)
% MAT5_2 MATLAB code for Mat5_2.fig
% MAT5_2, by itself, creates a new MAT5_2 or raises the existing
% singleton*.
%
% H = MAT5_2 returns the handle to a new MAT5_2 or the handle to
% the existing singleton*.
%
% MAT5_2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MAT5_2.M with the given input arguments.
%
% MAT5_2('Property','Value',...) creates a new MAT5_2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Mat5_2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Mat5_2_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 Mat5_2
% Last Modified by GUIDE v2.5 21-Jul-2021 15:09:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Mat5_2_OpeningFcn, ...
'gui_OutputFcn', @Mat5_2_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 Mat5_2 is made visible.
function Mat5_2_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 Mat5_2 (see VARARGIN)
% Choose default command line output for Mat5_2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Mat5_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Mat5_2_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)
global Image
[filename,pathname]=uigetfile({'*.jpg';'*.bmp';'.gif'},'選擇圖片');
str=[pathname filename];
Image1 = imread(str);
Image=Image1;
axes(handles.axes1);
imshow(Image1);
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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 && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function BW = imbinarize(I,varargin) %#codegen
%imbinarize Binarize image by thresholding.
% Copyright 2015-2018 The MathWorks, Inc.
% Syntax
% ------
%
% BW = imbinarize(I)
% BW = imbinarize(I,method)
% BW = imbinarize(I,'adaptive',Param,Value,...)
% BW = imbinarize(I,t)
%
% Input Specs
% -----------
%
% I:
% real
% non-sparse
% 2d
% uint8, uint16, uint32, int8, int16, int32, single or double
%
% method:
% string with value: 'global' or 'adaptive'
% default: 'global'
%
% Sensitivity:
% numeric
% real
% non-sparse
% scalar
% non-negative
% <= 1
% default: 0.5
% converted to double
%
% ForegroundPolarity:
% string with value either 'bright' or 'dark'
% default: 'bright'
%
% t:
% numeric
% real
% non-sparse
% 2d
% either scalar or matrix of the same size as I
%
% Output Specs
% ------------
%
% BW:
% logical
% 2D matrix
% same size as I
%
% Validate the input image
validateImage(I);
% Parse and validate optional inputs
[isNumericThreshold,options] = parseOptionalInputs(I,varargin{:});
coder.internal.prefer_const(isNumericThreshold,options);
if isNumericThreshold
% BW = imbinarize(I,t)
BW = binarize(I,options.t);
else
if strcmp(options.method,'global')
% BW = imbinarize(I,'global')
t = computeGlobalThreshold(I);
else
% BW = imbinarize(I,'adaptive',...)
t = adaptthresh(I,options.sensitivity,'ForegroundPolarity',options.polarity);
end
BW = binarize(I,t);
end
三、執行結果
四、備註
版本:2014a