【水果识别】基于matlab GUI 柑橘质量检测及分级系统【含Matlab源码 738期】

511 阅读3分钟

一、简介

现在商业行为中,在水果出厂前都需要进行质量检测,需要将不同等级的水果进行分级包装,以保证商业利益最大化。可是传统方法都是依靠人工进行检测,效率低下,主观成分大,并不能很好客观地评价出货质量,导致工厂损失利益,增加客户投诉,从而造成品牌效率损失,造成隐形的损失。
该课题为基于MATLAB的水果分级系统。适用圆形水果,如苹果,橘子,柚子,柿子等,统计水果图片的面积,圆形度和色泽等多参数进行评价。该设计带一个GUI界面,通过设置直径和色泽,测试水果的这些参数,从而得出该水果所属等级。

二、源代码

function varargout = orangetest(varargin)
% ORANGETEST MATLAB code for orangetest.fig
%      ORANGETEST, by itself, creates a new ORANGETEST or raises the existing
%      singleton*.
%
%      H = ORANGETEST returns the handle to a new ORANGETEST or the handle to
%      the existing singleton*.
%
%      ORANGETEST('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ORANGETEST.M with the given input arguments.
%
%      ORANGETEST('Property','Value',...) creates a new ORANGETEST or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before orangetest_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to orangetest_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 orangetest

% Last Modified by GUIDE v2.5 09-Apr-2021 12:28:30

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
    'gui_Singleton',  gui_Singleton, ...
    'gui_OpeningFcn', @orangetest_OpeningFcn, ...
    'gui_OutputFcn',  @orangetest_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 orangetest is made visible.
function orangetest_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 orangetest (see VARARGIN)

% Choose default command line output for orangetest
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes orangetest wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = orangetest_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 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)
[filename,pathname]=uigetfile({'*.*';'*.bmp';'*.jpg';'*.tif';'*.jpg'},'选择图像');

image=[pathname,filename];%合成路径+文件名

im=imread(image);%读取图像

im=im2double(im);

axes(handles.axes1);

imshow(im);%在坐标axes1显示原图像

title('原始图像');

handles.X1=im;

guidata(hObject,handles);


% --- 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
Val=get(hObject,'Value')

strl=get(hObject,'string')

switch strl{Val};
    
    case '   面积'
        
        
        x1=handles.X1;
        
        w=rgb2gray(x1);
        
        L=medfilt2(w);
        
        level=graythresh(L);
        
        bw=im2bw(L,level);
        
        bw=imfill(~bw,'holes');
        
        axes(handles.axes2);
        
        imshow(bw);
        
        title('面积图像');
        
        strNC=[num2str(bwarea(bw))];
        
        set(handles.text9,'string',strNC);
        strNC1=('一级果');
        strNC2=('二级果');
        strNC3=('三级果');
        if bwarea(bw)>30000
            set(handles.text14,'string',strNC1);
        else if bwarea(bw)>1000&&bwarea(bw)<30000
                set(handles.text14,'string',strNC2);
            else
                set(handles.text14 ,'string',strNC3);
            end
        end
        
        %     strNC1=('无');
        
        %     set(handles.text14,'string',strNC1);
    case'   颜色'
        
        
        x1=handles.X1;
        
        hv=rgb2hsv(x1);
        
        H=hv(:,:,1);
        
        S=hv(:,:,2);
        
        V=hv(:,:,3);
        
        axes(handles.axes3);
        
        imshow(H)
        
        title('H分量图像');
        
        level=graythresh(x1);
        
        apple=im2bw(x1,level);
        
        count=length(x1);
        
        for i=1:count
            
            red_ratio=length(find((H>1/24 & H<3/24) | H>21/24 & H<23/24))/length(find(apple==1));%%%% 
            
        end
        r=red_ratio;
        strNC=[num2str(red_ratio*100),'%'];
        
        set(handles.text10,'string',strNC);
        
        strNC1=('三级果');
        strNC2=('二级果');
        strNC3=('一级果');
        if r>1
            set(handles.text15,'string',strNC1);
        else if r>0.8&&r<1
                set(handles.text15,'string',strNC2);
            else
                set(handles.text15 ,'string',strNC3);
            end
        end
    case'   圆形度'
        
        
        x1=handles.X1;
        
        I2=rgb2gray(x1);
        
        J=im2bw(I2,0.75);
        
        I=~J;
        

三、运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、备注

版本:2014a