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

举报
海神之光 发表于 2022/05/29 05:32:28 2022/05/29
【摘要】 一、卷积神经网络(CNN)简介 1 验证码获取及预处理 1.1 验证码训练集与测试集 图像处理库批量生成各项参数可调的数字验证码图片,包括图片大小、格式、干扰点等,自动生成3000张4位数字图片验证码,...

一、卷积神经网络(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});
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

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282

三、运行结果

在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本
2014a

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

文章来源: qq912100926.blog.csdn.net,作者:海神之光,版权归原作者所有,如需转载,请联系作者。

原文链接:qq912100926.blog.csdn.net/article/details/112461628

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。