Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9816 > unrolled thread

Efficient floodfill algorithm

Started byYaroslav Linder <yaroslav.linder@gmail.com>
First post2012-04-26 09:35 +0000
Last post2012-04-26 09:35 +0000
Articles 1 — 1 participant

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Efficient floodfill algorithm Yaroslav Linder <yaroslav.linder@gmail.com> - 2012-04-26 09:35 +0000

#9816 — Efficient floodfill algorithm

FromYaroslav Linder <yaroslav.linder@gmail.com>
Date2012-04-26 09:35 +0000
SubjectEfficient floodfill algorithm
Message-ID<jnb4ss$5l8$1@smc.vnet.net>
G is two-dimensional matrix (for example, binarized image) On the
picture 800x662 working time was 0.662 seconds.

FloodFill =
  Compile[{{G, _Real,
     2}, {p1, _Integer}, {p2, _Integer}, {tar, _Real}, {rep, _Real}},
   Module[{Gdata = G, Q = {{p1, p2}}, QNew = {{0, 0}}, ind, x, y, u,
     dim, MaxLen, Maxl},
    If [Gdata[[p1, p2]] != tar, Return[Gdata]];
    dim = Dimensions[Gdata];
    QNew = Table[{0, 0}, {i, 1, 2*Total[dim]}];
    While [Length[Q] > 0,
     ind = 0;
     For [u = 1, u <= Length[Q], u = u + 1,
      {x, y} = Q[[u]];
      If [Gdata[[x, y]] == tar,
       Gdata[[x, y]] = rep;
       If [x > 1 && Gdata[[x - 1, y]] == tar,
        QNew[[++ind]] = {x - 1, y}];
       If [x < dim[[1]] && Gdata[[x + 1, y]] == tar,
        QNew[[++ind]] = {x + 1, y}];
       If [y > 1 && Gdata[[x, y - 1]] == tar,
        QNew[[++ind]] = {x, y - 1}];
       If [y < dim[[2]] && Gdata[[x, y + 1]] == tar,
        QNew[[++ind]] = {x, y + 1}];
       ];
      ];
     Q = Take[QNew, ind];
     Q = DeleteDuplicates[Q];
     ];
    Return[Gdata]]
   ];

[toc] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web