# scipy 1.18.1 (BSD-3-Clause), harvested by tools/harvest_corpus.py
=== input

    Distance transform function by a brute force algorithm.

    This function calculates the distance transform of the `input`, by
    replacing each foreground (non-zero) element, with its
    shortest distance to the background (any zero-valued element).

    In addition to the distance transform, the feature transform can
    be calculated. In this case the index of the closest background
    element to each foreground element is returned in a separate array.

    Parameters
    ----------
    input : array_like
        Input
    metric : {'euclidean', 'taxicab', 'chessboard'}, optional
        'cityblock' and 'manhattan' are also valid, and map to 'taxicab'.
        The default is 'euclidean'.
    sampling : float, or sequence of float, optional
        This parameter is only used when `metric` is 'euclidean'.
        Spacing of elements along each dimension. If a sequence, must be of
        length equal to the input rank; if a single number, this is used for
        all axes. If not specified, a grid spacing of unity is implied.
    return_distances : bool, optional
        Whether to calculate the distance transform.
        Default is True.
    return_indices : bool, optional
        Whether to calculate the feature transform.
        Default is False.
    distances : ndarray, optional
        An output array to store the calculated distance transform, instead of
        returning it.
        `return_distances` must be True.
        It must be the same shape as `input`, and of type float64 if `metric`
        is 'euclidean', uint32 otherwise.
    indices : int32 ndarray, optional
        An output array to store the calculated feature transform, instead of
        returning it.
        `return_indices` must be True.
        Its shape must be ``(input.ndim,) + input.shape``.

    Returns
    -------
    distances : ndarray, optional
        The calculated distance transform. Returned only when
        `return_distances` is True and `distances` is not supplied.
        It will have the same shape as the input array.
    indices : int32 ndarray, optional
        The calculated feature transform. It has an input-shaped array for each
        dimension of the input. See distance_transform_edt documentation for an
        example.
        Returned only when `return_indices` is True and `indices` is not
        supplied.

    See Also
    --------
    distance_transform_cdt : Faster distance transform for taxicab and
                             chessboard metrics
    distance_transform_edt : Faster distance transform for euclidean metric

    Notes
    -----
    This function employs a slow brute force algorithm. See also the
    function `distance_transform_cdt` for more efficient taxicab [1]_ and
    chessboard algorithms [2]_.

    References
    ----------
    .. [1] Taxicab distance. Wikipedia, 2023.
           https://en.wikipedia.org/wiki/Taxicab_geometry
    .. [2] Chessboard distance. Wikipedia, 2023.
           https://en.wikipedia.org/wiki/Chebyshev_distance

    Examples
    --------
    Import the necessary modules.

    >>> import numpy as np
    >>> from scipy.ndimage import distance_transform_bf
    >>> import matplotlib.pyplot as plt
    >>> from mpl_toolkits.axes_grid1 import ImageGrid

    First, we create a toy binary image.

    >>> def add_circle(center_x, center_y, radius, image, fillvalue=1):
    ...     # fill circular area with 1
    ...     xx, yy = np.mgrid[:image.shape[0], :image.shape[1]]
    ...     circle = (xx - center_x) ** 2 + (yy - center_y) ** 2
    ...     circle_shape = np.sqrt(circle) < radius
    ...     image[circle_shape] = fillvalue
    ...     return image
    >>> image = np.zeros((100, 100), dtype=np.uint8)
    >>> image[35:65, 20:80] = 1
    >>> image = add_circle(28, 65, 10, image)
    >>> image = add_circle(37, 30, 10, image)
    >>> image = add_circle(70, 45, 20, image)
    >>> image = add_circle(45, 80, 10, image)

    Next, we set up the figure.

    >>> fig = plt.figure(figsize=(8, 8))  # set up the figure structure
    >>> grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=(0.4, 0.3),
    ...                  label_mode="1", share_all=True,
    ...                  cbar_location="right", cbar_mode="each",
    ...                  cbar_size="7%", cbar_pad="2%")
    >>> for ax in grid:
    ...     ax.axis('off')  # remove axes from images

    The top left image is the original binary image.

    >>> binary_image = grid[0].imshow(image, cmap='gray')
    >>> cbar_binary_image = grid.cbar_axes[0].colorbar(binary_image)
    >>> cbar_binary_image.set_ticks([0, 1])
    >>> grid[0].set_title("Binary image: foreground in white")

    The distance transform calculates the distance between foreground pixels
    and the image background according to a distance metric. Available metrics
    in `distance_transform_bf` are: ``euclidean`` (default), ``taxicab``
    and ``chessboard``. The top right image contains the distance transform
    based on the ``euclidean`` metric.

    >>> distance_transform_euclidean = distance_transform_bf(image)
    >>> euclidean_transform = grid[1].imshow(distance_transform_euclidean,
    ...                                      cmap='gray')
    >>> cbar_euclidean = grid.cbar_axes[1].colorbar(euclidean_transform)
    >>> colorbar_ticks = [0, 10, 20]
    >>> cbar_euclidean.set_ticks(colorbar_ticks)
    >>> grid[1].set_title("Euclidean distance")

    The lower left image contains the distance transform using the ``taxicab``
    metric.

    >>> distance_transform_taxicab = distance_transform_bf(image,
    ...                                                    metric='taxicab')
    >>> taxicab_transformation = grid[2].imshow(distance_transform_taxicab,
    ...                                         cmap='gray')
    >>> cbar_taxicab = grid.cbar_axes[2].colorbar(taxicab_transformation)
    >>> cbar_taxicab.set_ticks(colorbar_ticks)
    >>> grid[2].set_title("Taxicab distance")

    Finally, the lower right image contains the distance transform using the
    ``chessboard`` metric.

    >>> distance_transform_cb = distance_transform_bf(image,
    ...                                               metric='chessboard')
    >>> chessboard_transformation = grid[3].imshow(distance_transform_cb,
    ...                                            cmap='gray')
    >>> cbar_taxicab = grid.cbar_axes[3].colorbar(chessboard_transformation)
    >>> cbar_taxicab.set_ticks(colorbar_ticks)
    >>> grid[3].set_title("Chessboard distance")
    >>> plt.show()

    
