一、形态学缺陷检测简介

1 图像分析及预处理
拍摄图像会产生随机的扰动,图像有一定的噪声,为消除掉图像中的无关信息,对图像进行预处理。

1.1 灰度化
为降低运算量,需要将拍摄的三通道的RGB图像转化为单通道的灰度图像。采用加权平均法的灰度化方法,其中心理学灰度公式根据人眼对RGB三色的敏感程度选择不同的权重:

式(1)中,R、G、B分别为RGB三通道灰度值,灰度化结果如图1 (a)所示。

1.2 平滑处理
为了尽可能避免将背景当作缺陷,需要对图像进行平滑处理,这样虽然会使缺陷的边界模糊,但是有利于减少背景的干扰。注意所采用的去噪处理为均值滤波,均值滤波公式为:

式(2)中,m、n分别为所选择的滤波核的长和宽,Sxy是以(x,y)为中心的滤波核对应像素的位置集合,平滑处理结果如图1 (b)所示。均值滤波的缺点是会使一些细节如边缘等信息丢失,因此在找到种子点后,对没有经过平滑处理的图像进行区域生长,找到缺陷边界。

2 算法原理
2.1 阈值分割

阈值分割是图像分割中最简单、基础的方法,性能比较稳定,计算量较小,运算速度快;它主要有全局阈值分割、局部阈值分割、自适应阈值分割等方法。阈值算法基于阈值T,将像素灰度值大于阈值T和小于阈值T的部分分别叫做前景和背景。变换函数表达式:

图1 均值滤波处理

式(3)中,T为阈值,g (x,y)为原图像像素点(x,y)的灰度值,f (x,y)为分割后图像像素点(x,y)的灰度值,阈值分割结果如图2所示。

图2 阈值分割结果

2.2 形态学开运算降噪
数学形态学简称形态学,其处理方式为领域运算,即把领域结构元素与图像对应位置像素进行逻辑运算,这种运算的影响因素主要有结构元素大小、形状和逻辑运算的规则。形态学操作主要有膨胀、腐蚀、梯度运算、礼帽运算、黑帽运算、开运算和闭运算等,但其基础为腐蚀和膨胀,利用膨胀和腐蚀就能完成不同形式的运算。

腐蚀运算能消除轮廓边界点,使边界向内缩小,主要用于细化二值图像目标轮廓、去除噪声等。

式(4)中,A为原图像,B为结构元素。首先给结构元素B定义一个原点,当结构元素B的原点移动到图像A的(x,y)上时,如果结构元素B上等于1的像素点对应图像A也等于1,则将图像A的(x,y)的灰度值置为1,否则置为0,腐蚀示意图如图3所示。

图3 腐蚀示意图
膨胀运算则与腐蚀运算相反,使边界向外扩张,主要用于填补图像分割后的空白,使相近的不相连的轮廓相连。其公式为:

式(5)中,A为原图像,B为结构元素。首先给结构元素B定义一个原点,当结构元素B的原点移动到图像A的(x,y)上时,如果结构元素B上等于1的像素点对应图像A中至少有一个等于1,则将图像A的(x,y)的灰度值置为1,否则置为0。

先进行腐蚀操作,然后在腐蚀的基础上进行膨胀操作,主要用于去噪和计数等。其公式为:

式(6)中,A为原图像,B、C为结构元素。开运算效果如图4所示,图5为开运算处理的结果。

图4 开运算效果
2.3 区域生长法
区域生长的思想就是把领域(四领域、八领域等)相同的化为一个区域。首先需要一个种子点作为生长的开始,然后将种子点领域内满足相似准则要求的像素点合并到种子的区域,将这个区域的像素做为种子点,继续进行生长,直到没有符合要求的点,生长结束,所有种子点像素作为生长的区域。分割的好坏由初始种子点和相似准则决定。

图5 形态学开运算结果
2.3.1 种子点选择与检测
经过阈值分割和形态学处理后,将二值图像各轮廓中心作为待定种子点。如果选择的种子点位于缺陷的绝对区域,那么种子点总有一个方向各像素的深度值呈现高-低-高的形态。设计检测模板如图6所示,计算出种子点在0°、45°、90°、135°方向上的深度变化,判断其变化是否呈高低高形态。

图6 检测模板
种子点左右两侧r个像素的灰度平均值分别为:

各方向的灰度变化为:

深度形Si态变化判定:

