资源描述:
《基于MATLAB的图片中字符的提取(源代码).docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、基于MATLAB的图片中字符的提取(源代码)functiongetPicChar%%建立字符提取函数,在MATLAB平台上直接运行即可%运用MATLAB的UI,直接打牌需要提取的字符图片即可[filename,pathname,~]=uigetfile({'*.jpg';'*.bmp';'*.png'},'Choseapicture');picstr=[pathnamefilename];if~ischar(picstr)return;endpic=imread(picstr);%打开图片ifleng
2、th(size(pic))==3%判断图片的维数,统一为灰度图片pic=rgb2gray(pic);endpic=(pic<127);%转化为二值图片pic=xylimit(pic);%图片区域的第一次边界限定%%%%%%%第一阶段%%%%%%m=size(pic,1);Ycount=zeros(1,m);fori=1:mYcount(i)=sum(pic(i,:));endlenYcount=length(Ycount);Yflag=zeros(1,lenYcount);fork=1:lenYcou
3、nt-2ifYcount(k)<3&&Ycount(k+1)<3&&Ycount(k+2)<3Yflag(k)=1;endendfork=lenYcount:1+2ifYcount(k)<3&&Ycount(k-1)<3&&Ycount(k-2)<3Yflag(k)=1;endendYflag2=[0Yflag(1:end-1)];Yflag3=abs(Yflag-Yflag2);%做差分运算[~,row]=find(Yflag3==1);%找突变位置row=[1rowm];%调整突变位置点row1=
4、zeros(1,length(row)/2);%截取图像的起始位置向量row2=row1;%截取图像的终止位置向量fork=1:length(row)ifmod(k,2)==1;%奇数为起始row1((k+1)/2)=row(k);else%偶数为终止row2(k/2)=row(k);endendpic2=pic(row1(1):row2(1),:);%截取第一列字符alpha=1024/size(pic2,2);%计算放缩比例pic2=imresize(pic2,alpha);%调整第一列字符图片大
5、小,作为基准fork=2:length(row)/2pictemp=imresize(pic(row1(k):row2(k),:),[size(pic2,1)size(pic2,2)]);pic2=cat(2,pic2,pictemp);%横向连接图像块endpic=xylimit(pic2);%限定图像区域%%%%%%%第二阶段%%%%%%[~,n]=size(pic);Xcount=zeros(1,n);forj=1:nXcount(j)=sum(pic(:,j));endlenXcount=le
6、ngth(Xcount);Xflag=zeros(1,lenXcount);fork=1:lenXcount-2ifXcount(k)<3&&Xcount(k+1)<3&&Xcount(k+2)<3Xflag(k)=1;endendfork=lenXcount:1+2ifXcount(k)<3&&Xcount(k-1)<3&&Xcount(k-2)<3Xflag(k)=1;endendXflag2=[0Xflag(1:end-1)];Xflag3=abs(Xflag-Xflag2);[~,col]=f
7、ind(Xflag3==1);col=[1colsize(pic,2)];coltemp=col(2:end)-col(1:end-1);[~,ind]=find(coltemp<3);col(ind)=0;col(ind+1)=0;col=col(col>0);col1=zeros(1,length(col)/2);col2=col1;fork=1:length(col)ifmod(k,2)==1col1((k+1)/2)=col(k);elsecol2(k/2)=col(k);endendpicn
8、um2=length(col)/2;piccell2=cell(1,picnum2);fork=1:picnum2piccell2{k}=pic(:,col1(k):col2(k));piccell2{k}=xylimit(piccell2{k});piccell2{k}=imresize(piccell2{k},[128128]);end%显示提取出的字符,每行最多输出8个字符ifmod(picnum2,8)rownum=ceil(picnum2/8)