【图像处理】基于matlab GUI图像形态学处理【含Matlab源码 1287期】

309 阅读9分钟

一、形态学简介

1 概述 1.1 基本思想 用具有一定形态的结构元素去度量和提取图像中的对应形状,以达到对图像分析和识别的目的

1.2 基本运算 膨胀、腐蚀、开操作、闭操作

1.3 数学基础 集合论 在这里插入图片描述 结构元素: 在这里插入图片描述 原始图像需要扩充使得结构元素位于原始图像边缘时扩充部分可以涵盖整个结构元素。

2 二值图像形态学基本操作 2.1 腐蚀操作 结构元素B全部位于A中对应的位移量z,故会削弱边界 在这里插入图片描述 效果: 在这里插入图片描述 是一种收缩或细化的操作。

2.1 膨胀操作 结构元素B与A有交集对应的位移量z,故会扩张边界 在这里插入图片描述 效果: 在这里插入图片描述 是一种增长或粗化的操作。 注:腐蚀和膨胀是对偶的(即对前景腐蚀后求反=对背景膨胀的结果) 在这里插入图片描述 2.3 开操作 先腐蚀后膨胀,平滑物体的轮廓,断开较窄的连接条、消除细的突出物 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k35subPp-1626615953008)(p3-juejin.byteimg.com/tos-cn-i-k3…)] 在这里插入图片描述 2.4 闭操作 先膨胀后腐蚀,同样能平滑物体的轮廓,但会弥合较窄的间断和细长条 在这里插入图片描述 效果:向外的角保持不变,向内的角变圆了,填补间断—“加” 在这里插入图片描述 2.5 开操作和闭操作的比较 2.5.1 对偶性(闭操作后取反结果=对背景开操作) 在这里插入图片描述 2.5.2 性质 算子应用一次后,再用则无变化

2.5.3 对比效果 在这里插入图片描述 2.5.4 应用 开操作取出图像中的小目标(去噪) 闭操作连接目标(消除细小间隔)

2.6 击中击不中变换 目的:形状检测(检测某种特定形状的位置,要求每个物体至少被一个像素宽的背景所围绕) 将前景和背景一起匹配 在这里插入图片描述 在这里插入图片描述 3 二值图像形态学算法 3.1 边界提取 在这里插入图片描述 前景-被腐蚀的前景

3.2 孔洞填充 在这里插入图片描述 找一个起始点(位于孔洞中),不停膨胀后并上A反,直到不再发生变化

3.3 联通分量的提取 在这里插入图片描述 通过上述迭代公式可以从联通图案的一个已知点得到整个联通图案

3.4 凸壳 凸:集合A内连接任意两个点的连线都在A内,则称集合A是凸的 凸壳:任意集合S的凸壳H是包含于S的最小凸集 在这里插入图片描述 其中X0为A,Bi是不同方位的结构元素,一个Bi能够做出一个Di,A的凸壳为所有的Di相交 在这里插入图片描述 在这里插入图片描述 结果: 在这里插入图片描述 3.5 细化 A减去A与结构元B做击中击不中变换匹配到的那部分像素 在这里插入图片描述 其中B为结构元序列 在这里插入图片描述 在这里插入图片描述 步骤: A被B1~Bn按次序细化,再返回B1,从B1开始按次序细化,直到收敛 在这里插入图片描述 3.6 粗化 细化的形态学对偶 A并上A与结构元B做击中击不中变换匹配到的那部分像素的和 在这里插入图片描述 在这里插入图片描述 3.7 骨架 A的骨架可用腐蚀和开操作表达 在这里插入图片描述 其中k代表对A进行k次腐蚀,而K则是A被腐蚀为空集前最后一次迭代 在这里插入图片描述 3.8 剪裁 是对细化处理和骨架绘制算法的补充,因为这些处理会将附加部分保留下来,应清楚干净,则需要剪裁来取出寄生的多余部分

4 灰度级形态学 4.1 操作 腐蚀:输出图像变暗,亮的细节被减少 膨胀:输出图像变亮,暗的细节被减少 4.2 效果 在这里插入图片描述 开操作:去除较小的明亮细节 闭操作:去除较小的暗细节

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;

三、运行结果

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本 2014a

2 参考文献 [1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020. [2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013. [3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013. [4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.