动态阈值.docif (in tPeak2<=in tGrayLevel[i ntLoop]) and (in tLoop< >intln dx) the n
if (in tPeak2<=in tGrayLevel[i ntLoop]) and (in tLoop< >intln dx) the n
二值化阈值自动选取方法
双峰法
双峰法的原理及其简单:它认为图像由前景和背景组成, 在灰度直方图上,前后二景都形成
高峰,在双峰之间的最低谷处就是图像的阈值所在。根据这一原理,我们给出了它的实现, 部分代码如下(Pascal语言描述,以下同):
//intPeak、intPeak2、intValley :峰值和直方图值
//intlndx::相应的灰度值
in tPeak,i ntln dx,i ntPeak2,i ntl ndx2,i ntValley,i ntValleyl ndx:i nteger;
//初始双峰值
in tPeak:=0;
in tPeak2:=0;
//取得第一峰值
for in tLoop:=0 to 255 do
if in tPeak<=in tGrayLevel[i ntLoop] the n
begin
in tPeak:=i ntGrayLevel[i ntLoop];
intln dx:=in tLoop;
en d;
//取得第二峰值
for in tLoop:=0 to 255 do
Beg in
begin
if (in tPeak2<=in tGrayLevel[i ntLoop]) and (in tLoop< >intln dx) the n
if (in tPeak2<=in tGrayLevel[i ntLoop]) and (in tLoop< >intln dx) the n
in tPeak2:=i ntGrayLevel[i ntLoop];
in tRGrayLevel:=i ntLoop;
in tl ndx2:=in tLoop;
end
en d;
//取得双峰之间的谷值
in tValley:=i ntSize;
if in tl ndx2< intlndx the n
for in tLoop:=i ntl ndx2 to intlndx do
if in tValley >in tGrayLevel[i ntLoop] the n
begin
in tValley:=in tGrayLevel[ in tLoop];
in tValle yin dx:=i ntLoop;
en d;
从分割的效果来看,当前后景的对比较为强烈时,分割效果较好;否则基本无效。
迭代法
迭代法是基于逼近的思想,其步骤如下:
1 . 求出图象的最大灰度值和最小灰度值,分别记为 ZMAX 和ZMIN,令初始阈值
T0=(ZMAX+ZMIN)/2 ;
根据阈值TK将图象分割为前景和背景,分别求出两者的平均灰度值 ZO和ZB ;
求出新阈值 TK+1=(ZO+ZB)/2 ;
若TK=TK+1 ,则所得即为阈值;否则转 2,迭代计算。
以下给出迭代求阈值的部分实现:
in tRGrayLe
动态阈值 来自淘豆网m.daumloan.com转载请标明出处.