# Pixel connectivity

In image processing, pixel connectivity is the way in which pixels in 2-dimensional (or hypervoxels in n-dimensional) images relate to their neighbors.

## Formulation

The 9 possible connectivities in a 5x5x5 neighborhood

In order to specify a set of connectivities, the dimension ${\displaystyle N}$ and the width of the neighborhood ${\displaystyle n}$, must be specified. The dimension of a neighborhood is valid for any dimension ${\displaystyle n\geq 1}$. A common width is 3, which means along each dimension, the central cell will be adjacent to 1 cell on either side for all dimensions.

Let ${\displaystyle M_{N}^{n}}$ represent a N-dimensional hypercubic neighborhood with size on each dimension of ${\displaystyle n=2k+1,k\in \mathbb {Z} }$

Let ${\displaystyle {\vec {q}}}$ represent a discrete vector in the first orthant from the center structuring element to a point on the boundary of ${\displaystyle M_{N}^{n}}$. This implies that each element ${\displaystyle q_{i}\in \{0,1,...,k\},\forall i\in \{1,2,...,N\}}$ and that at least one component ${\displaystyle q_{i}=k}$

Let ${\displaystyle S_{N}^{d}}$ represent a N-dimensional hypersphere with radius of ${\displaystyle d=\left\Vert {\vec {q}}\right\Vert }$.

Define the amount of elements on the hypersphere ${\displaystyle S_{N}^{d}}$ within the neighborhood ${\displaystyle M_{N}^{n}}$ as ${\displaystyle E}$. For a given ${\displaystyle {\vec {q}}}$, ${\displaystyle E}$ will be equal to the amount of permutations of ${\displaystyle {\vec {q}}}$ multiplied by the number of orthants.

Let ${\displaystyle n_{j}}$ represent the amount of elements in vector ${\displaystyle {\vec {q}}}$ which take the value ${\displaystyle j}$. ${\displaystyle n_{j}=\sum _{i=1}^{N}(q_{i}=j)}$

The total number of permutation of ${\displaystyle {\vec {q}}}$ can be represented by a multinomial as ${\displaystyle {\frac {N!}{\prod _{j=0}^{k}n_{j}!}}}$

If any ${\displaystyle q_{i}=0}$, then the vector ${\displaystyle {\vec {q}}}$ is shared in common between orthants. Because of this, the multiplying factor on the permutation must be adjusted from ${\displaystyle 2^{N}}$ to be ${\displaystyle 2^{N-n_{0}}}$

Multiplying the number of amount of permutations by the adjusted amount of orthants yields,

${\displaystyle E={\frac {N!}{\prod _{j=0}^{k}n_{j}!}}2^{N-n_{0}}}$

Let ${\displaystyle V}$ represent the number of elements inside of the hypersphere ${\displaystyle S_{N}^{d}}$ within the neighborhood ${\displaystyle M_{N}^{n}}$. ${\displaystyle V}$ will be equal to the number of elements on the hypersphere plus all of the elements on the inner shells. The shells must be ordered by increasing order of ${\displaystyle \left\Vert {\vec {q}}\right\Vert =r}$. Assume the ordered vectors ${\displaystyle {\vec {q}}}$ are assigned a coefficient ${\displaystyle p}$ representing its place in order. Then an ordered vector ${\displaystyle {\vec {q_{p}}},p\in \{1,2,...,\sum _{x=1}^{k}(x+1)\}}$ if all ${\displaystyle r}$ are unique. Therefore ${\displaystyle V}$ can be defined iteratively as

${\displaystyle V_{\vec {q_{p}}}=V_{\vec {q_{p-1}}}+E_{\vec {q_{p}}},V_{\vec {q_{0}}}=0}$,

or

${\displaystyle V_{\vec {q_{p}}}=\sum _{x=1}^{p}E_{\vec {q_{x}}}}$

If some ${\displaystyle \left\Vert {\vec {q_{x}}}\right\Vert =\left\Vert {\vec {q_{y}}}\right\Vert }$, then both vectors should be considered as the same ${\displaystyle p}$ such that

${\displaystyle V_{\vec {q_{p}}}=V_{\vec {q_{p-1}}}+E_{\vec {q_{p,1}}}+E_{\vec {q_{p,2}}},V_{\vec {q_{0}}}=0}$

Note that each neighborhood will need to have the values from the next smallest neighborhood added. Ex. ${\displaystyle V_{{\vec {q}}=(0,2)}=V_{{\vec {q}}=(1,1)}+E_{{\vec {q}}=(0,2)}}$

