打印本文 打印本文 关闭窗口 关闭窗口
图像分割的一些简单实现
作者:武汉SEO闵涛  文章来源:敏韬网  点击数1963  更新时间:2009/4/23 18:38:21  文章录入:mintao  责任编辑:mintao
f intGrayLevel[intLoop]<>0 then

      intTotalGrayLevel:=intTotalGrayLevel+intLoop*intGrayLevel[intLoop];

 

  //求出初始最大灰度值

  for intLoop:=0 to 255 do

    if intGrayLevel[intLoop]>0 then

    begin

      intLGrayLevel:=intLoop;

      intThresholdVal:=intLoop;

      break;

    end;

 

  //求出初始最小灰度值和初始阈值

  for intLoop:=255 downto 0 do

    if intGrayLevel[intLoop]>0 then

    begin

      intRGrayLevel:=intLoop;

      intThresholdVal:=(intThresholdVal+intLoop)div 2;

      break;

    end;

 

  //迭代求解

  while intThresholdVal<>intThresholdVal2 do

    begin

      intThresholdVal2:=intThresholdVal;

      intCount:=0;

      intLGrayLevel:=0;

      for intLoop:=0 to intThresholdVal do

        if intGrayLevel[intLoop]<>0 then

        begin

          intCount:=intCount+intGrayLevel[intLoop];

          intLGrayLevel:=intLGrayLevel+intLoop*intGrayLevel[intLoop];

        end;

      intRGrayLevel:=intTotalGrayLevel-intLGrayLevel;

      intLGrayLevel:=intLGrayLevel div intCount;

      intRGrayLevel:=intRGrayLevel div (intSize-intCount);

      intThresholdVal:=(intLGrayLevel+intRGrayLevel)div 2;

    end;

迭代所得的阈值分割的图象效果良好。基于迭代的阈值能区分出图像的前景和背景的主要区域所在,但在图像的细微处(如图1中的浅色线条)还没有很好的区分度。

但令人惊讶的是,对某些特定图象,微小数据的变化却会引起分割效果的巨大改变,两者的数据只是稍有变化,但分割效果却反差极大,个中原因还有待进一步研究。

3.  大津法(OTSU法)

大津法由大津于1979年提出,对图像Image,记t为前景与背景的分割阈值,前景点数占图像比例为w0, 平均灰度为u0;背景点数占图像比例为w1,平均灰度为u1。图像的总平均灰度为:u=w0*u0+w1*u1。从最小灰度值到最大灰度值遍历t,当t使得值g=w0*(u0-u)2+w1*(u1-u)2 最大时t即为分割的最佳阈值。对大津法可作如下理解:该式实际上就是类间方差值,阈值t分割出的前景和背景两部分构成了整幅图像,而前景取值u0,概率为w0,背景取值u1,概率为w1,总均值为u,根据方差的定义即得该式。因方差是灰度分布均匀性的一种度量,方差值越大,说明构成图像的两部分差别越大,当部分目标错分为背景或部分背景错分为目标都会导致两部分差别变小,因此使类间方差最大的分割意味着错分概率最小。

直接应用大津法计算量较大,因此我们在实现时采用了等价的公式g=w0*w1*(u0-u1)2。部分计算过程如下:

 

//遍历所有灰度值求Max g。

for intCurrentLevel:=0 to intArrLen do

  begin

    if intSclGrayLevel[intCurrentLevel]=0 then

      continue

    else

      begin

              //计算当阈值为intCurrentLevel时的g

        intCount:=0;

        intSumPels:=0;

        for intLoop:=0 to intCurrentLevel do

          begin

            intCount:=intCount+intSclGrayLevel[intLoop];

            intSumPels:=intSumPels+intSumPelsArr[intLoop];

          end;

        w0:=intCount/intSize;

        u0:=intSumPels/intCount;

        w1:=1-w0;

上一页  [1] [2] [3]  下一页

打印本文 打印本文 关闭窗口 关闭窗口