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

1 验证码获取及预处理
1.1 验证码训练集与测试集
图像处理库批量生成各项参数可调的数字验证码图片,包括图片大小、格式、干扰点等,自动生成3000张4位数字图片验证码, 默认图片大小为60×160, RGB格式, 包含少量的干扰点、线条及扭曲.本文使用其中的2400张图片作为训练集训练网络参数,600张图片作为测试集测试网络识别的效果.训练集与测试集无交叉重叠.部分验证码样本示例图
如图1所示.

图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所示.

图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 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});
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;
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');
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 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版本及参考文献

1 matlab版本
2014a

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

【验证码识别】基于matlab CNN卷积神经网络验证码识别【含Matlab源码 098期】相关推荐

  1. 【Matlab人脸识别】人脸实时检测与跟踪【含GUI源码 673期】

    一.代码运行视频(哔哩哔哩) [Matlab人脸识别]人脸实时检测与跟踪[含GUI源码 673期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]孟逸凡,柳益君 ...

  2. 【Matlab验证码识别】遗传算法和最大熵优化+大津法(OTSU)+自定义阈值数字验证码识别【含GUI源码 1694期】

    一.代码运行视频(哔哩哔哩) [Matlab验证码识别]遗传算法和最大熵优化+大津法(OTSU)+自定义阈值数字验证码识别[含GUI源码 1694期] 二.matlab版本及参考文献 1 matlab ...

  3. 卷积神经网络算法python实现车牌识别_车牌识别算法之CNN卷积神经网络

    原标题:车牌识别算法之CNN卷积神经网络 随着我国经济的发展,汽车,特别是小轿车的数量越来越多,智能交通管理系统应运而生.车牌智能自动识别作为智能交通管理系统中的重要组成部分,在智能交通管理中发挥着越 ...

  4. 基于 SoC 的卷积神经网络车牌识别系统设计(2-1)基于 Arm Cortex-M3 SoC 车牌识别系统的搭建

    基于 SoC 的卷积神经网络车牌识别系统设计(2-1)基于 Arm Cortex-M3 SoC 车牌识别系统的搭建 版权所有, ⌊ 新芯设计 ⌉ \lfloor新芯设计\rceil ⌊新芯设计⌉,转载 ...

  5. 基于 SoC 的卷积神经网络车牌识别系统设计(3-1)基于 Python 编程的车牌识别预处理、定位、分割、缩放的效果一览

    引言         这是一个在基于 OpenCV 的 Python 程序下,整体车牌定位.分割.识别的各个步骤的处理结果的展示,相当于算法的验证,只有先在软件上经过正确的严格的验证,才能进行硬件上的 ...

  6. 【Matlab人脸识别】BP神经网络人脸识别(含识别率)【含GUI源码 891期】

    一.代码运行视频(哔哩哔哩) [Matlab人脸识别]BP神经网络人脸识别(含识别率)[含GUI源码 891期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] ...

  7. 【Matlab人脸识别】形态学教室人数统计(带面板)【含GUI源码 1703期】

    一.代码运行视频(哔哩哔哩) [Matlab人脸识别]形态学教室人数统计(带面板)[含GUI源码 1703期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1]孟 ...

  8. 【Matlab身份证识别】身份证号码识别【含GUI源码 014期】

    一.代码运行视频(哔哩哔哩) [Matlab身份证识别]身份证号码识别[含GUI源码 014期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 蔡利梅.MAT ...

  9. 【Matlab树叶分类】BP神经网络植物叶片分类【含GUI源码 916期】

    一.代码运行视频(哔哩哔哩) [Matlab树叶分类]BP神经网络植物叶片分类[含GUI源码 916期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 蔡利梅 ...

  10. 【Matlab人脸识别】KL变换人脸识别【含GUI源码 859期】

    一.代码运行视频(哔哩哔哩) [Matlab人脸识别]KL变换人脸识别[含GUI源码 859期] 二.matlab版本及参考文献 1 matlab版本 2014a 2 参考文献 [1] 蔡利梅.MAT ...

最新文章

  1. Linux 下搭建 WordPress 个人站点
  2. 2020-08-23logloss对数损失函数
  3. 【Flutter】Flutter 应用主题 ( ThemeData | 动态修改主题 )
  4. 23种设计模式及OOP7大原则
  5. 分布式下服务注册的地位和原理
  6. 服务器防渗透(1)--信息收集
  7. ssm框架搭建连接mysql_从零开始搭建SSM框架(Spring + Spring MVC + Mybatis)
  8. 请问你写书法多少年了?领悟到了什么?
  9. 企业IT部门主管告诉你,DevOps给我们带来了这些变化
  10. android中ScrollView和GridView/ListView共存时,ScrollView不在顶部的解决方法
  11. android 方法不会覆盖或实现超类型的方法,React Native Android:方法不会覆盖或实现超类型的方法...
  12. 文件共享文件传输samba ftp nfs
  13. Exchange 2010升级到Exchange 2016汇总
  14. 我的世界服务器物品箱子,我的世界:使用箱子储存物品居多,难不成他们很“鸡肋”?...
  15. win7如何设置通电自动开机_Win7环境下如何设置操作系统自动开机/关机
  16. 2010QQ游戏登录器(分析+感叹)
  17. 计算机英语900句小e,计算机英语100句
  18. 小公司一个人如何进行测试
  19. 营销物料(内容)可复用,别忽视了这个神器的作用!
  20. 程序员今年必看!!拖更了三年带回了一个抖音,虎牙,哔哩哔哩都在用的库|墙裂推荐

热门文章

  1. ReentrantLock深入学习
  2. Jenkins checkout的文件 , TortoiseSVN 无法提交。 问题已经解决啦!
  3. Gravatar - globally recognized avatar
  4. 190617每日一句;我们可以普通,但我们必须拒绝平庸,别再去追随他人的脚步,开拓属于自己的道路
  5. 深度学习CNN, R-CNN
  6. Atitit 搜索工程师的知识点体系总结 目录 1.1. 理论类 索引 与查询 1 1.2. 类库类 1 1.3. 关联知识类 1 1.4. 其他 1 2. Ref 2 2.1. Atitit 文
  7. Atitit midi art tech midi的艺术 目录 1. MIDI 1 2. 4 组成结构 2 2.1. ▪ 序列器 2 2.2. ▪ 接口 2 2.3. ▪ 标准 常见的MIDI标准
  8. atitit.故障排除------有时会错误com.microsoft.sqlserver.jdbc.SQLServerException: Connection reset by peer: soc
  9. paip.提升中文分词准确度---新词识别
  10. paip. C#.NET循环获取不同随机数的方法根据时间