?? diedai.txt
字號:
迭代法
迭代法是基于逼近的思想,其步驟如下:
1. 求出圖象的最大灰度值和最小灰度值,分別記為ZMAX和ZMIN,令初始閾值T0=(ZMAX+ZMIN)/2;
2. 根據(jù)閾值TK將圖象分割為前景和背景,分別求出兩者的平均灰度值ZO和ZB;
3. 求出新閾值TK+1=(ZO+Z/2;
4. 若TK=TK+1,則所得即為閾值;否則轉2,迭代計算。
以下給出迭代求閾值的部分實現(xiàn):
//閾值初始為0
intThresholdVal:=0;
intThresholdVal2:=0;
//總灰度值
intTotalGrayLevel:=0;
for intLoop:=0 to 255 do
if 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;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -