|
楼主 |
发表于 2008-4-8 16:20
|
显示全部楼层
我把整个程序贴出来吧
function [BW,Datay1,Datay2,Datax]=image2data(filename,BWLevel,GridLevelx,GridLevely,DataYstart,DataYstep,DataXstep)
filedata=imread(filename);
BW = im2bw(filedata,BWLevel);
imshow(BW);
filesize=size(BW);
Lx=filesize(2);%%%%%%%%%%%x
Ly=filesize(1);%%%%%%%%%%%%%%%%%y
%%%%%%%%%%%%%%%%%%%%找到XY坐标轴
Lxstart=0;%%%%%%%%%%%y开始的地方
Ldeltax=0;
Ldelta=0;
Ldeltai=1;
for Li=1:Lx
temp=0;
for Lj=1:Ly
if BW(Lj,Li)==0
temp=temp+1;
end
end
if temp>Ly*GridLevely %%%%%%%%%%%%%%%表明是网格
BW(1:Ly,Li)=1;%%%%%%%%%%%%grid off
Ldelta(Ldeltai)=Li;
Ldeltai=Ldeltai+1;
%if Lxstart==0
% Lxstart=Li;
%end
end
end
Lxstart=1;
Lxstop=1;
for Li=1:temp
if (BW(Lystep(Li),Ldelta(Lxstart)+1:Ldelta(Lxstart)+7)<4)%六个中有三个黑,则认为是标轴
BW(Lystep(Li),Ldelta(Lxstart)+1:Ldelta(Lxstart)+7)=ones(1,7);
if sum((BW(Lystep(Li),:)))>(1-GridLevelx)*Lx
tfalse=0;
ti=1;
while(tfalse==0)
if(BW(Lystep(Li),Ldelta(Lxstart)+7+ti)==0)
BW(Lystep(Li),Ldelta(Lxstart)+7+ti)=1;
else
tfalse=1;
end
ti=ti+1;
end
end
end
end
Lxstop=floor(mean(Ldelta(Lxstop:Ldeltai-1)));
Ldeltax=Ldelta(2)-Ldelta(1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%去 网格
%%%%%%%%%%%%%%%%%去Y轴
filesize=size(BW);
Lx=filesize(2);%%%%%%%%%%%x
Ly=filesize(1);%%%%%%%%%%%%%%%%%y
imshow(BW);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Lynumber=0;
temp=0;
for Li=1:Lx
if Li==361
Li=361;
end
Datax(Li)=Li;
number=find(BW(1:Ly,Li)==0);
numbersize=size(number);
Lynumber(Li)=numbersize(1);
if numbersize(1)>0%%%%%%%%%%%%%%有数据
if numbersize(1)<2%%%%%%%%%%%%%%%刚好有两个点
if numbersize(1)==1
Datay1(Li)=number(1);
% Datay2(Li)=number(2);
%else
%%Datay1(Li)=number(1);
%Datay2(Li)=0;
end
elseif numbersize(1)>1%%%%%%%%%%%%%%有很多点,就空
temp1=0;
%temp(1)=number(1);
temp=number(1);
Datay1(Li)=0;
% Datay2(Li)=0;
for LLi=2:numbersize(1)
if (number(LLi)-number(LLi-1))<3 %%%%%%%%%表示这两个点很近,认为是一个点
temp(LLi-temp1)=number(LLi);
else%%%%%%%%%%%%%%%%%%有离开的点
%%%%%%%%%%%%%%%%%%%取前两个点进行复制
% if Datay1(Li)==0&Datay2(Li)==0
% Datay1(Li)=floor(mean(temp));
% elseif Datay1(Li)>0&Datay2(Li)==0
% Datay2(Li)=floor(mean(temp));
% end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
temp=number(LLi);
temp1=LLi-1;
end
end
%%%%%%%%%%%%%%%%%55无离开的点,
if temp1==0
Datay1(Li)=floor(mean(temp));
% Datay2(Li)=0;
end
% if Datay2(Li)==0&temp1>0
% Datay2(Li)=floor(mean(temp));
% end
end
else %%%%%%%%%%%%无数据
Datay1(Li)=0;
Datay2(Li)=0;
end
if Datay1(Li)==0&Li>1
Datay1(Li)=Datay1(Li-1);
end
%if Datay2(Li)==0&Li>1
% Datay2(Li)=Datay2(Li-1);
%end
number;
Datay1(Li);
%Datay2(Li);
%figure(3);
%plot(Datay1);
end
%%%%将前面由于判断坐标去掉的点加上
temp=min(find(Datay1>0));
if temp>1
Datay1(1:temp-1)=ones(1,temp-1)*Datay1(temp);
end |
|