Image.apply2#

Image.apply2(other: Image, func: Callable[..., Any], vectorize: bool = False, **kwargs) Image[source]#

Apply a function to two images

Parameters:
  • func (callable) – function to apply to image or pixel

  • vectorize (bool, optional) – if True apply function to each pixel, defaults to False

  • kwargs – additional keyword arguments passed to the function

Raises:

ValueError – images must have same size

Returns:

transformed image

Return type:

Image

If vectorize is False:

  • the function func is called with two arguments which are the underlying NumPy array of self and other.

  • the function must return a NumPy array, which can have different dimensions to its arguments. This allows for a large number of NumPy or OpenCV functions to be applied to an image.

  • For a multiplane image the function is called with a 3D array, and can return an array with the same or a different number of channels.

  • the returned NumPy array is encapsulated in a new Image.

If vectorize is True:

  • the function func is called for every pixel with two arguments which are the corresponding scalar pixel values from self and other.

  • the images must have the same width, height, and number of channels

  • for a color image the function is called for every pixel on every plane.

  • the return array will have the same dimensions (width, height, planes) as its argument.

Example:

>>> from machinevisiontoolbox import Image
>>> import numpy as np
>>> import math
>>> img1 = Image([[1, 2], [3, 4]])
>>> img2 = Image([[5, 6], [7, 8]])
>>> img1.apply2(img2, np.hypot).print()
   5.10 6.32
   7.62 8.95
>>> img1.apply2(img2, lambda x, y: math.hypot(x,y), vectorize=True).print()
   5.10 6.32
   7.62 8.94

Note

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

References:
Seealso:

apply numpy.vectorize