=== parse numpydoc
short_description: 'Distance transform function by a brute force algorithm.'
long_description: |
  This function calculates the distance transform of the `input`, by
  replacing each foreground (non-zero) element, with its
  shortest distance to the background (any zero-valued element).
  
  In addition to the distance transform, the feature transform can
  be calculated. In this case the index of the closest background
  element to each foreground element is returned in a separate array.
blank_after_short_description: True
blank_after_long_description: True
style: numpydoc
- DocstringParam
  description: 'Input'
  kind: param
  key: 'param'
  arg_name: 'input'
  type_name: 'array_like'
  is_optional: False
- DocstringParam
  description: |
    'cityblock' and 'manhattan' are also valid, and map to 'taxicab'.
    The default is 'euclidean'.
  kind: param
  key: 'param'
  arg_name: 'metric'
  type_name: "{'euclidean', 'taxicab', 'chessboard'}"
  is_optional: True
  default: "'euclidean'"
  raw_type_name: "{'euclidean', 'taxicab', 'chessboard'}, optional"
- DocstringParam
  description: |
    This parameter is only used when `metric` is 'euclidean'.
    Spacing of elements along each dimension. If a sequence, must be of
    length equal to the input rank; if a single number, this is used for
    all axes. If not specified, a grid spacing of unity is implied.
  kind: param
  key: 'param'
  arg_name: 'sampling'
  type_name: 'float, or sequence of float'
  is_optional: True
  raw_type_name: 'float, or sequence of float, optional'
- DocstringParam
  description: |
    Whether to calculate the distance transform.
    Default is True.
  kind: param
  key: 'param'
  arg_name: 'return_distances'
  type_name: 'bool'
  is_optional: True
  default: 'True'
  raw_type_name: 'bool, optional'
- DocstringParam
  description: |
    Whether to calculate the feature transform.
    Default is False.
  kind: param
  key: 'param'
  arg_name: 'return_indices'
  type_name: 'bool'
  is_optional: True
  default: 'False'
  raw_type_name: 'bool, optional'
