(随便看看就好,主要是课设结束了电脑没空间了舍不得删)
本篇博客主要是有下面两个方面的内容:
一、App Designer工具设计交互界面
二、MATLAB深度学习模块中VGG-19模型的使用
目录
(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);
else
miu = cell2mat(A);
X = str2num(miu); %#ok<ST2NM>
B = maxk(X,1);
end
for a = 1:B
lname = string(loginname(a));
lcode = string(logincode(a));
if (*==lname && *==lcode)
delete(gcf);
*; %跳转至主页面
break;
end
if a == B
errordlg('用户名或密码错误','错误提醒','modal');
app.*.Value='';
app.*.Value='';
end
end
else
errordlg('用户名或密码错误','错误提醒','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='';
else
helpdlg('请输入管理员账户以继续','账户注册指引');
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 components
properties (Access = public)
UIFigure matlab.ui.Figure
Label_3 matlab.ui.control.Label
Label_2 matlab.ui.control.Label
Label matlab.ui.control.Label
alter matlab.ui.control.Button
signup matlab.ui.control.Button
make matlab.ui.control.Button
code matlab.ui.control.EditField
name matlab.ui.control.EditField
UIAxes matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function 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: UIAxes
function UIAxesButtonDown(app, event)
end
% Button pushed function: signup
function 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);
else
miu = cell2mat(A);
X = str2num(miu); %#ok<ST2NM>
B = maxk(X,1);
end
for a = 1:B
lname = string(loginname(a));
lcode = string(logincode(a));
if (namevalue==lname && codevalue==lcode)
delete(gcf);
app1;
break;
end
if a == B
errordlg('用户名或密码错误','错误提醒','modal');
app.code.Value='';
app.name.Value='';
end
end
else
errordlg('用户名或密码错误','错误提醒','modal');
app.code.Value='';
app.name.Value='';
end
end
% Value changed function: name
function nameValueChanged(app, event)
global namevalue;
namevalue = app.name.Value;
end
% Value changed function: code
function codeValueChanged(app, event)
global codevalue;
codevalue = app.code.Value;
end
% Close request function: UIFigure
function UIFigureCloseRequest(app, event)
selection = questdlg('确定要退出登录系统吗?','关闭提醒','是','否','是');
switch selection
case '是'
delete(gcf);
case '否'
return;
end
end
% Button pushed function: make
function 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='';
else
uiwait(helpdlg('请输入管理员账户以继续','账户注册指引'));
app.code.Value='';
app.name.Value='';
end
end
% Button pushed function: alter
function alterButtonPushed(app, event)
altercode;
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.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 UIAxes
app.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 name
app.name = uieditfield(app.UIFigure, 'text');
app.name.ValueChangedFcn = createCallbackFcn(app, @nameValueChanged, true);
app.name.Position = [255 240 170 39];
% Create code
app.code = uieditfield(app.UIFigure, 'text');
app.code.ValueChangedFcn = createCallbackFcn(app, @codeValueChanged, true);
app.code.Tag = 'code';
app.code.Position = [255 153 170 39];
% Create make
app.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 signup
app.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 alter
app.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 Label
app.Label = uilabel(app.UIFigure);
app.Label.FontSize = 18;
app.Label.FontWeight = 'bold';
app.Label.Position = [176 230 63 59];
app.Label.Text = '用户:';
% Create Label_2
app.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_3
app.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 created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = test
runningApp = getRunningApp(app);
% Check for running singleton app
if isempty(runningApp)
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
else
% Focus the running singleton app
figure(runningApp.UIFigure)
app = runningApp;
end
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
4.修改密码页面组件的相关代码
function startupFcn(app)
%空白操作警告
fig = app.UIFigure;
uialert(fig,'请输入用户账号和原始密码以继续', '修改密码指引','CloseFcn','uiresume(fig)')
uiwait(fig);
end
% Value changed function: signname
function signnameValueChanged(app, event)
%登录名输入框
end
% Value changed function: signcode
function signcodeValueChanged(app, event)
%登录密码输入框
end
% Value changed function: changecode
function changecodeValueChanged(app, event)
global value;
%密码修改输入框
value = app.changecode.Value;
end
% Button pushed function: confirm
function 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));
else
miu = cell2mat(A);
X = str2num(miu); %#ok<ST2NM>
B = maxk(X,1);
str = string(zeros(B,2));
end
for a = 1:B
lname = string(loginname(a));
lcode = string(logincode(a));
if (app.signname.Value==lname && app.signcode.Value==lcode)
for b = 1:B
str(b,1) = double(b);
if b==a
str(b,2) = string(value);
else
str(b,2) = logincode(b);
end
end
writematrix(str,'lcode.xls',"WriteMode","overwritesheet");
msgbox('密码修改成功',"non-modal");
close("MATLAB App");
break;
end
if a == B
errordlg('用户名或密码错误','错误提醒','modal');
app.signname.Value = '';
app.signcode.Value = '';
app.changecode.Value = '';
end
end
else
errordlg('密码必须不为空','错误提醒');
end
else
errordlg('用户名或密码错误','错误提醒','modal');
app.code.Value='';
app.name.Value='';
end
end
end
修改密码界面代码详细
(看个大概,大部分是不需要操作的,需要自己添加的已经在上面演示了)
classdef altercode < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
signcode matlab.ui.control.EditField
Label_4 matlab.ui.control.Label
Label_3 matlab.ui.control.Label
changecode matlab.ui.control.EditField
confirm matlab.ui.control.Button
Label_2 matlab.ui.control.Label
Label matlab.ui.control.Label
signname matlab.ui.control.EditField
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
fig = app.UIFigure;
uialert(fig,'请输入用户账号和原始密码以继续', '修改密码指引','CloseFcn','uiresume(fig)')
uiwait(fig);
end
% Value changed function: signname
function signnameValueChanged(app, event)
end
% Value changed function: signcode
function signcodeValueChanged(app, event)
end
% Value changed function: changecode
function changecodeValueChanged(app, event)
global value;
value = app.changecode.Value;
end
% Button pushed function: confirm
function 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));
else
miu = cell2mat(A);
X = str2num(miu); %#ok<ST2NM>
B = maxk(X,1);
str = string(zeros(B,2));
end
for a = 1:B
lname = string(loginname(a));
lcode = string(logincode(a));
if (app.signname.Value==lname && app.signcode.Value==lcode)
for b = 1:B
str(b,1) = double(b);
if b==a
str(b,2) = string(value);
else
str(b,2) = logincode(b);
end
end
writematrix(str,'lcode.xls',"WriteMode","overwritesheet");
msgbox('密码修改成功',"non-modal");
close("MATLAB App");
break;
end
if a == B
errordlg('用户名或密码错误','错误提醒','modal');
app.signname.Value = '';
app.signcode.Value = '';
app.changecode.Value = '';
end
end
else
errordlg('密码必须不为空','错误提醒');
end
else
errordlg('用户名或密码错误','错误提醒','modal');
app.code.Value='';
app.name.Value='';
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [500 300 547 480];
app.UIFigure.Name = 'MATLAB App';
% Create signname
app.signname = uieditfield(app.UIFigure, 'text');
app.signname.ValueChangedFcn = createCallbackFcn(app, @signnameValueChanged, true);
app.signname.Position = [225 300 170 39];
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.FontSize = 18;
app.Label.FontWeight = 'bold';
app.Label.Position = [139 290 113 59];
app.Label.Text = '用户:';
% Create Label_2
app.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 confirm
app.confirm = uibutton(app.UIFigure, 'push');
app.confirm.ButtonPushedFcn = createCallbackFcn(app, @confirmButtonPushed, true);
app.confirm.Position = [225 73 122 48];
app.confirm.Text = '确认';
% Create changecode
app.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_3
app.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_4
app.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 signcode
app.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 created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = altercode
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
5.注册页面组件的相关代码
% Code that executes after component creation
function startupFcn(app)
global value1;
global value2;
global value3;
%初始化
value1 = '';
value2 = '';
value3 = '';
end
% Value changed function: signname
function signnameValueChanged(app, event)
global value1;
%用户名输入
value1 = app.signname.Value;
end
% Value changed function: signcode
function signcodeValueChanged(app, event)
global value2;
%密码输入
value2 = app.signcode.Value;
end
% Value changed function: signcodeagain
function signcodeagainValueChanged(app, event)
global value3;
%确认密码输入
value3 = app.signcodeagain.Value;
end
% Button pushed function: confirm
function 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);
else
miu = cell2mat(A);
X = str2num(miu); %#ok<ST2NM>
B = maxk(X,1);
end
x = 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);
else
errordlg('密码输入不一致','账户注册指引');
end
else
errordlg('密码必须不为空','账户注册指引');
end
elseif isempty(value1)
errordlg('用户名必须不为空','账户注册指引');
elseif isempty(value2)
errordlg('密码必须不为空','账户注册指引');
end
end
注册界面代码详细
(看个大概,大部分是不需要操作的,需要自己添加的已经在上面演示了)
classdef regester < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
signcode matlab.ui.control.EditField
Label_7 matlab.ui.control.Label
Label_6 matlab.ui.control.Label
signcodeagain matlab.ui.control.EditField
confirm matlab.ui.control.Button
Label_5 matlab.ui.control.Label
Label matlab.ui.control.Label
signname matlab.ui.control.EditField
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
global value1;
global value2;
global value3;
value1 = '';
value2 = '';
value3 = '';
end
% Value changed function: signname
function signnameValueChanged(app, event)
global value1;
value1 = app.signname.Value;
end
% Value changed function: signcode
function signcodeValueChanged(app, event)
global value2;
value2 = app.signcode.Value;
end
% Value changed function: signcodeagain
function signcodeagainValueChanged(app, event)
global value3;
value3 = app.signcodeagain.Value;
end
% Button pushed function: confirm
function 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);
else
miu = cell2mat(A);
X = str2num(miu); %#ok<ST2NM>
B = maxk(X,1);
end
x = 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);
else
errordlg('密码输入不一致','账户注册指引');
end
else
errordlg('密码必须不为空','账户注册指引');
end
elseif isempty(value1)
errordlg('用户名必须不为空','账户注册指引');
elseif isempty(value2)
errordlg('密码必须不为空','账户注册指引');
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 570 480];
app.UIFigure.Name = 'MATLAB App';
% Create signname
app.signname = uieditfield(app.UIFigure, 'text');
app.signname.ValueChangedFcn = createCallbackFcn(app, @signnameValueChanged, true);
app.signname.Position = [225 300 170 39];
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.FontSize = 18;
app.Label.FontWeight = 'bold';
app.Label.Position = [118 290 113 59];
app.Label.Text = '注册账户:';
% Create Label_5
app.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 confirm
app.confirm = uibutton(app.UIFigure, 'push');
app.confirm.ButtonPushedFcn = createCallbackFcn(app, @confirmButtonPushed, true);
app.confirm.Position = [225 73 122 48];
app.confirm.Text = '确认';
% Create signcodeagain
app.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_6
app.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_7
app.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 signcode
app.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 created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = regester
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
以上是关于登录、注册、修改密码的控件与界面的代码部分
接下来是系统主界面的代码
6.系统主页面控件代码
% Code that executes after component creation
function startupFcn(app)
global name;
global val;
global num;
%读标签
num = 0;
val = 0;
[~,name] = xlsread('labels.xls');
end
% Button pushed function: imageread
function 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: OriginalField
function OriginalFieldValueChanged(app, event)
%“图片类别”与空白显示框
end
% Button pushed function: analysis
function analysisButtonPushed(app, event) %“分析”函数
global I;
global name;
global val;
global num;
if val == 1
app.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;
else
errordlg('请输入待识别图片','错误提醒','modal');
end
end
% Button pushed function: exit
function exitButtonPushed(app, event)
delete(gcf); %“退出”函数
end
% Button down function: imageshow
function imageshowButtonDown(app, event)
%图片展示坐标轴函数
end
% Value changed function: Field
function FieldValueChanged(app, event)
%“分析结果”与空白显示框
end
% Button pushed function: verify
function verifyPushed(app, event) %“分析结果”函数
global filenames;
global num;
if num == 1
app.OriginalField.Value = filenames;
else
errordlg('操作错误,请先获得分析结果','错误提醒','modal');
end
end
系统主界面详细代码
(看个大概,大部分是不需要操作的,需要自己添加的已经在上面演示了)
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
verify matlab.ui.control.Button
Label_2 matlab.ui.control.Label
OriginalField matlab.ui.control.EditField
Field matlab.ui.control.EditField
Label matlab.ui.control.Label
exit matlab.ui.control.Button
analysis matlab.ui.control.Button
imageread matlab.ui.control.Button
imageshow matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
global name;
global val;
global num;
num = 0;
val = 0;
[~,name] = xlsread('labels.xls');
end
% Callback function
function EditFieldValueChanged(app, event)
end
% Button pushed function: imageread
function 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: OriginalField
function OriginalFieldValueChanged(app, event)
end
% Button pushed function: analysis
function analysisButtonPushed(app, event)
global I;
global name;
global val;
global num;
if val == 1
app.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;
else
errordlg('请输入待识别图片','错误提醒','modal');
end
end
% Button pushed function: exit
function exitButtonPushed(app, event)
delete(gcf);
end
% Button down function: imageshow
function imageshowButtonDown(app, event)
end
% Value changed function: Field
function FieldValueChanged(app, event)
end
% Button down function: UIFigure
function UIFigureButtonDown(app, event)
end
% Button pushed function: verify
function verifyPushed(app, event)
global filenames;
global num;
if num == 1
app.OriginalField.Value = filenames;
else
errordlg('操作错误,请先获得分析结果','错误提醒','modal');
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.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 imageshow
app.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 imageread
app.imageread = uibutton(app.UIFigure, 'push');
app.imageread.ButtonPushedFcn = createCallbackFcn(app, @imagereadPushed, true);
app.imageread.Position = [64 370 106 34];
app.imageread.Text = '图片读取';
% Create analysis
app.analysis = uibutton(app.UIFigure, 'push');
app.analysis.ButtonPushedFcn = createCallbackFcn(app, @analysisButtonPushed, true);
app.analysis.Position = [64 275 106 34];
app.analysis.Text = '分析';
% Create exit
app.exit = uibutton(app.UIFigure, 'push');
app.exit.ButtonPushedFcn = createCallbackFcn(app, @exitButtonPushed, true);
app.exit.Position = [64 85 106 34];
app.exit.Text = {'退出'; ''};
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.HorizontalAlignment = 'right';
app.Label.FontSize = 18;
app.Label.Position = [224 62 77 24];
app.Label.Text = '分析结果';
% Create Field
app.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 OriginalField
app.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_2
app.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 verify
app.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 created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
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
至此,关于“植物病虫害识别系统”课设的所有代码相关内容已完成上传
由于本博主第一次发表此类内容,如果有不足请大家多多指教.
以上