machinevisiontoolbox.Image.window

Image.window(func, h=None, se=None, border='reflect', bordervalue=0, **kwargs)

Generalized spatial operator

Parameters:
  • func (callable) – function applied to window

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

  • se (ndarray(N,M), optional) – structuring element

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

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

Raises:
  • ValueErrorborder is not a valid option

  • TypeErrorfunc not callable

  • ValueError – single channel images only

Returns:

transformed image

Return type:

Image

Returns an image where each pixel is the result of applying the function func to a neighbourhood centred on the corresponding pixel in image. The return value of func becomes the corresponding pixel value.

The neighbourhood is defined in two ways:

  • If se is given then it is the the size of the structuring element se which should have odd side lengths. The elements in the neighbourhood corresponding to non-zero elements in se are packed into a vector (in column order from top left) and passed to the specified callable function func.

  • If se is None then h is the half width of a \(w \times w\) square structuring element of ones, where \(w =2h+1\).

Example:

>>> from machinevisiontoolbox import Image
>>> import numpy as np
>>> img = Image.Read('monalisa.png', grey=True)
>>> out = img.window(np.median, h=3)
Note:
  • The structuring element should have an odd side length.

  • Is slow since the function func must be invoked once for every output pixel.

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

Seealso:

scipy.ndimage.generic_filter