# Documentation

### This is machine translation

Translated by
Mouse over text to see original. Click the button below to return to the English verison of the page.

# ordfilt2

2-D order-statistic filtering

## Syntax

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

## Description

example

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

Code Generation support: Yes.

MATLAB Function Block support: Yes.

## Examples

collapse all

Read image into workspace and display it.

figure
imshow(A)

Filter the image and display the result.

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

## Input Arguments

collapse all

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

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

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

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 filterB = ordfilt2(A,5,ones(3,3))
Minimum filterB = ordfilt2(A,1,ones(3,3))
Maximum filterB = ordfilt2(A,9,ones(3,3))
Minimum of north, east, south, and west neighborsB = 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

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

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

Padding option, specified as either of the following values:

OptionDescription
'symmetric'

Pad array with mirror reflections of itself.

Data Types: char

## Output Arguments

collapse all

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

collapse all

### Code Generation

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.

### MATLAB Function Block

You can use this function in the MATLAB Function Block in Simulink.

### Tips

• 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