式(10)中,I (u)为检测模板中第u个像素的灰度值,w=1,2,3,4,分别代表0°,45°,90°,135°方向,mwm为w方向两侧的最小灰度值,T1为形态变化阈值。如果种子点不满足深度形态变化判定,则去除该待定种子点。

2.3.2 生长过程
区域生长的具体流程如下:
(1)将种子点坐标放入种子点集seeds。
(2)顶出种子点集中的一个种子点,对种子点八邻域的像素点进行相似准则判断;满足相似准则条件的点,视为种子点放入种子点集seeds。
(3)将顶出的种子点存入种子集S。
(4)如果种子点集内没有元素,则跳到步骤(4);如果种子点集中还有元素,则跳到步骤(2)。
(5)生成一张和输入图像长宽一致,像素值全为0的图像I。
(6)将图像I中对应种子集S坐标的像素值置为255,得到分割图像I’。
其中生长的相似准则为:

式(11)中,gray (seed)为当轮种子点的灰度值,gray (8_n)为其八邻域各点的像素值,Thresh为设置的阈值。区域生长结果如图7所示。

图7 区域生长结果
3 实验过程
图像分割就是按照预先设定的规则,将图像分割为有意义的前景和背景的过程。区域生长是一个分割效果比较好的算法,但前提是需要找到适合的种子点。单一的分割算法就容易遇到这种不足的情况,结合使用形态学和阈值分割的方法来找到合适的种子点,帮助区域生长算法完成分割任务,达到满足要求的分割效果,分割方法流程如图8所示。

图8 分割流程图
首先对输入的图片进行灰度化处理,变成单通道的灰度图片,然后滤波去除噪声,使图像更平滑,选择合适的阈值进行阈值分割,在利用开运算去除掉分割后较小的前景,以各前景区域的中心点为起始种子点,进行区域生长,得到最终所满足要求的前景。

二、部分源代码

