`applylut`

performs a neighborhood operation
on a binary image by producing a matrix of indices into `lut`

,
and then replacing the indices with the actual values in `lut`

.
The specific algorithm used depends on whether you use 2-by-2 or 3-by-3
neighborhoods.

### 2-by-2 Neighborhoods

For 2-by-2 neighborhoods, `length(lut)`

is
16. There are four pixels in each neighborhood, and two possible states
for each pixel, so the total number of permutations is 2^{4} =
16.

To produce the matrix of indices, `applylut`

convolves
the binary image `BW`

with this matrix.

The resulting convolution contains integer values in the range
[0,15]. `applylut`

uses the central part of the convolution,
of the same size as `BW`

, and adds 1 to each value
to shift the range to [1,16]. It then constructs `A`

by
replacing the values in the cells of the index matrix with the values
in `lut`

that the indices point to.

### 3-by-3 Neighborhoods

For 3-by-3 neighborhoods, `length(lut)`

is
512. There are nine pixels in each neighborhood, and two possible
states for each pixel, so the total number of permutations is 2^{9} =
512.

To produce the matrix of indices, `applylut`

convolves
the binary image `BW`

with this matrix.

The resulting convolution contains integer values in the range
[0,511]. `applylut`

uses the central part of the
convolution, of the same size as `BW`

, and adds 1
to each value to shift the range to [1,512]. It then constructs `A`

by
replacing the values in the cells of the index matrix with the values
in `lut`

that the indices point to.