Hilditch 细化算法

Hilditch 细化算法是经典的二值图像细化算法,然而,在网上却很难找到一个详细、正确的介绍和实现。可以找到一辆个 Hilditch 算法的C实现,但缺乏注释,代码可读性也很差。在期刊网上找到几篇论文,提及了Hilditch 算法,结果一篇说的罗哩罗嗦根本看不懂,另一篇说的说的易懂,却是错误的!拿来主义是行不通了,于是只好结合着这几个论文和代码,从头写 Hilditch 细化算法。

假设像素p的3×3邻域结构为:

image

Hilditch 细化算法的步骤为:

对图像从左向右从上向下迭代每个像素,是为一个迭代周期。在每个迭代周期中,对于每一个像素p,如果它同时满足6个条件,则标记它。在当前迭代周期结束时,则把所有标记的像素的值设为背景值。如果某次迭代周期中不存在标记点(即满足6个条件的像素),则算法结束。假设背景值为0,前景值为1,则:

6个条件为:

(I):p 为1,即p不是背景;

(2):x1,x3,x5,x7不全部为1(否则把p标记删除,图像空心了);

(3):x1~x8 中,至少有2个为1(若只有1个为1,则是线段的端点。若没有为1的,则为孤立点);

(4):p的8连通联结数为1;

联结数指在像素p的3*3邻域中,和p连接的图形分量的个数:

image

上图中,左图的4连通联结数是2,8连通联结数是1,而右图的4联通联结数和8联通联结数都是2。

4连通联结数计算公式是:

image

8连通联结数计算公式是:

image其中,

image 

至于公式怎么来的就不管了,直接用就行了。

(5)假设x3已经标记删除,那么当x3为0时,p的8联通联结数为1;

(6)假设x5已经标记删除,那么当x5为0时,p的8联通联结数为1。

 

 

 

代码如下:

 

 

======

在程序中,我使用的是这样的邻域编码:

image

为了方便计算联结数,以0作为前景,1作为背景。程序如下(完整程序见:http://smartimage.googlecode.com/svn/trunk/src/Orc.SmartImage.Common/UnmanagedImage/ImageU8.cs):

/// <summary> 
/// 计算八联结的联结数,计算公式为: 
///     (p6 - p6*p7*p0) + sigma(pk - pk*p(k+1)*p(k+2)), k = {0,2,4) 
/// </summary> 
/// <param name="list"></param> 
/// <returns></returns> 
private unsafe Int32 DetectConnectivity(Int32* list) 

    Int32 count = list[6] - list[6] * list[7] * list[0]; 
    count += list[0] - list[0] * list[1] * list[2]; 
    count += list[2] - list[2] * list[3] * list[4]; 
    count += list[4] - list[4] * list[5] * list[6]; 
    return count; 
}

private unsafe void FillNeighbors(Byte* p, Int32* list, Int32 width, Byte foreground = 255) 

    // list 存储的是补集,即前景点为0,背景点为1,以方便联结数的计算

    list[0] = p[1] == foreground ? 0 : 1; 
    list[1] = p[1 - width] == foreground ? 0 : 1; 
    list[2] = p[-width] == foreground ? 0 : 1; 
    list[3] = p[-1 - width] == foreground ? 0 : 1; 
    list[4] = p[-1] == foreground ? 0 : 1; 
    list[5] = p[-1 + width] == foreground ? 0 : 1; 
    list[6] = p[width] == foreground ? 0 : 1; 
    list[7] = p[1 + width] == foreground ? 0 : 1; 
}

/// <summary> 
/// 使用 hilditch 算法进行细化 
/// </summary> 
public unsafe void Thinning(Byte foreground = 255) 

    Byte* start = this.Start; 
    Int32 width = this.Width; 
    Int32 height = this.Height; 
    Int32* list = stackalloc Int32[8]; 
    Byte background = (Byte)(255 - foreground); 
    Int32 length = this.Length;

    using (ImageU8 mask = new ImageU8(this.Width, this.Height)) 
    { 
        mask.Fill(0);

        Boolean loop = true; 
        while (loop == true) 
        { 
            loop = false; 
            for (Int32 r = 1; r < height - 1; r++) 
            { 
                for (Int32 c = 1; c < width - 1; c++) 
                { 
                    Byte* p = start + r * width + c;

                    // 条件1:p 必须是前景点 
                    if (*p != foreground) continue;

                    //  p3  p2  p1 
                    //  p4  p   p0 
                    //  p5  p6  p7 
                    // list 存储的是补集,即前景点为0,背景点为1,以方便联结数的计算 
                    FillNeighbors(p, list, width, foreground);

                    // 条件2:p0,p2,p4,p6 不皆为前景点 
                    if (list[0] == 0 && list[2] == 0 && list[4] == 0 && list[6] == 0) 
                        continue;

                    // 条件3: p0~p7至少两个是前景点 
                    Int32 count = 0; 
                    for (int i = 0; i < 8; i++) 
                    { 
                        count += list[i]; 
                    }

                    if (count > 6) continue;

                    // 条件4:联结数等于1 
                    if (DetectConnectivity(list) != 1) continue;

                    // 条件5: 假设p2已标记删除,则令p2为背景,不改变p的联结数 
                    if (mask[r - 1, c] == 1) 
                    { 
                        list[2] = 1; 
                        if (DetectConnectivity(list) != 1) 
                            continue; 
                        list[2] = 0; 
                    }

                    // 条件6: 假设p4已标记删除,则令p4为背景,不改变p的联结数 
                    if (mask[r, c - 1] == 1) 
                    { 
                        list[4] = 1; 
                        if (DetectConnectivity(list) != 1) 
                            continue; 
                    } 
                    mask[r, c] = 1; // 标记删除 
                    loop = true; 
                } 
            }

            for (int i = 0; i < length; i++) 
            { 
                if (mask[i] == 1) 
                { 
                    this[i] = background; 
                } 
            } 
        } 
    } 
}

