1 简介
本文以树叶为实验对象,针对传统分类问题耗时长,效率低的不足,提出了一个基于BP神经网络植物智能分类系统。这个计算机辅助分类系统不仅能够帮助提高植物分类的准确率同时也能缩减工作人员的工作量。
2 部分代码
function varargout = Processing(varargin)
% PROCESSING MATLAB code for Processing.fig
% PROCESSING, by itself, creates a new PROCESSING or raises the existing
% singleton*.
%
% H = PROCESSING returns the handle to a new PROCESSING or the handle to
% the existing singleton*.
%
% PROCESSING('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PROCESSING.M with the given input arguments.
%
% PROCESSING('Property','Value',...) creates a new PROCESSING or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Processing_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Processing_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 Processing
% Last Modified by GUIDE v2.5 22-May-2021 22:54:29
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Processing_OpeningFcn, ...
'gui_OutputFcn', @Processing_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 Processing is made visible.
function Processing_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 Processing (see VARARGIN)
% Choose default command line output for Processing
handles.output = hObject;
setappdata(handles.Processing,'X',0);
setappdata(handles.Processing,'bw',0);
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Processing wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Processing_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 pushbutton12.
function pushbutton12_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton12 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton13.
function pushbutton13_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton13 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton14.
function pushbutton14_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton14 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton15.
function pushbutton15_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton15 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton16.
function pushbutton16_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton16 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in InputImage.
function InputImage_Callback(hObject, eventdata, handles)
% hObject handle to InputImage (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({'*.jpg';'*.bmp';'*.tif';'*.*'},'载入图像');
file=[pathname,filename];
% global S %设置一个全局变量S,保存初始图像路径,以便之后的还原操作
% S=file;
X=imread(file);
set(handles.imageshow1,'HandleVisibility','ON');
axes(handles.imageshow1);
imshow(X);
handles.img=X;
guidata(hObject,handles);
setappdata(handles.Processing,'X',X);
% --- Executes on button press in pushbutton9.
function pushbutton9_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton9 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function imageshow1_CreateFcn(hObject, eventdata, handles)
% hObject handle to imageshow1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% global fname fpath see
% [fname,fpath]=uigetfile('*.*','打开');
% see=[fpath fname];
% I=imread(see);
% axes(handles.axes1);
% imshow(I,'notruesize');
% title('原始图片');
% --- Executes on button press in pushbutton22.
function pushbutton22_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton22 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in preprocess.
function preprocess_Callback(hObject, eventdata, handles)
% hObject handle to preprocess (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
X=getappdata(handles.Processing,'X');
I = rgb2gray(X);
threshold = graythresh(X(:,:,2));%前景背景阈值分割
bw = im2bw(I,threshold);%二值化
set(handles.imageshow2,'HandleVisibility','ON');
axes(handles.imageshow2);
imshow(bw);
handles.img=bw;
guidata(hObject,handles);
setappdata(handles.Processing,'bw',bw);
% % --- Executes on button press in edge.
% function edge_Callback(hObject, eventdata, handles)
% % hObject handle to edge (see GCBO)
% % eventdata reserved - to be defined in a future version of MATLAB
% % handles structure with handles and user data (see GUIDATA)
% bw=getappdata(handles.Processing,'bw');
%
% K=medfilt2(bw,[3,3]); % 中值滤波
% ed=edge(K,'prewitt',0.04);
% set(handles.imageshow2,'HandleVisibility','ON');
% axes(handles.imageshow2);
% imshow(ed);
% handles.img=ed;
% guidata(hObject,handles);
% --- Executes on button press in fourier.
function fourier_Callback(hObject, eventdata, handles)
% hObject handle to fourier (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%边界链码的生成
% ed=getappdata(handles.Processing,'ed');
bw=getappdata(handles.Processing,'bw');
B=boundaries(~bw,8,'cw');
[nr,nc]=size(B{1});%nr为边界的点数
Code=[];
for i=2:nr
x=B{1}(i,1)-B{1}(i-1,1);
y=B{1}(i,2)-B{1}(i-1,2);
if x==1&y==0
Code=[Code,0];
elseif x==1&y==1
Code=[Code,1];
elseif x==0&y==1
Code=[Code,2];
elseif x==-1&y==1
Code=[Code,3];
elseif x==-1&y==0
Code=[Code,4];
elseif x==-1&y==-1
Code=[Code,5];
elseif x==0&y==-1
Code=[Code,6];
elseif x==1&y==-1
Code=[Code,7];
end
end
Code2=[Code,Code(1)]; %用于生成差分链码时的后补第一元素的链码。
%生成差分链码
SubCode=[];
for i=2:nr
SubCode=[SubCode,Code2(i)-Code2(i-1)];
end
[lr,ln]=size(SubCode);
for i=1:ln
if SubCode(i)<0
SubCode(i)=SubCode(i)+8;
end
end
edgg=fft(SubCode); % 分析边缘对应的傅里叶描述子
edgg=abs(edgg);
% edgg1=edgg(2:end);
edgg1=edgg/edgg(1);
databtest(2:10)=edgg1(1:9);
% figure;
% plot(databtest)
% 导入四类
load datab % 160*10
%---------------------------------------------------
% 训练样本与测试样本
P = datab(:,2:10)';% 训练样本
T = datab(:,1)';% 训练目标
p = databtest(:,2:10)'; % 测试样本
%---------------------------------------------------
% 函数接口赋值
NodeNum = 10; % 隐层节点数
TypeNum = 1; % 输出维数
Epochs = 2000; % 训练次数
% 设置网络参数
% RTF1 = 'tansig';TF2 = 'purelin'; % 缺省值
% TF1 = 'tansig';TF2 = 'logsig';
TF1 = 'logsig';TF2 = 'purelin'
%TF1 = 'tansig';TF2 = 'tansig';
% TF1 = 'logsig';TF2 = 'logsig';
%TF1 = 'purelin';TF2 = 'purelin';
net = newff(minmax(P),[NodeNum TypeNum],{TF1 TF2},'trainlm','learngdm');
% 指定训练参数
%net.trainFcn = 'trainlm'; % 内存使用最多(快)
%net.trainFcn = 'trainbfg';
%net.trainFcn = 'trainrp'; % 内存使用最少(慢)
%net.trainFcn = 'traingda'; % 变学习率
%net.trainFcn = 'traingdx';
net.trainParam.epochs = Epochs; % 最大训练次数
net.trainParam.goal = 1e-3; % 最小均方误差
net.trainParam.min_grad = 1e-20; % 最小梯度
net.trainParam.show = 100; % 训练显示间隔
net.trainParam.time = inf; % 最大训练时间
net.trainParam.lr=0.05; %网络训练速率,学习率
% 训练与测试
net = train(net,P,T); % 训练
Y = sim(net,P); % 训练 - 输出为预测值
% plot(p,t,p,y,'o');
% y = full(compet(y)) % 竞争输出
% 结果统计
Result = sum(abs(Y-T))
y = sim(net,p);
y1=round(y);
% figure,plot(y1,'o');
% 设定每组输出信号
% if y1==1
% msgbox('Purplevine紫藤又名藤萝、朱藤,是优良的观花藤木植物');
% elseif y1==2
% msgbox('Oil Camellia ');
% elseif y1==3
% msgbox('萝摩');
% elseif y1==4
% msgbox('Red maple,Scarlet maple ');
% end
y1=find_describe(y1);
setappdata(handles.Processing,'y1',y1);
% --- Executes on button press in pushbutton29.
function pushbutton29_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton29 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function result_CreateFcn(hObject, eventdata, handles)
% hObject handle to result (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
%X=getappdata(handles.Processing,'y1');
%y=find_describe(y1);
3 仿真结果
4 参考文献
[1]杨蒙蒙. 基于混合生物地理学优化算法和BP神经网络的树叶分类系统[D]. 南京师范大学.
部分理论引用网络文献,若有侵权联系博主删除。
5 MATLAB代码与数据下载地址
见博客主页