U
    ?h                      @   s   d Z ddlmZ ddlZddlmZmZ ddlm	Z	 ddl
mZ dd	lmZ dd
lmZ ddlmZ ddgZdd Zdd ZdddZddddZdS )zConvex Hull.    )productN)
ConvexHull
QhullError   )grid_points_in_poly   )possible_hull)label)unique_rows)warnconvex_hull_imageconvex_hull_objectc                 C   sB   t d|  | f}ttt| dD ]\}\}}||||f< q$|S )Nr   )g      g      ?)npzeros	enumerater   range)ndimoffsetsZvertexZaxisoffset r   P/var/www/html/venv/lib/python3.8/site-packages/skimage/morphology/convex_hull.py_offsets_diamond   s    r   c                 C   s   | j \}}|j d }tj|td}tj|tjd}tj|tjd}tj|td}	t|D ]T}
tj||
d|f | |d tj|||
|df |d tj	|||	d ||	9 }qX|S )a`  Checks all the coordinates for inclusiveness in the convex hull.

    Parameters
    ----------
    gridcoords : (M, N) ndarray
        Coordinates of ``N`` points in ``M`` dimensions.
    hull_equations : (M, N) ndarray
        Hyperplane equations of the facets of the convex hull.
    tolerance : float
        Tolerance when determining whether a point is inside the hull. Due
        to numerical floating point errors, a tolerance of 0 can result in
        some points erroneously being classified as being outside the hull.

    Returns
    -------
    coords_in_hull : ndarray of bool
        Binary 1D ndarray representing points in n-dimensional space
        with value ``True`` set for points inside the convex hull.

    Notes
    -----
    Checking the inclusiveness of coordinates in a convex hull requires
    intermediate calculations of dot products which are memory-intensive.
    Thus, the convex hull equations are checked individually with all
    coordinates to keep within the memory limit.

    References
    ----------
    .. [1] https://github.com/scikit-image/scikit-image/issues/5019

    r   ZdtypeN)out)
shaper   ZonesboolemptyZfloat64r   dotaddless)
gridcoordsZhull_equations	tolerancer   Zn_coordsZn_hull_equationscoords_in_hullZ	dot_arrayZtest_ineq_tempZcoords_single_ineqidxr   r   r   _check_coords_in_hull   s     


r$   T绽|=c              
   C   s  | j }t| dkr.tdt tj| jtdS |dkrLttj	| tj
d}nrtt| }|rzt|}W nD tk
r } z&td|  tj| jtd W Y S d}~X Y nX |j|j }|rt| j }|ddtjddf | d|}t|}zt|}	W nF tk
rJ } z&td|  tj| jtd W Y S d}~X Y nX |	j|	j }
|dkrt| j|
dd	}|r|d
kn|d
k}n>ttjttt| j |df}t||	j|}t|| j}|S )a  Compute the convex hull image of a binary image.

    The convex hull is the set of pixels included in the smallest convex
    polygon that surround all white pixels in the input image.

    Parameters
    ----------
    image : array
        Binary input image. This array is cast to bool before processing.
    offset_coordinates : bool, optional
        If ``True``, a pixel at coordinate, e.g., (4, 7) will be represented
        by coordinates (3.5, 7), (4.5, 7), (4, 6.5), and (4, 7.5). This adds
        some "extent" to a pixel when computing the hull.
    tolerance : float, optional
        Tolerance when determining whether a point is inside the hull. Due
        to numerical floating point errors, a tolerance of 0 can result in
        some points erroneously being classified as being outside the hull.
    include_borders: bool, optional
        If ``False``, vertices/edges are excluded from the final hull mask.

    Returns
    -------
    hull : (M, N) array of bool
        Binary image with pixels in convex hull set to True.

    References
    ----------
    .. [1] https://blogs.mathworks.com/steve/2011/10/04/binary-image-convex-hull-algorithm-notes/

    r   zIInput image is entirely zero, no valid convex hull. Returning empty imager   r   zQFailed to get convex hull image. Returning empty image, see error message below:
NF)Zbinarizer   )r   r   Zcount_nonzeror   UserWarningr   r   r   r   ZascontiguousarrayZuint8Z	transposeZnonzeror   r   Zpointsverticesr   ZnewaxisZreshaper
   r   Zmgridtuplemapslicer$   Z	equations)imageZoffset_coordinatesr!   Zinclude_bordersr   ZcoordsZhull0errr   Zhullr(   labelsmaskr    r"   r   r   r   r   I   sL     &
$&
 )connectivityc                C   s   | j dkrtd|dkr"tdt| |dd}tj| jtd}tj| jtd}td| d D ]}t	||k}t
||}qb|S )	a  Compute the convex hull image of individual objects in a binary image.

    The convex hull is the set of pixels included in the smallest convex
    polygon that surround all white pixels in the input image.

    Parameters
    ----------
    image : (M, N) ndarray
        Binary input image.
    connectivity : {1, 2}, int, optional
        Determines the neighbors of each pixel. Adjacent elements
        within a squared distance of ``connectivity`` from pixel center
        are considered neighbors.::

            1-connectivity      2-connectivity
                  [ ]           [ ]  [ ]  [ ]
                   |               \  |  /
             [ ]--[x]--[ ]      [ ]--[x]--[ ]
                   |               /  |  \
                  [ ]           [ ]  [ ]  [ ]

    Returns
    -------
    hull : ndarray of bool
        Binary image with pixels inside convex hull set to ``True``.

    Notes
    -----
    This function uses ``skimage.morphology.label`` to define unique objects,
    finds the convex hull of each using ``convex_hull_image``, and combines
    these regions with logical OR. Be aware the convex hulls of unconnected
    objects may overlap in the result. If this is suspected, consider using
    convex_hull_image separately on each object or adjust ``connectivity``.
    r   zInput must be a 2D image)r   r   z%`connectivity` must be either 1 or 2.r   )r0   
backgroundr   r   )r   
ValueErrorr	   r   r   r   r   r   maxr   
logical_or)r,   r0   Z
labeled_imZ
convex_objZ
convex_imgir   r   r   r      s    #
)Tr%   T)__doc__	itertoolsr   numpyr   Zscipy.spatialr   r   Zmeasure.pnpolyr   Z_convex_hullr   Zmeasure._labelr	   utilr
   Z_shared.utilsr   __all__r   r$   r   r   r   r   r   r   <module>   s   4  
]