【手写数字识别】基于matlab GUI知识库手写数字识别(写字板+图片)【含Matlab源码 1227期】_数字手写系统代码-程序员宅基地

技术标签: matlab  Matlab图像处理(进阶版)  

博主简介:热爱科研的Matlab仿真开发者,修心和技术同步精进,Matlab项目合作可私信。
个人主页:海神之光
代码获取方式:
海神之光Matlab王者学习之路—代码获取方式
️座右铭:行百里者,半于九十。

更多Matlab仿真内容点击
Matlab图像处理(进阶版)
路径规划(Matlab)
神经网络预测与分类(Matlab)
优化求解(Matlab)
语音处理(Matlab)
信号处理(Matlab)
车间调度(Matlab)

一、手写数字识别技术简介

1 案例背景
手写体数字识别是图像识别学科下的一个分支,是图像处理和模式识别研究领域的重要应用之一,并且具有很强的通用性。由于手写体数字的随意性很大,如笔画粗细、字体大小、倾斜角度等因素都有可能直接影响到字符的识别准确率,所以手写体数字识别是一个很有挑战性的课题。在过去的数十年中,研究者们提出了许多识别方法,并取得了一定的成果。手写体数字识别的实用性很强,在大规模数据统计如例行年检、人口普查、财务、税务、邮件分拣等应用领域都有广阔的应用前景"。
本案例讲述了图像中手写阿拉伯数字的识别过程,对手写数字识别的基于统计的方法进行了简要介绍和分析,并通过开发一个小型的手写体数字识别系统来进行实验。手写数字识别系统需要实现手写数字图像的读取功能、特征提取功能、数字的模板特征库的建立功能及识别功能。

2 理论基础
2.1算法流程
首先,读入手写数字图片进行图像归一化处理,统一尺寸,默认为24x24图像块,并通过ostu算法进行二值化; 其次, 对二值化图像进行图像细化等形态学操作,并按照算法要求进行特征提取;最后,载入模板矩阵进行比对,选用欧式距离测度,得到识别结果。其算法流程图如图所示。
在这里插入图片描述
2.2 特征提取
根据手写数字图像本身的结构特征,通过计算端点、指定方向直线的交叉点个数来作为特征向量。其主要步骤如下。
(1)垂直交点
对细化后的手写数字图像分别在其列宽的:5/12 1/2 7/12处生成垂直的三条直线,提取这三条垂直直线与数字笔划的交点数并存储。
(2)水平交点
对细化后的手写数字图像分别在其列宽的号号处生成水平的三条直线,提取这1/3 1/2 2/3三条水平直线与数字笔划的交点数并存储。
(3)对角交点
对细化后的手写数字图像分别取两条对角直线,提取这两条对角直线与数字笔划的交点数并存储。
由于以上步骤均作用于经细化后的数字图像,其笔划简单且特征稳定,因此对其提取的基本交点及结构端点能反映数字的本质特征,可快速、有效地识别数字字符,并达到较好的识别正确率。
其中,提取笔划结构端点特征的算法如下。
①目标定位
对细化后的手写数字图像按行从上到下、按列从左到右进行顺序扫描,定位选择黑像素点P作为手写笔划目标。
②邻域统计
计算黑色像素P的8邻域之和N,若N=1,则像素P为端点,端点计数器加1;否则舍弃该点。
③遍历图像
遍历整个图像,重复进行目标定位、邻域统计的操作流程,提取端点特征。依据上述对手写数字图像的交点、端点特征提取方法, 本案例中的特征向量VEC由9个分量组成,其排列如下:
VEC=[垂直5/12处交点数, 垂直中线交点数, 垂直7/12处交点数,水平1/3处交点数,水平中线交点数,水平2/3处交点数,左对角线交点数,右对角线交点数,端点数]

2.3 模式识别
本案例采用的是基于模式知识库的识别方法,所以系统调研的关键步骤就是对数字字符的结构特征的分析及其模型的构造。因此,本案例首先对0~9这10个数字字符进行结构分析并建模,然后提取相关特征,最后构造模板库。在实验过程中,我们选择规范手写和自由手写两组样本作为训练样本对知识库进行参数调整,这些训练样本由200个规范手写体样本和200个自由手写体样本组成,通过计算样本对应分量的算术平均值获得知识库中特征向量的每个分量。
通过上述步骤得到的知识库由两套模板组成,在本次实验过程中,我们选择基于模板匹配的识别方法,通过计算欧式距离来衡量匹配程度。识别系统中的特征向量包含9个分量,且计算距离公式是欧式距离:
在这里插入图片描述
因此,在识别过程中分别计算待识别图像与知识库中各个模板特征向量之间的欧式距离,即与0~9这10个数字逐个比较,选择最小距离对应的数字作为最后的识别结果。

