U
    ?h                      @   s2   d dl Z d dlZd dlmZ dddZdd ZdS )	    N)cKDTreestandardc                 C   s   |dkrt d| tt| }tt|}t|dkrXt|dkrRdS tjS t|dkrjtjS t|j|ddd t|j|ddd  }}|dkrtt|t|S |dkrtt	|t	|S dS )	a  Calculate the Hausdorff distance between nonzero elements of given images.

    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.
    method : {'standard', 'modified'}, optional, default = 'standard'
        The method to use for calculating the Hausdorff distance.
        ``standard`` is the standard Hausdorff distance, while ``modified``
        is the modified Hausdorff distance.

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

    Notes
    -----
    The Hausdorff distance [1]_ is the maximum distance between any point on
    ``image0`` and its nearest point on ``image1``, and vice-versa.
    The Modified Hausdorff Distance (MHD) has been shown to perform better
    than the directed Hausdorff Distance (HD) in the following work by
    Dubuisson et al. [2]_. The function calculates forward and backward
    mean distances and returns the largest of the two.

    References
    ----------
    .. [1] http://en.wikipedia.org/wiki/Hausdorff_distance
    .. [2] M. P. Dubuisson and A. K. Jain. A Modified Hausdorff distance for object
       matching. In ICPR94, pages A:566-568, Jerusalem, Israel, 1994.
       :DOI:`10.1109/ICPR.1994.576361`
       http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.1.8155

    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   modifiedzunrecognized method r      )kr   r   N)

ValueErrornp	transposenonzeroleninfr   querymaxZmean)image0image1methoda_pointsb_pointsZfwdZbwd r   M/var/www/html/venv/lib/python3.8/site-packages/skimage/metrics/set_metrics.pyhausdorff_distance   s    2r   c                 C   s   t t | }t t |}t|dks8t|dkrJtjddd dS t||\}}t||\}}| }| }	|| }
||	 }||
kr||	 |||	  fS |||  || fS d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   N)	r   r	   r
   r   warningswarnr   r   Zargmax)r   r   r   r   Znearest_dists_from_bZnearest_a_point_indices_from_bZnearest_dists_from_aZnearest_b_point_indices_from_aZmax_index_from_aZmax_index_from_bZmax_dist_from_aZmax_dist_from_br   r   r   hausdorff_pairR   s,    #

r   )r   )r   numpyr   Zscipy.spatialr   r   r   r   r   r   r   <module>   s   
K