machinevisiontoolbox.Image.apply

Image.apply(func, vectorize=False)

Apply a function to an image

Parameters:

func (callable) – function to apply to image or pixel

Returns:

transformed image

Return type:

Image

If vectorize is False the function is called with a single argument which is the underlying NumPy array, and it must return a NumPy array. The return array can have different dimensions to its argument.

If vectorize is True the function is called for every pixel with a single argument which is a scalar or a 1d-array of length equal to the number of color planes. The return array will have the same dimensions to its argument.

Example:

>>> from machinevisiontoolbox import Image
>>> import numpy as np
>>> import math
>>> img = Image([[1, 2], [3, 4]])
>>> img.apply(np.sqrt).image
array([[1.   , 1.414],
       [1.732, 2.   ]], dtype=float16)
>>> img.apply(lambda x: math.sqrt(x), vectorize=True).image
array([[1.    , 1.4142],
       [1.7321, 2.    ]])
Note:

Slow when vectorize=True which involves a large number of calls to func.

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

Seealso:

apply2