天天看點

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

一、形态學簡介

1 概述

1.1 基本思想

用具有一定形态的結構元素去度量和提取圖像中的對應形狀,以達到對圖像分析和識别的目的

1.2 基本運算

膨脹、腐蝕、開操作、閉操作

1.3 數學基礎

集合論

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

結構元素:

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

原始圖像需要擴充使得結構元素位于原始圖像邊緣時擴充部分可以涵蓋整個結構元素。

2 二值圖像形态學基本操作

2.1 腐蝕操作

結構元素B全部位于A中對應的位移量z,故會削弱邊界

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

效果:

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

是一種收縮或細化的操作。

2.1 膨脹操作

結構元素B與A有交集對應的位移量z,故會擴張邊界

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

效果:

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

是一種增長或粗化的操作。

注:腐蝕和膨脹是對偶的(即對前景腐蝕後求反=對背景膨脹的結果)

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

2.3 開操作

先腐蝕後膨脹,平滑物體的輪廓,斷開較窄的連接配接條、消除細的突出物

[外鍊圖檔轉存失敗,源站可能有防盜鍊機制,建議将圖檔儲存下來直接上傳(img-k35subPp-1626615953008)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0959bcda0e5c49a6801fd0c7c94e20e7~tplv-k3u1fbpfcp-zoom-1.image)]

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

2.4 閉操作

先膨脹後腐蝕,同樣能平滑物體的輪廓,但會彌合較窄的間斷和細長條

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

效果:向外的角保持不變,向内的角變圓了,填補間斷—“加”

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

2.5 開操作和閉操作的比較

2.5.1 對偶性(閉操作後取反結果=對背景開操作)

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

2.5.2 性質

算子應用一次後,再用則無變化

2.5.3 對比效果

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

2.5.4 應用

開操作取出圖像中的小目标(去噪)

閉操作連接配接目标(消除細小間隔)

2.6 擊中擊不中變換

目的:形狀檢測(檢測某種特定形狀的位置,要求每個物體至少被一個像素寬的背景所圍繞)

将前景和背景一起比對

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

3 二值圖像形态學算法

3.1 邊界提取

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

前景-被腐蝕的前景

3.2 孔洞填充

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

找一個起始點(位于孔洞中),不停膨脹後并上A反,直到不再發生變化

3.3 聯通分量的提取

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

通過上述疊代公式可以從聯通圖案的一個已知點得到整個聯通圖案

3.4 凸殼

凸:集合A内連接配接任意兩個點的連線都在A内,則稱集合A是凸的

凸殼:任意集合S的凸殼H是包含于S的最小凸集

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

其中X0為A,Bi是不同方位的結構元素,一個Bi能夠做出一個Di,A的凸殼為所有的Di相交

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

結果:

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

3.5 細化

A減去A與結構元B做擊中擊不中變換比對到的那部分像素

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

其中B為結構元序列

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

步驟:

A被B1~Bn按次序細化,再傳回B1,從B1開始按次序細化,直到收斂

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

3.6 粗化

細化的形态學對偶

A并上A與結構元B做擊中擊不中變換比對到的那部分像素的和

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

3.7 骨架

A的骨架可用腐蝕和開操作表達

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

其中k代表對A進行k次腐蝕,而K則是A被腐蝕為空集前最後一次疊代

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

3.8 剪裁

是對細化處理和骨架繪制算法的補充,因為這些處理會将附加部分保留下來,應清楚幹淨,則需要剪裁來取出寄生的多餘部分

4 灰階級形态學

4.1 操作

腐蝕:輸出圖像變暗,亮的細節被減少

膨脹:輸出圖像變亮,暗的細節被減少

4.2 效果

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

開操作:去除較小的明亮細節

閉操作:去除較小的暗細節

4.3 算法

圖像平滑:先開(抑制亮細節)後閉(抑制暗細節)

形态學梯度:膨脹-腐蝕

頂帽變換:校正不均勻光照,增強陰影的細節

二、部分源代碼

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

% Last Modified by GUIDE v2.5 31-Aug-2021 23:16:44

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

% Update handles structure
guidata(hObject, handles);
guidata(hObject, handles);
movegui(gcf,'center');

% --- Outputs from this function are returned to the command line.
function varargout = shape_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 selection change in jiegouyuansu1.
function jiegouyuansu1_Callback(hObject, eventdata, handles)
% hObject    handle to jiegouyuansu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
h1=findobj('tag','jiegouyuansu1');
if get(h1,'value')==1
   set(handles.text4,'Visible','on');
   set(handles.text4,'String','邊長W');
   set(handles.text5,'Visible','off');
   set(handles.text6,'Visible','off');
   set(handles.edit2,'Visible','on');
   set(handles.edit2,'String','3');
   set(handles.edit3,'Visible','off');
   set(handles.edit4,'Visible','off');
elseif get(h1,'value')==2
   set(handles.text4,'Visible','off');
   set(handles.text5,'Visible','on');
   set(handles.text5,'String','長M');
   set(handles.text6,'Visible','on');
   set(handles.text6,'String','寬N');
   set(handles.edit2,'Visible','off');
   set(handles.edit3,'Visible','on');
   set(handles.edit3,'String','6');
   set(handles.edit4,'Visible','on');
   set(handles.edit4,'String','3');
