【图像处理】基于matlab GUI图像形态学处理【含Matlab源码 1287期】
一、形态学简介
1 概述
1.1 基本思想
用具有一定形态的结构元素去度量和提取图像中的对应形状,以达到对图像分析和识别的目的
1.2 基本运算
膨胀、腐蚀、开操作、闭操作
1.3 数学基础
集合论
结构元素:
原始图像需要扩充使得结构元素位于原始图像边缘时扩充部分可以涵盖整个结构元素。
2 二值图像形态学基本操作
2.1 腐蚀操作
结构元素B全部位于A中对应的位移量z,故会削弱边界
效果:
是一种收缩或细化的操作。
2.1 膨胀操作
结构元素B与A有交集对应的位移量z,故会扩张边界
效果:
是一种增长或粗化的操作。
注:腐蚀和膨胀是对偶的(即对前景腐蚀后求反=对背景膨胀的结果)
2.3 开操作
先腐蚀后膨胀,平滑物体的轮廓,断开较窄的连接条、消除细的突出物
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-k35subPp-1626615953008)(https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/0959bcda0e5c49a6801fd0c7c94e20e7~tplv-k3u1fbpfcp-zoom-1.image)]
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;
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 209
- 210
- 211
- 212
- 213
- 214
- 215
- 216
- 217
- 218
- 219
- 220
- 221
- 222
- 223
- 224
- 225
- 226
- 227
- 228
- 229
- 230
- 231
- 232
- 233
- 234
- 235
- 236
- 237
- 238
- 239
- 240
- 241
- 242
- 243
- 244
- 245
- 246
- 247
- 248
- 249
- 250
- 251
- 252
- 253
- 254
- 255
- 256
- 257
- 258
- 259
- 260
- 261
- 262
- 263
- 264
- 265
- 266
- 267
- 268
- 269
- 270
- 271
- 272
- 273
- 274
- 275
- 276
- 277
- 278
- 279
- 280
- 281
- 282
- 283
- 284
- 285
- 286
- 287
- 288
- 289
- 290
- 291
- 292
- 293
- 294
- 295
- 296
- 297
- 298
- 299
- 300
- 301
- 302
- 303
- 304
- 305
- 306
- 307
- 308
- 309
- 310
- 311
- 312
- 313
- 314
- 315
- 316
- 317
- 318
- 319
- 320
- 321
- 322
- 323
- 324
- 325
- 326
- 327
- 328
- 329
- 330
- 331
- 332
- 333
- 334
- 335
- 336
- 337
- 338
- 339
- 340
- 341
- 342
- 343
- 344
- 345
- 346
- 347
- 348
- 349
- 350
- 351
- 352
- 353
- 354
- 355
- 356
- 357
- 358
- 359
- 360
- 361
- 362
- 363
- 364
- 365
- 366
- 367
三、运行结果
四、matlab版本及参考文献
1 matlab版本
2014a
2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.
文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。
原文链接:qq912100926.blog.csdn.net/article/details/120048496
- 点赞
- 收藏
- 关注作者
评论(0)