Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Efficient floodfill algorithm' topicStudent Support Forum > General > Archives > "Efficient floodfill algorithm"

Help | Reply To Topic
Author Comment/Response
Dremora
04/25/12 03:08am

G is two-dimensional matrix (for example, binary image). On picture 800x667 working time was 0.776 seconds

FloodFill =
Compile[{{G, _Real,
2}, {p1, _Integer}, {p2, _Integer}, {tar, _Real}, {rep, _Real}},
Module[{Gdata = G, Q = {{p1, p2}},
QNew = Table[{0, 0}, {i, 1, 2000}], ind, x, y, u, dim, MaxLen,
Maxl},
If [Gdata[[p1, p2]] != tar, Return[Gdata]];
dim = Dimensions[Gdata];
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];
];
Return[Gdata]]
];

URL: ,
Help | Reply To Topic