I want to find groups of contiguous cells in a matrix.
So for example let’s consider a 2D matrix below.
In the given matrix there are 2 contiguous groups of cells with value 1:

Here is one way to find these groups:
-
Assign 1st cell with value 1 a different value: let’s say
A. Then examine cells with value1which are adjacent toAand set the value in those cells asA. Search this way until no more contiguous cells are found. -
In the next step increment
AtoBand start with a cell having value1. Then follow the same steps as above.
This is kind of brute force and it won’t be efficient in 3D. Does anyone know of any algorithm that I could use with a little tweaking?
Or any easy solution to this problem?

Here is some psuedo code for a simple flood fill algorithm: