# extractLBPFeatures

Extract local binary pattern (LBP) features

## Description

## Examples

### Using LBP Features to Differentiate Images by Texture

Read images that contain different textures.

brickWall = imread('bricks.jpg'); rotatedBrickWall = imread('bricksRotated.jpg'); carpet = imread('carpet.jpg');

Display the images.

```
figure
imshow(brickWall)
title('Bricks')
```

```
figure
imshow(rotatedBrickWall)
title('Rotated Bricks')
```

```
figure
imshow(carpet)
title('Carpet')
```

Extract LBP features from the images to encode their texture information.

lbpBricks1 = extractLBPFeatures(brickWall,'Upright',false); lbpBricks2 = extractLBPFeatures(rotatedBrickWall,'Upright',false); lbpCarpet = extractLBPFeatures(carpet,'Upright',false);

Gauge the similarity between the LBP features by computing the squared error between them.

brickVsBrick = (lbpBricks1 - lbpBricks2).^2; brickVsCarpet = (lbpBricks1 - lbpCarpet).^2;

Visualize the squared error to compare bricks versus bricks and bricks versus carpet. The squared error is smaller when images have similar texture.

figure bar([brickVsBrick; brickVsCarpet]','grouped') title('Squared Error of LBP Histograms') xlabel('LBP Histogram Bins') legend('Bricks vs Rotated Bricks','Bricks vs Carpet')

### Apply L1 Normalization to LBP Features

Read in a sample image and convert it to grayscale.

```
I = imread('gantrycrane.png');
I = im2gray(I);
```

Extract unnormalized LBP features so that you can apply a custom normalization.

lbpFeatures = extractLBPFeatures(I,'CellSize',[32 32],'Normalization','None');

Reshape the LBP features into a *number of neighbors* -by- *number of cells* array to access histograms for each individual cell.

numNeighbors = 8; numBins = numNeighbors*(numNeighbors-1)+3; lbpCellHists = reshape(lbpFeatures,numBins,[]);

Normalize each LBP cell histogram using L1 norm.

lbpCellHists = bsxfun(@rdivide,lbpCellHists,sum(lbpCellHists));

Reshape the LBP features vector back to 1-by- *N* feature vector.

lbpFeatures = reshape(lbpCellHists,1,[]);

## Input Arguments

`I`

— Input image

*M*-by-*N* 2-D grayscale
image

Input image, specified as an *M*-by-*N* 2-D
grayscale image that is real, and non-sparse.

**Data Types: **`logical`

| `single`

| `double`

| `int16`

| `uint8`

| `uint16`

### Name-Value Arguments

Specify optional pairs of arguments as
`Name1=Value1,...,NameN=ValueN`

, where `Name`

is
the argument name and `Value`

is the corresponding value.
Name-value arguments must appear after other arguments, but the order of the
pairs does not matter.

*
Before R2021a, use commas to separate each name and value, and enclose*
`Name`

*in quotes.*

**Example: **`'NumNeighbors'`

,`8`

**Algorithm Parameters**

The
LBP algorithm parameters control how local binary patterns are computed
for each pixel in the input image.

`NumNeighbors`

— Number of neighbors

`8`

(default) | positive integer

Number of neighbors used to compute the LBP for each pixel in
the input image, specified as the comma-separated pair consisting
of '`NumNeighbors`

' and a positive integer. The
set of neighbors is selected from a circularly symmetric pattern around
each pixel. Increase the number of neighbors to encode greater detail
around each pixel. Typical values range from `4`

to `24`

.

`Radius`

— Radius of circular pattern to select neighbors

`1`

(default) | positive integer

Radius of circular pattern used to select neighbors for each
pixel in the input image, specified as the comma-separated pair consisting
of '`Radius`

' and a positive integer. To capture
detail over a larger spatial scale, increase the radius. Typical values
range from `1`

to `5`

.

`Upright`

— Rotation invariance flag

`true`

| logical scalar

Rotation invariance flag, specified as the comma-separated pair
consisting of '`Upright`

' and a logical scalar.
When you set this property to `true`

, the LBP features
do not encode rotation information. Set '`Upright`

'
to `false`

when rotationally invariant features are
required.

`Interpolation`

— Interpolation method

`'Linear'`

(default) | `'Nearest'`

Interpolation method used to compute pixel neighbors, specified as the comma-separated pair
consisting of '`Interpolation`

' and either
`'Linear'`

or `'Nearest'`

. Use
`'Nearest'`

for faster computation, but with less
accuracy.

**Histogram Parameters**

The
histogram parameters determine how the distribution of binary patterns
is aggregated over the image to produce the output features.

`Normalization`

— Type of normalization

`'L2'`

(default) | `'None'`

Type of normalization applied to each LBP cell histogram, specified as the comma-separated
pair consisting of '`Normalization`

' and either
`'L2'`

or `'None'`

. To apply a
custom normalization method as a post-processing step, set this value to
`'None'`

.

## Output Arguments

`features`

— LBP feature vector

*1*-by-*N* vector

LBP feature vector, returned as a 1-by-*N* vector
of length *N* representing the number of features.
LBP features encode local texture information, which you can use for
tasks such as classification, detection, and recognition. The function
partitions the input image into non-overlapping cells. To collect
information over larger regions, select larger cell sizes . However,
when you increase the cell size, you lose local detail. *N*,
depends on the number of cells in the image, *numCells*,
the number of neighbors, *P*, and the `Upright`

parameter.

The number of cells is calculated as:

numCells = prod(`floor` (`size` (`I` )/`CellSize` )) |

The figure shows an image with nine cell histograms. Each histogram describes an LBP feature.

The size of the histogram
in each cell is [1,*B*], where *B* is
the number of bins in the histogram. The number of bins depends on
the `Upright`

property and the number of neighbors, *P*.

Upright | Number of Bins |
---|---|

`true` | (P x P–1) + 3) |

`false` | (P + 2) |

The overall LBP feature length, *N*,
depends on the number of cells and the number of bins, *B*:

N = numCells x B |

## References

[1] Ojala, T., M. Pietikainen, and T. Maenpaa. “Multiresolution
Gray Scale and Rotation Invariant Texture Classification With Local
Binary Patterns.” *IEEE Transactions on Pattern Analysis
and Machine Intelligence*. Vol. 24, Issue 7, July 2002,
pp. 971-987.

## Extended Capabilities

### C/C++ Code Generation

Generate C and C++ code using MATLAB® Coder™.

Usage notes and limitations:

Does not generate a platform-dependent library.

## Version History

**Introduced in R2015b**

## 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)