o
    :ήcr                     @   s0   d dl Z d dlZd dlmZ dd Zdd ZdS )    N)cKDTreec                 C   s   t t | }t t |}t|dkr!t|dkrdS t jS t|dkr*t jS ttt|j|ddd tt|j|ddd S )a  Calculate the Hausdorff distance between nonzero elements of given images.

    The Hausdorff distance [1]_ is the maximum distance between any point on
    ``image0`` and its nearest point on ``image1``, and vice-versa.

    Parameters
    ----------
    image0, image1 : ndarray
        Arrays where ``True`` represents a point that is included in a
        set of points. Both arrays must have the same shape.

    Returns
    -------
    distance : float
        The Hausdorff distance between coordinates of nonzero pixels in
        ``image0`` and ``image1``, using the Euclidian distance.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Hausdorff_distance

    Examples
    --------
    >>> points_a = (3, 0)
    >>> points_b = (6, 0)
    >>> shape = (7, 1)
    >>> image_a = np.zeros(shape, dtype=bool)
    >>> image_b = np.zeros(shape, dtype=bool)
    >>> image_a[points_a] = True
    >>> image_b[points_b] = True
    >>> hausdorff_distance(image_a, image_b)
    3.0

    r      )k)np	transposenonzeroleninfmaxr   query)image0image1a_pointsb_points r   B/tmp/pip-target-vg8gfxp4/lib/python/skimage/metrics/set_metrics.pyhausdorff_distance   s   #r   c                 C   s   t t | }t t |}t|dkst|dkr%tjddd dS t||\}}t||\}}| }| }	|| }
||	 }||
krU||	 |||	  fS |||  || fS )a  Returns pair of points that are Hausdorff distance apart between nonzero
    elements of given images.

    The Hausdorff distance [1]_ is the maximum distance between any point on
    ``image0`` and its nearest point on ``image1``, and vice-versa.

    Parameters
    ----------
    image0, image1 : ndarray
        Arrays where ``True`` represents a point that is included in a
        set of points. Both arrays must have the same shape.

    Returns
    -------
    point_a, point_b : array
        A pair of points that have Hausdorff distance between them.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Hausdorff_distance

    Examples
    --------
    >>> points_a = (3, 0)
    >>> points_b = (6, 0)
    >>> shape = (7, 1)
    >>> image_a = np.zeros(shape, dtype=bool)
    >>> image_b = np.zeros(shape, dtype=bool)
    >>> image_a[points_a] = True
    >>> image_b[points_b] = True
    >>> hausdorff_pair(image_a, image_b)
    (array([3, 0]), array([6, 0]))

    r   z#One or both of the images is empty.   )
stacklevel)r   r   )	r   r   r   r   warningswarnr   r   argmax)r   r   r   r   nearest_dists_from_bnearest_a_point_indices_from_bnearest_dists_from_anearest_b_point_indices_from_amax_index_from_amax_index_from_bmax_dist_from_amax_dist_from_br   r   r   hausdorff_pair9   s,   #

r    )r   numpyr   scipy.spatialr   r   r    r   r   r   r   <module>   s
    2