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

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

一、简介

基于matlab的知识库的手写体数字识别。首先,读入手写数字图片进行图像归一化处理,统一尺寸,默认为24X24图像块,并通过ostu算法进行二值化;其次,对二值化图像进行图像细等形态学操作,并按照算法要求特征提取;最后,载入模版矩阵进行比对,选用欧氏距离测度,得到识别结果。

二、源代码

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;endfileName = 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;endfunction num = Main_Process(I, flag)if nargin < 2    flag = 1;endI1 = 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');   '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;endv{   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/

你可能感兴趣的文章
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>