求VB区域生长算法示例
求VB区域生长算法示例啊,最好有详细注释,谁有发份给我好吗?taitbwei@163.com
[解决办法]
参考一下吧
区域生长算法代码
//函数名称:FillDibEx
//函数功能:区域生长
//入口参数:SrcImg : TGrayImg - 原图象
// Seed : TPoint - 起始种子坐标
// DestImg : TGrayImg - 目的图象
//返回参数:Boolean - 成功返回True,否则返回False
//===================================================================//
function FillDibEx(SrcImg : TGrayImg; Seed : TPoint; var DestImg : TGrayImg) : Boolean;
var
i, j : Integer;
Seeds : array of TPoint; //种子堆栈
StackPoint : Integer; //堆栈指针
iCurrentPixelx, iCurrentPixely : Integer; //当前象素位置
pixel : Byte;
begin
//初始化种子
try
SetLength(Seeds, SrcImg.Width * SrcImg.Height);
except
Result := False;
Exit;
end;
Seeds[1].Y := Seed.Y;
Seeds[1].X := Seed.X;
StackPoint := 1;
While (StackPoint <> 0) do
begin
//取出种子
iCurrentPixelx := Seeds[StackPoint].X;
iCurrentPixely := Seeds[StackPoint].Y;
//退栈
Dec(StackPoint);
pixel := SrcImg.Img[iCurrentPixely, iCurrentPixelx];
//不是二值图象
if (pixel <> 255) and (pixel <> 0) and (pixel <> 128) then
begin
Result := False;
Exit;
end;
//将当前的点涂黑
SrcImg.Img[iCurrentPixely, iCurrentPixelx] := 128;
//判断左边的点,如果为白,则压入堆栈
//注意防止越界
if iCurrentPixelx > 0 then
begin
pixel := SrcImg.Img[iCurrentPixely, iCurrentPixelx - 1];
if pixel = 255 then
begin
Inc(StackPoint);
Seeds[StackPoint].Y := iCurrentPixely;
Seeds[StackPoint].X := iCurrentPixelx - 1;
end;
if (pixel <> 0) and (pixel <> 128) and (pixel <> 255) then
begin
Result := False;
Exit;
end;
end;
//判断下面的点,如果为白,压入堆栈
//注意防止越界
if (iCurrentPixely < SrcImg.Height - 1) then
begin
pixel := SrcImg.Img[iCurrentPixely + 1, iCurrentPixelx];
if pixel = 255 then
begin
Inc(StackPoint);
Seeds[StackPoint].Y := iCurrentPixely + 1;
Seeds[StackPoint].X := iCurrentPixelx;
end;
if (pixel <> 0) and (pixel <> 128) and (pixel <> 255) then
begin
Result := False;
Exit;
end;
end;
//判断右边的点,如果为白,则压入堆栈
//注意防止越界
if iCurrentPixelx < SrcImg.Width - 1 then
begin
pixel := SrcImg.Img[iCurrentPixely, iCurrentPixelx + 1];
if pixel = 255 then
begin
Inc(StackPoint);
Seeds[StackPoint].Y := iCurrentPixely;
Seeds[StackPoint].X := iCurrentPixelx + 1;
end;
if (pixel <> 0) and (pixel <> 128) and (pixel <> 255) then
begin
Result := False;
Exit;
end;
end;
//判断上面的点,如果为白,压入堆栈
//注意防止越界
if (iCurrentPixely > 0) then
begin
pixel := SrcImg.Img[iCurrentPixely - 1, iCurrentPixelx];
if pixel = 255 then
begin
Inc(StackPoint);
Seeds[StackPoint].Y := iCurrentPixely - 1;
Seeds[StackPoint].X := iCurrentPixelx;
end;
if (pixel <> 0) and (pixel <> 128) and (pixel <> 255) then
begin
Result := False;
Exit;
end;
end;
end;
//保存填充区域,恢复原始图象
if not SetImgArray(SrcImg.Width, SrcImg.Height, DestImg) then
begin
Result := False;
Exit;
end;
for i := 0 to SrcImg.Height - 1 do
for j := 0 to SrcImg.Width - 1 do
begin
DestImg.Img[i, j] := SrcImg.Img[i, j];
if SrcImg.Img[i, j] = 128 then
begin
// SrcImg.Img[i,j] := 255;
DestImg.Img[i,j] := 0;
end;
end;
Result := True;
end;