1. 程式人生 > 其它 >【物體檢測】基於matlab GUI形態學物體檢測【含Matlab原始碼 945期】

【物體檢測】基於matlab GUI形態學物體檢測【含Matlab原始碼 945期】

一、簡介

數學形態學操作可以分為二值形態學和灰度形態學,灰度形態學由二值形態學擴充套件而來。數學形態學有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 = GUI_NIT(varargin)
% GUI_NIT MATLAB code for GUI_NIT.fig
%      GUI_NIT, by itself, creates a new GUI_NIT or raises the existing
%      singleton*.
%
%      H = GUI_NIT returns the handle to a new GUI_NIT or the handle to
%      the existing singleton*.
%
%      GUI_NIT('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in GUI_NIT.M with the given input arguments.
%
%      GUI_NIT('Property','Value',...) creates a new GUI_NIT or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before GUI_NIT_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to GUI_NIT_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 GUI_NIT

% Last Modified by GUIDE v2.5 17-May-2021 14:54:59

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_NIT_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_NIT_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 GUI_NIT is made visible.
function GUI_NIT_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 GUI_NIT (see VARARGIN)

% Choose default command line output for GUI_NIT
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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


% --- Outputs from this function are returned to the command line.
function varargout = GUI_NIT_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 BROWSE.
function BROWSE_Callback(hObject, eventdata, handles)
% hObject    handle to BROWSE (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('*.mp4', 'Pick an video');

    if isequal(filename,0) | isequal(pathname,0)
        
       warndlg('Video is not selected');
       
    else
       
    
        

% Update handles structure
guidata(hObject, handles);

    end


    
% --- Executes on button press in track.
function track_Callback(hObject, eventdata, handles)
% hObject    handle to track (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
global c;
singleObject(c);
% handles    structure with handles and user data (see GUIDATA)
% --- 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)
close all;


% --- Executes on button press in load_image.

function load_image_Callback(hObject, eventdata, handles)
% hObject    handle to load_image (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({' *.jpg';'*png';'*bmp';'Pick an image'});
 %global c_image;  
    if isequal(filename,0) | isequal(pathname,0)
        
       warndlg('Image is not selected');
       
    else
       global c_image; 
      c_image=strcat(pathname,filename);     
      imshow(c_image);
      disp(c_image)
    
    end

% --- Executes on button press in detect_people.
function detect_people_Callback(hObject, eventdata, handles)
% hObject    handle to detect_people (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global c_image;
detectPeople(c_image);

% --- Executes on button press in detect_ball.
function detect_ball_Callback(hObject, eventdata, handles)
global c_image;
detectBalls(c_image);
% hObject    handle to detect_ball (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

三、執行結果








四、備註

版本:2014a

完整程式碼或代寫加1564658423