//--------------计算晶粒周长---------------
type TMessageGrid = array of array of boolean;
Myarray: TMessageGrid;
backarray:TMessageGrid;
procedure jinglicc;
var
cx,cy,ch,cw,m,n,i,j:integer;
str1,str2,tempaaa:string;
dx,dy:double;
label
aaa,bbb,ccc,ddd;
begin
with mainform.Image2 do begin
ch:=Picture.Height;
cw:=Picture.Width;
end;
//SetLength(myarray,intw,inth); 已有值
// SetLength(backarray,intw,inth); 已有值
backarray:= myarray;
dx:=1;
dy:=1;
dbnum:=0.0; //晶粒数
for cx:=0 to cw-1 do
begin
for cy:=0 to ch-1 do
begin
if backarray[cx,cy] then begin
// 找到最左点
length:=0.0;
xleft:=cx;
yleft:=cy;
m:=cx;
n:=cy;
//寻找最下点
aaa: for i:=-2 to 3 do begin
if (m+i>=0)and (M+i<=cw-1)and (n+1<=ch-1)and (backarray[m+i,n+1])then
begin
M:=M+i;
N:=N+1;
length:=length+sqrt(1+i*i)*dy;
goto aaa;
end;
end;
xbuttom:=m;
ybuttom:=n;
// 寻找最右点
bbb: for i:=-3 to 2do begin
if (m-i>=0)and (M-i<=cw-1)and (n-1>=0)and (backarray[m-i,n-1])then
begin
M:=M-i;
N:=N-1;
length:=length+sqrt(1+i*i)*dy;
goto bbb;
end;
end;
xright:=m;
yright:=n;
// 寻找最高点
ccc: for i:=-2 to 3 do begin
if (m-i>=0)and (M-i<=cw-1)and (n-1>=0)and (backarray[m-i,n-1])then
begin
M:=M-i;
N:=N-1;
length:=length+sqrt(1+i*i)*dy;
goto ccc;
end;
end;
xtop:=m;
ytop:=n;
// 寻找起点
ddd: for i:=-3to 2 do begin
if (m+i>=0)and (M+i<=cw-1)and (n+1<=ch-1)and (backarray[m+i,n+1])then
begin
M:=M+i;
N:=N+1;
length:=length+sqrt(1+I*i)*dy;
goto ddd;
end;
end;
xend:=m;
yend:=n;
if (abs(xleft-xend)<=2)and (abs(yleft-yend)<=2)then begin
tempaaa:='好好';
end;
//标记遍历过
for i:=xleft to xright do begin
for j:=ytop to ybuttom do
begin
backarray[i,j]:=false;
end;
end;
//写入数据库
if length<((strtofloat(szform.edit3.Text))*pi) then
begin
;
end else
begin
dbnum:=dbnum+1;
try
form11.table1.Insert;
form11.Table1.FieldByName('number').AsString:=floattostr(dbnum);
form11.table1.FieldByName('length').AsString:=floattostr(length);
form11.table1.FieldByName('r').AsString:=floattostr(length/(2*pi));
form11.table1.FieldByName('a').AsString:=floattostr(length*length/(4*pi));
form11.table1.FieldByName('rmin').AsString:=IntToStr(xleft);
form11.table1.FieldByName('rmax').AsString:=IntToStr(yleft);
form11.table1.FieldByName('bi').AsString:=floatToStr(xtop);
form11.table1.FieldByName('size').AsString:=floatToStr(ytop);
form11.Table1.Post;
numi2:=numi2+1;
except
showmessage('保存数据失败');
end;
end;
end;
end;
end;
上面的问题是只能计算一遍,再计算时dnum为0,但我在每次处理前已保存了,
同时也有个数太多,并且有的白色不规则物体周长
为1和.周长
太小的值..