欢迎来到海神之光博客之家✅博主简介热爱科研的Matlab仿真开发者修心和技术同步精进个人主页海神之光代码获取方式海神之光Matlab王者学习之路—代码获取方式⛳️座右铭行百里者半于九十。更多Matlab图像处理仿真内容点击①Matlab图像处理进阶版②付费专栏Matlab图像处理初级版⛳️关注CSDN海神之光更多资源等你来⛄一、卷积神经网络(CNN)简介1 验证码获取及预处理1.1 验证码训练集与测试集图像处理库批量生成各项参数可调的数字验证码图片包括图片大小、格式、干扰点等自动生成3000张4位数字图片验证码 默认图片大小为60×160 RGB格式 包含少量的干扰点、线条及扭曲.本文使用其中的2400张图片作为训练集训练网络参数600张图片作为测试集测试网络识别的效果.训练集与测试集无交叉重叠.部分验证码样本示例图如图1所示.图1 验证码样本1.2验证码图片预处理1.2.1灰度化处理对于自动生成的RGB格式图片验证码 从图1中可以看出会有噪点、线条及相互连接等一定的干扰来模拟网络环境中的验证码.由于灰度图像的像素点变化范围较RGB格式的图像像素点小得多 因而在进行图像处理时 会首先进行灰度化图像.灰度化方法一般有分量法、平均值法、最大值法、加权平均法.本文采用加权平均法提取灰度图将原RGB图像三个分量的像素值以不同的权重进行加权平均后得到的像素值作为灰度值其常用的计算公式如下Gray0.2989R0.5780G0.1140B其中 Gray表示所求坐标(i j) 位置处的像素值 R G B分别为三个分量的坐标(ij) 位置的像素值.1.2.2二值化处理图像的二值化处理是指将灰度图像像素点的灰度值由某个阈值划分为两部分使图像显示出明显的黑色及白色效果便于对图像进一步处理使图像计算更为简单且有利于凸显出关注目标的轮廓.二值化操作的关键在于阈值的选取.本文中阈值设为200预处理前的图片与预处理后的图片对比如图2与图3所示.图2 预处理前的图片图3 预处理后的图片3 网络模型设计3.1 卷积神经网络概念CNN以二维矩阵数据形式输入 与传统的深度神经网络(Deep Neural Network DNN) 的不同在于其隐藏层的神经元仅与局部区域(即局部感受野)输入层的神经元相连.在结构上它主要由多个卷积层和池化组合而成.卷积层采用卷积来代替传统DNN的全连接 卷积层的每一个神经元只和前一层的一个局部窗口即感受野的神经元相连 构成卷积核.卷积核在卷积操作时对应的权值和偏移值共享 使得CNN的训练简单化 提高了迭代效率.池化目的是降维能够简化卷积层的输出参数提高所提取特征的鲁棒性.特征图像经过池化操作后通道数不会改变.下采样尺度为2*2的池化应用频率非常高其效果相当于高度和宽度缩减一半大大降低了模型参数.激活函数的作用是为了增加神经网络模型的非线性从而提升神经网络模型表达能力解决线性模型所不能解决的问题.不同的激活函数带来的效果有一定的差异 从计算量、梯度消失、反向传播求误差等多方面考虑 sigmoid函数在BP神经网络中用得较多 目前Re Lu函数及其改进函数在CNN中用得较多.损失函数是用来衡量模型的预测值与真实值不一致的程度从而评估模型的好坏.神经网络优化的过程实质就是最小化损失函数的过程损失函数越小说明模型的预测值愈接近真实值模型的健壮性也就越好.22 网络模型的设计与实现本次实验中采用的神经网络模型共四层前三层每层进行两次卷积操作和一次池化操作第四层为全连接层结构如图4所示.图4 网络结构⛄二、部分源代码function varargout interface(varargin)% INTERFACE MATLAB code for interface.fig% INTERFACE, by itself, creates a new INTERFACE or raises the existing% singleton*.%% H INTERFACE returns the handle to a new INTERFACE or the handle to% the existing singleton*.%% INTERFACE(‘CALLBACK’,hObject,eventData,handles,…) calls the local% function named CALLBACK in INTERFACE.M with the given input arguments.%% INTERFACE(‘Property’,‘Value’,…) creates a new INTERFACE or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before interface_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to interface_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 interfaceglobal iteration;global trainnet;global recognet;global testnet;% Last Modified by GUIDE v2.5 21-Jun-2018 12:51:57% Begin initialization code - DO NOT EDITgui_Singleton 1;gui_State struct(‘gui_Name’, mfilename, …‘gui_Singleton’, gui_Singleton, …‘gui_OpeningFcn’, interface_OpeningFcn, …‘gui_OutputFcn’, interface_OutputFcn, …‘gui_LayoutFcn’, [] , …‘gui_Callback’, []);if nargin ischar(varargin{1})gui_State.gui_Callback str2func(varargin{1});endif nargout[varargout{1:nargout}] gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% — Executes just before interface is made visible.function interface_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 interface (see VARARGIN)global iteration;global trainnet;global recognet;global testnet;iteration 10;trainnet ‘cnn_net_500’;cnnnetmat dir(fullfile(‘*.mat’));for i 1:length(cnnnetmat)str{i} cnnnetmat(i).name;endset(handles.popupmenu2,‘String’,str);set(handles.popupmenu1,‘String’,str);recognet cnnnetmat(1).name;testnet cnnnetmat(1).name;% Choose default command line output for interfacehandles.output hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes interface wait for user response (see UIRESUME)% uiwait(handles.figure1);% — Outputs from this function are returned to the command line.function varargout interface_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 structurevarargout{1} handles.output;% — 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)global testnet;[ err_rate ] test_cnn( testnet );set(handles.text3,‘String’,[num2str(err_rate*100) ‘%’]);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 doubleglobal iteration;iteration str2double(get(hObject,‘String’));% — 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 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)global iteration;global trainnet;train_cnn( iteration );trainnetname strcat(trainnet,‘.mat’);dos([‘rename’ 32 ‘cnn_net.mat’ 32 trainnetname]);cnnnetmat dir(fullfile(‘*.mat’));for i 1:length(cnnnetmat)str{i} cnnnetmat(i).name;endset(handles.popupmenu2,‘String’,str);set(handles.popupmenu1,‘String’,str);% — 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)global tr_dir;tr_dir uigetdir({},‘选择文件夹’);picture dir(fullfile(tr_dir,‘.bmp.png’));tr_dir strcat(tr_dir,‘’);for i 1:length(picture)str{i} picture(i).name;endset(handles.listbox1,‘String’,str);% — Executes on selection change in listbox1.function listbox1_Callback(hObject, eventdata, handles)% hObject handle to listbox1 (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 listbox1 contents as cell array% contents{get(hObject,‘Value’)} returns selected item from listbox1global recognet;global tr_dir;contents cellstr(get(hObject,‘String’));fn contents{get(hObject,‘Value’)};fn [tr_dir fn];[ result ] recog_cnn( imread(fn) , recognet );set(handles.text6,‘String’,result);axes(handles.axes1);imshow(imread(fn));% — Executes during object creation, after setting all properties.function listbox1_CreateFcn(hObject, eventdata, handles)% hObject handle to listbox1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: listbox 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 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)global recognet;[fn,impathname,~]uigetfile(‘*.bmp’,‘选择图片’);fn[impathname fn];[ result ] recog_cnn( imread(fn) , recognet );set(handles.text6,‘String’,result);axes(handles.axes1);imshow(imread(fn));% — 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 popupmenu1global testnet;contents cellstr(get(hObject,‘String’));testnet contents{get(hObject,‘Value’)};% — Executes during object creation, after setting all properties.function popupmenu1_CreateFcn(hObject, eventdata, handles)% hObject handle to popupmenu1 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu 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’);endfunction 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 doubleglobal trainnet;trainnet get(hObject,‘String’);% — 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 on selection change in popupmenu2.function popupmenu2_Callback(hObject, eventdata, handles)% hObject handle to popupmenu2 (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 popupmenu2 contents as cell array% contents{get(hObject,‘Value’)} returns selected item from popupmenu2global recognet;contents cellstr(get(hObject,‘String’));recognet contents{get(hObject,‘Value’)};% — Executes during object creation, after setting all properties.function popupmenu2_CreateFcn(hObject, eventdata, handles)% hObject handle to popupmenu2 (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles empty - handles not created until after all CreateFcns called% Hint: popupmenu 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⛄三、运行结果⛄四、matlab版本及参考文献1 matlab版本2014a2 参考文献[1]黎秋宇.基于卷积循环神经网络的不定长验证码识别[J].现代信息科技. 2021,5(07)3 备注简介此部分摘自互联网仅供参考若侵权联系删除 仿真咨询1 各类智能优化算法改进及应用生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化2 机器学习和深度学习方面卷积神经网络CNN、LSTM、支持向量机SVM、最小二乘支持向量机LSSVM、极限学习机ELM、核极限学习机KELM、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断3 图像处理方面图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知4 路径规划方面旅行商问题TSP、车辆路径问题VRP、MVRP、CVRP、VRPTW等、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化5 无人机应用方面无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配6 无线传感器定位及布局方面传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化7 信号处理方面信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化8 电力系统方面微电网优化、无功优化、配电网重构、储能配置9 元胞自动机方面交通流 人群疏散 病毒扩散 晶体生长10 雷达方面卡尔曼滤波跟踪、航迹关联、航迹融合