一、项目简介
本系统基于MATLAB平台,利用卷积神经网络(CNN)与GUI界面,实现5种不同花卉种类的自动图像识别。训练模块中,系统通过imageDatastore自动加载dbx文件夹中的图像并按子文件夹命名标注标签,按8:2随机划分为训练集与测试集。自定义CNN网络输入层为112×112×3的RGB图像,包含4个卷积层(卷积核从3×3到5×5,通道数从32→16→16→32)、批归一化层、ReLU激活层及步长为2的最大池化层,最终经全连接层(输出5维)、Softmax层和分类层完成分类。采用SGDM优化器,训练25轮,小批量大小20,并实时绘制训练进度曲线。GUI模块基于GUIDE设计,支持BMP图像选取、模型载入、单张识别(结果弹窗显示)及测试集总体准确率评估,操作直观便捷,适用于植物学教学与智能农业等场景
二、部分源码
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)
fn,pn,\~=uigetfile('*.bmp','请选择所要识别的图像');
I = imread(pn fn);
axes(handles.axes1);
imshow(I,\[\]);
title('所选图像');
handles.I = I;
guidata(gcbo,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)
load("CNNnet.mat");
y_pred = classify(CNNnet,handles.I);
% disp(y_pred);
set(handles.edit1,'string',y_pred);
result=strcat('花卉种类:',string(y_pred),'');
msgbox(result,'识别结果','warn')
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- 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)
%% 加载数据
allImages = imageDatastore('dbx', ...
'IncludeSubfolders' ,true, ...
'LabelSource' , 'foldernames' );% 图像加载为图像数据存储
% imageDatastore函数会根据文件夹名称自动标记图像
% 划分训练集(80%)和测试集(20%)
imgsTrain,imgsTest = splitEachLabel(allImages,0.8,'randomized');
load("CNNnet.mat");
y_pred = classify(CNNnet,imgsTest); % 使用训练好的网络测试
accuracy = mean(y_pred == imgsTest.Labels);% 计算准确率
set(handles.text2,'string','总体准确率: ',num2str(100\*accuracy),'%','FontSize',12);
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)·
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes during object creation, after setting all properties.
function text1_CreateFcn(hObject, eventdata, handles)
% hObject handle to text1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
load("CNNnet.mat");
msgbox('网络模型已加载!','提示')
三、运行结果



四、总结
系统测试集总体准确率达84.375%,验证了自定义CNN在花卉分类中的有效性,能够提取花瓣形状、颜色和纹理等关键特征。GUI界面友好,用户无需编程即可完成识别全流程,兼具实用与教学价值。但系统仍存在不足:网络结构较浅(仅4层卷积),对姿态变化和复杂背景的泛化能力有限;仅支持BMP格式,未覆盖JPG、PNG等常用格式;未显示各类别准确率,难以定位易误判类别;训练过程缺乏数据增强,限制模型多样性适应能力。后续可通过迁移学习(如ResNet)、数据增强(旋转、缩放)、多格式支持及混淆矩阵可视化进一步提升性能与鲁棒性。
五、代码获取
接matlab程序定制和论文设计,方向如下:
图像处理|语音识别|图像识别|目标检测|深度学习|神经网络|强化学习|机器学习|通信系统|信号处理|时频分析|小波降噪|路径规划|优化算法|智能算法|数据处理|数学建模|文献复现|算法复现|模型复现等
程序包运行成功,零基础的可以远程帮你运行,赠送安装包。
作为初学者,遇见不会的问题是非常正常的事情,具体代码仿真可通过主页 私信博主。