function varargout = fruit_identify(varargin)
% FRUIT_IDENTIFY M-file for fruit_identify.fig
%      FRUIT_IDENTIFY, by itself, creates a new FRUIT_IDENTIFY or raises the existing
%      singleton*.
%
%      H = FRUIT_IDENTIFY returns the handle to a new FRUIT_IDENTIFY or the handle to
%      the existing singleton*.
%
%      FRUIT_IDENTIFY('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in FRUIT_IDENTIFY.M with the given input arguments.
%
%      FRUIT_IDENTIFY('Property','Value',...) creates a new FRUIT_IDENTIFY or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before fruit_identify_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to fruit_identify_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 fruit_identify% Last Modified by GUIDE v2.5 22-May-2021 10:27:49% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...'gui_Singleton',  gui_Singleton, ...'gui_OpeningFcn', @fruit_identify_OpeningFcn, ...'gui_OutputFcn',  @fruit_identify_OutputFcn, ...'gui_LayoutFcn',  [] , ...'gui_Callback',   []);
if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});
endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
elsegui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT% --- Executes just before fruit_identify is made visible.
function fruit_identify_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 fruit_identify (see VARARGIN)% Choose default command line output for fruit_identify
handles.output = hObject;
im=imread('pic.jpg');
axes(handles.axes1)
imshow(im)% Update handles structure
guidata(hObject, handles);% UIWAIT makes fruit_identify wait for user response (see UIRESUME)
% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.
function varargout = fruit_identify_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 popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu1% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu1 (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');
endfunction edit1_Callback(hObject, eventdata, handles)
% hObject    handle to edit1 (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 edit1 as text
%        str2double(get(hObject,'String')) returns contents of edit1 as a double% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit1 (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');
endfunction 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');
endfunction 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');
endfunction 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');
endfunction edit5_Callback(hObject, eventdata, handles)
% hObject    handle to edit5 (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 edit5 as text
%        str2double(get(hObject,'String')) returns contents of edit5 as a double% --- Executes during object creation, after setting all properties.
function edit5_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit5 (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

三、运行结果


四、matlab版本及参考文献

1 matlab版本
2014a

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

【水果识别】基于matlab GUI形态学水果大小识别【含Matlab源码 920期】相关推荐

  1. 【运动学】基于matlab GUI三体运动模拟【含Matlab源码 871期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[运动学]基于matlab GUI三体运动模拟[含Matlab源码 871期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: ...

  2. 【音乐播放】基于matlab GUI动感音乐播放【含Matlab源码 778期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[音乐播放]基于matlab GUI动感音乐播放[含Matlab源码 778期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: ...

  3. 【雷达通信】基于matlab GUI相控阵雷达方向图【含Matlab源码 1048期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[雷达通信]基于matlab GUI相控阵雷达方向图[含Matlab源码 1048期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方 ...

  4. 【数据分析】基于matlab GUI齿轮箱振动数据分析【含Matlab源码 2122期】

    一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[数据分析]基于matlab GUI齿轮箱振动数据分析[含Matlab源码 2122期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方 ...

  5. 【光学】基于matlab GUI维达尔之眼计算【含Matlab源码 2545期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[光学]基于matlab GUI维达尔之眼计算[含Matlab源码 2545期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: ...

  6. 【光学】基于matlab GUI双孔干涉【含Matlab源码 2119期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[光学]基于matlab GUI双孔干涉[含Matlab源码 2119期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2: 付费 ...

  7. 【光学】基于matlab GUI双缝干涉和牛顿环【含Matlab源码 2165期】

    ⛄一.获取代码方式 获取代码方式1: 完整代码已上传我的资源:[光学]基于matlab GUI双缝干涉和牛顿环[含Matlab源码 2165期] 点击上面蓝色字体,直接付费下载,即可. 获取代码方式2 ...

  8. Matlab GUI/APP 浅谈(附计算器源码)

    Matlab GUI/APP 浅谈(附计算器源码) 今天没有什么段子,也没有心灵鸡汤.毒鸡汤啥的,纯粹聊一聊这些年从有关MATLAB GUI/APP开发中悟出的一点道理,顺便把计算器的源代码给大家. ...

  9. 【课程设计】基于java GUI实现学生个人信息管理系统(源码+论文+ppt+视频)

    源码资料 免费下载 不经常在线,需要源码和资料的留言私信我,主页有联系方式 技术架构 开发语言 主要用的是Java语言中的GUI(图形用户界面)和AWT(抽象窗口工具包)编程. (1) GUI 图形用 ...

  10. 【身份证识别】基于matlab GUI形态学二代身份证识别系统【含Matlab源码 947期】

    ⛄一.身份证号码识别简介 1 引言 作为居民身份的象征,身份证是居民身份的唯一标识,它已成为生活中必不可少的证件.在火车站.酒吧等公共场所,流动人口大人员复杂,警察需要对公民的身份证进行核对,排除可疑 ...

最新文章

  1. 2019微生物组—宏基因组分析专题研讨会
  2. dubbo启动时检查服务
  3. java装箱和拆箱的意义_java的自动拆箱和装箱是每个程序员都要知道的
  4. python批量下载b站_python 批量下载bilibili视频的gui程序
  5. 力扣-389 找不同
  6. ros ubuntu 卸载_Ubuntu18.04 ROS Melodic安装与卸载
  7. 【图像配准】基于surf算法实现图像配准附Matlab代码
  8. xp系统能安装32位的python吗-32位XP系统 Python3.4.4安装scrapy1.6.0
  9. java 自动登录_java代码实现自动登录功能
  10. WORD2010文档里面突然不能输入汉字的解决办法
  11. 苹果手机怎么下载铃声
  12. 再聊聊Python中文社区的翻译
  13. 背包九讲之二:完全背包问题
  14. 【误差】方差、标准差、均方误差和均方根误差的区别总结
  15. Sumo+Veins+OMNeT仿真调试记录
  16. 逆向学习:破解一个小程序
  17. java刘备猜拳游戏类_基于java实现人机猜拳游戏
  18. 让欺诈风险无处遁形的计算机视觉
  19. AEJoy —— 表达式之无缝循环摆/扭动【JS】
  20. 中国SEO可持续性发展问题

热门文章

  1. python提示AttributeError: 'NoneType' object has no attribute 'append'
  2. 双缓冲-- double framebuffer
  3. Golang操作数据库
  4. 2020-12-28 learning opencv3: 十:Smoothing
  5. Atitit 未来趋势把控的书籍 attilax总结 v3
  6. Atitit. 项目文档目录大纲 总集合  v2
  7. Atitit.antlr实现词法分析
  8. atitit.网络文件访问协议.unc smb nfs ftp http的区别
  9. atitit.html5 vs 原生 app的区别与选择
  10. paip. java的 函数式编程 大法