o
    e¾!  ã                   @   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   úND:\Projects\ConvertPro\env\Lib\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j||
d|…f | |d tj|||
|d…f |d tj	|||	d ||	9 }q,|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r&ttj	| tj
dƒ}n8t t | ¡¡}|r^zt|ƒ}W n! tyW } ztd|› ƒ tj| jtdW  Y d}~S d}~ww |j|j }|rwt| j ƒ}|dd…tjdd…f |  d|¡}t|ƒ}zt|ƒ}	W n! ty¢ } ztd|› ƒ tj| jtdW  Y d}~S d}~ww |	j|	j }
|dkrÃt| j|
dd	}|r½|d
k}|S |d
k}|S 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:
NéÿÿÿÿF)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   ÚcoordsZhull0Úerrr   Zhullr(   ÚlabelsÚmaskr    r"   r   r   r   r   I   s`    ÿþ€ü
$þ€ü	÷	ùÿÿ)Úconnectivityc                C   s„   | j dkr	tdƒ‚|dvrtdƒ‚t| |dd}tj| jtd}tj| jtd}td| ¡ d ƒD ]}t	||kƒ}t 
||¡}q1|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   )r1   Ú
backgroundr   r   )r   Ú
ValueErrorr	   r   r   r   r   r   Úmaxr   Ú
logical_or)r,   r1   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
ÿ]