【水果识别】基于matlab GUI形态学水果识别【含Matlab源码 1364期】
一、获取代码方式
获取代码方式1:
完整代码已上传我的资源:【水果识别】基于matlab GUI形态学水果识别【含Matlab源码 1364期】
获取代码方式2:
通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。
备注:订阅紫极神光博客付费专栏,可免费获得1份代码(有效期为订阅日起,三天内有效);
二、形态学简介
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 = the4th(varargin)
%========filename:the4th.m============%
%=========水果图像的模式识别== =====%
%==========include files:the4th.m===================%
%========================exam.m===================%
%========================exam1.m==================%
%========================do.m=====================%
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @the4th_OpeningFcn, ...
'gui_OutputFcn', @the4th_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 the4th is made visible.
function the4th_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 the4th (see VARARGIN)
% Choose default command line output for the4th
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes the4th wait for user response (see UIRESUME)
% uiwait(handles.figure1);
clc
clear
% --- Outputs from this function are returned to the command line.
function varargout = the4th_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;
% --------------------------------------------------------------------
function menu_1_Callback(hObject, eventdata, handles)
% hObject handle to menu_1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global fea testimg mask;
% --------------------------------------------------------------------
function menu_2_Callback(hObject, eventdata, handles)
% hObject handle to menu_2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menu_3_Callback(hObject, eventdata, handles)
% hObject handle to menu_3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menu_4_Callback(hObject, eventdata, handles)
% hObject handle to menu_4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menu_5_Callback(hObject, eventdata, handles)
% hObject handle to menu_5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menu_6_Callback(hObject, eventdata, handles)
% hObject handle to menu_6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
% --------------------------------------------------------------------
function menu_61_Callback(hObject, eventdata, handles)
% hObject handle to menu_61 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clear
% 读入样本图像
img1 = imread('梨.jpg');
bwpic = RGB2bw(img1);
samfea(1,:) = get_features(img1, bwpic);
img2 = imread('青椒.jpg');
bwpic = RGB2bw(img2);
samfea(2,:) = get_features(img2, bwpic);
img3 = imread('西红柿.jpg');
bwpic = RGB2bw(img3);
samfea(3,:) = get_features(img3, bwpic);
img4 = imread('香蕉.jpg');
bwpic = RGB2bw(img4);
samfea(4,:) = get_features(img4, bwpic);
%读入测试图像
testimg = imread('test.jpg');
% 测试图像二值化
bwpic = RGB2bw(testimg);
%figure, imshow(testimg)
% 分离各个目标
L = bwlabel(bwpic,4);
mask = zeros(size(bwpic,1),size(bwpic,2), 4);
for i=1:4
Tmask = zeros(size(bwpic,1),size(bwpic,2));
[r, c] = find(L==i);
for j=1:length(r)
Tmask(r(j), c(j)) = 1;
end
%figure, image(Tmask)
mask(:,:,i) = Tmask;
end
% 特征提取
for i=1:4
testfea(i,:) = get_features(testimg, mask(:,:,i));
end
name = {'梨', '青椒', '西红柿', '香蕉'};
% 识别
index = recognition(testfea, samfea);
% 显示结果
%figure
imshow(testimg)
hold on
for i=1:4
R = regionprops(mask(:,:,i),'boundingbox' );
Rect = R.BoundingBox;
if i==3
text(Rect(1)+Rect(3),Rect(2), name(index(i)-1))
else
text(Rect(1)+Rect(3),Rect(2), name(index(i)))
end
end
% --------------------------------------------------------------------
function menu_62_Callback(hObject, eventdata, handles)
% hObject handle to menu_62 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
close
% --------------------------------------------------------------------
function menu_51_Callback(hObject, eventdata, handles)
% hObject handle to menu_51 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global samfea
img1 = imread('梨.jpg');
bwimg = RGB2bw(img1);
samfea(1,:) = get_features(img1, bwimg);
img2 = imread('青椒.jpg');
bwimg = RGB2bw(img2);
samfea(2,:) = get_features(img2, bwimg);
img3 = imread('西红柿.jpg');
bwimg = RGB2bw(img3);
samfea(3,:) = get_features(img3, bwimg);
img4 = imread('香蕉.jpg');
bwimg = RGB2bw(img4);
samfea(4,:) = get_features(img4, bwimg);
samfea
% --------------------------------------------------------------------
function menu_52_Callback(hObject, eventdata, handles)
% hObject handle to menu_52 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global testimg mask testfea
for i=1:4
testfea(i,:) = get_features(testimg, mask(:,:,i));
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
四、运行结果
五、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/120616545
- 点赞
- 收藏
- 关注作者
评论(0)