【图像处理】基于模板匹配的验证码识别
识别思路:这个验证码比较规则,数字都是显示在固定的区域,数字也无粘连,实现步骤如下
图片去噪
对图像进行分割,分割成一个图像显示一个数字
对每个图像进行灰化处理,就是设置一个阈值将他们变成黑白图片
建立一个标准的数字图像库
将每个被分割的小图片与标准库比较,像素点重合最多的就是该数字
function varargout = MainForm(varargin)MAINFORM MATLAB code for MainForm.figMAINFORM, by itself, creates a new MAINFORM or raises the existingsingleton*.%H = MAINFORM returns the handle to a new MAINFORM or the handle tothe existing singleton*.%MAINFORM('CALLBACK',hObject,eventData,handles,...) calls the localfunction named CALLBACK in MAINFORM.M with the given input arguments.%MAINFORM('Property','Value',...) creates a new MAINFORM or raises theexisting singleton*. Starting from the left, property value pairs areapplied to the GUI before MainForm_OpeningFcn gets called. Anunrecognized property name or invalid value makes property applicationstop. All inputs are passed to MainForm_OpeningFcn via varargin.%*See GUI Options on GUIDE's Tools menu. Choose "GUI allows only oneinstance to run (singleton)".%See also: GUIDE, GUIDATA, GUIHANDLESEdit the above text to modify the response to help MainFormLast Modified by GUIDE v2.5 28-Jun-2013 10:42:40Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...gui_Singleton, ...@MainForm_OpeningFcn, ...@MainForm_OutputFcn, ...[] , ...[]);if nargin && ischar(varargin{1})= str2func(varargin{1});endif nargout:nargout}] = gui_mainfcn(gui_State, varargin{:});elsevarargin{:});endEnd initialization code - DO NOT EDIT--- Executes just before MainForm is made visible.function MainForm_OpeningFcn(hObject, eventdata, handles, varargin)This function has no output args, see OutputFcn.hObject handle to figureeventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)varargin command line arguments to MainForm (see VARARGIN)Choose default command line output for MainForm= hObject;clc;'Box', 'on', 'Color', 'c', 'XTickLabel', '', 'YTickLabel', '');'Box', 'on', 'Color', 'c', 'XTickLabel', '', 'YTickLabel', '');'String', '');= 0;= 0;= 0;= 0;Update handles structurehandles);UIWAIT makes MainForm wait for user response (see UIRESUME)uiwait(handles.figure1);--- Outputs from this function are returned to the command line.function varargout = MainForm_OutputFcn(hObject, eventdata, handles)varargout cell array for returning output args (see VARARGOUT);hObject handle to figureeventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)Get default command line output from handles structure= handles.output;function varargout = pushbutton1_CreateFcn(hObject, eventdata, handles)--- 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 MATLABhandles structure with handles and user data (see GUIDATA)载入图像file = fullfile(pwd, 'test/下载.jpg');Pathname] = uigetfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...Files' }, '载入验证码图像',...file);if isequal(Filename, 0) || isequal(Pathname, 0)return;end显示图像cla reset;cla reset;'Box', 'on', 'Color', 'c', 'XTickLabel', '', 'YTickLabel', '');'Box', 'on', 'Color', 'c', 'XTickLabel', '', 'YTickLabel', '');'String', '');存储fileurl = fullfile(Pathname,Filename);Img = imread(fileurl);[], 'Parent', handles.axes1);'String', '验证码图像');= fileurl;= Img;handles);--- 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 MATLABhandles structure with handles and user data (see GUIDATA)if isequal(handles.Img, 0)return;endImg = handles.Img;颜色空间转换hsv = rgb2hsv(Img);h = hsv(:, :, 1);s = hsv(:, :, 2);v = hsv(:, :, 3);定位噪音点bw1 = h > 0.16 & h < 0.30;bw2 = s > 0.65 & s < 0.80;bw = bw1 & bw2;过滤噪音点Imgr = Img(:, :, 1);Imgg = Img(:, :, 2);Imgb = Img(:, :, 3);= 255;= 255;= 255;去噪结果Imgbw = cat(3, Imgr, Imgg, Imgb);[], 'Parent', handles.axes2);'String', '验证码图像去噪');= Imgbw;handles);--- 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 MATLABhandles structure with handles and user data (see GUIDATA)if isequal(handles.Imgbw, 0)return;endImgbw = handles.Imgbw;灰度化Ig = rgb2gray(Imgbw);二值化Ibw = im2bw(Ig, 0.8);常量参数sz = size(Ibw);cs = sum(Ibw, 1);mincs = min(cs);maxcs = max(cs);masksize = 16;初始化S1 = []; E1 = [];1对应开始,2对应结束flag = 1;s1 = 1;tol = maxcs;while s1 < sz(2)for i = s1 : sz(2)移动游标s2 = i;if cs(s2) < tol && flag == 1达到起始位置flag = 2;S1 = [S1 s2-1];break;elseif cs(s2) >= tol && flag == 2达到结束位置flag = 1;E1 = [E1 s2];break;endends1 = s2 + 1;end图像反色Ibw = ~Ibw;图像细化Ibw = bwmorph(Ibw, 'thin', inf);for i = 1 : length(S1)图像裁剪Ibwi = Ibw(:, S1(i):E1(i));面积滤波num] = bwlabel(Ibwi);stats = regionprops(L);Ar = cat(1, stats.Area);ind_maxAr] = max(Ar);recti = stats(ind_maxAr).BoundingBox;= recti(1) + S1(i) - 1;= recti(2);= recti(3);= recti(4);= recti;图像裁剪Ibwi = imcrop(Ibw, recti);rate = masksize/max(size(Ibwi));Ibwi = imresize(Ibwi, rate, 'bilinear');ti = zeros(masksize, masksize);rsti = round((size(ti, 1)-size(Ibwi, 1))/2);csti = round((size(ti, 2)-size(Ibwi, 2))/2);:rsti+size(Ibwi,1), csti+1:csti+size(Ibwi,2))=Ibwi;存储= ti;end[], 'Parent', handles.axes2); hold on;for i = 1 : length(Rect)Rect{i}, 'EdgeColor', 'r', 'LineWidth', 2);endhold off;'String', '验证码数字定位');= Ti;handles);--- 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 MATLABhandles structure with handles and user data (see GUIDATA)if isequal(handles.Ti, 0)return;end加入红色边框Ti = handles.Ti;It = [];spcr = ones(size(Ti{1}, 1), 3)*255;spcg = ones(size(Ti{1}, 1), 3)*0;spcb = ones(size(Ti{1}, 1), 3)*0;spc = cat(3, spcr, spcg, spcb);整合到一起It = [It spc];for i = 1 : length(Ti)ti = Ti{i};ti = cat(3, ti, ti, ti);ti = im2uint8(mat2gray(ti));It = [It ti spc];end[], 'Parent', handles.axes2); hold on;'String', '验证码归一化');--- Executes on button press in pushbutton5.function pushbutton5_Callback(hObject, eventdata, handles)hObject handle to pushbutton5 (see GCBO)eventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)if isequal(handles.Ti, 0)return;endTi = handles.Ti;比对识别fileList = GetAllFiles(fullfile(pwd, 'Databse'));Tj = [];for i = 1 : length(fileList)filenamei = fileList{i};name, ext] = fileparts(filenamei);if isequal(ext, '.jpg')ti = imread(filenamei);ti = im2bw(ti, 0.5);ti = double(ti);提取不变矩特征数据phii = invmoments(ti);开始比对OTj = [];for j = 1 : length(Ti)tij = double(Ti{j});phij = invmoments(tij);ad = norm(phii-phij);= filenamei;= ad;OTj = [OTj otij];endTj = [Tj; OTj];endend生成结果r = [];for i = 1 : size(Tj, 2)ti = Tj(:, i);adi = cat(1, ti.ad);ind] = min(adi);filenamei = ti(ind).filename;name, ext] = fileparts(filenamei);name = name(1);r = [r name];end'String', r);--- Executes on button press in pushbutton6.function pushbutton6_Callback(hObject, eventdata, handles)hObject handle to pushbutton6 (see GCBO)eventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)弹出对话框输入正确数值choice = questdlg('确定要更新验证码样本库?(请在识别有错误的情况进行更新!)', ......'确定','取消','取消');switch choicecase '确定'prompt={'请输入正确的验证码:'};name='手动入库';numlines=1;defaultanswer={''};answer=inputdlg(prompt,name,numlines,defaultanswer);if isempty(answer)return;endif isequal(handles.fileurl, 0)return;end入库fileurl = handles.fileurl;answer = answer{1};fileout = fullfile(pwd, sprintf('images/%s.jpg', answer));flag = 1;while 1if exist(fileout, 'file')fileout = fullfile(pwd, sprintf('images/%s_%d.jpg', answer, flag));flag = flag + 1;elsecopyfile(fileurl,fileout);fileout), '提示信息', 'modal');break;endendcase '取消'return;end--- Executes on button press in pushbutton7.function pushbutton7_Callback(hObject, eventdata, handles)hObject handle to pushbutton7 (see GCBO)eventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)退出菜单choice = questdlg('确定要退出系统?', ......'确定','取消','取消');switch choicecase '确定'close;case '取消'return;end--- Executes on button press in pushbutton8.function pushbutton8_Callback(hObject, eventdata, handles)hObject handle to pushbutton8 (see GCBO)eventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)GetDatabase();function edit1_Callback(hObject, eventdata, handles)hObject handle to edit1 (see GCBO)eventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)Hints: get(hObject,'String') returns contents of edit1 as textstr2double(get(hObject,'String')) returns contents of edit1 as a double--- 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 MATLABhandles empty - handles not created until after all CreateFcns calledHint: 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 pushbutton9.function pushbutton9_Callback(hObject, eventdata, handles)hObject handle to pushbutton9 (see GCBO)eventdata reserved - to be defined in a future version of MATLABhandles structure with handles and user data (see GUIDATA)= uiputfile({'*.jpg;*.tif;*.png;*.gif','All Image Files';...Files' },'Save Image',...'screen.jpg'));if isequal(FileName, 0) || isequal(PathName, 0)return;endf = getframe(gcf);f = frame2im(f);fullfile(PathName, FileName));'提示信息', 'modal');
