使用svm进行手写字体识别
clc,clear,close all;
f=imread('num.jpg');
bw=~im2bw(f);
se=strel('line',200,0);
bw_dilate=imdilate(bw,se);
bw_dilate_erode=imerode(bw_dilate,strel('line',1000,0));
bw_re=imreconstruct(bw_dilate_erode,bw_dilate);
result=imreconstruct(bw_re,bw);[r,c]=find(result);
nextresult=result(min(r) :max(r),min(c):max(c));
[h,w]=size(nextresult);
hs=sum(nextresult);
a=1;b=1;i=1;
splitfs={};points=[];
figure
while(a<w)
while(hs(a)==0&&a<w)
a=a+1;
end
b=a;
while(hs(b)>0&&b<w)
b=b+1;
end
if(b-a>2)
hresult=nextresult(:,a:b);
[r,c]=find(hresult);
result2=hresult(min(r):max(r),:);
m=min(r);n=max(r);
xi=hresult(m:n,:);
splitfs{i}=xi;
points=[points;m,n,a,b];
subplot(3,6,i),imshow(result2);
i=i+1;
end
a=b;
end
function x=picPretreatment(im)
bw=im2bw(im);
[r,c]=find(bw);
x=bw(min(r):max(r),min(c):max(c));
x=imresize(x,[16,16]);
clear,clc,close all;
k=1;
for m=0:9
for n=1:15
f=imread(strcat('nums\',int2str(m),'\',int2str(n),'.bmp'));
f=picPretreatment(f);
xx=double(f(:));
P(k,:)=xx';
T(k,1)=m;
k=k+1;
end
end
net=fitcecoc(P,T);
save mynet net;
xs=[splitfs,points];
fonts='0123456789';
endresult='手写数字为:';
load mynet;
for m_18=1:size(xs,2)-1
g=xs{m_18};
g=double(picPretreatment(g));
lastresult=predict(net,g(:)');
endresult=strcat(endresult,string(lastresult));
end
endresult
链接:https://pan.baidu.com/s/1PcWrdyP41BGWuEu4TyWIvQ
提取码:lnke