当前位置 : 主页 > 编程语言 > python >

【水果识别】基于计算机视觉实现水果识别含Matlab源码

来源:互联网 收集:自由互联 发布时间:2022-06-15
1 简介 自“农业4.0”时代的来临,以“互联网+”为驱动的农业技术已成为发展农业强有力的支撑。在果蔬业中,果蔬分类通常由经过训练的人员人工评估农产品或农作物的质量。但是,


 1 简介

自“农业 4.0”时代的来临,以“互联网+”为驱动的农业技术已成为发展农业强有力的支撑。在果蔬业中,果蔬分类通常由经过训练的人员人工评估农产品或农作物的质量。但是,人工分类会带来许多相关的限制,工作人员需要熟悉果蔬的许多特征,并且高强度的机械性工作带来的失误不可避 免,因而分类技术的提高是农产品质量提升的关键。计算机视觉技术具有效率高、非接触、抗干扰能力强等优点,在多个领域已经广泛应用,能实现农产品快速无损检测的要求。果蔬分级效率的提升对我国产品质量提升、农民增产增收具有重要意义。 1 图像处理技术在果蔬分类领域的应用本文对基于计算机视觉技术的果蔬分类进行了比较调查,发现研究人员多使用一种或者多种传感器或者机器学习技术进行农产品的分类与分级,但是由于相同水果品种不同造成形状、大小与颜色的不同,果蔬的分类依然面临着许多挑战。为解决这些问题研究人员已经进行了多种实验,其中结合机器学习技术对于系统有明显提升。通过使用传感器捕获水果与蔬菜的特征,使用机器学习对任务进行优化,以提高系统整体性能。

2 部分代码

function varargout = fruit(varargin)% FRUIT MATLAB code for fruit.fig% FRUIT, by itself, creates a new FRUIT or raises the existing% singleton*.%% H = FRUIT returns the handle to a new FRUIT or the handle to% the existing singleton*.%% FRUIT('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in FRUIT.M with the given input arguments.%% FRUIT('Property','Value',...) creates a new FRUIT or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before fruit_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to fruit_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% Last Modified by GUIDE v2.5 07-May-2015 11:37:32% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @fruit_OpeningFcn, ... 'gui_OutputFcn', @fruit_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{:});else gui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before fruit is made visible.function fruit_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 (see VARARGIN)set(handles.axes1,'xtick',[]);set(handles.axes1,'ytick',[]);set(handles.axes1,'box','on');set(handles.axes2,'xtick',[]);set(handles.axes2,'ytick',[]);set(handles.axes2,'box','on');% Choose default command line output for fruithandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes fruit wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = fruit_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 structurevarargout{1} = handles.output;% --- Executes on button press in pushbutton1.function pushbutton1_Callback(hObject, eventdata, handles)% hObject handle to pushbutton1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)str=uigetfile({'*.gif';'*.png';'*.bmp';'*.jpg'},'读取图像');%str=handles.b;I = imread(str);axes(handles.axes1)%在axes1上作图imshow(I);handles.a=I;guidata(hObject,handles)% --- Executes on button press in pushbutton2.function pushbutton2_Callback(hObject, eventdata, handles)% hObject handle to pushbutton2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)img=handles.a;R=img(:,:,1); %获取彩色图像的r值。G=img(:,:,2); %获取彩色图像的g值。B=img(:,:,3); %获取彩色图像的b值。I2=rgb2gray(img); %将图像灰度化。%figure,imshow(I2),title('灰度图像');BW=im2bw(I2,0.9); %将图像二值化。SE=strel('rectangle',[40 30]); % 结构定义J2=imopen(BW,SE); axes(handles.axes2)% 进行开运算去除噪声和平滑边界imshow(J2);handles.b=J2;guidata(hObject,handles)% --- Executes on button press in pushbutton3.function pushbutton3_Callback(hObject, eventdata, handles)% hObject handle to pushbutton3 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)J2=handles.b;img=handles.a;SE=strel('square',3); % 定义3×3腐蚀结构元素J=imerode(~J2,SE); %对图像进行腐蚀操作。BW2=(~J2)-J; % 检测边缘%填充了已有的检测的连续形状边界B = imfill(BW2,'holes'); %对图像填充孔洞。B = bwmorph(B,'remove'); %获得图像中区域边界。%将不同的图形进行分别标记,num表示连接的图形对象的个数[Label,num] = bwlabel(B); %进行标记。for i = 1 : num Area(i) = 0;endLabel = imfill(Label,'holes'); %填充打过标记的边界线中间围成的图形区域%计算各个图像的hsv颜色(色度)HSV = rgb2hsv(img); %转换为HSV颜色模型。[row,col] = size(Label); %统计填充后的图形中各块图形所含像素的个数的多少MeanHue = zeros(1,num); %初始化 for i = 1 : num Hue = zeros(Area(i),1); %初始化 nPoint = 0; %初始化 for j = 1 : row for k = 1 : col if(Label(j,k) == i) nPoint = nPoint + 1; %对于是连通区域中的点npoint+1. Hue(nPoint,1) = HSV(j,k,1); %把hsv的值赋给Hue数组。 end end end Hue(:,i) = sort(Hue(:,1)); for j = floor(nPoint*0.1) : floor(nPoint*0.9) MeanHue(i) = MeanHue(i) + Hue(j,1); %将hsv(i)的值赋给MeanHue(i) end MeanHue(i) = MeanHue(i) / (0.8*nPoint); %计算出平均的色度值 end%调用regionprops函数获得各个联通区域的属性值(中心点坐标,外接椭圆的长短轴长度,面积)。[L,num]=bwlabel(BW2); %重新进行区域标记。stats= regionprops(L, 'ALL'); %调用regionprops函数。for i= 1:numlongth(i)=stats(i).MajorAxisLength; %获得外接椭圆的长轴长度width(i)=stats(i).MinorAxisLength; %获得外接椭圆的短轴长度end%d in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)

3 仿真结果

【水果识别】基于计算机视觉实现水果识别含Matlab源码_ide

【水果识别】基于计算机视觉实现水果识别含Matlab源码_ide_02编辑


【水果识别】基于计算机视觉实现水果识别含Matlab源码_ide_03

【水果识别】基于计算机视觉实现水果识别含Matlab源码_ide_04编辑


【水果识别】基于计算机视觉实现水果识别含Matlab源码_计算机视觉_05

【水果识别】基于计算机视觉实现水果识别含Matlab源码_彩色图像_06编辑


4 参考文献

[1]朱枫. 基于计算机视觉的果蔬识别技术的研究[D]. 东南大学, 2014.

博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。

部分理论引用网络文献,若有侵权联系博主删除。

​​5 代码下载​​



上一篇:pytest断言 pytest使用xfail处理失败
下一篇:没有了
网友评论