- DocstringParam
  description: |
    An output array to store the calculated distance transform, instead of
    returning it.
    `return_distances` must be True.
    It must be the same shape as `input`, and of type float64 if `metric`
    is 'euclidean', uint32 otherwise.
  kind: param
  key: 'param'
  arg_name: 'distances'
  type_name: 'ndarray'
  is_optional: True
  raw_type_name: 'ndarray, optional'
- DocstringParam
  description: |
    An output array to store the calculated feature transform, instead of
    returning it.
    `return_indices` must be True.
    Its shape must be ``(input.ndim,) + input.shape``.
  kind: param
  key: 'param'
  arg_name: 'indices'
  type_name: 'int32 ndarray'
  is_optional: True
  raw_type_name: 'int32 ndarray, optional'
- DocstringReturns
  description: |
    The calculated distance transform. Returned only when
    `return_distances` is True and `distances` is not supplied.
    It will have the same shape as the input array.
  kind: returns
  key: 'returns'
  type_name: 'ndarray, optional'
  is_generator: False
  return_name: 'distances'
- DocstringReturns
  description: |
    The calculated feature transform. It has an input-shaped array for each
    dimension of the input. See distance_transform_edt documentation for an
    example.
    Returned only when `return_indices` is True and `indices` is not
    supplied.
  kind: returns
  key: 'returns'
  type_name: 'int32 ndarray, optional'
  is_generator: False
  return_name: 'indices'
- DocstringMeta
  description: |
    distance_transform_cdt : Faster distance transform for taxicab and
                             chessboard metrics
    distance_transform_edt : Faster distance transform for euclidean metric
  kind: text
  key: 'see_also'
  title: 'See Also'
- DocstringMeta
  description: |
    This function employs a slow brute force algorithm. See also the
    function `distance_transform_cdt` for more efficient taxicab [1]_ and
    chessboard algorithms [2]_.
  kind: text
  key: 'notes'
  title: 'Notes'
- DocstringMeta
  description: |
    .. [1] Taxicab distance. Wikipedia, 2023.
           https://en.wikipedia.org/wiki/Taxicab_geometry
    .. [2] Chessboard distance. Wikipedia, 2023.
           https://en.wikipedia.org/wiki/Chebyshev_distance
  kind: text
  key: 'references'
  title: 'References'
- DocstringExample
  description: 'Import the necessary modules.'
  kind: examples
  key: 'examples'
  snippet: '>>> import numpy as np'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> from scipy.ndimage import distance_transform_bf'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> import matplotlib.pyplot as plt'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> from mpl_toolkits.axes_grid1 import ImageGrid'
- DocstringExample
  description: 'First, we create a toy binary image.'
  kind: examples
  key: 'examples'
  snippet: |
    >>> def add_circle(center_x, center_y, radius, image, fillvalue=1):
    ...     # fill circular area with 1
    ...     xx, yy = np.mgrid[:image.shape[0], :image.shape[1]]
    ...     circle = (xx - center_x) ** 2 + (yy - center_y) ** 2
    ...     circle_shape = np.sqrt(circle) < radius
    ...     image[circle_shape] = fillvalue
    ...     return image
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> image = np.zeros((100, 100), dtype=np.uint8)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> image[35:65, 20:80] = 1'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> image = add_circle(28, 65, 10, image)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> image = add_circle(37, 30, 10, image)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> image = add_circle(70, 45, 20, image)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> image = add_circle(45, 80, 10, image)'
- DocstringExample
  description: 'Next, we set up the figure.'
  kind: examples
  key: 'examples'
  snippet: '>>> fig = plt.figure(figsize=(8, 8))  # set up the figure structure'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: |
    >>> grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=(0.4, 0.3),
    ...                  label_mode="1", share_all=True,
    ...                  cbar_location="right", cbar_mode="each",
    ...                  cbar_size="7%", cbar_pad="2%")
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: |
    >>> for ax in grid:
    ...     ax.axis('off')  # remove axes from images
