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

Posted hust-阿超

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像细化函数(背景为黑色像素值为0,前景为白色像素值255,必须只含有0和255的像素值,即二值化后的图像)相关的知识,希望对你有一定的参考价值。

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;

以上是关于图像细化函数(背景为黑色像素值为0,前景为白色像素值255,必须只含有0和255的像素值,即二值化后的图像)的主要内容,如果未能解决你的问题,请参考以下文章

在 OpenCV python 中将白色像素转换为黑色

检查灰度图像中的像素是不是为黑色(OpenCV)

将图像对象分成 N 个相等像素的部分(方法)

图像处理------调整亮度与对比度

将RGB转换为黑色或白色

RGB模式的图像中每个像素的颜色值都有R、G、B三个数值来决定,每个数值的范围是0到255。当R、G、B 数值相