Image Gradients

Functional Interface

torchmetrics.functional.image.image_gradients(img)[source]

Compute Gradient Computation of Image of a given image using finite difference.

Parameters:

img (Tensor) – An (N, C, H, W) input tensor where C is the number of image channels

Return type:

tuple[Tensor, Tensor]

Returns:

Tuple of (dy, dx) with each gradient of shape [N, C, H, W]

Raises:

Example

>>> from torchmetrics.functional.image import image_gradients
>>> image = torch.arange(0, 1*1*5*5, dtype=torch.float32)
>>> image = torch.reshape(image, (1, 1, 5, 5))
>>> dy, dx = image_gradients(image)
>>> dy[0, 0, :, :]
tensor([[5., 5., 5., 5., 5.],
        [5., 5., 5., 5., 5.],
        [5., 5., 5., 5., 5.],
        [5., 5., 5., 5., 5.],
        [0., 0., 0., 0., 0.]])

Note

The implementation follows the 1-step finite difference method as followed by the TF implementation. The values are organized such that the gradient of [I(x+1, y)-[I(x, y)]] are at the (x, y) location