关于迭代算法 |
尚未結案
|
chenliyan163
一般會員 發表:30 回覆:30 積分:12 註冊:2003-09-15 發送簡訊給我 |
迭代法是基于逼近的思想,其步骤如下: 1. 求出图象的最大灰度值和最小灰度值,分别记为ZMAX和ZMIN,令初始阈值T0=(ZMAX ZMIN)/2; 2. 根据阈值TK将图象分割为前景和背景,分别求出两者的平均灰度值ZO和ZB; 3. 求出新阈值TK 1=(ZO ZB)/2; 4. 若TK=TK 1,则所得即为阈值;否则转2,迭代计算。 以下给出迭代求阈值的部分实现: //阈值初始为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;
试问高手,这个算法对不对,为什么我在算的时候总得不出结果,哪里须改动
|
chenliyan163
一般會員 發表:30 回覆:30 積分:12 註冊:2003-09-15 發送簡訊給我 |
这是整个过程,请个位帮帮忙(为什么会出错);
procedure TForm1.Button2Click(Sender: TObject);
var
p: PByteArray;
x, y, i, j: Integer;
Bmp, bmp2: TBitmap;
Gray: byte;
color: Tcolor;
maxvalue: integer;
intThresholdVal,intThresholdVal2,intTotalGrayLevel,intLoop,intLGrayLevel,intRGrayLevel,intCount:integer;
begin
Bmp := TBitmap.Create;
Bmp.Assign(Self.Image3.Picture.Bitmap);
//24位图处理
Bmp.PixelFormat := pf24Bit;
for y := 0 to Bmp.Height - 1 do
begin
p := Bmp.scanline[y];
for x := 0 to Bmp.Width - 1 do
begin
//算出每一点的灰度值
Gray := round(p[x * 3 2] * 0.3 p[x * 3 1] * 0.59 p[x
* 3]
* 0.11);
for i := 0 to 255 do
begin
if Gray = i then
begin
//统计出每一个灰度级上象素点的个数
Grayclass[i] := Grayclass[i] 1;
end;
end;
end;
end;
//阈值初始为0
intThresholdVal:=0;
intThresholdVal2:=0;
//总灰度值
intTotalGrayLevel:=0;
for intLoop:=0 to 255 do
if Grayclass[intLoop]<>0 then
intTotalGrayLevel:=intTotalGrayLevel intLoop*Grayclass[intLoop]; //求出初始最大灰度值
for intLoop:=1 to 255 do
if Grayclass[intLoop]>0 then
begin
intLGrayLevel:=intLoop;
intThresholdVal:=intLoop;
break;
end;
//求出初始最小灰度值和初始阈值
for intLoop:=255 downto 1 do
if Grayclass[intLoop]>0 then
begin
intRGrayLevel:=intLoop;
intThresholdVal:=(intThresholdVal intLoop)div 2;
break;
end;
//迭代求解
while intThresholdVal<>intThresholdVal2 do
begin
intThresholdVal2:=intThresholdVal;
intCount:=1;
intLGrayLevel:=1;
for intLoop:=1 to intThresholdVal-1 do
if Grayclass[intLoop]<>0 then
begin
intCount:=intCount Grayclass[intLoop];
intLGrayLevel:=intLGrayLevel intLoop*Grayclass[intLoop];
end;
intRGrayLevel:=intTotalGrayLevel-intLGrayLevel;
intLGrayLevel:=intLGrayLevel div intCount;
intRGrayLevel:=intRGrayLevel div intCount;//(intSize-intCount);
intThresholdVal:=(intLGrayLevel intRGrayLevel)div 2;
end;
end;
|
Chance36
版主 發表:31 回覆:1033 積分:792 註冊:2002-12-31 發送簡訊給我 |
|
chenliyan163
一般會員 發表:30 回覆:30 積分:12 註冊:2003-09-15 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |