# Need idea for algorithm - fill holes in matrix

This topic is 2485 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Need idea for algorithm - fill holes in matrix

I need idea to write a algorithm that will fill "holes" in my matrix.
Matrix looks like on this picture (white squares have FALSE values and black squares have TRUE values):

I need matrix to look like this:

Any idea how to find this holes in matrix and fill them with black squares (i.e. assign TRUE values to those matrix members)?

p.s. My matrix is quite large and I have a lot of matrices to examine.

##### Share on other sites
You would pick a random white cell that is considered "outside" the black area, then you flood to the surrounding tiles until you have no more white cells to flood. Then you find all the white cells that didnt get flooded and turn them black.

If you [s]want[/s] need ugly optimizations, you could make the pic in a quadtree with all bottom level nodes existing, and have the not-top-level nodes be either black white or both, so you can flood at a higher level if the high nodes are only of single color, and then go deeper in the tree to flood smaller areas when you get to an node with both black and white.

Something like that.

Edit: Though im not sure if that would be even faster...

##### Share on other sites

You would pick a random white cell that is considered "outside" the black area, then you flood to the surrounding tiles until you have no more white cells to flood. Then you find all the white cells that didnt get flooded and turn them black.

If you [s]want[/s] need ugly optimizations, you could make the pic in a quadtree with all bottom level nodes existing, and have the not-top-level nodes be either black white or both, so you can flood at a higher level if the high nodes are only of single color, and then go deeper in the tree to flood smaller areas when you get to an node with both black and white.

Something like that.

Edit: Though im not sure if that would be even faster...

Great! That was fast.
Thx!

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5
khawk
11

• 9
• 9
• 11
• 11
• 23
• ### Forum Statistics

• Total Topics
633677
• Total Posts
3013283
×