一、卷积神经网络(CNN)简介

1 验证码获取及预处理
1.1 验证码训练集与测试集
图像处理库批量生成各项参数可调的数字验证码图片,包括图片大小、格式、干扰点等,自动生成3000张4位数字图片验证码, 默认图片大小为60×160, RGB格式, 包含少量的干扰点、线条及扭曲.本文使用其中的2400张图片作为训练集训练网络参数,600张图片作为测试集测试网络识别的效果.训练集与测试集无交叉重叠.部分验证码样本示例图
如图1所示.
【验证码识别】基于matlab CNN卷积神经网络验证码识别【含Matlab源码 098期】_卷积
图1 验证码样本
1.2验证码图片预处理
1.2.1灰度化处理
对于自动生成的RGB格式图片验证码, 从图1中可以看出会有噪点、线条及相互连接等一定的干扰来模拟网络环境中的验证码.由于灰度图像的像素点变化范围较RGB格式的图像像素点小得多, 因而在进行图像处理时, 会首先进行灰度化图像.灰度化方法一般有分量法、平均值法、最大值法、加权平均法.本文采用加权平均法提取灰度图,将原RGB图像三个分量的像素值以不同的权重进行加权平均后得到的像素值作为灰度值,其常用的计算公式如下:Gray=0.2989R+0.5780G+0.1140B
其中, Gray表示所求坐标(i, j) 位置处的像素值, R, G, B分别为三个分量的坐标(ij) 位置的像素值.

1.2.2二值化处理
图像的二值化处理,是指将灰度图像像素点的灰度值由某个阈值划分为两部分,使图像显示出明显的黑色及白色效果,便于对图像进一步处理,使图像计算更为简单,且有利于凸显出关注目标的轮廓.
二值化操作的关键在于阈值的选取.本文中阈值设为200,预处理前的图片与预处理后的图片对比如图2与图3所示.
【验证码识别】基于matlab CNN卷积神经网络验证码识别【含Matlab源码 098期】_卷积_02
图2 预处理前的图片
【验证码识别】基于matlab CNN卷积神经网络验证码识别【含Matlab源码 098期】_神经网络_03
图3 预处理后的图片
3 网络模型设计
3.1 卷积神经网络概念
CNN以二维矩阵数据形式输入, 与传统的深度神经网络(Deep Neural Network, DNN) 的不同在于其隐藏层的神经元仅与局部区域(即局部感受野)输入层的神经元相连.在结构上,它主要由多个卷积层和池化组合而成.卷积层采用卷积来代替传统DNN的全连接, 卷积层的每一个神经元只和前一层的一个局部窗口即感受野的神经元相连, 构成卷积核.卷积核在卷积操作时对应的权值和偏移值共享, 使得CNN的训练简单化, 提高了迭代效率.池化,目的是降维,能够简化卷积层的输出参数,提高所提取特征的鲁棒性.特征图像经过池化操作后通道数不会改变.下采样尺度为2*2的池化,应用频率非常高,其效果相当于高度和宽度缩减一半,大大降低了模型参数.激活函数的作用是为了增加神经网络模型的非线性,从而提升神经网络模型表达能力,解决线性模型所不能解决的问题.不同的激活函数带来的效果有一定的差异, 从计算量、梯度消失、反向传播求误差等多方面考虑, sigmoid函数在BP神经网络中用得较多, 目前Re Lu函数及其改进函数在CNN中用得较多.
损失函数,是用来衡量模型的预测值与真实值不一致的程度,从而评估模型的好坏.神经网络优化的过程实质就是最小化损失函数的过程,损失函数越小,说明模型的预测值愈接近真实值,模型的健壮性也就越好.
22 网络模型的设计与实现
本次实验中,采用的神经网络模型共四层,前三层每层进行两次卷积操作和一次池化操作,第四层为全连接层,结构如图4所示.
【验证码识别】基于matlab CNN卷积神经网络验证码识别【含Matlab源码 098期】_cnn_04
图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 interface
global iteration;
global trainnet;
global recognet;
global testnet;
% Last Modified by GUIDE v2.5 21-Jun-2018 12:51:57
 
% Begin initialization code - DO NOT EDIT
gui_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});
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 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;
end
set(handles.popupmenu2,'String',str);
set(handles.popupmenu1,'String',str);
recognet = cnnnetmat(1).name;
testnet =  cnnnetmat(1).name;
% Choose default command line output for interface
handles.output = hObject;
 
% Update handles structure
guidata(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 structure
varargout{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 double
global 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;
end
set(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;
end
set(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 listbox1
global 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 popupmenu1
global 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');
end
 
 
 
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
global 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 popupmenu2
global 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 CNN卷积神经网络验证码识别【含Matlab源码 098期】_matlab_05

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1] 蔡利梅.MATLAB图像处理——理论、算法与实例分析[M].清华大学出版社,2020.
[2]杨丹,赵海滨,龙哲.MATLAB图像处理实例详解[M].清华大学出版社,2013.
[3]周品.MATLAB图像处理与图形用户界面设计[M].清华大学出版社,2013.
[4]刘成龙.精通MATLAB图像处理[M].清华大学出版社,2015.