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.