基于MATALB的植物病虫害识别系统设计与实现(已完结)
(随便看看就好,主要是课设结束了电脑没空间了舍不得删)
本篇博客主要是有下面两个方面的内容:
一、App Designer工具设计交互界面
二、MATLAB深度学习模块中VGG-19模型的使用
目录
一、App Designer工具使用
1.创建新的APP
2.设置背景图和初始化相关组件
3.登录页面组件的相关代码
4.修改密码页面组件的相关代码
5.注册页面组件的相关代码
6.系统主页面控件代码
二、深度学习VGG-19模型
1.VGG-19模型的查找与应用
2.网络模型MATLAB代码段
三、最后一点补充
1.关于标签转换的一些简单代码样例
2.关于图片预处理的一些简单样例
(MATLAB R2021a)
一、App Designer工具使用
1.创建新的App(简单的操作我就不多说了)
登录界面
注册用户界面
密码修改界面
主页面
App Designer工具一般情况下只有给加入的组件添加了回调函数才能编辑代码界面,以下直接贴相关控件的代码。
2.设置背景图和初始化相关组件
% Code that executes after component creation
function startupFcn(app)im =axes('units','normalized','pos',[0 0 1 1]); i = imread('*.jpg'); %随便寻找一张图片image(app.UIAxes,i); %在坐标区显示背景set(im,'handlevisibility','off','visible','off'); app.*.Value=''; %初始化/清空账号框app.*.Value=''; %初始化/清空密码框
end% Button down function: UIAxes
function UIAxesButtonDown(app, event) %设坐标区回调来固定背景图片%啥也不用写
end
3.登录页面组件的相关代码
% Button pushed function: *
function *ButtonPushed(app, event) %“登录”功能global *; %账户框数据全局变量global *; %密码框数据全局变量 if isempty(app.*.Value)errordlg('用户名或密码错误','错误提醒','modal');app.*.Value='';app.*.Value='';elseif isempty(app.*.Value)errordlg('用户名或密码错误','错误提醒','modal');app.*.Value='';app.*.Value='';elseif(*=="******" && *=="******") %设置登录账号密码delete(gcf);*; %跳转至主页面elseif ~isempty(app.*..Value)[~,loginname] = xlsread('*.xls','B:B'); %调用账号Excel表[~,logincode] = xlsread('*.xls','B:B'); %调用密码Excel表[~,A] = xlsread('*.xls','A:A'); %读表头 if ~iscell(A)B = maxk(A,1);elsemiu = cell2mat(A);X = str2num(miu); %#ok<ST2NM>B = maxk(X,1);endfor a = 1:Blname = string(loginname(a));lcode = string(logincode(a));if (*==lname && *==lcode)delete(gcf);*; %跳转至主页面break;endif a == Berrordlg('用户名或密码错误','错误提醒','modal');app.*.Value='';app.*.Value='';endendelseerrordlg('用户名或密码错误','错误提醒','modal');app.*.Value='';app.*.Value='';end
end% Value changed function: * %账号框数据获取
function *ValueChanged(app, event)global *;* = app.*.Value;
end% Value changed function: * %密码框数据获取
function *ValueChanged(app, event)global *;* = app.*.Value;
end% Button pushed function: *
function *ButtonPushed(app, event) %注册功能if(app.*.Value=="******" && app.*.Value=="******") %设置管理员账号密码*; %跳转至注册界面elseif ~isempty(app.*.Value)errordlg('请输入正确的管理员账户以继续','账户注册指引');app.*.Value='';app.*.Value='';elseif ~isempty(app.*.Value)errordlg('请输入正确的管理员账户以继续','账户注册指引');app.*.Value='';app.*.Value='';elsehelpdlg('请输入管理员账户以继续','账户注册指引');app.*.Value='';app.*.Value='';end
end% Button pushed function: * %修改密码功能
function *ButtonPushed(app, event)*; %跳转至密码修改界面
end
tips:最近更新 2022.05.13
系统登录界面代码详细
(看个大概,大部分是不需要操作的,需要自己添加的已经在上面演示了)
classdef test < matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access = public)UIFigure matlab.ui.FigureLabel_3 matlab.ui.control.LabelLabel_2 matlab.ui.control.LabelLabel matlab.ui.control.Labelalter matlab.ui.control.Buttonsignup matlab.ui.control.Buttonmake matlab.ui.control.Buttoncode matlab.ui.control.EditFieldname matlab.ui.control.EditFieldUIAxes matlab.ui.control.UIAxesend% Callbacks that handle component eventsmethods (Access = private)% Code that executes after component creationfunction startupFcn(app)im =axes('units','normalized','pos',[0 0 1 1]);i = imread('plants.jpg');image(app.UIAxes,i);set(im,'handlevisibility','off','visible','off');app.code.Value='';app.name.Value='';end% Button down function: UIAxesfunction UIAxesButtonDown(app, event)end% Button pushed function: signupfunction signupButtonPushed(app, event)global namevalue;global codevalue;if isempty(app.name.Value)errordlg('用户名或密码错误','错误提醒','modal');app.code.Value='';app.name.Value='';elseif isempty(app.code.Value)errordlg('用户名或密码错误','错误提醒','modal');app.code.Value='';app.name.Value='';elseif(namevalue=="wyzn1901" && codevalue=="06193034")delete(gcf);app1;elseif ~isempty(app.name.Value)[~,loginname] = xlsread('lname.xls','B:B');[~,logincode] = xlsread('lcode.xls','B:B');[~,A] = xlsread('lcode.xls','A:A');if ~iscell(A)B = maxk(A,1);elsemiu = cell2mat(A); X = str2num(miu); %#ok<ST2NM> B = maxk(X,1);endfor a = 1:Blname = string(loginname(a));lcode = string(logincode(a));if (namevalue==lname && codevalue==lcode)delete(gcf);app1;break;endif a == Berrordlg('用户名或密码错误','错误提醒','modal');app.code.Value='';app.name.Value='';endendelseerrordlg('用户名或密码错误','错误提醒','modal');app.code.Value='';app.name.Value='';endend% Value changed function: namefunction nameValueChanged(app, event)global namevalue;namevalue = app.name.Value;end% Value changed function: codefunction codeValueChanged(app, event)global codevalue;codevalue = app.code.Value;end% Close request function: UIFigurefunction UIFigureCloseRequest(app, event)selection = questdlg('确定要退出登录系统吗?','关闭提醒','是','否','是');switch selectioncase '是'delete(gcf);case '否'return;endend% Button pushed function: makefunction makeButtonPushed(app, event)if(app.name.Value=="admin123" && app.code.Value=="abc123")regester;elseif ~isempty(app.name.Value)errordlg('请输入正确的管理员账户以继续','账户注册指引');app.code.Value='';app.name.Value='';elseif ~isempty(app.code.Value)errordlg('请输入正确的管理员账户以继续','账户注册指引');app.code.Value='';app.name.Value='';elseuiwait(helpdlg('请输入管理员账户以继续','账户注册指引'));app.code.Value='';app.name.Value='';endend% Button pushed function: alterfunction alterButtonPushed(app, event)altercode;endend% Component initializationmethods (Access = private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and hide until all components are createdapp.UIFigure = uifigure('Visible', 'off');app.UIFigure.AutoResizeChildren = 'off';app.UIFigure.Position = [500 300 590 435];app.UIFigure.Name = '植物病虫害识别系统';app.UIFigure.Resize = 'off';app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);app.UIFigure.HandleVisibility = 'on';% Create UIAxesapp.UIAxes = uiaxes(app.UIFigure);app.UIAxes.Toolbar.Visible = 'off';app.UIAxes.XTickLabel = '';app.UIAxes.YTickLabel = '';app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);app.UIAxes.BusyAction = 'cancel';app.UIAxes.Interruptible = 'off';app.UIAxes.HitTest = 'off';app.UIAxes.PickableParts = 'all';app.UIAxes.Position = [-10 -47 627 498];% Create nameapp.name = uieditfield(app.UIFigure, 'text');app.name.ValueChangedFcn = createCallbackFcn(app, @nameValueChanged, true);app.name.Position = [255 240 170 39];% Create codeapp.code = uieditfield(app.UIFigure, 'text');app.code.ValueChangedFcn = createCallbackFcn(app, @codeValueChanged, true);app.code.Tag = 'code';app.code.Position = [255 153 170 39];% Create makeapp.make = uibutton(app.UIFigure, 'push');app.make.ButtonPushedFcn = createCallbackFcn(app, @makeButtonPushed, true);app.make.Tag = 'make';app.make.FontWeight = 'bold';app.make.Position = [37 39 139 46];app.make.Text = '注册账户';% Create signupapp.signup = uibutton(app.UIFigure, 'push');app.signup.ButtonPushedFcn = createCallbackFcn(app, @signupButtonPushed, true);app.signup.Tag = 'sign';app.signup.FontWeight = 'bold';app.signup.Position = [228 39 139 46];app.signup.Text = '登录';% Create alterapp.alter = uibutton(app.UIFigure, 'push');app.alter.ButtonPushedFcn = createCallbackFcn(app, @alterButtonPushed, true);app.alter.Tag = 'alter';app.alter.FontWeight = 'bold';app.alter.Position = [423 39 139 46];app.alter.Text = '修改密码';% Create Labelapp.Label = uilabel(app.UIFigure);app.Label.FontSize = 18;app.Label.FontWeight = 'bold';app.Label.Position = [176 230 63 59];app.Label.Text = '用户:';% Create Label_2app.Label_2 = uilabel(app.UIFigure);app.Label_2.FontSize = 18;app.Label_2.FontWeight = 'bold';app.Label_2.Position = [176 143 63 59];app.Label_2.Text = '密码:';% Create Label_3app.Label_3 = uilabel(app.UIFigure);app.Label_3.HorizontalAlignment = 'center';app.Label_3.FontName = 'Adobe 仿宋 Std R';app.Label_3.FontSize = 22;app.Label_3.FontWeight = 'bold';app.Label_3.Position = [173 359 249 29];app.Label_3.Text = {'植物病虫害识别系统登录'; ''};% Show the figure after all components are createdapp.UIFigure.Visible = 'on';endend% App creation and deletionmethods (Access = public)% Construct appfunction app = testrunningApp = getRunningApp(app);% Check for running singleton appif isempty(runningApp)% Create UIFigure and componentscreateComponents(app)% Register the app with App DesignerregisterApp(app, app.UIFigure)% Execute the startup functionrunStartupFcn(app, @startupFcn)else% Focus the running singleton appfigure(runningApp.UIFigure)app = runningApp;endif nargout == 0clear appendend% Code that executes before app deletionfunction delete(app)% Delete UIFigure when app is deleteddelete(app.UIFigure)endend
end
4.修改密码页面组件的相关代码
function startupFcn(app)%空白操作警告fig = app.UIFigure;uialert(fig,'请输入用户账号和原始密码以继续', '修改密码指引','CloseFcn','uiresume(fig)')uiwait(fig);end% Value changed function: signnamefunction signnameValueChanged(app, event)%登录名输入框end% Value changed function: signcodefunction signcodeValueChanged(app, event)%登录密码输入框end% Value changed function: changecodefunction changecodeValueChanged(app, event)global value;%密码修改输入框value = app.changecode.Value;end% Button pushed function: confirmfunction confirmButtonPushed(app, event)global value;%确定密码修改控件响应判断函数,通过匹配原有数据判断修改是否有效if isempty(app.signname.Value)errordlg('用户名或密码错误','错误提醒','modal');app.signcode.Value='';app.signname.Value='';elseif isempty(app.signcode.Value)errordlg('用户名或密码错误','错误提醒','modal');app.signcode.Value='';app.signname.Value='';elseif ~isempty(app.signname.Value)[~,loginname] = xlsread('lname.xls','B:B');[~,logincode] = xlsread('lcode.xls','B:B');[~,A] = xlsread('lcode.xls','A:A');if (value==string(app.signcode.Value))helpdlg('密码输入一致','修改密码指引');elseif ~isempty(value)if ~iscell(A)B = maxk(A,1);str = string(zeros(B,2));elsemiu = cell2mat(A); X = str2num(miu); %#ok<ST2NM> B = maxk(X,1);str = string(zeros(B,2));endfor a = 1:Blname = string(loginname(a));lcode = string(logincode(a));if (app.signname.Value==lname && app.signcode.Value==lcode)for b = 1:Bstr(b,1) = double(b);if b==astr(b,2) = string(value);elsestr(b,2) = logincode(b);endendwritematrix(str,'lcode.xls',"WriteMode","overwritesheet");msgbox('密码修改成功',"non-modal");close("MATLAB App");break;endif a == Berrordlg('用户名或密码错误','错误提醒','modal');app.signname.Value = '';app.signcode.Value = '';app.changecode.Value = '';endendelseerrordlg('密码必须不为空','错误提醒');endelseerrordlg('用户名或密码错误','错误提醒','modal');app.code.Value='';app.name.Value='';endendend
修改密码界面代码详细
(看个大概,大部分是不需要操作的,需要自己添加的已经在上面演示了)
classdef altercode < matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access = public)UIFigure matlab.ui.Figuresigncode matlab.ui.control.EditFieldLabel_4 matlab.ui.control.LabelLabel_3 matlab.ui.control.Labelchangecode matlab.ui.control.EditFieldconfirm matlab.ui.control.ButtonLabel_2 matlab.ui.control.LabelLabel matlab.ui.control.Labelsignname matlab.ui.control.EditFieldend% Callbacks that handle component eventsmethods (Access = private)% Code that executes after component creationfunction startupFcn(app)fig = app.UIFigure;uialert(fig,'请输入用户账号和原始密码以继续', '修改密码指引','CloseFcn','uiresume(fig)')uiwait(fig);end% Value changed function: signnamefunction signnameValueChanged(app, event)end% Value changed function: signcodefunction signcodeValueChanged(app, event)end% Value changed function: changecodefunction changecodeValueChanged(app, event)global value;value = app.changecode.Value;end% Button pushed function: confirmfunction confirmButtonPushed(app, event)global value;if isempty(app.signname.Value)errordlg('用户名或密码错误','错误提醒','modal');app.signcode.Value='';app.signname.Value='';elseif isempty(app.signcode.Value)errordlg('用户名或密码错误','错误提醒','modal');app.signcode.Value='';app.signname.Value='';elseif ~isempty(app.signname.Value)[~,loginname] = xlsread('lname.xls','B:B');[~,logincode] = xlsread('lcode.xls','B:B');[~,A] = xlsread('lcode.xls','A:A');if (value==string(app.signcode.Value))helpdlg('密码输入一致','修改密码指引');elseif ~isempty(value)if ~iscell(A)B = maxk(A,1);str = string(zeros(B,2));elsemiu = cell2mat(A); X = str2num(miu); %#ok<ST2NM> B = maxk(X,1);str = string(zeros(B,2));endfor a = 1:Blname = string(loginname(a));lcode = string(logincode(a));if (app.signname.Value==lname && app.signcode.Value==lcode)for b = 1:Bstr(b,1) = double(b);if b==astr(b,2) = string(value);elsestr(b,2) = logincode(b);endendwritematrix(str,'lcode.xls',"WriteMode","overwritesheet");msgbox('密码修改成功',"non-modal");close("MATLAB App");break;endif a == Berrordlg('用户名或密码错误','错误提醒','modal');app.signname.Value = '';app.signcode.Value = '';app.changecode.Value = '';endendelseerrordlg('密码必须不为空','错误提醒');endelseerrordlg('用户名或密码错误','错误提醒','modal');app.code.Value='';app.name.Value='';endendend% Component initializationmethods (Access = private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and hide until all components are createdapp.UIFigure = uifigure('Visible', 'off');app.UIFigure.Position = [500 300 547 480];app.UIFigure.Name = 'MATLAB App';% Create signnameapp.signname = uieditfield(app.UIFigure, 'text');app.signname.ValueChangedFcn = createCallbackFcn(app, @signnameValueChanged, true);app.signname.Position = [225 300 170 39];% Create Labelapp.Label = uilabel(app.UIFigure);app.Label.FontSize = 18;app.Label.FontWeight = 'bold';app.Label.Position = [139 290 113 59];app.Label.Text = '用户:';% Create Label_2app.Label_2 = uilabel(app.UIFigure);app.Label_2.HorizontalAlignment = 'center';app.Label_2.FontName = 'Adobe 仿宋 Std R';app.Label_2.FontSize = 22;app.Label_2.FontWeight = 'bold';app.Label_2.Position = [138 395 295 29];app.Label_2.Text = {'植物病虫害识别系统密码修改'; ''};% Create confirmapp.confirm = uibutton(app.UIFigure, 'push');app.confirm.ButtonPushedFcn = createCallbackFcn(app, @confirmButtonPushed, true);app.confirm.Position = [225 73 122 48];app.confirm.Text = '确认';% Create changecodeapp.changecode = uieditfield(app.UIFigure, 'text');app.changecode.ValueChangedFcn = createCallbackFcn(app, @changecodeValueChanged, true);app.changecode.Tag = 'code';app.changecode.Position = [225 160 170 39];% Create Label_3app.Label_3 = uilabel(app.UIFigure);app.Label_3.FontSize = 18;app.Label_3.FontWeight = 'bold';app.Label_3.Position = [139 150 111 59];app.Label_3.Text = '修改:';% Create Label_4app.Label_4 = uilabel(app.UIFigure);app.Label_4.FontSize = 18;app.Label_4.FontWeight = 'bold';app.Label_4.Position = [139 220 104 59];app.Label_4.Text = '密码:';% Create signcodeapp.signcode = uieditfield(app.UIFigure, 'text');app.signcode.ValueChangedFcn = createCallbackFcn(app, @signcodeValueChanged, true);app.signcode.Tag = 'code';app.signcode.Position = [225 230 170 39];% Show the figure after all components are createdapp.UIFigure.Visible = 'on';endend% App creation and deletionmethods (Access = public)% Construct appfunction app = altercode% Create UIFigure and componentscreateComponents(app)% Register the app with App DesignerregisterApp(app, app.UIFigure)% Execute the startup functionrunStartupFcn(app, @startupFcn)if nargout == 0clear appendend% Code that executes before app deletionfunction delete(app)% Delete UIFigure when app is deleteddelete(app.UIFigure)endend
end
5.注册页面组件的相关代码
% Code that executes after component creationfunction startupFcn(app)global value1;global value2;global value3;%初始化value1 = '';value2 = '';value3 = '';end% Value changed function: signnamefunction signnameValueChanged(app, event)global value1;%用户名输入value1 = app.signname.Value;end% Value changed function: signcodefunction signcodeValueChanged(app, event)global value2;%密码输入value2 = app.signcode.Value;end% Value changed function: signcodeagainfunction signcodeagainValueChanged(app, event)global value3;%确认密码输入value3 = app.signcodeagain.Value;end% Button pushed function: confirmfunction confirmButtonPushed(app, event)global value1;global value2;global value3;%检查新用户账号密码注册是否符合规范[~,~] = xlsread('lname.xls');[~,~] = xlsread('lcode.xls');[~,A] = xlsread('lcode.xls','A:A');if ~iscell(A)B = maxk(A,1);elsemiu = cell2mat(A);X = str2num(miu); %#ok<ST2NM>B = maxk(X,1);endx = B+1;if ~isempty(value1)if ~isempty(value2)if (value2==value3)v1 = {x,string(value1)};v2 = {string(x),string(value2)};writecell(v1,'lname.xls',"WriteMode","append");writecell(v2,'lcode.xls','WriteMode','append');app.signname.Value = '';app.signcode.Value = '';app.signcodeagain.Value = '';uiwait(msgbox('用户创建成功',"non-modal"));delete(gcf);elseerrordlg('密码输入不一致','账户注册指引');endelseerrordlg('密码必须不为空','账户注册指引');endelseif isempty(value1)errordlg('用户名必须不为空','账户注册指引');elseif isempty(value2)errordlg('密码必须不为空','账户注册指引');endend
注册界面代码详细
(看个大概,大部分是不需要操作的,需要自己添加的已经在上面演示了)
classdef regester < matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access = public)UIFigure matlab.ui.Figuresigncode matlab.ui.control.EditFieldLabel_7 matlab.ui.control.LabelLabel_6 matlab.ui.control.Labelsigncodeagain matlab.ui.control.EditFieldconfirm matlab.ui.control.ButtonLabel_5 matlab.ui.control.LabelLabel matlab.ui.control.Labelsignname matlab.ui.control.EditFieldend% Callbacks that handle component eventsmethods (Access = private)% Code that executes after component creationfunction startupFcn(app)global value1;global value2;global value3;value1 = '';value2 = '';value3 = '';end% Value changed function: signnamefunction signnameValueChanged(app, event)global value1;value1 = app.signname.Value;end% Value changed function: signcodefunction signcodeValueChanged(app, event)global value2;value2 = app.signcode.Value;end% Value changed function: signcodeagainfunction signcodeagainValueChanged(app, event)global value3;value3 = app.signcodeagain.Value;end% Button pushed function: confirmfunction confirmButtonPushed(app, event)global value1;global value2;global value3;[~,~] = xlsread('lname.xls');[~,~] = xlsread('lcode.xls');[~,A] = xlsread('lcode.xls','A:A');if ~iscell(A)B = maxk(A,1);elsemiu = cell2mat(A);X = str2num(miu); %#ok<ST2NM>B = maxk(X,1);endx = B+1;if ~isempty(value1)if ~isempty(value2)if (value2==value3)v1 = {x,string(value1)};v2 = {string(x),string(value2)};writecell(v1,'lname.xls',"WriteMode","append");writecell(v2,'lcode.xls','WriteMode','append');app.signname.Value = '';app.signcode.Value = '';app.signcodeagain.Value = '';uiwait(msgbox('用户创建成功',"non-modal"));delete(gcf);elseerrordlg('密码输入不一致','账户注册指引');endelseerrordlg('密码必须不为空','账户注册指引');endelseif isempty(value1)errordlg('用户名必须不为空','账户注册指引');elseif isempty(value2)errordlg('密码必须不为空','账户注册指引');endendend% Component initializationmethods (Access = private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and hide until all components are createdapp.UIFigure = uifigure('Visible', 'off');app.UIFigure.Position = [100 100 570 480];app.UIFigure.Name = 'MATLAB App';% Create signnameapp.signname = uieditfield(app.UIFigure, 'text');app.signname.ValueChangedFcn = createCallbackFcn(app, @signnameValueChanged, true);app.signname.Position = [225 300 170 39];% Create Labelapp.Label = uilabel(app.UIFigure);app.Label.FontSize = 18;app.Label.FontWeight = 'bold';app.Label.Position = [118 290 113 59];app.Label.Text = '注册账户:';% Create Label_5app.Label_5 = uilabel(app.UIFigure);app.Label_5.HorizontalAlignment = 'center';app.Label_5.FontName = 'Adobe 仿宋 Std R';app.Label_5.FontSize = 22;app.Label_5.FontWeight = 'bold';app.Label_5.Position = [138 395 295 29];app.Label_5.Text = {'植物病虫害识别系统账号注册'; ''};% Create confirmapp.confirm = uibutton(app.UIFigure, 'push');app.confirm.ButtonPushedFcn = createCallbackFcn(app, @confirmButtonPushed, true);app.confirm.Position = [225 73 122 48];app.confirm.Text = '确认';% Create signcodeagainapp.signcodeagain = uieditfield(app.UIFigure, 'text');app.signcodeagain.ValueChangedFcn = createCallbackFcn(app, @signcodeagainValueChanged, true);app.signcodeagain.Tag = 'code';app.signcodeagain.Position = [225 160 170 39];% Create Label_6app.Label_6 = uilabel(app.UIFigure);app.Label_6.FontSize = 18;app.Label_6.FontWeight = 'bold';app.Label_6.Position = [118 150 111 59];app.Label_6.Text = '确认密码:';% Create Label_7app.Label_7 = uilabel(app.UIFigure);app.Label_7.FontSize = 18;app.Label_7.FontWeight = 'bold';app.Label_7.Position = [118 220 104 59];app.Label_7.Text = '输入密码:';% Create signcodeapp.signcode = uieditfield(app.UIFigure, 'text');app.signcode.ValueChangedFcn = createCallbackFcn(app, @signcodeValueChanged, true);app.signcode.Tag = 'code';app.signcode.Position = [225 230 170 39];% Show the figure after all components are createdapp.UIFigure.Visible = 'on';endend% App creation and deletionmethods (Access = public)% Construct appfunction app = regester% Create UIFigure and componentscreateComponents(app)% Register the app with App DesignerregisterApp(app, app.UIFigure)% Execute the startup functionrunStartupFcn(app, @startupFcn)if nargout == 0clear appendend% Code that executes before app deletionfunction delete(app)% Delete UIFigure when app is deleteddelete(app.UIFigure)endend
end
以上是关于登录、注册、修改密码的控件与界面的代码部分
接下来是系统主界面的代码
6.系统主页面控件代码
% Code that executes after component creationfunction startupFcn(app)global name;global val;global num;%读标签num = 0;val = 0;[~,name] = xlsread('labels.xls');end% Button pushed function: imagereadfunction imagereadPushed(app, event)global I;global val;global filenames;%读入图片以显示在坐标系中filefold ='*'; %图片读取路径[file,path] = uigetfile(filefold);image = fullfile(path,file);I = imresize(imread(image),[224,224]);imshow(I,'parent',app.imageshow);val = 1;imagenames = split(path,"\");filenames = string(imagenames(5,1));app.Field.Value = '';app.OriginalField.Value = '';end% Value changed function: OriginalFieldfunction OriginalFieldValueChanged(app, event)%“图片类别”与空白显示框end% Button pushed function: analysis function analysisButtonPushed(app, event) %“分析”函数global I;global name;global val;global num;if val == 1app.Field.Value = '';load("CNNtestmini.mat"); %#ok<LOAD> sz = net.Layers(1).InputSize;I = I(1:sz(1),1:sz(2),1:sz(3));label = classify(net,I); %#ok<NASGU> labelchange();tag = name(l,1);app.Field.Value = string(tag);num = 1;elseerrordlg('请输入待识别图片','错误提醒','modal');endend% Button pushed function: exitfunction exitButtonPushed(app, event)delete(gcf); %“退出”函数end% Button down function: imageshowfunction imageshowButtonDown(app, event)%图片展示坐标轴函数end% Value changed function: Fieldfunction FieldValueChanged(app, event)%“分析结果”与空白显示框end% Button pushed function: verifyfunction verifyPushed(app, event) %“分析结果”函数global filenames;global num;if num == 1app.OriginalField.Value = filenames;elseerrordlg('操作错误,请先获得分析结果','错误提醒','modal');endend
系统主界面详细代码
(看个大概,大部分是不需要操作的,需要自己添加的已经在上面演示了)
classdef app1 < matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access = public)UIFigure matlab.ui.Figureverify matlab.ui.control.ButtonLabel_2 matlab.ui.control.LabelOriginalField matlab.ui.control.EditFieldField matlab.ui.control.EditFieldLabel matlab.ui.control.Labelexit matlab.ui.control.Buttonanalysis matlab.ui.control.Buttonimageread matlab.ui.control.Buttonimageshow matlab.ui.control.UIAxesend% Callbacks that handle component eventsmethods (Access = private)% Code that executes after component creationfunction startupFcn(app)global name;global val;global num;num = 0;val = 0;[~,name] = xlsread('labels.xls');end% Callback functionfunction EditFieldValueChanged(app, event)end% Button pushed function: imagereadfunction imagereadPushed(app, event)global I;global val;global filenames;filefold ='D:\机器视觉课设\课设大数据\验证\';[file,path] = uigetfile(filefold);image = fullfile(path,file);I = imresize(imread(image),[224,224]);imshow(I,'parent',app.imageshow);val = 1;imagenames = split(path,"\");filenames = string(imagenames(5,1));app.Field.Value = '';app.OriginalField.Value = '';end% Value changed function: OriginalFieldfunction OriginalFieldValueChanged(app, event)end% Button pushed function: analysisfunction analysisButtonPushed(app, event)global I;global name;global val;global num;if val == 1app.Field.Value = '';load("CNNtestmini.mat"); %#ok<LOAD> sz = net.Layers(1).InputSize;I = I(1:sz(1),1:sz(2),1:sz(3));label = classify(net,I); %#ok<NASGU> labelchange();tag = name(l,1);app.Field.Value = string(tag);num = 1;elseerrordlg('请输入待识别图片','错误提醒','modal');endend% Button pushed function: exitfunction exitButtonPushed(app, event)delete(gcf);end% Button down function: imageshowfunction imageshowButtonDown(app, event)end% Value changed function: Fieldfunction FieldValueChanged(app, event)end% Button down function: UIFigurefunction UIFigureButtonDown(app, event)end% Button pushed function: verifyfunction verifyPushed(app, event)global filenames;global num;if num == 1app.OriginalField.Value = filenames;elseerrordlg('操作错误,请先获得分析结果','错误提醒','modal');endendend% Component initializationmethods (Access = private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and hide until all components are createdapp.UIFigure = uifigure('Visible', 'off');app.UIFigure.IntegerHandle = 'on';app.UIFigure.AutoResizeChildren = 'off';app.UIFigure.Colormap = [];app.UIFigure.Position = [500 300 640 480];app.UIFigure.Name = '植物病虫害分析系统';app.UIFigure.Resize = 'off';app.UIFigure.ButtonDownFcn = createCallbackFcn(app, @UIFigureButtonDown, true);app.UIFigure.HandleVisibility = 'callback';app.UIFigure.WindowStyle = 'modal';% Create imageshowapp.imageshow = uiaxes(app.UIFigure);title(app.imageshow, '图片展示')app.imageshow.Toolbar.Visible = 'off';app.imageshow.Colormap = [];app.imageshow.Clipping = 'off';app.imageshow.ButtonDownFcn = createCallbackFcn(app, @imageshowButtonDown, true);app.imageshow.BusyAction = 'cancel';app.imageshow.Interruptible = 'off';app.imageshow.PickableParts = 'all';app.imageshow.Position = [202 147 388 309];% Create imagereadapp.imageread = uibutton(app.UIFigure, 'push');app.imageread.ButtonPushedFcn = createCallbackFcn(app, @imagereadPushed, true);app.imageread.Position = [64 370 106 34];app.imageread.Text = '图片读取';% Create analysisapp.analysis = uibutton(app.UIFigure, 'push');app.analysis.ButtonPushedFcn = createCallbackFcn(app, @analysisButtonPushed, true);app.analysis.Position = [64 275 106 34];app.analysis.Text = '分析';% Create exitapp.exit = uibutton(app.UIFigure, 'push');app.exit.ButtonPushedFcn = createCallbackFcn(app, @exitButtonPushed, true);app.exit.Position = [64 85 106 34];app.exit.Text = {'退出'; ''};% Create Labelapp.Label = uilabel(app.UIFigure);app.Label.HorizontalAlignment = 'right';app.Label.FontSize = 18;app.Label.Position = [224 62 77 24];app.Label.Text = '分析结果';% Create Fieldapp.Field = uieditfield(app.UIFigure, 'text');app.Field.ValueChangedFcn = createCallbackFcn(app, @FieldValueChanged, true);app.Field.Editable = 'off';app.Field.HorizontalAlignment = 'center';app.Field.FontSize = 18;app.Field.Position = [319 56 211 37];% Create OriginalFieldapp.OriginalField = uieditfield(app.UIFigure, 'text');app.OriginalField.ValueChangedFcn = createCallbackFcn(app, @OriginalFieldValueChanged, true);app.OriginalField.Editable = 'off';app.OriginalField.HorizontalAlignment = 'center';app.OriginalField.FontSize = 18;app.OriginalField.Position = [319 111 211 37];% Create Label_2app.Label_2 = uilabel(app.UIFigure);app.Label_2.HorizontalAlignment = 'right';app.Label_2.FontSize = 18;app.Label_2.Position = [225 117 77 24];app.Label_2.Text = '图片类别';% Create verifyapp.verify = uibutton(app.UIFigure, 'push');app.verify.ButtonPushedFcn = createCallbackFcn(app, @verifyPushed, true);app.verify.Position = [64 180 106 34];app.verify.Text = '分类验证';% Show the figure after all components are createdapp.UIFigure.Visible = 'on';endend% App creation and deletionmethods (Access = public)% Construct appfunction app = app1% Create UIFigure and componentscreateComponents(app)% Register the app with App DesignerregisterApp(app, app.UIFigure)% Execute the startup functionrunStartupFcn(app, @startupFcn)if nargout == 0clear appendend% Code that executes before app deletionfunction delete(app)% Delete UIFigure when app is deleteddelete(app.UIFigure)endend
end
二、深度学习VGG-19模型
1.VGG-19模型的查找与应用
首先按照顺序进行操作
等待最终的训练结果曲线,大概结果如下:
至此,模型训练完毕可以直接使用了。
2.网络模型MATLAB代码段
(不需要刻意准备,一般模型训练结果会直接导出)
clear;clc;
%%数据集载入
datapath = "filepath";
imds = imageDatastore(datapath,'IncludeSubfolders',true,'LabelSource','foldernames');
%%数据集拆分为训练集imTrain与验证集imTest
[imTrain,imTest] = splitEachLabel(imds,0.7,'randomize');
%%导入VGG-19模型框架表
params = load("D:\MatlabR2021a\bin\params_VGG19.mat");
%%VGG-19模型图层
layers = [imageInputLayer([224 224 3],"Name","input","Mean",params.input.Mean)convolution2dLayer([3 3],64,"Name","conv1_1","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv1_1.Bias,"Weights",params.conv1_1.Weights)reluLayer("Name","relu1_1")convolution2dLayer([3 3],64,"Name","conv1_2","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv1_2.Bias,"Weights",params.conv1_2.Weights)reluLayer("Name","relu1_2")maxPooling2dLayer([2 2],"Name","pool1","Stride",[2 2])convolution2dLayer([3 3],128,"Name","conv2_1","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv2_1.Bias,"Weights",params.conv2_1.Weights)reluLayer("Name","relu2_1")convolution2dLayer([3 3],128,"Name","conv2_2","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv2_2.Bias,"Weights",params.conv2_2.Weights)reluLayer("Name","relu2_2")maxPooling2dLayer([2 2],"Name","pool2","Stride",[2 2])convolution2dLayer([3 3],256,"Name","conv3_1","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv3_1.Bias,"Weights",params.conv3_1.Weights)reluLayer("Name","relu3_1")convolution2dLayer([3 3],256,"Name","conv3_2","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv3_2.Bias,"Weights",params.conv3_2.Weights)reluLayer("Name","relu3_2")convolution2dLayer([3 3],256,"Name","conv3_3","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv3_3.Bias,"Weights",params.conv3_3.Weights)reluLayer("Name","relu3_3")convolution2dLayer([3 3],256,"Name","conv3_4","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv3_4.Bias,"Weights",params.conv3_4.Weights)reluLayer("Name","relu3_4")maxPooling2dLayer([2 2],"Name","pool3","Stride",[2 2])convolution2dLayer([3 3],512,"Name","conv4_1","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv4_1.Bias,"Weights",params.conv4_1.Weights)reluLayer("Name","relu4_1")convolution2dLayer([3 3],512,"Name","conv4_2","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv4_2.Bias,"Weights",params.conv4_2.Weights)reluLayer("Name","relu4_2")convolution2dLayer([3 3],512,"Name","conv4_3","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv4_3.Bias,"Weights",params.conv4_3.Weights)reluLayer("Name","relu4_3")convolution2dLayer([3 3],512,"Name","conv4_4","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv4_4.Bias,"Weights",params.conv4_4.Weights)reluLayer("Name","relu4_4")maxPooling2dLayer([2 2],"Name","pool4","Stride",[2 2])convolution2dLayer([3 3],512,"Name","conv5_1","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv5_1.Bias,"Weights",params.conv5_1.Weights)reluLayer("Name","relu5_1")convolution2dLayer([3 3],512,"Name","conv5_2","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv5_2.Bias,"Weights",params.conv5_2.Weights)reluLayer("Name","relu5_2")convolution2dLayer([3 3],512,"Name","conv5_3","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv5_3.Bias,"Weights",params.conv5_3.Weights)reluLayer("Name","relu5_3")convolution2dLayer([3 3],512,"Name","conv5_4","Padding",[1 1 1 1],"WeightL2Factor",0,"Bias",params.conv5_4.Bias,"Weights",params.conv5_4.Weights)reluLayer("Name","relu5_4")maxPooling2dLayer([2 2],"Name","pool5","Stride",[2 2])fullyConnectedLayer(4096,"Name","fc6","WeightL2Factor",0,"Bias",params.fc6.Bias,"Weights",params.fc6.Weights)reluLayer("Name","relu6")dropoutLayer(0.5,"Name","drop6")fullyConnectedLayer(4096,"Name","fc7","WeightL2Factor",0,"Bias",params.fc7.Bias,"Weights",params.fc7.Weights)reluLayer("Name","relu7")dropoutLayer(0.5,"Name","drop7")fullyConnectedLayer(15,"Name","fc","WeightL2Factor",0)softmaxLayer("Name","softmax")classificationLayer("Name","classoutput")];
%%超参数设置
options = trainingOptions('sgdm',...
'MiniBatchSize',11,...
'MaxEpochs',30,...
'InitialLearnRate',0.001,...
'Shuffle','every-epoch',...
'ValidationData',imTest,...
'ValidationFrequency',30,...
'ExecutionEnvironment','multi-gpu',...
'Verbose',false,...
'Plots','training-progress');
%%开始训练
net = trainNetwork(imTrain,layers,options);
%%模型结果保存
save('CNNtestmini','net');
%%精度验证
ypred = classify(net,imTest);
ytest = imTest.Labels;
Accuracy = sum(ypred==ytest)/numel(ytest);
三、最后一点补充
1.关于标签转换的一些简单代码样例
if label == 'Apple_Normal' %#ok<BDSCA>l = 1;
elseif label == 'Apple_RD' %#ok<BDSCA>l = 2;
elseif label == 'Apple_GSD' %#ok<BDSCA>l = 3;
elseif label == 'Apple_CR' %#ok<BDSCA>l = 4;
elseif label == 'Apple_ALS' %#ok<BDSCA>l = 5;
elseif label == 'Cabbage_Normal' %#ok<BDSCA>l = 6;
elseif label == 'Cabbage_SR' %#ok<BDSCA>l = 7;
elseif label == 'Cabbage_BBD' %#ok<BDSCA>l = 8;
elseif label == 'Grape_Normal' %#ok<BDSCA>l = 9;
elseif label == 'Grape_BBD' %#ok<BDSCA>l = 10;
elseif label == 'Grape_DM' %#ok<BDSCA>l = 11;
elseif label == 'Rice_Normal' %#ok<BDSCA>l = 12;
elseif label == 'Rice_RB' %#ok<BDSCA>l = 13;
elseif label == 'Rice_BBD' %#ok<BDSCA>l = 14;
elseif label == 'Rice_BLB' %#ok<BDSCA>l = 15;
end
标签转化目的是为了对应最终展示结果
2.关于图片预处理的一些简单样例(裁剪图片尺寸大小)
%%filepath为需要修改的图片所在的父目录
imagePath = 'filepath';
%%读取目录文件下的所有图片文件
imageFiles = dir(imagePath);
%%获取图片的数量
numFiles = length(imageFiles);for i=3:numFiles j = i-2; imageFile = strcat(imagePath,imageFiles(i).name);%%读入图片A = imread(imageFile);%%修改图片尺寸B = imresize(A,[224 224]); %%保存图片 imwrite(B,imageFile);
end
至此,关于“植物病虫害识别系统”课设的所有代码相关内容已完成上传
由于本博主第一次发表此类内容,如果有不足请大家多多指教.
以上
资源下载:《植物病虫害识别系统课程设计》-Matlab文档类资源-CSDN下载
基于MATALB的植物病虫害识别系统设计与实现(已完结)相关推荐
- 【叶片识别】基于matlab LBP植物叶片识别【含Matlab源码 1702期】
一.LBP简介(附lunwen) 第一章 引言 植物在我们的身边随处可见,它们从产生发展进化到现在,其间经历了漫长的岁月.地球上的植物种类繁多.数量浩瀚,它们是生物圈的重要组成部分,在维持整个生物界的 ...
- matlab幼苗识别,基于MATLAB的植物幼苗识别
基于MATLAB的植物幼苗识别(论文11000字,外文翻译) 摘要:杂草种类繁多,严重影响了农作物的生产与产量,使用图像处理技术识别区分杂草和作物幼苗已成为一种最科学最有效的方法.通过提取植物图像的有 ...
- 【设计方案分享】基于单片机温度监测监控报警系统设计-基于单片机钞票自动智能识别系统设计-基于单片机乒乓球游戏机控制系统设计-基于单片机温度监测监控报警系统设计-基于单片机矩阵键盘的电子密码锁设计
820基于单片机温度监测监控报警系统设计-设计资料 温度监测器功能描述: 1.主控芯片用的是51单片机(STC89C51). 2.使用温度传感器DS18b20采集温度. 3.用1602液晶显示显示温度 ...
- 基于单片机乒乓球游戏机控制系统设计-基于单片机矩阵键盘的电子密码锁设计-基于单片机温度监测监控报警系统设计-基于单片机钞票自动智能识别系统设计-设计资料【转发分享】
819基于单片机乒乓球游戏机控制系统设计-设计资料下载 乒乓球游戏机设计任务为: (1)使用乒乓游戏机的甲乙双方各在不同的位置发球或击球. (2)乒乓球的位置和移动方向由灯亮及依次点燃的方向决定,球移 ...
- 【病虫害识别】基于matlab GUI SVM病虫害识别系统【含Matlab源码 2429期】
⛄一.基于机器视觉的农作物病害识别技术 1 叶片图像采集 进行农作物病害自动检测与识别首先要对病害叶片的图像进行采集.自动识别的前提是获得数字图像, 数字图像质量的好坏决定着之后叶片病害的识别特征能否 ...
- 表单识别(五)——票据识别-论文研读:基于深度学习的票据识别系统设计与实现,卞飞飞(上)
(论文研读:基于深度学习的票据识别系统设计与实现,卞飞飞:) 引言: 传统的文本检测与识别算法主要指利用数字图像处理等非深度学习技术进行文本检测的方法,依赖于数字图像特征(如颜色.纹理.轮廓.形状等特 ...
- 基于SVM的语音情感识别系统设计
基于SVM的语音情感识别系统设计 目录 基于SVM的语音情感识别系统设计 1 引言 2 情感语音信号的采集 3. 特征参数 §3.1 基音频率参数 §3.3 能量参数 4 语音情感的识别 §4. ...
- 【电路方案】基于单片机智能市电温度控制系统设计-基于单片机RGB颜色智能识别系统设计-基于单片机四路红外遥控开关电路设计-基于单片机自行车自动防盗报警系统设计-基于单片机智能无线病床呼叫系统设计
822基于单片机智能无线病床呼叫系统设计-设计资料下载 硬件构成:单片机+最小系统+LCD1602液晶显示模块+无线收发模块+蜂鸣器模块+LED指示灯模块+按键模块 本设计基于STC89C51/52( ...
- 基于MATLAB的农业病虫害识别系统
摘 要 目前,图像处理技术的应用与研究都有了很大进展,其应用也已经渗透到农业生产的各个领域.在农业生产中,病虫害一直是困扰农作物生长的基本问题.因此研究农作物病虫草害的自动检测与识别,开发智能化控制系 ...
最新文章
- Device.js——检测设备平台、操作系统的Javascript 库
- 利用ListView实现新闻客户端的新闻内容图文混排
- 详解如何充分发挥先验信息优势,用MRC框架解决各类NLP任务
- footer bar in SAP Fiori as a service
- 多维数组元素的下标引用与指针访问
- Bootstrap开篇理解:
- Linux内核分析 - 网络[八]:IP协议
- 世界大学生超算竞赛正式启动,再次引发全球关注
- 7-3 算术入门之加减乘除 (10 分)
- 剑指Offer:跳台阶
- [ 淘宝商城 ] 商城SEO
- 工厂流水线数据采集方案
- PHP强大自适应OA协同办公系统源码 含CRM客户管理系统+内部聊天工具
- IAR for STM8的简介、下载、安装及注册教程
- 计算机配置很不错但是卡,高手告诉你win10电脑明明配置很好却卡顿的详尽处理手法...
- VirtualBox 虚拟电脑控制台错误
- 公共经济学(开卷)期末复习题
- 第0篇日记 先立一个flag——开始有了做阅读器的想法
- Mybatis层次结构图
- HTML5七夕520情人节表白网页❤ 属于我们的浪漫星空 ❤ HTML+CSS+JavaScript
热门文章
- EasyAds:曾经自研的广告聚合SDK,如今在开源平台中唾手可得
- 优维科技招商基金 | 招商基金DevSecOps实践与避坑指南
- mysql sql 工资加密_MYSQL 加密SQL
- linux 文件夹修改名字,linux命令修改文件名_修改文件夹名字怎么操作
- 自定义控件实战四 音量增减控件
- 有用的win7快捷键
- 06世界杯8强备忘录
- COleDateTime的使用
- 【Nexus(docker)】login attempt to http://xxx/v2/ failed with status: 401 Unauthorized
- 华硕笔记本 触摸板开关控制