二、部分源代码

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

% Last Modified by GUIDE v2.5 28-Jun-2021 11:22:33

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct(‘gui_Name’, mfilename, …
‘gui_Singleton’, gui_Singleton, …
‘gui_OpeningFcn’, @NumDemo_OpeningFcn, …
‘gui_OutputFcn’, @NumDemo_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 NumDemo is made visible.
function NumDemo_OpeningFcn(hObject, ~, 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 NumDemo (see VARARGIN)

% Choose default command line output for NumDemo
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

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

% — Outputs from this function are returned to the command line.
function varargout = NumDemo_OutputFcn(~, ~, 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;
axis([0 250 0 250]);
clc;

% — Executes on mouse press over figure background, over a disabled or
% — inactive control, or over an axes background.
function figure1_WindowButtonDownFcn(~, ~, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

global ButtonDown pos1
if strcmp(get(gcf, ‘SelectionType’), ‘normal’)
ButtonDown = 1;
pos1 = get(handles.axes1, ‘CurrentPoint’);
end

% — Executes on mouse motion over figure - except title and menu.
function figure1_WindowButtonMotionFcn(~, ~, handles)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ButtonDown pos1
if(ButtonDown == 1)
pos = get(handles.axes1,‘CurrentPoint’);
line([pos1(1,1) pos(1,1)], [pos1(1,2) pos(1,2)], ‘Color’,‘Black’,‘LineStyle’,‘-’, ‘LineWidth’,8,‘Marker’,‘.’, ‘MarkerSize’,20);
pos1 = pos;
end

% — Executes on mouse press over figure background, over a disabled or
% — inactive control, or over an axes background.
function figure1_WindowButtonUpFcn(~, ~, ~)
% hObject handle to figure1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global ButtonDown
ButtonDown = 0;

% — Executes on button press in pushbutton_clear.
function pushbutton_clear_Callback(~, ~, handles)
% hObject handle to pushbutton_clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%cla;
cla(handles.axes1);

% — Executes on button press in pushbutton_save.
function pushbutton_save_Callback(~, ~, handles)
% hObject handle to pushbutton_save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[f, p] = uiputfile({‘*.bmp’},‘Save Image’);
str = strcat(p, f);
px = getframe(handles.axes3);
curImg = frame2im(px);
imwrite(curImg, str,‘bmp’);

% — Executes on button press in pushbutton_classify.
function pushbutton_classify_Callback(~, ~, handles)
% hObject handle to pushbutton_classify (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%strSample = ‘Data.mat’;
cla(handles.axes4,handles.axes5,handles.axes6);
load ‘Data.mat’;
I1=handles.I1;
curFea = GetFeature(I1);
I2 = Normalize_Img(I1);
bw1 = Bw_Img(I1);
bw2 = Thin_Img(bw1);
bw = bw2;
sz = size(bw);
[r, c] = find(bw==1);
rect = [min min max-min max-min];
vs = rect(1)+rect(3)[5/12 1/2 7/12];
hs = rect(2)+rect(4)
[1/3 1/2 2/3];
pt1 = [rect(1:2); rect(1:2)+rect(3:4)];
pt2 = [rect(1)+rect(3) rect(2); rect(1) rect(2)+rect(4)];
k1 = (pt1(1,2)-pt1(2,2)) / (pt1(1,1)-pt1(2,1));
x1 = 1:sz(2);
y1 = k1*(x1-pt1(1,1)) + pt1(1,2);
k2 = (pt2(1,2)-pt2(2,2)) / (pt2(1,1)-pt2(2,1));
x2 = 1:sz(2);
y2 = k2*(x2-pt2(1,1)) + pt2(1,2);
axes(handles.axes4);
imshow(I2,[]);
handles.I2 = I2;
hold on;
h = rectangle(‘Position’, [rect(1:2)-1 rect(3:4)+2], ‘EdgeColor’, ‘r’, ‘LineWidth’, 2);
legend(h, ‘数字区域标记’, ‘Location’, ‘South’);
axes(handles.axes5);
imshow(bw1,[]);
handles.bw1 = bw1;
axes(handles.axes6);
imshow(bw,[],‘Border’, ‘Loose’);
handles.bw = bw;
hold on;
h = [];
for i = 1 : length(hs)
h = [h plot([1 sz(2)], [hs(i) hs(i)], ‘r-’)];
end
for i = 1 : length(vs)
h = [h plot([vs(i) vs(i)], [1 sz(1)], ‘g-’)];
end
h = [h plot(x1, y1, ‘y-’)];
h = [h plot(x2, y2, ‘m-’)];
legend([h(1) h(4) h(7) h(8)], {‘水平线’, ‘竖直线’, ‘左对角线’, ‘右对角线’}, ‘Location’, ‘BestOutside’);
hold off;

%yLabel = euclid(curFea);
yLabel = MaskRecon(Data, curFea);
h = warndlg(num2str(yLabel),‘识别结果’);

% — Executes on mouse press over axes background.
function axes1_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

% — If Enable == ‘on’, executes on mouse press in 5 pixel border.
% — Otherwise, executes on mouse press in 5 pixel border or over pushbutton_clear.
function pushbutton_clear_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to pushbutton_clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
function testsample = GetFeature(I)
clc;
testsample = zeros(1,9); %1行9列的矩阵
%提取特征
I1 = Normalize_Img(I);
bw1 = Bw_Img(I1);
bw2 = Thin_Img(bw1);
bw = bw2;
sz = size(bw);
[r, c] = find(bw==1);
rect = [min min max-min max-min];
vs = rect(1)+rect(3)[5/12 1/2 7/12];
hs = rect(2)+rect(4)
[1/3 1/2 2/3];
pt1 = [rect(1:2); rect(1:2)+rect(3:4)];
pt2 = [rect(1)+rect(3) rect(2); rect(1) rect(2)+rect(4)];
k1 = (pt1(1,2)-pt1(2,2)) / (pt1(1,1)-pt1(2,1));
x1 = 1:sz(2);
y1 = k1*(x1-pt1(1,1)) + pt1(1,2);
k2 = (pt2(1,2)-pt2(2,2)) / (pt2(1,1)-pt2(2,1));
x2 = 1:sz(2);
y2 = k2*(x2-pt2(1,1)) + pt2(1,2);
if flag
% figure(‘Name’, ‘数字识别’, ‘NumberTitle’, ‘Off’, ‘Units’, ‘Normalized’, ‘Position’, [0.2 0.45 0.5 0.3]);
%subplot(2, 2, 1); imshow(I1, []); title(‘原图像’, ‘FontWeight’, ‘Bold’);
%subplot(2, 2, 2); imshow(I2, []); title(‘归一化图像’, ‘FontWeight’, ‘Bold’);
axes(handles.axes4);
imshow(I2,[]); title(‘归一化图像’, ‘FontWeight’, ‘Bold’);
handles.I2 = I2;
hold on;
h = rectangle(‘Position’, [rect(1:2)-1 rect(3:4)+2], ‘EdgeColor’, ‘r’, ‘LineWidth’, 2);

% subplot(2, 2, 3); imshow(bw1, []); title(‘二值化图像’, ‘FontWeight’, ‘Bold’);

% subplot(2, 2, 4); imshow(bw, [], ‘Border’, ‘Loose’); title(‘细化图像’, ‘FontWeight’, ‘Bold’);
hold on;
h = [];
for i = 1 : length(hs)
h = [h plot([1 sz(2)], [hs(i) hs(i)], ‘r-’)];
end
for i = 1 : length(vs)
h = [h plot([vs(i) vs(i)], [1 sz(1)], ‘g-’)];
end

hold off;

end
v{1} = [1:sz(2); repmat(hs(1), 1, sz(2))]‘;
v{2} = [1:sz(2); repmat(hs(2), 1, sz(2))]’;
v{3} = [1:sz(2); repmat(hs(3), 1, sz(2))]‘;
v{4} = [repmat(vs(1), 1, sz(1)); 1:sz(1)]’;
v{5} = [repmat(vs(2), 1, sz(1)); 1:sz(1)]‘;
v{6} = [repmat(vs(3), 1, sz(1)); 1:sz(1)]’;
v{7} = [x1; y1]‘;
v{8} = [x2; y2]’;

for i = 1 : 8
num(i) = GetImgLinePts(bw, round(v{i})-1);
end
num(9) = sum(sum(endpoints(bw)));
testsample=num;

三、运行结果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本
2014a

2 参考文献
[1]陈巧琴,郑文波.基于知识的无约束手写数字识别系统[J].福州大学学报(自然科学版). 1997,(03)

3 备注
简介此部分摘自互联网,仅供参考,若侵权,联系删除

仿真咨询
1 各类智能优化算法改进及应用

生产调度、经济调度、装配线调度、充电优化、车间调度、发车优化、水库调度、三维装箱、物流选址、货位优化、公交排班优化、充电桩布局优化、车间布局优化、集装箱船配载优化、水泵组合优化、解医疗资源分配优化、设施布局优化、可视域基站和无人机选址优化

2 机器学习和深度学习方面
卷积神经网络(CNN)、LSTM、支持向量机(SVM)、最小二乘支持向量机(LSSVM)、极限学习机(ELM)、核极限学习机(KELM)、BP、RBF、宽度学习、DBN、RF、RBF、DELM、XGBOOST、TCN实现风电预测、光伏预测、电池寿命预测、辐射源识别、交通流预测、负荷预测、股价预测、PM2.5浓度预测、电池健康状态预测、水体光学参数反演、NLOS信号识别、地铁停车精准预测、变压器故障诊断

3 图像处理方面
图像识别、图像分割、图像检测、图像隐藏、图像配准、图像拼接、图像融合、图像增强、图像压缩感知

4 路径规划方面
旅行商问题(TSP)、车辆路径问题(VRP、MVRP、CVRP、VRPTW等)、无人机三维路径规划、无人机协同、无人机编队、机器人路径规划、栅格地图路径规划、多式联运运输问题、车辆协同无人机路径规划、天线线性阵列分布优化、车间布局优化

5 无人机应用方面
无人机路径规划、无人机控制、无人机编队、无人机协同、无人机任务分配

6 无线传感器定位及布局方面
传感器部署优化、通信协议优化、路由优化、目标定位优化、Dv-Hop定位优化、Leach协议优化、WSN覆盖优化、组播优化、RSSI定位优化

7 信号处理方面
信号识别、信号加密、信号去噪、信号增强、雷达信号处理、信号水印嵌入提取、肌电信号、脑电信号、信号配时优化

8 电力系统方面
微电网优化、无功优化、配电网重构、储能配置

9 元胞自动机方面
交通流 人群疏散 病毒扩散 晶体生长

10 雷达方面
卡尔曼滤波跟踪、航迹关联、航迹融合

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/TIQCmatlab/article/details/119753420

智能推荐

应用软件安装_launch manager ultimate-程序员宅基地

文章浏览阅读905次。在进行开发中,会使用到很多各种类型的软件,现在将其进行整理,列表如下,如有需要,请给我留言,我可以给分享给大家。001_application software 序号 名称 备注 1 极速PDF.7z PDF查看器 2 鲁大师.7z 硬件检测工具 3 金山打字通.7z 打字练习 4 ..._launch manager ultimate

[Windows]_[初级]_[Release程序的崩溃报告minidump解决方案]_minidump 格式的崩溃报告-程序员宅基地

文章浏览阅读3.9k次。场景:1. Release的程序崩溃时,崩溃报告可以让开发人员查明代码哪里出了问题,用处大大的。2. 只有用VS的编译器才支持,所以MinGW就无缘了。3. 使用了未处理异常过滤处理函数._minidump 格式的崩溃报告

华为云服务之弹性云服务器ECS的深度使用和云端实践_华为云ecs虚拟化技术栈-程序员宅基地

文章浏览阅读1.7k次,点赞5次,收藏9次。什么是弹性云服务器ECS?ECS的产品架构是什么?它有哪些优势和有应用场景?弹性云服务器ECS的使用流程是怎样的?如何运用弹性云服务器ECS搭建微信公众号后台并收发文本消息?让我们带着这些疑问,去弹性云服务器ECS的世界里一探究竟,玩转ECS!!..._华为云ecs虚拟化技术栈

微信小程序蓝牙连接打印机_微信小程序蓝牙打印失败-程序员宅基地

文章浏览阅读6.8k次,点赞2次,收藏27次。微信小程序蓝牙连接打印机_微信小程序蓝牙打印失败

python安装opencv库、提示找不到pip_【Python】pip安装opencv-python包失败的一个处理方法...-程序员宅基地

文章浏览阅读687次。出现的问题在Windows10系统中,在cmd中(或者在pycharm的Terminal中)输入pip install opencv-python会出现如下报错ERROR: Exception:Traceback (most recent call last):File "C:\Users\bzm84\Anaconda3\lib\site-packages\pip\_vendor\urllib3\..._file "c:\users\lneovo\anaconda3\envs\tf2.1\lib\site-packages\pip\_vendor\url

举例说明使用MATLAB Coder从MATLAB生成C/C++代码步骤_matlab 生成c-程序员宅基地

文章浏览阅读1.1k次。MATLAB Coder可以从MATLAB代码生成独立的、可读性强、可移植的C/C++代码。使用MATLAB Coder产生代码的3个步骤:准备用于产生代码的MATLAB算法;检查MATLAB代码的兼容性(有些matlab代码语句并不能生成c/c++代码);产生最终使用的源代码或MEX。利用MATLAB Coder生成c++代码,并在vs2008中验证:一个简单的例子,两数相乘_matlab 生成c

随便推点

机器学习 广告欺诈检测_基于机器学习的数字欺诈检测-程序员宅基地

文章浏览阅读802次。机器学习 广告欺诈检测Payment fraud has a long history and is the most common form of online fraud in the United States and the world. Recently, however, digital fraud has increased so much that it is difficult ..._a survey of artificial intelligence in advertising fraud detection

内存刷脏机制触发的系统故障案例一则-程序员宅基地

文章浏览阅读1.4k次,点赞50次,收藏22次。LINUX刷脏导致系统阻塞案例一则

Python程序员真的觉得写代码比女朋友重要吗?这个回答16,205 人赞同!!_python要写代码吗-程序员宅基地

文章浏览阅读296次。我的第一个女友95年的,她出身很好,很庄重。我的第二个女友94年的,她是个自由的女孩,敢爱敢恨,常带着一顶米色的草帽。我的第三个女友91年的,她是个简洁的女孩,身边常捏着一本你没听过名字的诗集。。。。尽管我内心认为爱情应该从一而终,找与自己年龄相仿的然后善始善终,但我却克制不住自己的,因为我并不太喜欢太小的女孩,我喜欢那种年龄相对我大一些的,所以我后来又交往了一个73年的,和一个82年的姑娘..._python要写代码吗

小米架构调整:拆分成立人工智能部,直接向CEO雷军汇报-程序员宅基地

文章浏览阅读2.7k次。允中 发自 凹非寺量子位 出品 | 公众号 QbitAI小米昨天宣布再次进行大规模组织架构调整。其中最为关键的举措,就是进一步加强对技术团队的重视。包括任命崔宝秋担任小米..._小米集团季旭简历

Clay Codes — 从生成矩阵的角度来看_clay codes: moulding mds codes to yield an msr cod-程序员宅基地

文章浏览阅读1.4k次,点赞2次,收藏9次。Clay Codes ( Clay Codes: Moulding MDS Codes to Yield an MSR Code ) 是FAST18 上提出的一种编码方法,文章地址,Clay 码能够将一般的MDS 码(最优容错)转化为具有最优修复的编码方法,具有以下性质:Minimum Storage (最小存储开销,同经典RS码和最小存储再生码,MSR) Maximum Failure Tolerance(最大容错,即 (n,k)-Clay 码可以容任意n-k 失效) Optimal Repair_clay codes: moulding mds codes to yield an msr code

HTML的弹性布局用法_html弹性布局怎么用-程序员宅基地

文章浏览阅读7.4k次,点赞13次,收藏55次。HTMl中的弹性布局的实际用法弹性布局是html中十分方便的一个布局,它可以让结构中的内容自适应不同的分辨率,简化了许多繁琐的代码,今天就简单来介绍一下弹性布局的用法。弹性布局:display:flex;顾名思义,就是让内容变得像弹簧一样具有弹性,在需要内容自适应的结构中,它是一个极其方便的工具,它分为主轴和侧轴两个轴,就像X轴跟Y轴一样。弹性布局的语法分为两块儿:一.加给父容器的语法。二..._html弹性布局怎么用