elseif get(h1,'value')==3
   set(handles.text4,'Visible','on');
   set(handles.text4,'String','半徑R');
   set(handles.text5,'Visible','off');
   set(handles.text6,'Visible','off');
   set(handles.edit2,'Visible','on');
   set(handles.edit2,'String','3');
   set(handles.edit3,'Visible','off');
   set(handles.edit4,'Visible','off');
elseif get(h1,'value')==4
   set(handles.text4,'Visible','off');
   set(handles.text5,'Visible','on');
   set(handles.text5,'String','長度LEN');
   set(handles.text6,'Visible','on');
   set(handles.text6,'String','角度DEG');
   set(handles.edit2,'Visible','off');
   set(handles.edit3,'Visible','on');
   set(handles.edit3,'String','9');
   set(handles.edit4,'Visible','on');
   set(handles.edit4,'String','45');
elseif get(h1,'value')==5
   set(handles.text4,'Visible','on');
   set(handles.text4,'String','半徑R');
   set(handles.text5,'Visible','off');
   set(handles.text6,'Visible','off');
   set(handles.edit2,'Visible','on');
   set(handles.edit2,'String','3');
   set(handles.edit3,'Visible','off');
   set(handles.edit4,'Visible','off');
end
% Hints: contents = get(hObject,'String') returns jiegouyuansu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from jiegouyuansu1


% --- Executes during object creation, after setting all properties.
function jiegouyuansu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to jiegouyuansu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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








% --- Executes during object creation, after setting all properties.
function caozuoleixing_CreateFcn(hObject, eventdata, handles)
% hObject    handle to caozuoleixing (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu 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 edit2_Callback(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit2 as text
%        str2double(get(hObject,'String')) returns contents of edit2 as a double


% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit2 (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 edit3_Callback(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit3 as text
%        str2double(get(hObject,'String')) returns contents of edit3 as a double


% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit3 (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 edit4_Callback(hObject, eventdata, handles)
% hObject    handle to edit4 (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 edit4 as text
%        str2double(get(hObject,'String')) returns contents of edit4 as a double


% --- Executes during object creation, after setting all properties.
function edit4_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit4 (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




% --- Executes on button press in queding.
function queding_Callback(hObject, eventdata, handles)
% hObject    handle to queding (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global f;
R=get(handles.edit2, 'String');
R=str2num(R);
LEN=get(handles.edit3, 'String');
LEN=str2num(LEN);
M=LEN;
DEG=get(handles.edit4, 'String');
DEG=str2num(DEG);
N=DEG;
jgys= get(handles.jiegouyuansu1, 'Value');
switch jgys

    case 1
        se=strel('square',R);
    case 2
        se=strel('rectangle',[M N]);
    case 3
        se=strel('disk',R);
    case 4
        se=strel('line',LEN,DEG);
    case 5
        se=strel('diamond',R);
end
czlx = get(handles.caozuoleixing1, 'Value');
switch czlx
    case 1
        f0=imdilate(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('膨脹後的圖像');
        hold;
        hold;
    case 2
        f0=imerode(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('腐蝕後的圖像');
        hold;
        hold;
    case 3
        f0=imopen(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('開運算後的圖像');
        hold;
        hold;
    case 4
        f0=imclose(f,se);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('閉運算後的圖像');
        hold;
        hold;
    case 5
        B1=strel([0 0 0;0 1 1;0 1 0]);
        B2=strel([1 1 1;1 0 0 ;1 0 0]);
        f0=bwhitmiss(f,B1,B2);
        axes(handles.axes3);
        cla;
        imshow(f0);
        title('閉運算後的圖像');
        hold;
        hold;
end




% --- Executes on selection change in caozuoleixing.
function caozuoleixing_Callback(hObject, eventdata, handles)
% hObject    handle to caozuoleixing (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns caozuoleixing contents as cell array
%        contents{get(hObject,'Value')} returns selected item from caozuoleixing




% --------------------------------------------------------------------
function Untitled_2_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global f;
file = uigetfile('*.tif;*.jpg');
if ~isequal(file, 0)
    f=imread(file);
end
axes(handles.axes1);
cla;
imshow(f);
title('原圖像');
hold;
hold;


% --------------------------------------------------------------------
function Untitled_3_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[filename ,pathname]=uiputfile({'*.mat','MAT-files(*.mat)'},'儲存');
str=strcat(pathname,filename);
a='shape.fig';
save(char(str), 'a')

% --------------------------------------------------------------------
function Untitled_4_Callback(hObject, eventdata, handles)
% hObject    handle to Untitled_4 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global f;

button2=questdlg('你确定退出嗎?','退出程式','Yes','No','Yes');
if strcmp(button2,'Yes')
close;
f=0;
end;
           

三、運作結果

【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】
【圖像處理】基于GUI圖像形态學處理【Matlab 1287期】

四、matlab版本及參考文獻

1 matlab版本

2014a

2 參考文獻

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

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

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

[4]劉成龍.精通MATLAB圖像處理[M].清華大學出版社,2015.

繼續閱讀