迭代法阈值选择算法是对双峰法的改进,他⾸先选择⼀个近似的阈值T,将图像分割成两个部分,R1和R2,计算出区域R1和R2的均值u1和u2,再选择新的阈值。 迭代法的原理
迭代法是⼀种⽐较简单的阈值分割⽅法,其思想:设置阈值的初始值为图像灰度最⼤值和最⼩值的平均,根据阈值划分图像为⽬标和背景,并分别将其灰度值求和,计算⽬标和背景的平均灰度,并判断阈值是否等于⽬标和背景平均灰度的和的平均,若相等,则阈值即为其平均,否则,将阈值设置为⽬标和背景灰度平局值的和的⼀半,继续迭代,直⾄计算出阈值。 算法过程
2011新课标文综
1、设置阈值的初始值:
ZMax = max(max(I));
ZMin = min(min(I));
TK = (ZMax+ZMin)/2;
2、根据阈值划分图像为⽬标和背景,分别计算其灰度均值
ZO=ForegroundSum/iForeground;
ZB=BackgroundSum/iBackground;
TKTmp=uint8((ZO+ZB)/2);
3、判断tk是否等于tktmp,若相等则退出循环,阈值为tktmp,否则,将tktmp赋给tk,转2继续执⾏迭代。
参考代码
function img =adaptvie_segment(I)
%⾃适应阈值分割
I=uint8(I);
ZMax=max(max(I));
ZMin=min(min(I));
TK=(ZMax+ZMin)/2;
bCal=1;
乙二酸iSize=size(I);
while(bCal)
iForeground=0;
轮滑天地iBackground=0;
ForegroundSum=0;
BackgroundSum=0;
for i=1:iSize(1)
for j=1:iSize(2)
tmp=I(i,j);
if(tmp>=TK)
孔子的人性论iForeground=iForeground+1;
ForegroundSum=ForegroundSum+double(tmp);
else
iBackground=iBackground+1;
BackgroundSum=BackgroundSum+double(tmp); end
end
阈值电压end
ZO=ForegroundSum/iForeground;
ZB=BackgroundSum/iBackground;
TKTmp=uint8((ZO+ZB)/2);
if(TKTmp==TK)
bCal=0;
else
恒温器
TK=TKTmp;
end
end
newI=im2bw(I,double(TK)/255);
img = newI;
end