当前位置:网站首页>【数字识别】基于知识库实现手写体数字识别附matlab代码
【数字识别】基于知识库实现手写体数字识别附matlab代码
2022-07-27 20:19:00 【Matlab科研工作室】
1 内容介绍
摘要:随着科学技术的发展,机器学习成为一大学科热门领域,是一门专门研究计算机怎样模拟或实现人类的学习行为的交叉学科。文章在matlab软件的基础上,利用BP神经网络算法完成手写体数字的识别。
关键词:机器学习;手写体数字识别;BP神经网络
机器学习是一门多领域交叉学科,专门研究计算机怎样模拟或实现人类的学习行为,以获取新的知识或技能,重新组织己有的知识结构,使之不断改善自身的性能。实现机器学习的方法多种多样,常见到的主要有神经网络算法、CNN卷积神经网络算法、RNN循环神经网络算法、EM算法、贝叶斯算法、聚类算法、回归算法、SVM等。本文将利用知识库算法来完成手写体数字的识别[1]。
2 仿真代码
<span style="color:#333333"><span style="background-color:rgba(0, 0, 0, 0.03)"><code>clc; clear all; close all;</code><code>load Data.mat;</code><code>[FileName,PathName,FilterIndex] = uigetfile({<span style="color:#dd1144">'*.jpg;*.tif;*.png;*.gif'</span>, ...</code><code> <span style="color:#dd1144">'所有图像文件'</span>;...</code><code> <span style="color:#dd1144">'*.*'</span>,<span style="color:#dd1144">'所有文件'</span> },<span style="color:#dd1144">'载入数字图像'</span>,...</code><code> <span style="color:#dd1144">'.\\images\\手写数字\\t0.jpg'</span>);</code><code><span style="color:#ca7d37">if</span> isequal(FileName, <span style="color:#0e9ce5">0</span>) || isequal(PathName, <span style="color:#0e9ce5">0</span>)</code><code> <span style="color:#ca7d37">return</span>;</code><code><span style="color:#ca7d37">end</span></code><code>fileName = fullfile(PathName, FileName);</code><code>I = imread(fileName);</code><code>flag = <span style="color:#0e9ce5">1</span>;</code><code>I1 = Normalize_Img(I);</code><code>bw1 = Bw_Img(I1);</code><code>bw2 = Thin_Img(bw1);</code><code>bw = bw2;</code><code>sz = size(bw);</code><code>[r, c] = find(bw==<span style="color:#0e9ce5">1</span>);</code><code>rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];</code><code>vs = rect(<span style="color:#0e9ce5">1</span>)+rect(<span style="color:#0e9ce5">3</span>)*[<span style="color:#0e9ce5">5</span>/<span style="color:#0e9ce5">12</span> <span style="color:#0e9ce5">1</span>/<span style="color:#0e9ce5">2</span> <span style="color:#0e9ce5">7</span>/<span style="color:#0e9ce5">12</span>];</code><code>hs = rect(<span style="color:#0e9ce5">2</span>)+rect(<span style="color:#0e9ce5">4</span>)*[<span style="color:#0e9ce5">1</span>/<span style="color:#0e9ce5">3</span> <span style="color:#0e9ce5">1</span>/<span style="color:#0e9ce5">2</span> <span style="color:#0e9ce5">2</span>/<span style="color:#0e9ce5">3</span>];</code><code>pt1 = [rect(<span style="color:#0e9ce5">1</span><span style="color:#dd1144">:</span><span style="color:#0e9ce5">2</span>); rect(<span style="color:#0e9ce5">1</span><span style="color:#dd1144">:</span><span style="color:#0e9ce5">2</span>)+rect(<span style="color:#0e9ce5">3</span><span style="color:#dd1144">:</span><span style="color:#0e9ce5">4</span>)];</code><code>pt2 = [rect(<span style="color:#0e9ce5">1</span>)+rect(<span style="color:#0e9ce5">3</span>) rect(<span style="color:#0e9ce5">2</span>); rect(<span style="color:#0e9ce5">1</span>) rect(<span style="color:#0e9ce5">2</span>)+rect(<span style="color:#0e9ce5">4</span>)];</code><code>k1 = (pt1(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">2</span>)-pt1(<span style="color:#0e9ce5">2</span>,<span style="color:#0e9ce5">2</span>)) / (pt1(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">1</span>)-pt1(<span style="color:#0e9ce5">2</span>,<span style="color:#0e9ce5">1</span>));</code><code>x1 = <span style="color:#0e9ce5">1</span><span style="color:#dd1144">:sz</span>(<span style="color:#0e9ce5">2</span>);</code><code>y1 = k1*(x1-pt1(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">1</span>)) + pt1(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">2</span>);</code><code>k2 = (pt2(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">2</span>)-pt2(<span style="color:#0e9ce5">2</span>,<span style="color:#0e9ce5">2</span>)) / (pt2(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">1</span>)-pt2(<span style="color:#0e9ce5">2</span>,<span style="color:#0e9ce5">1</span>));</code><code>x2 = <span style="color:#0e9ce5">1</span><span style="color:#dd1144">:sz</span>(<span style="color:#0e9ce5">2</span>);</code><code>y2 = k2*(x2-pt2(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">1</span>)) + pt2(<span style="color:#0e9ce5">1</span>,<span style="color:#0e9ce5">2</span>);</code><code><span style="color:#ca7d37">if</span> flag</code><code> figure(<span style="color:#dd1144">'Name'</span>, <span style="color:#dd1144">'数字识别'</span>, <span style="color:#dd1144">'NumberTitle'</span>, <span style="color:#dd1144">'Off'</span>, <span style="color:#dd1144">'Units'</span>, <span style="color:#dd1144">'Normalized'</span>, <span style="color:#dd1144">'Position'</span>, [<span style="color:#0e9ce5">0</span>.<span style="color:#0e9ce5">2</span> <span style="color:#0e9ce5">0</span>.<span style="color:#0e9ce5">45</span> <span style="color:#0e9ce5">0</span>.<span style="color:#0e9ce5">5</span> <span style="color:#0e9ce5">0</span>.<span style="color:#0e9ce5">3</span>]);</code><code> subplot(<span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">1</span>); imshow(I, []); title(<span style="color:#dd1144">'原图像'</span>, <span style="color:#dd1144">'FontWeight'</span>, <span style="color:#dd1144">'Bold'</span>);</code><code> subplot(<span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">2</span>); imshow(I1, []); title(<span style="color:#dd1144">'归一化图像'</span>, <span style="color:#dd1144">'FontWeight'</span>, <span style="color:#dd1144">'Bold'</span>);</code><code> hold on;</code><code> h = rectangle(<span style="color:#dd1144">'Position'</span>, [rect(<span style="color:#0e9ce5">1</span><span style="color:#dd1144">:</span><span style="color:#0e9ce5">2</span>)-<span style="color:#0e9ce5">1</span> rect(<span style="color:#0e9ce5">3</span><span style="color:#dd1144">:</span><span style="color:#0e9ce5">4</span>)+<span style="color:#0e9ce5">2</span>], <span style="color:#dd1144">'EdgeColor'</span>, <span style="color:#dd1144">'r'</span>, <span style="color:#dd1144">'LineWidth'</span>, <span style="color:#0e9ce5">2</span>);</code><code> xlabel(<span style="color:#dd1144">'数字区域标记'</span>);</code><code> subplot(<span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">3</span>); imshow(bw1, []); title(<span style="color:#dd1144">'二值化图像'</span>, <span style="color:#dd1144">'FontWeight'</span>, <span style="color:#dd1144">'Bold'</span>);</code><code> subplot(<span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">2</span>, <span style="color:#0e9ce5">4</span>); imshow(bw, [], <span style="color:#dd1144">'Border'</span>, <span style="color:#dd1144">'Loose'</span>); title(<span style="color:#dd1144">'细化图像'</span>, <span style="color:#dd1144">'FontWeight'</span>, <span style="color:#dd1144">'Bold'</span>);</code><code> hold on;</code><code> h = [];</code><code> <span style="color:#ca7d37">for</span> i = <span style="color:#0e9ce5">1</span> : length(hs)</code><code> h = [h plot([<span style="color:#0e9ce5">1</span> sz(<span style="color:#0e9ce5">2</span>)], [hs(i) hs(i)], <span style="color:#dd1144">'r-'</span>)];</code><code> <span style="color:#ca7d37">end</span></code><code> <span style="color:#ca7d37">for</span> i = <span style="color:#0e9ce5">1</span> : length(vs)</code><code> h = [h plot([vs(i) vs(i)], [<span style="color:#0e9ce5">1</span> sz(<span style="color:#0e9ce5">1</span>)], <span style="color:#dd1144">'g-'</span>)];</code><code> <span style="color:#ca7d37">end</span></code><code> h = [h plot(x1, y1, <span style="color:#dd1144">'y-'</span>)];</code><code> h = [h plot(x2, y2, <span style="color:#dd1144">'m-'</span>)];</code><code> legend([h(<span style="color:#0e9ce5">1</span>) h(<span style="color:#0e9ce5">4</span>) h(<span style="color:#0e9ce5">7</span>) h(<span style="color:#0e9ce5">8</span>)], {<span style="color:#dd1144">'水平线'</span>, <span style="color:#dd1144">'竖直线'</span>, <span style="color:#dd1144">'左对角线'</span>, <span style="color:#dd1144">'右对角线'</span>}, <span style="color:#dd1144">'Location'</span>, <span style="color:#dd1144">'BestOutside'</span>);</code><code> hold off;</code><code><span style="color:#ca7d37">end</span></code><code>v{<span style="color:#0e9ce5">1</span>} = [<span style="color:#0e9ce5">1</span><span style="color:#dd1144">:sz</span>(<span style="color:#0e9ce5">2</span>); repmat(hs(<span style="color:#0e9ce5">1</span>), <span style="color:#0e9ce5">1</span>, sz(<span style="color:#0e9ce5">2</span>))]<span style="color:#dd1144">';</span></code><code>v{2} = [1:sz(2); repmat(hs(2), 1, sz(2))]';</code><code>v{<span style="color:#0e9ce5">3</span>} = [<span style="color:#0e9ce5">1</span><span style="color:#dd1144">:sz</span>(<span style="color:#0e9ce5">2</span>); repmat(hs(<span style="color:#0e9ce5">3</span>), <span style="color:#0e9ce5">1</span>, sz(<span style="color:#0e9ce5">2</span>))]<span style="color:#dd1144">';</span></code><code>v{4} = [repmat(vs(1), 1, sz(1)); 1:sz(1)]';</code><code>v{<span style="color:#0e9ce5">5</span>} = [repmat(vs(<span style="color:#0e9ce5">2</span>), <span style="color:#0e9ce5">1</span>, sz(<span style="color:#0e9ce5">1</span>)); <span style="color:#0e9ce5">1</span><span style="color:#dd1144">:sz</span>(<span style="color:#0e9ce5">1</span>)]<span style="color:#dd1144">';</span></code><code>v{6} = [repmat(vs(3), 1, sz(1)); 1:sz(1)]';</code><code>v{<span style="color:#0e9ce5">7</span>} = [x1; y1]<span style="color:#dd1144">';</span></code><code>v{8} = [x2; y2]';</code><code><span style="color:#ca7d37">for</span> i = <span style="color:#0e9ce5">1</span> : <span style="color:#0e9ce5">8</span></code><code> num(i) = GetImgLinePts(bw, round(v{i})-<span style="color:#0e9ce5">1</span>);</code><code><span style="color:#ca7d37">end</span></code><code>num(<span style="color:#0e9ce5">9</span>) = sum(sum(endpoints(bw)));</code><code>result = MaskRecon(Datas, num);</code><code>msgbox(sprintf(<span style="color:#dd1144">'识别结果:%d'</span>, result), <span style="color:#dd1144">'提示信息'</span>, <span style="color:#dd1144">'modal'</span>);</code><code></code></span></span>3 运行结果

4 参考文献
[1]刘思慧, 江维. 基于MATLAB手写体数字识别程序设计[J]. 电子世界, 2019(3):2.
[2]丁禹鑫, 丁会, 张红娟,等. 基于matlab的手写体数字识别系统研究[J]. 无线互联科技, 2017(18):2.
[3]张鱼米. 自制人工智能:用神经网络识别手写数字[J]. 2018.
博主简介:擅长智能优化算法、神经网络预测、信号处理、元胞自动机、图像处理、路径规划、无人机等多种领域的Matlab仿真,相关matlab代码问题可私信交流。
部分理论引用网络文献,若有侵权联系博主删除。
边栏推荐
- Main security risks and Countermeasures of cloud computing services
- "The faster the code is written, the slower the program runs."
- Purple light FPGA solves the mask problem! Boost the overall speed-up of mask production
- What is the b+tree index of MySQL? How does the cluster index grow?
- 营收、利润两位数增长,华润怡宝悄悄打造了这些过亿新品
- 2022/6/9 考试总结
- China Internet Security Report 2021: API threat soared by more than 200%, and the security risk of software supply chain increased
- 【StoneDB故障诊断】数据库实例crash
- MySQL basic installation and startup
- [NOI2018]归程(Kruskal重构树/可持久化并查集)
猜你喜欢

UDF and analysis cases of sparksql, 220726,,

图论的小技巧以及扩展

迪赛智慧数——其他图表(平行坐标图):家庭未来资产配置意愿

MySQL basic installation and startup
C language explanation series -- understanding of functions (5) function recursion and iteration

Safety Fundamentals 1

看GAN如何一步步控制图像生成风格?详解StyleGAN进化过程

Cron expression

jvm组成及内存模型

Cloudcompare & PCL point cloud equally spaced slices
随机推荐
Memoirs of three years in junior high school
2022/4/8考试总结
2022/3/11 考试总结
2022/6/9 考试总结
catch all in one draft! Introduction to 10 data visualization software
Parameter transmission of components
Summary of exam on May 17, 2022
细胞CLE19多肽荧光成像牛血清白蛋白荧光猝灭量子点的制备
Tips and extensions of graph theory
Lanproxy映射本地开发环境
Cloud security daily 220714: Cisco identity service engine found an authentication bypass vulnerability and needs to be upgraded as soon as possible
Five network management trends in 2022
Basic lighting of unity
2022/4/8 exam summary
云计算服务主要安全风险及应对措施
The significance of enterprise digital transformation, digital transformation is not a choice
UDF and analysis cases of sparksql, 220726,,
WWW 2019 | HAN:异质图注意力网络
简单实用的数据可视化案例
Containerd CTR run the ansible container and execute the complete command of ansible playbook task