第 08 章 基于知识库的手写体数字识别MATLAB深度学习应用实战

在这里插入图片描述
基于知识库的手写体数字识别MATLAB深度学习应用实战。
主函数如下
main.m

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
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)));
result = MaskRecon(Datas, num);
msgbox(sprintf('识别结果:%d', result), '提示信息', 'modal');

MaskRecon,m函数

function result = MaskRecon(Data, v)

for i = 1 : size(Data, 1)
    dis(i) = norm(v-Data(i, :));
end
[mindis, ind] = min(dis);
if ind < 11
    result = ind-1;
else
    result = ind-11;
end

预处理函数

% 读取图像
clc; clear all; close all;
% 载入图像
[FileName,PathName,FilterIndex] = uigetfile(...
    {'*.jpg;*.tif;*.png;*.gif', ...
    '所有图像文件';...
    '*.*','所有文件' },'载入数字图像',...
    '.\\images\\手写数字\\t0.jpg');
if isequal(FileName, 0) || isequal(PathName, 0)    
    return;    
end
fileName = fullfile(PathName, FileName);
% 读取图像
Img = imread(fileName);
% 图像预处理
if ndims(Img) == 3    
    I = rgb2gray(Img);    
else    
    I = Img;    
end
% 非线性字体大小归一化为24×24点阵
I1 = imresize(I, [24 24], 'bicubic');
% 中值滤波
I2 = medfilt2(I1, 'symmetric');
% 二值化
bw = im2bw(I2, graythresh(I2));
% 反色
bw = ~bw;
% 显示处理结果
figure('Name', '图像预处理', 'NumberTitle', 'Off', ...
    'Units', 'Normalized', 'Position', [0.2 0.2 0.7 0.5]);
subplot(2, 2, 1); imshow(Img, []); title('原图像');
subplot(2, 2, 2); imshow(I1, []); title('灰度图像');
subplot(2, 2, 3); imshow(I2, []); title('滤波图像');
subplot(2, 2, 4); imshow(bw, []); title('二值化图像');

图像预处理结果
在这里插入图片描述
最终识别结果

在这里插入图片描述
全套源码下载–>传送门

在这里插入图片描述
链接为:https://download.csdn.net/download/dongbao520/85693564

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

海宝7号

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值