博客
关于我
【手写数字识别】基于matlab知识库手写体数字识别【含Matlab源码 311期】
阅读量:159 次
发布时间:2019-02-27

本文共 3609 字,大约阅读时间需要 12 分钟。

一、简介

基于MATLAB的知识库的手写体数字识别系统。系统通过以下步骤实现数字识别:首先,读取手写数字图片并进行图像归一化处理,统一尺寸,默认为24x24图像块;接着,采用OSTU算法对图像进行二值化处理;然后,对二值化图像进行图像细节增强和形态学操作,并提取特征;最后,通过模板匹配算法与预定义的数字矩阵进行比对,采用欧氏距离测度获得识别结果。

二、源代码

以下是系统的主要源代码: ```matlab clc; clear all; close all; load Data.mat; [FileName, PathName, FilterIndex] = uigetfile({ '*.jpg;*.tif;*.png;*.gif', ... '所有图像文件';... '*.*','所有文件' },'载入数字图像',... '.\\images\\手写数字\\t0.jpg'); if isequal(FileName, 0) || isequal(PathName, 0) return; end fileName = fullfile(PathName, FileName); I = imread(fileName); flag = 1; I1 = Normalize_Img(I); bw1 = Bw_Img(I1); bw2 = Thin_Img(bw1); bw = bw2; sz = size(bw); [r, c] = find(bw==1); rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)]; 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(I, []); title('原图像', 'FontWeight', 'Bold'); subplot(2, 2, 2); imshow(I1, []); title('归一化图像', 'FontWeight', 'Bold'); hold on; h = rectangle('Position', [rect(1:2)-1 rect(3:4)+2], 'EdgeColor', 'r', 'LineWidth', 2); xlabel('数字区域标记'); 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 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; end function num = Main_Process(I, flag) if nargin < 2 flag = 1; end I1 = Normalize_Img(I); bw1 = Bw_Img(I1); bw2 = Thin_Img(bw1); bw = bw2; sz = size(bw); [r, c] = find(bw==1); rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)]; 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(I, []); title('原图像', 'FontWeight', 'Bold'); subplot(2, 2, 2); imshow(I1, []); title('归一化图像', 'FontWeight', 'Bold'); hold on; h = rectangle('Position', [rect(1:2)-1 rect(3:4)+2], 'EdgeColor', 'r', 'LineWidth', 2); legend(h, '数字区域标记', 'Location', 'BestOutside'); 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; 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]'; ```

三、运行结果

系统运行结果如图所示,通过对手写数字图像的归一化、二值化、细化处理后,成功识别出数字字符。

四、备注

完整代码或功能扩展请添加QQ 1564658423。

转载地址:http://lwqf.baihongyu.com/

你可能感兴趣的文章
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置https(一)—— 自签名证书
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx 配置清单(一篇够用)
查看>>
Nginx 配置解析:从基础到高级应用指南
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>
Nginx-http-flv-module流媒体服务器搭建+模拟推流+flv.js在前端html和Vue中播放HTTP-FLV视频流
查看>>
Nginx下配置codeigniter框架方法
查看>>
Nginx之二:nginx.conf简单配置(参数详解)
查看>>
Nginx代理websocket配置(解决websocket异常断开连接tcp连接不断问题)
查看>>
Nginx代理初探
查看>>
Nginx代理外网映射
查看>>
Nginx代理模式下 log-format 获取客户端真实IP
查看>>
Nginx代理静态资源(gis瓦片图片)实现非固定ip的url适配网络环境映射ip下的资源请求解决方案
查看>>