
    0-Ph                          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 Zd Z	 ddZ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                     t          j        d| z  | f          }t          t          t	          |           d                    D ]\  }\  }}||||f<   |S )Nr   )g      g      ?)npzeros	enumerater   range)ndimoffsetsvertexaxisoffsets        ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/morphology/convex_hull.py_offsets_diamondr      s`    hD$'((G"+GE$KK,M,M"N"N ' 'v &N    c                     | j         \  }}|j         d         }t          j        |t                    }t          j        |t          j                  }t          j        |t          j                  }t          j        |t                    }	t          |          D ]`}
t          j        ||
d|f         | |           t          j        |||
|df         |           t          j	        |||	           ||	z  }a|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   dtypeN)out)
shaper   onesboolemptyfloat64r   dotaddless)
gridcoordshull_equations	tolerancer   n_coordsn_hull_equationscoords_in_hull	dot_arraytest_ineq_tempcoords_single_ineqidxs              r   _check_coords_in_hullr2      s   @  %ND(%+A.WXT222N 444IXhbj999N($777 %&& - -
~c5D5j):9EEEE
y.dee4.IIII
	/ABBBB,,r   T绽|=c           	         | j         }t          j        |           dk    r5t          dt                     t          j        | j        t                    S |dk    r.t          t          j	        | t          j
                            }nt          j        t          j        |                     }|rl	 t          |          }nI# t          $ r<}t          d|            t          j        | j        t                    cY d}~S d}~ww xY w|j        |j                 }|rDt#          | j                   }|ddt          j        ddf         |z                       d|          }t)          |          }	 t          |          }	nI# t          $ r<}t          d|            t          j        | j        t                    cY d}~S d}~ww xY w|	j        |	j                 }
|dk    r&t+          | j        |
d	          }|r|d
k    n|d
k    }n|t          j        t          j        t/          t1          t2          | j                                     |df          }t5          ||	j        |          }t          j        || 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)binarizer   )r   r   count_nonzeror   UserWarningr   r    r"   r	   ascontiguousarrayuint8	transposenonzeror   r   pointsverticesr   newaxisreshaper   r   mgridtuplemapslicer2   	equations)imageoffset_coordinatesr*   include_bordersr   coordshull0errr   hullr>   labelsmaskr(   r-   s                  r   r   r   J   s   B :D	!##$	
 	
 	

 x40000 qyyr3EJJJKKbj//00 	29"6** 9 9 9   
 x48888888889 \%.1F  H"5:..BJ)*W4==b$GG   F1&!! 1 1 1 	
 	
 	

 x40000000001 {4=)H qyy$U[(UKKK .>v{{6Q;Zs5%+/F/F)G)G H4QS*UU
.z4>9UUz.%+66Ks<   2C 
D1D=DD3F 
G	1G>G	G	)connectivityc                   | j         dk    rt          d          |dvrt          d          t          | |d          }t          j        | j        t                    }t          j        | j        t                    }t          d|                                dz             D ]*}t          ||k              }t          j
        ||          }+|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   )rO   
backgroundr   r   )r   
ValueErrorr
   r   r   r    r"   r   maxr   
logical_or)rF   rO   
labeled_im
convex_obj
convex_imgis         r   r   r      s    F zA~~34446!!@AAAu<AFFFJ%+T222J%+T222J1jnn&&*++ ; ;&zQ77
]:z::

r   )Tr3   T)__doc__	itertoolsr   numpyr   scipy.spatialr   r   measure.pnpolyr   _convex_hullr	   measure._labelr
   utilr   _shared.utilsr   __all__r   r2   r   r    r   r   <module>rd      s               0 0 0 0 0 0 0 0 0 0 0 0 0 0 ' ' ' ' ' ' " " " " " "                   4
5  1 1 1j FJ` ` ` `F /0 1 1 1 1 1 1 1r   