Main Content

ordfilt2

R2026b

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 the neighborhood defined by 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 with a 5-by-5 maximum filter 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.

Read an image into workspace and display it.

A = imread("snowflakes.png");
A = im2double(A);
figure
imshow(A,[])

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

Define a skewed 5-by-5 structuring element that has higher intensities in the right columns than the left columns.

SE = repmat([1 1 1 5 5],5,1)
SE = 5×5

     1     1     1     5     5
     1     1     1     5     5
     1     1     1     5     5
     1     1     1     5     5
     1     1     1     5     5

The domain of the ordfilt2 function requires only the positions of the nonzero elements in the structuring element, and not the values. Define the domain as the logical values of the structuring element.

domain = SE > 0
domain = 5×5 logical array

   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1
   1   1   1   1   1

To perform morphological dilation, the order of the ordfilt2 function must be specified as maximum. Specify the order has the number of nonzero elements in the domain.

order = nnz(domain)
order = 
25

Filter the image with the maximum filter with the structuring element as the additive offset. The additive offset controls the directions in which the morphological effect occurs.

B = ordfilt2(A,order,domain,SE);

Display the filtered image. Observe that the snowflakes dilate in different proportions on the left and right because of the intensities of the structuring element.

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. The function replaces each element in A by the orderth element in the sorted set of neighbors specified by the nonzero elements in the neighborhood defined by domain.

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 OperationSample codeNeighborhoodSample Image Data, Indicating Selected Element
Median filterB = ordfilt2(A,5,ones(3,3))

3-by-3 matrix of ones

3-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 ones

3-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 ones

3-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 neighborhood 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. The function adds the values of S corresponding to the nonzero values of domain before sorting. You can use additive offsets to implement grayscale morphological operations, including grayscale dilation and erosion. The additive offset controls the directions in which the morphological effect occurs.

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

Padding option, specified as one of the following values. The nature of padding controls how the function computes the values of the output pixels at the edges of the image.

OptionDescriptionExample
"zeros"Pad array boundaries with the value 0.

[314159265][0000000000000000314000015900002650000000000000000]

"symmetric"

Pad array with mirror reflections of itself.

[314159265][5115995133144113314415115995622655662265565115995]

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 data type 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

expand all

Version History

Introduced before R2006a

expand all