# applylut

Neighborhood operations on binary images using lookup tables

`applylut`

is not recommended. Use `bwlookup`

instead.

## Syntax

## Description

## Examples

### Perform Erosion Using a 2-by-2 Neighborhood

Create the LUT.

lutfun = @(x)(sum(x(:))==4); lut = makelut(lutfun,2);

Read image into the workspace and then apply the LUT to the image. An output pixel is `on`

only if all four of the input pixel's neighborhood pixels are `on`

.

```
BW1 = imread('text.png');
BW2 = applylut(BW1,lut);
```

Show the original image and the eroded image.

figure, imshow(BW1);

figure, imshow(BW2);

## Input Arguments

`BW`

— Input image

2-D binary image

Input image, specified as a 2-D binary image. For numeric input,
any nonzero pixels are considered to be `1`

(`true`

).

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `logical`

`lut`

— Lookup table of output pixel values

16-element numeric vector | 512-element numeric vector

Lookup table of output pixel values, specified as a 16- or 512-element vector as returned
by `makelut`

.

**Data Types: **`single`

| `double`

| `int8`

| `int16`

| `int32`

| `int64`

| `uint8`

| `uint16`

| `uint32`

| `uint64`

| `logical`

## Output Arguments

`A`

— Output image

binary image | grayscale image

Output image, returned as a grayscale or binary image whose distribution of pixel values
are determined by the content of the lookup table, `lut`

. The output image
`J`

is the same size as the input image `I`

.

If all elements of

`lut`

are`0`

or`1`

, then`A`

has data type`logical`

.If all elements of

`lut`

are integers between 0 and 255, then`A`

has data type`uint8`

.For all other cases,

`A`

has data type`double`

.

**Data Types: **`double`

| `uint8`

| `logical`

## Algorithms

`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.

8 2 4 1

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]. The function
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.

256 32 4 128 16 2 64 8 1

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.

## Version History

**Introduced before R2006a**

### R2012b: `applylut`

is not recommended

Starting in R2012b, use `bwlookup`

to perform neighborhood operations on
binary images using lookup tables. For `bwlookup`

, the data type of the
returned image is the same as the data type of the lookup table. `bwlookup`

supports code generation. There are no plans to remove `applylut`

at this
time.

To update your code, replace instances of `applylut`

with
`bwlookup`

. You do not need to change the input arguments.

## See Also

## Open Example

You have a modified version of this example. Do you want to open this example with your edits?

## MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)