Certain binary image operations can be implemented most easily
through lookup tables. A lookup table is a column vector in which
each element represents the value to return for one possible combination
of pixels in a neighborhood. To create lookup tables for various operations,
use the makelut
function. makelut
creates
lookup tables for 2by2 and 3by3 neighborhoods. The following figure
illustrates these types of neighborhoods. Each neighborhood pixel
is indicated by an x, and the center pixel is the one with a circle.
For a 2by2 neighborhood, there are 16 possible permutations of the pixels in the neighborhood. Therefore, the lookup table for this operation is a 16element vector. For a 3by3 neighborhood, there are 512 permutations, so the lookup table is a 512element vector.
Note

Once you create a lookup table, you can use it to perform the
desired operation by using the applylut
function.
The example below illustrates using lookup table operations
to modify an image containing text. The example creates an anonymous
function that returns 1 if three or more pixels in the 3by3 neighborhood
are 1; otherwise, it returns 0
. The example then
calls makelut
, passing in this function as the
first argument, and using the second argument to specify a 3by3
lookup table.
f = @(x) sum(x(:)) >= 3; lut = makelut(f,3);
lut
is returned as a 512element vector of
1's and 0's. Each value is the output from the function for one of
the 512 possible permutations.
You then perform the operation using applylut
.
BW1 = imread('text.png'); BW2 = applylut(BW1,lut); imshow(BW1) figure, imshow(BW2)
Image Before and After Applying Lookup Table Operation
For information about how applylut
maps pixel
combinations in the image to entries in the lookup table, see the
reference page for applylut
.