machinevisiontoolbox.Image.apply2

Image.apply2(other, func, vectorize=False)

Apply a function to two images

Parameters:

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

Raises:

ValueError – images must have same size

Returns:

transformed image

Return type:

Image

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

If vectorize is True the function is called for every pixel in both images with two arguments which are the corresponding pixel values as a scalar or 1d-array of length equal to the number of color planes. The function returns a scalar or a 1d-array. The return array will have the same dimensions to 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).image
array([[5.098, 6.324],
       [7.617, 8.945]], dtype=float16)
>>> img1.apply2(img2, lambda x, y: math.hypot(x,y), vectorize=True).image
array([[5.099 , 6.3246],
       [7.6158, 8.9443]])
Note:

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

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

Seealso:

apply