源码:
https://download.csdn.net/download/weixin_38719187/12135708
一、课题介绍
随着互联网技术的发展和应用,网络在给人们提供丰富资源和极大便利的同时,伴随而来的是互联网系统的安全性问题。而数字验证码的出现证书加强web系统安全性的产物。研究数字验证码的识别显得非常有必要。
本设计基于MATLAB hu不变矩,进行数字验证码的识别,建立一个模板库,采用GUI设计界面,自由读取数字验证码进行识别并且输出0-9这10个数字。
二、GUI界面设计
四、主程序
1、打开图像
file = fullfile(pwd, ‘test/下载.jpg’);
[Filename, Pathname] = uigetfile({’.jpg;.tif;.png;.gif’,‘All Image Files’;…
‘.’,‘All Files’ }, ‘载入验证码图像’,…
file);
if isequal(Filename, 0) || isequal(Pathname, 0)
return;
end
% 显示图像
axes(handles.axes1); cla reset;
axes(handles.axes2); cla reset;
set(handles.axes1, ‘Box’, ‘on’, ‘Color’, ‘c’, ‘XTickLabel’, ‘’, ‘YTickLabel’, ‘’);
set(handles.axes2, ‘Box’, ‘on’, ‘Color’, ‘c’, ‘XTickLabel’, ‘’, ‘YTickLabel’, ‘’);
set(handles.axes3, ‘Box’, ‘on’, ‘Color’, ‘c’, ‘XTickLabel’, ‘’, ‘YTickLabel’, ‘’);
set(handles.axes4, ‘Box’, ‘on’, ‘Color’, ‘c’, ‘XTickLabel’, ‘’, ‘YTickLabel’, ‘’);
%set(handles.text4, ‘String’, ‘’);
% 存储
fileurl = fullfile(Pathname,Filename);
Img = imread(fileurl);
imshow(Img, [], ‘Parent’, handles.axes1);
title(‘原图’)
2、去噪
% 颜色空间转换
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);
Imgr(bw) = 255;
Imgg(bw) = 255;
Imgb(bw) = 255;
% 去噪结果
Imgbw = cat(3, Imgr, Imgg, Imgb);
axes(handles.axes2)
imshow(Imgbw, [], ‘Parent’, handles.axes2);
%figure(3)
%imshow(Imgbw)
title(‘去噪结果图’)
3、定位
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;
end
end
s1 = s2 + 1;
end
4、分割
% 加入红色边框
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);
% 整合到一起
length(Ti)
It = [It spc];
for i = 1 : length(Ti)
ti = Ti{i};
ti = cat(3, ti, ti, ti);
ti = im2uint8(mat2gray(ti));
axes(handles.(sprintf(‘axes%d’, i+4)));
imshow(ti)
It = [It ti spc];
end
imshow(It, [], ‘Parent’, handles.axes4); hold on;
5、识别
% 比对识别
fileList = GetAllFiles(fullfile(pwd, ‘Databse’));%GetALLFiles.m这个是子函数,到文件夹对应看这个代码
Tj = [];
for i = 1 : length(fileList)%
filenamei = fileList{i};
[pathstr, name, ext] = fileparts(filenamei);
if isequal(ext, ‘.jpg’)
ti = imread(filenamei);
ti = im2bw(ti, 0.5);
ti = double(ti);
% 提取不变矩特征数据
phii = invmoments(ti);%invmoments.m
% 开始比对
OTj = [];
for j = 1 : length(Ti)
tij = double(Ti{j});
phij = invmoments(tij);
ad = norm(phii-phij);%不变矩对比做差
otij.filename = filenamei;
otij.ad = ad;
OTj = [OTj otij];
end
Tj = [Tj; OTj];
end
end
% 生成结果
r = [];
for i = 1 : size(Tj, 2)
ti = Tj(:, i);
adi = cat(1, ti.ad);
[minadi, ind] = min(adi);
filenamei = ti(ind).filename;
[pathstr, name, ext] = fileparts(filenamei);
name = name(1);
r = [r name];
end
set(handles.text11, ‘String’, r);