Results 1 to 5 of 5

Thread: non-recursive flood fill method?

  1. #1
    Join Date
    Dec 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Quoted
    44 Post(s)

    Default non-recursive flood fill method?

    I want to have a function that gives coordinates of everything in a continuous area based on a HUE criteria. If that doesn't exist, I am thinking of using making one with the quick fill algorithm, http://www.codeproject.com/Articles/...fill-algorithm

    Some more information: I want to directly sample each coordinates for its color from the client itself. Standard Non-recursive flood fill doesn't work because its arrays store an additional piece of information (it can change a target color to the fill color, and use that to distinguish coordinates that's already been targeted). I don't want to store colors into a 3D Tpoint array, and then use standard flood fill methods. I rather have an algorithm that directly tells you a continuous area of a specific color.

    To me, the quickfill alogrithm is the most interesting. Any thoughts on what algorithm to use?
    Last edited by Grunt; 04-23-2014 at 08:27 AM.

  2. #2
    Join Date
    Nov 2011
    Location
    England
    Posts
    3,072
    Mentioned
    296 Post(s)
    Quoted
    1094 Post(s)

    Default

    @slacky may have a idea!

  3. #3
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    Meh, I don't understand what he wants to achieve.

    Is this all you want: "an algorithm that directly tells you a continuous area of a specific color.".. from a given starting point on an image ??
    !No priv. messages please

  4. #4
    Join Date
    Dec 2008
    Posts
    135
    Mentioned
    0 Post(s)
    Quoted
    44 Post(s)

    Default

    Quote Originally Posted by slacky View Post

    Is this all you want: "an algorithm that directly tells you a continuous area of a specific color.".. from a given starting point on an image ??
    Yes, with minimal repeated sampling

  5. #5
    Join Date
    Feb 2012
    Location
    Norway
    Posts
    995
    Mentioned
    145 Post(s)
    Quoted
    596 Post(s)

    Default

    This may not be exactly what you are looking for but you should be able to modify it to suit your needs. It floodfills by a given tolerance, and does so in a continuous manner. It can be shortened down to one single function (or preferably two), but I have written it in the most obvious way, so it's easy to modify and open for optimizations.
    Simba Code:
    procedure TPointArray.Add(Pt:TPoint);
    var L: Integer;
    begin
      L := Length(Self);
      SetLength(Self, L+1);
      Self[L] := Pt;
    end;

    function TPointArray.Pop(): TPoint;
    var L: Integer;
    begin
      L := High(Self);
      Result := Self[L];
      SetLength(Self, L);
    end;


    function ConnectedColors(const Img:T2DIntArray; Start:TPoint; Color, Tol, HueTol: Int32): TPointArray;
    var
      I,j,x,y,W,H:Integer;
      face,queue:TPointArray;
      Matrix:Array of TBoolArray;
      hit: TPoint;

    procedure GetAdjacent(var adj:TPointArray; n:TPoint; EightWay:Boolean);
    begin
      adj[0] := Point(n.x-1,n.y);
      adj[1] := Point(n.x,n.y-1);
      adj[2] := Point(n.x+1,n.y);
      adj[3] := Point(n.x,n.y+1);
      if EightWay then
      begin
        adj[4] := Point(n.x-1,n.y-1);
        adj[5] := Point(n.x+1,n.y+1);
        adj[6] := Point(n.x-1,n.y+1);
        adj[7] := Point(n.x+1,n.y-1);
      end;
    end;

    //redefine me to use other means of comparison.
    function CompareColors(colorA, ColorB: Int32; GenTol, HueTol: Int32): Boolean;
    var H0,H1,S0,S1,L0,L1: Extended;
    begin
      ColorToHSL(ColorA, H0,S0,L0);
      ColorToHSL(ColorB, H1,S1,L1);
      Result := ((Trunc(H1) - Trunc(H0) + 50) mod 100) - 50 <= HueTol;
      if not(Result) then Exit;
      Result := Abs(L1-L0) + Abs(S1-S0) <= GenTol;
    end;

    begin
      W := Length(Img[0]);
      H := Length(Img);

      SetLength(Matrix, H, W);
      //SetLength(Result, H * W); //PreInit result? Extra mem usage..

      SetLength(Face, 8); //4 or 8 (connectivity)
      Queue.Add(Start);
      while Length(Queue) > 0 do
      begin
        hit := Queue.Pop;
        GetAdjacent(Face, hit, True); //True = 8 way connectivity
        for j:=0 to 7 do
        begin
          x := face[j].x;
          y := face[j].y;
          if ((x >= 0) and (y >= 0) and (x < W) and (y < H)) then
          begin
            if (Matrix[y][x] <> True) and
               (CompareColors(img[y][x], img[hit.y][hit.x],Tol,HueTol)) then
            begin
              Matrix[y][x] := True;
              Queue.Add(face[j]);
              //Result[i] := Point(x, y); //if PreInit
              Result.Add(face[j]);
              Inc(I);
            end;
          end;
        end;
      end;
      //SetLength(Result, I); //if PreInit
    end;

    then.. (pseudocode)
    Code:
      BMP := BitmapFromClient(...);
      TPA := ConnectedColors(BitmapToMatrix(BMP), Point(300, 300), color, Tolernance, HueTolerance);
    Hope you are able to modify/use it.
    !No priv. messages please

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •