{ } Raw JSON

bundles / scipy latest / scipy / ndimage / _morphology / generate_binary_structure

function

scipy.ndimage._morphology:generate_binary_structure

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

Signature

def   generate_binary_structure ( rank connectivity )

Summary

Generate a binary structure for binary morphological operations.

Parameters

rank : int

Number of dimensions of the array to which the structuring element will be applied, as returned by np.ndim.

connectivity : int

connectivity determines which elements of the output array belong to the structure, i.e., are considered as neighbors of the central element. Elements up to a squared distance of connectivity from the center are considered neighbors. connectivity may range from 1 (no diagonal elements are neighbors) to rank (all elements are neighbors).

Returns

output : ndarray of bools

Structuring element which may be used for binary morphological operations, with rank dimensions and all dimensions equal to 3.

Notes

generate_binary_structure can only create structuring elements with dimensions equal to 3, i.e., minimal dimensions. For larger structuring elements, that are useful e.g., for eroding large objects, one may either use iterate_structure, or create directly custom arrays with numpy functions such as numpy.ones.

Array API Standard Support

generate_binary_structure is not in-scope for support of Python Array API Standard compatible backends other than NumPy.

See dev-arrayapi for more information.

Examples

from scipy import ndimage
import numpy as np
struct = ndimage.generate_binary_structure(2, 1)
struct
a = np.zeros((5,5))
a[2, 2] = 1
a
b = ndimage.binary_dilation(a, structure=struct).astype(a.dtype)
b
ndimage.binary_dilation(b, structure=struct).astype(a.dtype)
struct = ndimage.generate_binary_structure(2, 2)
struct
struct = ndimage.generate_binary_structure(3, 1)
struct # no diagonal elements

See also

binary_dilation
binary_erosion
iterate_structure

Aliases

  • scipy.ndimage.generate_binary_structure

Referenced by