- DocstringExample
  description: 'The top left image is the original binary image.'
  kind: examples
  key: 'examples'
  snippet: ">>> binary_image = grid[0].imshow(image, cmap='gray')"
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_binary_image = grid.cbar_axes[0].colorbar(binary_image)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_binary_image.set_ticks([0, 1])'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> grid[0].set_title("Binary image: foreground in white")'
- DocstringExample
  description: |
    The distance transform calculates the distance between foreground pixels
    and the image background according to a distance metric. Available metrics
    in `distance_transform_bf` are: ``euclidean`` (default), ``taxicab``
    and ``chessboard``. The top right image contains the distance transform
    based on the ``euclidean`` metric.
  kind: examples
  key: 'examples'
  snippet: '>>> distance_transform_euclidean = distance_transform_bf(image)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: |
    >>> euclidean_transform = grid[1].imshow(distance_transform_euclidean,
    ...                                      cmap='gray')
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_euclidean = grid.cbar_axes[1].colorbar(euclidean_transform)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> colorbar_ticks = [0, 10, 20]'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_euclidean.set_ticks(colorbar_ticks)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> grid[1].set_title("Euclidean distance")'
- DocstringExample
  description: |
    The lower left image contains the distance transform using the ``taxicab``
    metric.
  kind: examples
  key: 'examples'
  snippet: |
    >>> distance_transform_taxicab = distance_transform_bf(image,
    ...                                                    metric='taxicab')
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: |
    >>> taxicab_transformation = grid[2].imshow(distance_transform_taxicab,
    ...                                         cmap='gray')
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_taxicab = grid.cbar_axes[2].colorbar(taxicab_transformation)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_taxicab.set_ticks(colorbar_ticks)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> grid[2].set_title("Taxicab distance")'
- DocstringExample
  description: |
    Finally, the lower right image contains the distance transform using the
    ``chessboard`` metric.
  kind: examples
  key: 'examples'
  snippet: |
    >>> distance_transform_cb = distance_transform_bf(image,
    ...                                               metric='chessboard')
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: |
    >>> chessboard_transformation = grid[3].imshow(distance_transform_cb,
    ...                                            cmap='gray')
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_taxicab = grid.cbar_axes[3].colorbar(chessboard_transformation)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> cbar_taxicab.set_ticks(colorbar_ticks)'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> grid[3].set_title("Chessboard distance")'
- DocstringExample
  kind: examples
  key: 'examples'
  snippet: '>>> plt.show()'
=== detect
numpydoc
=== compose numpydoc
Distance transform function by a brute force algorithm.

This function calculates the distance transform of the `input`, by
replacing each foreground (non-zero) element, with its
shortest distance to the background (any zero-valued element).

In addition to the distance transform, the feature transform can
be calculated. In this case the index of the closest background
element to each foreground element is returned in a separate array.

Parameters
----------
input : array_like
    Input
metric : {'euclidean', 'taxicab', 'chessboard'}, optional
    'cityblock' and 'manhattan' are also valid, and map to 'taxicab'.
    The default is 'euclidean'.
sampling : float, or sequence of float, optional
    This parameter is only used when `metric` is 'euclidean'.
    Spacing of elements along each dimension. If a sequence, must be of
    length equal to the input rank; if a single number, this is used for
    all axes. If not specified, a grid spacing of unity is implied.
return_distances : bool, optional
    Whether to calculate the distance transform.
    Default is True.
return_indices : bool, optional
    Whether to calculate the feature transform.
    Default is False.
distances : ndarray, optional
    An output array to store the calculated distance transform, instead of
    returning it.
    `return_distances` must be True.
    It must be the same shape as `input`, and of type float64 if `metric`
    is 'euclidean', uint32 otherwise.
indices : int32 ndarray, optional
    An output array to store the calculated feature transform, instead of
    returning it.
    `return_indices` must be True.
    Its shape must be ``(input.ndim,) + input.shape``.

Returns
-------
distances : ndarray, optional
    The calculated distance transform. Returned only when
    `return_distances` is True and `distances` is not supplied.
    It will have the same shape as the input array.
indices : int32 ndarray, optional
    The calculated feature transform. It has an input-shaped array for each
    dimension of the input. See distance_transform_edt documentation for an
    example.
    Returned only when `return_indices` is True and `indices` is not
    supplied.

