0
点赞
收藏
分享

微信扫一扫

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

一、案例

本项目基于MATLAB完成数字验证码识别的GUI设计,图像处理,验证码生成、识别等功能。采用BP神经网络来实现对验证码图像的识别。验证码的识别,大概分为图片预处理、分割字符、识别字符三个过程,其中分割字符最为困难。本文采用基于遗传算法和最大熵优化的图像分割技术、大津法(OTSU)、自定义阈值三种技术进行字符分割,并作进一步分析。利用英国萨里大学提供的印刷体数字数据集,共10160张图片,90%的数据用于训练BP神经网络,剩余10%的数据用于测试,最终识别准确率达到93.47%,利用训练所得BP模型完成识别字符,最终验证码图像识别效果较佳。

二、验证码识别简介

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 预处理后的图片

三、部分源代码

function varargout = appgui(varargin)
% APPGUI MATLAB code for appgui.fig
%      APPGUI, by itself, creates a new APPGUI or raises the existing
%      singleton*.
%
%      H = APPGUI returns the handle to a new APPGUI or the handle to
%      the existing singleton*.
%
%      APPGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in APPGUI.M with the given input arguments.
%
%      APPGUI('Property','Value',...) creates a new APPGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before appgui_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to appgui_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 appgui

% Last Modified by GUIDE v2.5 27-Dec-2021 00:45:34

% Begin initialization code - DO NOT EDIT
global I_white
I_white = uint8(ones(80,200)*255);
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @appgui_OpeningFcn, ...
                   'gui_OutputFcn',  @appgui_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 appgui is made visible.
function appgui_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 appgui (see VARARGIN)
h = handles.figure1; %获取句柄
newIcon = javax.swing.ImageIcon('.\Icon.png'); %读取图片文件
figFrame = get(h,'JavaFrame');
figFrame.setFigureIcon(newIcon);
% Choose default command line output for appgui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes appgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = appgui_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 testCode_btn.
function testCode_btn_Callback(hObject, eventdata, handles)
% hObject    handle to testCode_btn (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global str filename level bpnn lastfilename lastway change_level

if isequal(level,[])
    level = 0.9;
end
way = get(handles.way_num, 'value');
if filename == 0 
    h=warndlg('请先选择一张图片!','警告','modal');
    return
end
if isequal(lastfilename,filename) && way ==lastway && change_level == 0
    h=warndlg('请重新选择一张图片或方法!','警告','modal');
    return
end

load bp.mat
bpnn = net;
way = get(handles.way_num, 'value');
[resultCode,gray_I,BW_I,Med_I,bwarea_I,Idx] = testcode(bpnn,str,filename,false,way,level);
axes(handles.axes3);
imshow(gray_I);
axes(handles.axes4);
imshow(BW_I);
axes(handles.axes5);
imshow(Med_I);
axes(handles.axes6);
imshow(bwarea_I);
axes(handles.axes7);
imshow(bwarea_I);
[L,num] = bwlabel(bwarea_I,8); %找到图中的连通域,num为连通域数量
for i=1:num
    hold on
    plot([Idx(i,3),Idx(i,3)],[Idx(i,1),Idx(i,2)],'r--','LineWidth', 2);
    hold on
    plot([Idx(i,4),Idx(i,4)],[Idx(i,1),Idx(i,2)],'r--','LineWidth', 2);
    hold on
    plot([Idx(i,3),Idx(i,4)],[Idx(i,1),Idx(i,1)],'r--','LineWidth', 2);
    hold on
    plot([Idx(i,3),Idx(i,4)],[Idx(i,2),Idx(i,2)],'r--','LineWidth', 2);
end
code = '';
for i=1:length(resultCode)
    code =[code,num2str(resultCode(i))];
end
set(handles.result_edit,'String',code)
lastfilename = filename;
lastway = way;
change_level = 0;



function result_Callback(hObject, eventdata, handles)
% hObject    handle to result (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 result as text
%        str2double(get(hObject,'String')) returns contents of result as a double


% --- Executes during object creation, after setting all properties.
function result_CreateFcn(hObject, eventdata, handles)
% hObject    handle to result (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



function level_num_Callback(hObject, eventdata, handles)
% hObject    handle to level_num (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 level_num as text
%        str2double(get(hObject,'String')) returns contents of level_num as a double


% --- Executes during object creation, after setting all properties.
function level_num_CreateFcn(hObject, eventdata, handles)
% hObject    handle to level_num (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 pushbutton19.
function pushbutton19_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton19 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
global level change_level
level_temp = get(handles.level_num,'String');
if ~isequal(size(level_temp),[1 0])
    level = str2double(level_temp);
    msgbox('灰度值修改成功!', '提示');
    change_level = 1;
else
    h=warndlg('请输入数值!','警告','modal');
end

四、运行结果

在这里插入图片描述

五、matlab版本及参考文献

1 matlab版本
2014a

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

举报

相关推荐

0 条评论