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:
If
vectorizeis False:the function
funcis called with two arguments which are the underlying NumPy array ofselfandother.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
vectorizeis True:the function
funcis called for every pixel with two arguments which are the corresponding scalar pixel values fromselfandother.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
vectorizeisTruewhich involves a large number of calls tofunc.
- References:
- Seealso:
applynumpy.vectorize