找回密码
 注册
关于网站域名变更的通知
查看: 629|回复: 3
打印 上一主题 下一主题

基于matlab CNN卷积神经网络之验证码识别

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2021-6-1 10:38 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

EDA365欢迎您登录!

您需要 登录 才可以下载或查看,没有帐号?注册

x
' E5 a, X2 f) }8 n, M
一、卷积神经网络(CNN)简介
9 B% r* l( o2 o1 a* T关于什么是卷积神经网络(CNN),请自行查阅资料进行学习。如果是初学者,这里推荐一下台湾的李宏毅的深度学习课程。链接就不给了,这些资料网站上随处可见。
8 s/ d# v- b) E3 b7 W+ [6 Q$ {, O3 Q) s8 E
值得一提的是,CNN虽然在图像处理的领域具有不可阻挡的势头,但是它绝对不仅仅只能用来图像处理领域,大家熟知的alphaGo下围棋也可以通过CNN的结构进行处理,因为下围棋与图像有着相似之处,所以说,CNN提供给我们的是一种处理问题的思想,有学者归纳出了可以用CNN解决的问题所具备的三个性质:
1 R9 M$ O2 R4 x6 T/ `
/ C1 z8 U' ]" W8 K; _* v- i局部性: Y! J6 T+ g" s2 g! S+ B% Q! U
对于一张图片而言,需要检测图片中的特征来决定图片的类别,通常情况下这些特征都不是由整张图片决定的,而是由一些局部的区域决定的。例如在某张图片中的某个局部检测出了鸟喙,那么基本可以判定图片中有鸟这种动物。$ E2 T1 z) N% K7 ?. X2 t
1 V( {# G2 h4 i' z+ U! f, Q7 I( F
相同性) Z6 t3 q$ ^! M5 M$ }
对于不同的图片,它们具有同样的特征,这些特征会出现在图片的不同位置,也就是说可以用同样的检测模式去检测不同图片的相同特征,只不过这些特征处于图片中不同的位置,但是特征检测所做的操作几乎一样。例如在不同的图片中,虽然鸟喙处于不同的位置,但是我们可以用相同的模式去检测。
5 G0 F! f- [5 r6 w9 @4 A$ \4 [% o' g7 U2 z& `
不变性! l. M: k" r$ x! V
对于一张图片,如果我们进行下采样,那么图片的性质基本保持不变。
7 z0 a: k- g; b) ~5 N  t" A; }: \, f/ K2 Q- }
验证码识别原理) U! R% {6 L( x$ c) Y) Z$ M
Matlab对图像读入处理,去掉噪声点和较浅的点,进行二值化,将图像转变为0/1矩阵,这样就完成了预处理。
% e* G) u6 R$ k0 H- c! r然后要对图像进行切割,取到每个数字的小图片位置,将其缩放至等大小,方便神经网络进一步处理。0 t% l2 K" n" J
最后将图片转成神经网络能够识别的格式,例如BP网络,则将其转为行向量,深卷积网络,则将其转为矩阵即可。
+ T9 b4 s& i: ?  d# ~9 b9 A# j' H4 x4 j
4 l5 [  c6 `1 A; F- Q4 q# k
二、源代码7 W8 h: @. {6 s" `) q# ~
5 P8 m4 f! m$ ^: S
  • 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% c3 h( l' z1 G: ?
                                                   
9 w9 M) C) _% r5 ?+ }( W" T2 {- S4 ]( _3 u6 ^
三、运行结果& k" {" C( u7 y, x! u
0 l. G0 d" g0 J1 F+ S
; e5 C3 D  J6 v: v, [  ?1 ]3 T2 u/ q( M
1 G2 W/ C. g: s! W* G4 {. k. S& v) I4 k

该用户从未签到

2#
发表于 2021-6-1 13:13 | 只看该作者
基于matlab CNN卷积神经网络之验证码识别
  • TA的每日心情
    难过
    2019-11-20 15:02
  • 签到天数: 1 天

    [LV.1]初来乍到

    3#
    发表于 2021-6-1 13:14 | 只看该作者
    基于matlab CNN卷积神经网络之验证码识别

    该用户从未签到

    4#
    发表于 2021-6-1 13:14 | 只看该作者
    基于matlab CNN卷积神经网络之验证码识别
    您需要登录后才可以回帖 登录 | 注册

    本版积分规则

    关闭

    推荐内容上一条 /1 下一条

    EDA365公众号

    关于我们|手机版|EDA365电子论坛网 ( 粤ICP备18020198号-1 )

    GMT+8, 2025-10-29 15:08 , Processed in 0.156250 second(s), 26 queries , Gzip On.

    深圳市墨知创新科技有限公司

    地址:深圳市南山区科技生态园2栋A座805 电话:19926409050

    快速回复 返回顶部 返回列表