${\displaystyle V}$ includes the center hypervoxel, which is not included in the connectivity. Subtracting 1 yields the neighborhood connectivity, ${\displaystyle G}$

${\displaystyle G=V-1}$[1]

### Table of Selected Connectivities

Neighborhood Size:

${\displaystyle M_{N}^{n}}$

Connectivity Type Typical Vector:

${\displaystyle {\vec {q}}}$

${\displaystyle d}$

Elements on Sphere:

${\displaystyle E}$

Elements in Sphere:

${\displaystyle V}$

Neighborhood Connectivity:

${\displaystyle G}$

1 edge (0) 0 1*1=1 1 0
3 point (1) 1 1*2=2 3 2
5 point-point (2) 4 1*2=2 5 4
...
1x1 face (0,0) 0 1*1=1 1 0
3x3 edge (0,1) 1 2*2=4 5 4
point (1,1) 2 1*4=4 9 8
5x5 edge-edge (0,2) 4 2*2=4 13 12
point-edge (1,2) 5 2*4=8 21 20
point-point (2,2) 8 1*4=4 25 24
...
1x1x1 volume (0,0,0) 0 1*1=1 1 0
3x3x3 face (0,0,1) 1 3*2=6 7 6
edge (0,1,1) 2 3*4=12 19 18
point (1,1,1) 3 1*8=8 27 26
5x5x5 face-face (0,0,2) 4 3*2=6 33 32
edge-face (0,1,2) 5 6*4=24 57 56
point-face (1,1,2) 6 3*8=24 81 80
edge-edge (0,2,2) 8 3*4=12 93 92
point-edge (1,2,2) 9 3*8=24 117 116
point-point (2,2,2) 12 1*8=8 125 124
...
1x1x1x1 hypervolume (0,0,0,0) 0 1*1=1 1 0
3x3x3x3 volume (0,0,0,1) 1 4*2=8 9 8
face (0,0,1,1) 2 6*4=24 33 32
edge (0,1,1,1) 3 4*8=32 65 64
point (1,1,1,1) 4 1*16=16 81 80
...

### Example

Consider solving for ${\displaystyle G|{\vec {q}}=(0,1,1)}$

In this scenario, ${\displaystyle N=3}$ since the vector is 3-dimensional. ${\displaystyle n_{0}=1}$ since there is one ${\displaystyle q_{i}=0}$. Likewise, ${\displaystyle n_{1}=2}$. ${\displaystyle k=1,n=3}$ since ${\displaystyle \max q_{i}=1}$. ${\displaystyle d={\sqrt {0^{2}+1^{2}+1^{2}}}={\sqrt {2}}}$. The neighborhood is ${\displaystyle M_{3}^{3}}$ and the hypersphere is ${\displaystyle S_{3}^{\sqrt {2}}}$

${\displaystyle E={\frac {3!}{1!*2!*0!}}2^{3-1}={\frac {6}{2}}4=12}$

The basic ${\displaystyle {\vec {q}}}$ in the neighborhood ${\displaystyle N_{3}^{3}}$, ${\displaystyle {\vec {q_{1}}}=(0,0,0)}$. The Manhattan Distance between our vector and the basic vector is ${\displaystyle \left\Vert {\vec {q}}-{\vec {q_{0}}}\right\Vert _{1}=2}$, so ${\displaystyle {\vec {q}}={\vec {q_{3}}}}$. Therefore,

${\displaystyle G_{\vec {q_{3}}}=V_{\vec {q_{3}}}-1=E_{\vec {q_{1}}}+E_{\vec {q_{2}}}+E_{\vec {q_{3}}}-1=E_{{\vec {q}}=(0,0,0)}+E_{{\vec {q}}=(0,0,1)}+E_{{\vec {q}}=(0,1,1)}}$
${\displaystyle E_{{\vec {q}}=(0,0,0)}={\frac {3!}{3!*0!*0!}}2^{3-3}={\frac {6}{6}}1=1}$
${\displaystyle E_{{\vec {q}}=(0,0,1)}={\frac {3!}{2!*1!}}2^{3-2}={\frac {6}{2}}2=6}$
${\displaystyle G=1+6+12-1=18}$

Which matches the supplied table

### Higher values of k & N

