首页 > 技术文章 > 图像细化函数(背景为黑色像素值为0,前景为白色像素值255,必须只含有0和255的像素值,即二值化后的图像)

shanghai-achao 2016-03-21 11:03 原文

procedure thinimage(srcimage:pIplImage);
var
count:Integer; //表示迭代次数
Zhangmude:array [1..9] of Integer;
deletelist1,deletelist2:Tlist<Tcvpoint>; //存放着要删除的点
idx,i,j:Integer;
whitepointtotal,ap:Integer;
tempoint:TCvPoint;
begin
deletelist1:=Tlist<Tcvpoint>.Create;
deletelist2:=Tlist<Tcvpoint>.Create;
count:=0;
while(True)do
begin
count:=count+1;
for I := 1 to srcimage.height-2 do //以后可以只考虑最小外接矩形,,,这样节省时间
begin
for j := 1 to srcimage.width - 2 do
begin
if srcimage.imageData[j+round(I*srcimage.widthStep)]>0 then
begin //需要删除的点,首先必须是 白色的点,值为1
Zhangmude[1]:=1;
if srcimage.imageData[j+round((I-1)*srcimage.widthStep)]>0 then
Zhangmude[2]:=1 else Zhangmude[2]:=0;
if srcimage.imageData[j+1+round((I-1)*srcimage.widthStep)]>0 then
Zhangmude[3]:=1 else Zhangmude[3]:=0;
if srcimage.imageData[j+1+round(I*srcimage.widthStep)]>0 then
Zhangmude[4]:=1 else Zhangmude[4]:=0;
if srcimage.imageData[j+1+round((I+1)*srcimage.widthStep)]>0 then
Zhangmude[5]:=1 else Zhangmude[5]:=0;
if srcimage.imageData[j+round((I+1)*srcimage.widthStep)]>0 then
Zhangmude[6]:=1 else Zhangmude[6]:=0;
if srcimage.imageData[j-1+round((I+1)*srcimage.widthStep)]>0 then
Zhangmude[7]:=1 else Zhangmude[7]:=0;
if srcimage.imageData[j-1+round(I*srcimage.widthStep)]>0 then
Zhangmude[8]:=1 else Zhangmude[8]:=0;
if srcimage.imageData[j-1+round((I-1)*srcimage.widthStep)]>0 then
Zhangmude[9]:=1 else Zhangmude[9]:=0;
whitepointtotal:=0;
for idx:=2 to 9 do
begin
whitepointtotal:=Zhangmude[idx]+ whitepointtotal;
end;
if (whitepointtotal>=2) and (whitepointtotal<=6) then
begin
ap:=0;
if ((Zhangmude[2]=0) and (Zhangmude[3]=1)) then ap:=ap+1;
if ((Zhangmude[3]=0) and (Zhangmude[4]=1)) then ap:=ap+1;
if ((Zhangmude[4]=0) and (Zhangmude[5]=1)) then ap:=ap+1;
if ((Zhangmude[5]=0) and (Zhangmude[6]=1)) then ap:=ap+1;
if ((Zhangmude[6]=0) and (Zhangmude[7]=1)) then ap:=ap+1;
if ((Zhangmude[7]=0) and (Zhangmude[8]=1)) then ap:=ap+1;
if ((Zhangmude[8]=0) and (Zhangmude[9]=1)) then ap:=ap+1;
if ((Zhangmude[9]=0) and (Zhangmude[2]=1)) then ap:=ap+1;
if ap=1 then
begin
if (Zhangmude[2]*Zhangmude[8]*Zhangmude[6]=0) and (Zhangmude[4]*Zhangmude[6]*Zhangmude[8]=0) then
begin
tempoint.x:=j;
tempoint.y:=i;
deletelist1.Add(tempoint);
end;
end;
end;
end;
end;
end;


for Idx := 0 to deletelist1.Count - 1 do
begin
tempoint:=deletelist1[Idx];
j:=tempoint.x;
i:=tempoint.y;
srcimage.imageData[j+round(i*srcimage.widthStep)]:=0;
end;
if deletelist1.Count=0 then Break else deletelist1.Clear;

for I := 1 to srcimage.height-2 do //以后可以只考虑最小外接矩形,,,这样节省时间
begin
for j := 1 to srcimage.width - 2 do
begin
if srcimage.imageData[j+round(I*srcimage.widthStep)]>0 then
begin //需要删除的点,首先必须是 白色的点,值为1
Zhangmude[1]:=1;
if srcimage.imageData[j+round((I-1)*srcimage.widthStep)]>0 then
Zhangmude[2]:=1 else Zhangmude[2]:=0;
if srcimage.imageData[j+1+round((I-1)*srcimage.widthStep)]>0 then
Zhangmude[3]:=1 else Zhangmude[3]:=0;
if srcimage.imageData[j+1+round(I*srcimage.widthStep)]>0 then
Zhangmude[4]:=1 else Zhangmude[4]:=0;
if srcimage.imageData[j+1+round((I+1)*srcimage.widthStep)]>0 then
Zhangmude[5]:=1 else Zhangmude[5]:=0;
if srcimage.imageData[j+round((I+1)*srcimage.widthStep)]>0 then
Zhangmude[6]:=1 else Zhangmude[6]:=0;
if srcimage.imageData[j-1+round((I+1)*srcimage.widthStep)]>0 then
Zhangmude[7]:=1 else Zhangmude[7]:=0;
if srcimage.imageData[j-1+round(I*srcimage.widthStep)]>0 then
Zhangmude[8]:=1 else Zhangmude[8]:=0;
if srcimage.imageData[j-1+round((I-1)*srcimage.widthStep)]>0 then
Zhangmude[9]:=1 else Zhangmude[9]:=0;
whitepointtotal:=0;
for idx:=2 to 9 do
begin
whitepointtotal:=Zhangmude[idx]+ whitepointtotal;
end;
if (whitepointtotal>=2) and (whitepointtotal<=6) then
begin
ap:=0;
if ((Zhangmude[2]=0) and (Zhangmude[3]=1)) then ap:=ap+1;
if ((Zhangmude[3]=0) and (Zhangmude[4]=1)) then ap:=ap+1;
if ((Zhangmude[4]=0) and (Zhangmude[5]=1)) then ap:=ap+1;
if ((Zhangmude[5]=0) and (Zhangmude[6]=1)) then ap:=ap+1;
if ((Zhangmude[6]=0) and (Zhangmude[7]=1)) then ap:=ap+1;
if ((Zhangmude[7]=0) and (Zhangmude[8]=1)) then ap:=ap+1;
if ((Zhangmude[8]=0) and (Zhangmude[9]=1)) then ap:=ap+1;
if ((Zhangmude[9]=0) and (Zhangmude[2]=1)) then ap:=ap+1;
if ap=1 then
begin
if (Zhangmude[2]*Zhangmude[4]*Zhangmude[6]=0) and (Zhangmude[2]*Zhangmude[4]*Zhangmude[8]=0) then
begin
tempoint.x:=j;
tempoint.y:=i;
deletelist2.Add(tempoint);
end;
end;
end;
end;
end;
end;
for Idx := 0 to deletelist2.Count - 1 do
begin
tempoint:=deletelist2[Idx];
j:=tempoint.x;
i:=tempoint.y;
srcimage.imageData[j+round(i*srcimage.widthStep)]:=0;
end;
if deletelist2.Count= 0 then Break else deletelist2.Clear;
end;
deletelist1.Free;
deletelist2.Free;
end;

推荐阅读