区域生长算法代码

区域生长算法代码
//函数名称: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;


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值