machinevisiontoolbox.Image.rank

Image.rank(footprint=None, h=None, rank=-1, border='replicate', bordervalue=0)

Rank filter

Parameters:
  • footprint (ndarray(N,M), optional) – filter footprint or structuring element

  • h (int, optional) – half width of structuring element

  • rank (int, str) – rank of filter

  • border (str, optional) – option for boundary handling, defaults to ‘replicate’

  • bordervalue (scalar, optional) – padding value, defaults to 0

Returns:

rank filtered image

Return type:

Image

Return a rank filtered version of image. Only pixels corresponding to non-zero elements of the structuring element are ranked, and the value that is rank in rank becomes the corresponding output pixel value. The highest rank, the maximum, is rank 0. The rank can also be given as a string: ‘min|imumum’, ‘max|imum’, ‘med|ian’, long or short versions are supported.

The structuring element is given as:

  • footprint a 2D Numpy array containing zero or one values, or

  • h which is the half width \(w=2h+1\) of an array of ones

Example:

>>> from machinevisiontoolbox import Image
>>> import numpy as np
>>> img = Image(np.arange(25).reshape((5,5)))
>>> img.print()
  0  1  2  3  4
  5  6  7  8  9
 10 11 12 13 14
 15 16 17 18 19
 20 21 22 23 24
>>> img.rank(h=1, rank=0).print()  # maximum filter
  6  7  8  9  9
 11 12 13 14 14
 16 17 18 19 19
 21 22 23 24 24
 21 22 23 24 24
>>> img.rank(h=1, rank=8).print()  # minimum filter
  0  0  1  2  3
  0  0  1  2  3
  5  5  6  7  8
 10 10 11 12 13
 15 15 16 17 18
>>> img.rank(h=1, rank=4).print()  # median filter
  1  2  3  4  4
  5  6  7  8  9
 10 11 12 13 14
 15 16 17 18 19
 20 20 21 22 23
>>> img.rank(h=1, rank='median').print()  # median filter
  1  2  3  4  4
  5  6  7  8  9
 10 11 12 13 14
 15 16 17 18 19
 20 20 21 22 23
Note:
  • The footprint should have an odd side length.

  • The input can be logical, uint8, uint16, float or double, the output is always double.

References:
  • Robotics, Vision & Control for Python, Section 11.5.3, P. Corke, Springer 2023.

Seealso:

scipy.ndimage.rank_filter