Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# ordfilt2

2-D order-statistic filtering

## Syntax

B = ordfilt2(A, order, domain)
B = ordfilt2(A, order, domain, S)

## Description

B = ordfilt2(A, order, domain) replaces each element in A by the orderth element in the sorted set of neighbors specified by the nonzero elements in domain.

B = ordfilt2(A, order, domain, S) where S is the same size as domain, uses the values of S corresponding to the nonzero values of domain as additive offsets.

B = ordfilt2(..., padopt) controls how the matrix boundaries are padded. Set padopt to 'zeros' (the default) or 'symmetric'. If padopt is 'zeros', A is padded with 0's at the boundaries. If padopt is 'symmetric', A is symmetrically extended at the boundaries.

## Class Support

The class of A can be logical, uint8, uint16, or double. The class of B is the same as the class of A, unless the additive offset form of ordfilt2 is used, in which case the class of B is double.

## Examples

This examples uses a maximum filter with a [5 5] neighborhood. This is equivalent to imdilate(image,strel('square',5)).

```A = imread('snowflakes.png');
B = ordfilt2(A,25,true(5));
figure, imshow(A), figure, imshow(B)
```

expand all

### Tips

domain is equivalent to the structuring element used for binary image operations. It is a matrix containing only 1's and 0's; the 1's define the neighborhood for the filtering operation.

For example, B = ordfilt2(A,5,ones(3,3)) implements a 3-by-3 median filter; B = ordfilt2(A,1,ones(3,3)) implements a 3-by-3 minimum filter; and B = ordfilt2(A,9,ones(3,3)) implements a 3-by-3 maximum filter. B = ordfilt2(A,1,[0 1 0; 1 0 1; 0 1 0]) replaces each element in A by the minimum of its north, east, south, and west neighbors.

The syntax that includes S (the matrix of additive offsets) can be used to implement grayscale morphological operations, including grayscale dilation and erosion.

### Performance Considerations

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