Connected-element Analysis



Find and label bi-dimensional subsets of connected elements.

Given an input grid, find the connected-components.

Checking neighborhood


First step is to define which element of the neighborhood is valid, for the current position.

4-conectivity


This analysis consider only the elements connected horizontally or vertically. It means that, for an element $(x, y)$, its neighborhood is only $(x \pm 1, y)$ or $(x, y \pm 1)$.

$$ \large \begin{bmatrix} 0 & & 1 & & 0 \\ & \nwarrow & \uparrow & \nearrow \\ 1 & \leftarrow & ? & \rightarrow & 1 \\ & \swarrow & \downarrow & \searrow \\ 0 & & 1 & & 0 \end{bmatrix} $$

8-conectivity


For each element $(x, y)$, this analysis consider as connected all neighbors that touch one of its corners.

$$ \large \begin{bmatrix} 1 & & 1 & & 1 \\ & \nwarrow & \uparrow & \nearrow \\ 1 & \leftarrow & ? & \rightarrow & 1 \\ & \swarrow & \downarrow & \searrow \\ 1 & & 1 & & 1 \end{bmatrix} $$