Main Content

ordfilt2

2-D order-statistic filtering

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.

example

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

Read image into workspace and display it.

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

Figure contains an axes object. The hidden axes object contains an object of type image.

Filter the image and display the result.

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

Figure contains an axes object. The hidden axes object contains an object of type image.

Input Arguments

collapse all

Data to filter, specified as a 2-D numeric matrix or 2-D logical matrix.

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

Element to replace the target pixel, specified as a real scalar integer.

Data Types: double

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

Type of Filtering OperationMATLAB codeNeighborhoodSample Image Data, Indicating Selected Element
Median filterB = ordfilt2(A,5,ones(3,3))3-by-3 matrix of ones3-by-3 matrix of numbers. The element with the fifth highest value in the neighborhood is circled.
Minimum filterB = ordfilt2(A,1,ones(3,3))3-by-3 matrix of ones3-by-3 matrix of numbers. The element with the lowest value in the neighborhood is circled.
Maximum filterB = ordfilt2(A,9,ones(3,3))3-by-3 matrix of ones3-by-3 matrix of numbers. The element with the highest value in the neighborhood is circled.
Minimum of north, east, south, and west neighborsB = ordfilt2(A,1,[0 1 0; 1 0 1; 0 1 0])3-by-3 neightborhood in which the north, south, east, and west pixels are true (1) and the center and corner pixels are false (0)3-by-3 matrix of numbers. The element with the lowest value in the specified neighborhood is circled. Elements excluded from the neighborhood are grayed out.

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

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

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

Padding option, specified as one of the following values.

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

Pad array with mirror reflections of itself.

Data Types: char | string

Output Arguments

collapse all

Filtered data, returned as a 2-D numeric matrix or 2-D logical matrix of the same class as the input data A.

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

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also