{ } Raw JSON

bundles / scipy 1.17.1 / scipy / ndimage / _morphology / morphological_gradient

function

scipy.ndimage._morphology:morphological_gradient

source: /scipy/ndimage/_morphology.py :1633

Signature

def   morphological_gradient ( input size = None footprint = None structure = None output = None mode = reflect cval = 0.0 origin = 0 * axes = None )

Summary

Multidimensional morphological gradient.

Extended Summary

The morphological gradient is calculated as the difference between a dilation and an erosion of the input with a given structuring element.

Parameters

input : array_like

Array over which to compute the morphlogical gradient.

size : tuple of ints

Shape of a flat and full structuring element used for the mathematical morphology operations. Optional if footprint or structure is provided. A larger size yields a more blurred gradient.

footprint : array of ints, optional

Positions of non-infinite elements of a flat structuring element used for the morphology operations. Larger footprints give a more blurred morphological gradient.

structure : array of ints, optional

Structuring element used for the morphology operations. structure may be a non-flat structuring element. The structure array applies offsets to the pixels in a neighborhood (the offset is additive during dilation and subtractive during erosion)

output : array, optional

An array used for storing the output of the morphological gradient may be provided.

mode : {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}, optional

The mode parameter determines how the array borders are handled, where cval is the value when mode is equal to 'constant'. Default is 'reflect'

cval : scalar, optional

Value to fill past edges of input if mode is 'constant'. Default is 0.0.

origin : scalar, optional

The origin parameter controls the placement of the filter. Default 0

axes : tuple of int or None

The axes over which to apply the filter. If None, input is filtered along all axes. If an origin tuple is provided, its length must match the number of axes.

Returns

morphological_gradient : ndarray

Morphological gradient of input.

Notes

For a flat structuring element, the morphological gradient computed at a given point corresponds to the maximal difference between elements of the input among the elements covered by the structuring element centered on the point.

Array API Standard Support

morphological_gradient has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.

====================  ====================  ====================
Library               CPU                   GPU
====================  ====================  ====================
NumPy                 ✅                     n/a                 
CuPy                  n/a                   ✅                   
PyTorch               ✅                     ⛔                   
JAX                   ⚠️ no JIT
Dask                  ⚠️ computes graph     n/a                 
====================  ====================  ====================

See dev-arrayapi for more information.

Examples

from scipy import ndimage
import numpy as np
a = np.zeros((7,7), dtype=int)
a[2:5, 2:5] = 1
ndimage.morphological_gradient(a, size=(3,3))
ndimage.grey_dilation(a, size=(3,3)) -\
 ndimage.grey_erosion(a, size=(3,3))
a = np.zeros((7,7), dtype=int)
a[2:5, 2:5] = 1
a[4,4] = 2; a[2,3] = 3
a
ndimage.morphological_gradient(a, size=(3,3))

See also

gaussian_gradient_magnitude
grey_dilation
grey_erosion

Aliases

  • scipy.ndimage.morphological_gradient