# ordfilt2

2-D order-statistic filtering

## Syntax

• `B = ordfilt2(A,order,domain)` example
• `B = ordfilt2(A,order,domain,S)`
• `B = ordfilt2(___,padopt)`

## Description

example

````B = ordfilt2(A,order,domain)` replaces each element in `A` by the `order`th element in the sorted set of neighbors specified by the nonzero elements in `domain`. This function supports code generation (see Tips).```
````B = ordfilt2(A,order,domain,S)` filters `A`, where `ordfilt2` uses the values of `S` corresponding to the nonzero values of `domain` as additive offsets. You can use this syntax to implement grayscale morphological operations, including grayscale dilation and erosion.```
````B = ordfilt2(___,padopt)` filters `A`, where `padopt` specifies how `ordfilt2` pads the matrix boundaries.```

## Examples

collapse all

### Filter Image with Maximum Filter

Read image into workspace and display it.

```A = imread('snowflakes.png'); figure imshow(A) ```

Filter the image and display the result.

```B = ordfilt2(A,25,true(5)); figure imshow(B) ```

## Input Arguments

collapse all

### `A` — Input matrix2-D, real, nonsparse, numeric or logical matrix

Input matrix, specified as a 2-D, real, nonsparse, numeric or logical array.

Example: `A = imread('snowflakes.png');`

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `uint8` | `uint16` | `uint32` | `logical`

### `order` — Element to replace the target pixelreal scalar integer

Element to replace the target pixel, specified as a real scalar integer of class `double`.

Example: `B = ordfilt2(A,25,true(5));`

Data Types: `double`

### `domain` — Neighborhoodnumeric or logical matrix

Neighborhood, specified as a numeric or logical matrix, containing `1`'s and `0`'s. `domain` is equivalent to the structuring element used for binary image operations. The 1-valued elements define the neighborhood for the filtering operation. The following table gives examples of some common filters.

Type of Filtering OperationMATLAB codeNeighborhood
Median filter`B = ordfilt2(A,5,ones(3,3))`
Minimum filter`B = ordfilt2(A,1,ones(3,3))`
Maximum filter`B = ordfilt2(A,9,ones(3,3))`
Minimum of north, east, south, and west neighbors`B = ordfilt2(A,1,[0 1 0; 1 0 1; 0 1 0])`

Example: `B = ordfilt2(A,25,true(5));`

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical`

### `S` — Additive offsetsmatrix

Additive offsets, specified as a matrix the same size as `domain`.

Data Types: `single` | `double` | `int8` | `int16` | `int32` | `int64` | `uint8` | `uint16` | `uint32` | `uint64` | `logical`

### `padopt` — Padding option`'zeros'` (default) | `'symmetric'`

Padding option, specified as either of the strings in this table. `'zeros'` or `'symmetric'`.

OptionDescription
`'zeros'`Pad array boundaries with `0`'s.
`'symmetric'`

Pad array with mirror reflections of itself.

Data Types: `char`

## Output Arguments

collapse all

### `B` — Output image2-D array

Output image, returned as a 2-D array of the same class as the input image `A`.

## More About

collapse all

### Tips

• This function supports the generation of C code using MATLAB® Coder™. Note that if you choose the generic `MATLAB Host Computer` target platform, the function generates code that uses a precompiled, platform-specific shared library. Use of a shared library preserves performance optimizations but limits the target platforms for which code can be generated. For more information, see Understanding Code Generation with Image Processing Toolbox.

When generating code, the `padopt` argument must be a compile-time constant.

• When working with large domain matrices that do not contain any zero-valued elements, `ordfilt2` can achieve higher performance if `A` is in an integer data format (`uint8`, `int8`, `uint16`, `int16`). The gain in speed is larger for `uint8` and `int8` than for the 16-bit data types. For 8-bit data formats, the domain matrix must contain seven or more rows. For 16-bit data formats, the domain matrix must contain three or more rows and 520 or more elements.

## References

[1] Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume I, Addison-Wesley, 1992.

[2] Huang, T.S., G.J.Yang, and G.Y.Tang. "A fast two-dimensional median filtering algorithm.", IEEE transactions on Acoustics, Speech and Signal Processing, Vol ASSP 27, No. 1, February 1979

## See Also

Was this topic helpful?

Get trial now