The assumption that all ${\displaystyle \left\Vert {\vec {q_{p}}}\right\Vert =r}$ are unique does not hold for higher values of k & N. Consider ${\displaystyle N=2,k=5}$, and the vectors ${\displaystyle {\vec {q_{A}}}=(0,5),{\vec {q_{B}}}=(3,4)}$. Although ${\displaystyle {\vec {q_{A}}}}$ is located in ${\displaystyle M_{2}^{5}}$, the value for ${\displaystyle r=25}$, whereas ${\displaystyle {\vec {q_{B}}}}$ is in the smaller space ${\displaystyle M_{2}^{4}}$ but has an equivalent value ${\displaystyle r=25}$. ${\displaystyle {\vec {q_{C}}}=(4,4)\in M_{2}^{4}}$ but has a higher value of ${\displaystyle r=32}$ than the minimum vector in ${\displaystyle M_{2}^{5}}$.

For the this assumption to hold, ${\displaystyle {\begin{cases}N=2,k\leq 4\\N=3,k\leq 2\\N=4,k\leq 1\end{cases}}}$

At higher values of ${\displaystyle k}$ & ${\displaystyle N}$, Values of ${\displaystyle d}$ will become ambiguous. This means that specification of a given ${\displaystyle d}$ could refer to multiple ${\displaystyle {\vec {q_{p}}}\in M_{n}^{N}}$.

## Types of connectivity

### 2-dimensional

Example of neighborhood of pixels - association of eight and four pixels

#### 4-connected

4-connected pixels are neighbors to every pixel that touches one of their edges. These pixels are connected horizontally and vertically. In terms of pixel coordinates, every pixel that has the coordinates

${\displaystyle \textstyle (x\pm 1,y)}$ or ${\displaystyle \textstyle (x,y\pm 1)}$

is connected to the pixel at ${\displaystyle \textstyle (x,y)}$.

#### 6-connected

6-connected pixels are neighbors to every pixel that touches one of their corners (which includes pixels that touch one of their edges) in a hexagonal grid or stretcher bond rectangular grid.

There are several ways to map hexagonal tiles to integer pixel coordinates. With one method, in addition to the 4-connected pixels, the two pixels at coordinates ${\displaystyle \textstyle (x+1,y+1)}$ and ${\displaystyle \textstyle (x-1,y-1)}$ are connected to the pixel at ${\displaystyle \textstyle (x,y)}$.

#### 8-connected

8-connected pixels are neighbors to every pixel that touches one of their edges or corners. These pixels are connected horizontally, vertically, and diagonally. In addition to 4-connected pixels, each pixel with coordinates ${\displaystyle \textstyle (x\pm 1,y\pm 1)}$ is connected to the pixel at ${\displaystyle \textstyle (x,y)}$.

### 3-dimensional

#### 6-connected

6-connected pixels are neighbors to every pixel that touches one of their faces. These pixels are connected along one of the primary axes. Each pixel with coordinates ${\displaystyle \textstyle (x\pm 1,y,z)}$, ${\displaystyle \textstyle (x,y\pm 1,z)}$, or ${\displaystyle \textstyle (x,y,z\pm 1)}$ is connected to the pixel at ${\displaystyle \textstyle (x,y,z)}$.

#### 18-connected

18-connected pixels are neighbors to every pixel that touches one of their faces or edges. These pixels are connected along either one or two of the primary axes. In addition to 6-connected pixels, each pixel with coordinates ${\displaystyle \textstyle (x\pm 1,y\pm 1,z)}$, ${\displaystyle \textstyle (x\pm 1,y\mp 1,z)}$, ${\displaystyle \textstyle (x\pm 1,y,z\pm 1)}$, ${\displaystyle \textstyle (x\pm 1,y,z\mp 1)}$, ${\displaystyle \textstyle (x,y\pm 1,z\pm 1)}$, or ${\displaystyle \textstyle (x,y\pm 1,z\mp 1)}$ is connected to the pixel at ${\displaystyle \textstyle (x,y,z)}$.

#### 26-connected

26-connected pixels are neighbors to every pixel that touches one of their faces, edges, or corners. These pixels are connected along either one, two, or all three of the primary axes. In addition to 18-connected pixels, each pixel with coordinates ${\displaystyle \textstyle (x\pm 1,y\pm 1,z\pm 1)}$, ${\displaystyle \textstyle (x\pm 1,y\pm 1,z\mp 1)}$, ${\displaystyle \textstyle (x\pm 1,y\mp 1,z\pm 1)}$, or ${\displaystyle \textstyle (x\mp 1,y\pm 1,z\pm 1)}$ is connected to the pixel at ${\displaystyle \textstyle (x,y,z)}$.