======


 

 

我的代码如下:

 

void HilditchThinning(int w,int h,BYTE *imgBuf)< xmlnamespace prefix ="o" ns ="urn:schemas-microsoft-com:office:office" />

{

    //           p3  p2  p1

    // 8近邻     p4  p   p0

    //           p5  p6  p7

    int neighbor[8]; 

    BYTE *mask=new BYTE[w*h];

    memset(mask,0,w*h);

 

    BOOL loop=TRUE;

    int x,y,k,index;

 

    while(loop)

    {

       loop=FALSE;

       loopNum++;

 

       for(y=0;y<h;y++)

       {

           for(x=0;x<w;x++)

           {

              index=y*w+x; ;

 

              //条件1:p必须是前景点

              if(imgBuf[index]==0 ) continue;

 

              neighbor[0]=x+1<w ? imgBuf[y*w+x+1] : 0;

              neighbor[1]=y-1>0&&x+1<w ? imgBuf[(y-1)*w+x+1] : 0;

              neighbor[2]=y-1>0 ? imgBuf[(y-1)*w+x] : 0;

              neighbor[3]=y-1>0&&x-1<0 ? imgBuf[(y-1)*w+x-1] : 0;

              neighbor[4]=x-1>0 ? imgBuf[y*w+x-1] : 0;

              neighbor[5]=x-1>0&&y+1<h ? imgBuf[(y+1)*w+x-1] : 0;

              neighbor[6]=y+1<h ? imgBuf[(y+1)*w+x] : 0;

              neighbor[7]=y+1<h&&x+1<w ? imgBuf[(y+1)*w+x+1] : 0;

 

              //条件2:p0,p2,p4,p6不全为前景色(否则把点p删了,图像空心)

              if(neighbor[0]&&neighbor[2]&&neighbor[4]&&neighbor[6])

                  continue;

 

              //条件3:p0~p7中,至少有个为前景色

              //(若只有一个为,则为端点,若没有为的,则为孤立点)

              int count=0;

              for(int i=0;i<8;i++)

              {

                  if(neighbor[i]==255)

                     count++;

              }

              if(count<2) 

              {

                  continue;

              }

 

              //条件4:p的八近邻连接数必须为1

              if(Get8Connectivity(neighbor)!=1) continue;

 

              //条件5:若p2已经被标记删除,则当p2为背景色时,P的连接数仍需为1

              k=(y-1)*w+x;

              if(y-1>0 && mask[k]==1)

              {            

                  imgBuf[k]=0;

                  if(Get8Connectivity(neighbor)!=1) continue;

                  imgBuf[k]=1;

              }

 

              //条件6:若p4已经被标记删除,则当p4为背景色时,P的连接数仍需为1

              k=y*w+x-1;

              if(x-1>0 && mask[k]==1)

              {            

                  imgBuf[k]=0;

                  if(Get8Connectivity(neighbor)!=1) continue;

                  imgBuf[k]=1;

              }

 

              //标记删除

              mask[w*y+x]=1;   

              loop=TRUE;

           }

       }

 

 

       //将标记删除的点置为背景色

       for(y=0;y<h;y++)

       {

           for(x=0;x<w;x++)

           {

              k=y*w+x;

              if(mask[k]==1) imgBuf[k]=0;

           }

       }  

 

 

    }

   

 

}

//                                  p3  p2  p1

//*************计算近邻的连接数     p4  p   p0

//                                  p5  p6  p7

int Get8Connectivity(intneighbor)

{  

 

    //计算补集x^=1-x;

    for(int i=0;i<8;i++)

    {

       neighbor[i]=neighbor[i]==0?1:0;

    }  

   

    int countneighbor[0]-(neighbor[0]&neighbor[1]&neighbor[2]);

    count+= neighbor[2]-(neighbor[2]&neighbor[3]&neighbor[4]);

    count+= neighbor[4]-(neighbor[4]&neighbor[5]&neighbor[6]);

    count+= neighbor[6]-(neighbor[6]&neighbor[7]&neighbor[0]);

 

    return count;

}

 

这个细化算法,细化后会产生毛刺

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值