See Also
--------
distance_transform_cdt : Faster distance transform for taxicab and
                         chessboard metrics
distance_transform_edt : Faster distance transform for euclidean metric

Notes
-----
This function employs a slow brute force algorithm. See also the
function `distance_transform_cdt` for more efficient taxicab [1]_ and
chessboard algorithms [2]_.

References
----------
.. [1] Taxicab distance. Wikipedia, 2023.
       https://en.wikipedia.org/wiki/Taxicab_geometry
.. [2] Chessboard distance. Wikipedia, 2023.
       https://en.wikipedia.org/wiki/Chebyshev_distance

Examples
--------
Import the necessary modules.

>>> import numpy as np
>>> from scipy.ndimage import distance_transform_bf
>>> import matplotlib.pyplot as plt
>>> from mpl_toolkits.axes_grid1 import ImageGrid

First, we create a toy binary image.

>>> def add_circle(center_x, center_y, radius, image, fillvalue=1):
...     # fill circular area with 1
...     xx, yy = np.mgrid[:image.shape[0], :image.shape[1]]
...     circle = (xx - center_x) ** 2 + (yy - center_y) ** 2
...     circle_shape = np.sqrt(circle) < radius
...     image[circle_shape] = fillvalue
...     return image
>>> image = np.zeros((100, 100), dtype=np.uint8)
>>> image[35:65, 20:80] = 1
>>> image = add_circle(28, 65, 10, image)
>>> image = add_circle(37, 30, 10, image)
>>> image = add_circle(70, 45, 20, image)
>>> image = add_circle(45, 80, 10, image)

Next, we set up the figure.

>>> fig = plt.figure(figsize=(8, 8))  # set up the figure structure
>>> grid = ImageGrid(fig, 111, nrows_ncols=(2, 2), axes_pad=(0.4, 0.3),
...                  label_mode="1", share_all=True,
...                  cbar_location="right", cbar_mode="each",
...                  cbar_size="7%", cbar_pad="2%")
>>> for ax in grid:
...     ax.axis('off')  # remove axes from images

The top left image is the original binary image.

>>> binary_image = grid[0].imshow(image, cmap='gray')
>>> cbar_binary_image = grid.cbar_axes[0].colorbar(binary_image)
>>> cbar_binary_image.set_ticks([0, 1])
>>> grid[0].set_title("Binary image: foreground in white")

The distance transform calculates the distance between foreground pixels
and the image background according to a distance metric. Available metrics
in `distance_transform_bf` are: ``euclidean`` (default), ``taxicab``
and ``chessboard``. The top right image contains the distance transform
based on the ``euclidean`` metric.

>>> distance_transform_euclidean = distance_transform_bf(image)
>>> euclidean_transform = grid[1].imshow(distance_transform_euclidean,
...                                      cmap='gray')
>>> cbar_euclidean = grid.cbar_axes[1].colorbar(euclidean_transform)
>>> colorbar_ticks = [0, 10, 20]
>>> cbar_euclidean.set_ticks(colorbar_ticks)
>>> grid[1].set_title("Euclidean distance")

The lower left image contains the distance transform using the ``taxicab``
metric.

>>> distance_transform_taxicab = distance_transform_bf(image,
...                                                    metric='taxicab')
>>> taxicab_transformation = grid[2].imshow(distance_transform_taxicab,
...                                         cmap='gray')
>>> cbar_taxicab = grid.cbar_axes[2].colorbar(taxicab_transformation)
>>> cbar_taxicab.set_ticks(colorbar_ticks)
>>> grid[2].set_title("Taxicab distance")

Finally, the lower right image contains the distance transform using the
``chessboard`` metric.

>>> distance_transform_cb = distance_transform_bf(image,
...                                               metric='chessboard')
>>> chessboard_transformation = grid[3].imshow(distance_transform_cb,
...                                            cmap='gray')
>>> cbar_taxicab = grid.cbar_axes[3].colorbar(chessboard_transformation)
>>> cbar_taxicab.set_ticks(colorbar_ticks)
>>> grid[3].set_title("Chessboard distance")
>>> plt.show()
