参考文献
[1]钱谡. 基于图像处理的字符识别系统研究[M]. 河北农业大学,2007.
[2]范艳峰,肖乐,甄彤.自由手写体数字识别技术研究[J]. 计算机工程,2005. [3]阮秋琦. 数字图像处理[M]. 北京:电子工业出版社,2001.
[4]杨丹,赵海滨,龙哲. MATLAB图像处理实例详解. 北京:清华大学出版社,2012. [5] Sahel Ba-Karait NO,ShamsuddinS M.HandwriRendigits recognition using
particleswarmoptimization[C].Modeling&Simulation,2008.AICMS08.Second AsiaInternational Conference on.IEEE,2008:6 1 5.6 1 9.
6
源代码
本程序采用模块化编程的思想进行设计,不同的函数模块实现各自的一个功能,然后由功能模块对各函数模块进行整合与调用。最后,再在主函数模块中调用功能模块,实现程序的最终功能。 二值化模块:
function bw = Bw_Img(I) if ndims(I) == 3 I = rgb2gray(I); end
bw = im2bw(I, graythresh(I)); bw = ~bw;
归一化模块
function I1 = Normalize_Img(I) if ndims(I) == 3 I = rgb2gray(I);
end
I1 = imresize(I, [24 24], 'bicubic');
特征提取模块
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
7
0.5 0.3]);
subplot(2, 2, 1); imshow(I, []); title('原图像', 'FontWeight', 'Bold');src=I;
subplot(2, 2, 2); imshow(I1, []); title('归一化图像', 'FontWeight', 'Bold');guiYi=I1; hold on;
h = rectangle('Position', [rect(1:2)-1 rect(3:4)+2], 'EdgeColor', 'r', 'LineWidth', 2); legend(h, '数字区域标记', 'Location', 'BestOutside');
subplot(2, 2, 3); imshow(bw1, []); title('二值化图像', 'FontWeight', 'Bold');erZh=bw1; subplot(2, 2, 4); imshow(bw, [], 'Border', 'Loose'); title('细化图像', 'FontWeight', 'Bold');xiHua=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; 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)));
计算交点函数:
function num = GetImgLinePts(bw, v) num = 0;
for i = 1 : size(v, 1)
if v(i, 2)>1 && v(i, 2) num = num + 1; end 8 end 计算端点函数: function num = GetImgEndPts(bw) num = 0; for i = 1 : size(bw, 1) for j = 1 : size(bw, 2) if i>2 && i && sum(sum(bw(i-1:i+1, j-1:j+1)))==1 num = num + 1; end end end 匹配识别模块: 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 主函数模块 function res = ShowNum( img ) load Data.mat v=Main_Process(img,0); res=MaskRecon(Datas,v); msgbox(sprintf('识别结果:%d',res),'提示信息','modal'); end 9