【水果识别】基于matlab GUI形态学水果识别【含Matlab源码 1364期】

1,206 阅读6分钟

一、形态学简介

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 = 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

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本 2014a

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