gradient
- arrayfire.gradient(array: Array, /) tuple[Array, Array]
Computes the horizontal and vertical gradients of a 2D ArrayFire array or a batch of 2D arrays.
The gradient is a vector that points in the direction of the greatest rate of increase of the function, and its magnitude is the slope of the graph in that direction. For images, this operation can highlight edges and changes in intensity.
Parameters
- arrayArray
The input ArrayFire array, which can be a 2D array representing a single image, or a multi-dimensional array representing a batch of images. For batch processing, the gradient is computed for each image in the batch independently.
Returns
- tuple[Array, Array]
A tuple containing two ArrayFire arrays: - The first array (dx) contains the horizontal gradients of the input array. - The second array (dy) contains the vertical gradients of the input array.
Examples
>>> import arrayfire as af >>> image = af.randu((3, 3)) # Generate a random 3x3 "image" >>> image [3 3 1 1] 0.4105 0.3543 0.3636 0.1583 0.6450 0.4165 0.3712 0.9675 0.5814
>>> dx, dy = af.gradient(image) >>> dx # Display the horizontal gradients [3 3 1 1] -0.2522 0.2907 0.0528 -0.0196 0.3066 0.1089 0.2129 0.3225 0.1650
>>> dy # Display the vertical gradients [3 3 1 1] -0.0562 -0.0234 0.0093 0.4867 0.1291 -0.2286 0.5962 0.1051 -0.3860
Note
The gradient operation is particularly useful in the context of image processing for identifying edges and textural patterns within images.
For higher-dimensional arrays representing batches of images, the gradient operation is applied independently to each image in the batch.