
    |h                          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y)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/engine/venv/lib/python3.12/site-packages/skimage/morphology/convex_hull.py_offsets_diamondr      sT    hhD$'(G"+GE$K,M"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  }_ |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.WWXT2N 4IXXhbjj9N($7 %& -
~c5D5j):9E
y.de4.I
	/AB,,- r   c           	      &   | j                   }t        j                  |       dk(  r5t        dt               t        j
                  | j                  t              S |dk(  r/t        t        j                  | t        j                              }nOt        j                  t        j                  |             }|r%	 t        |      }|j                  |j                      }|rBt#        | j                         }|ddt        j$                  ddf   |z   j'                  d|      }t)        |      }	 t        |      }	|	j                  |	j                      }
|dk(  r(t+        | j                  |
d	      }|r|d
k\  }|S |d
k(  }|S t        j&                  t        j,                  t/        t1        t2        | j                                 |df      }t5        ||	j6                  |      }t        j&                  || j                        }|S # t        $ r=}t        d|        t        j
                  | j                  t              cY d}~S d}~ww xY w# t        $ r=}t        d|        t        j
                  | j                  t              cY d}~S d}~ww xY w)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   s0   B ::D	!#$	

 xx400 qyr33EJKbjj/09"6* \\%..1F "5::.BJJ)*W4==b$G  F1&! {{4==)H qy$U[[(UK .v{ K 4:Q; K ZZs5%++/F)G H4QS*U
.z4>>9Uzz.%++6KU  9He
 xx4889*  1@e	

 xx4001s<   9H -I
 	I
2I<II
	J2JJJ)connectivityc                   | j                   dkD  rt        d      |dvrt        d      t        | |d      }t        j                  | j
                  t              }t        j                  | j
                  t              }t        d|j                         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   )rN   
backgroundr   r   )r   
ValueErrorr
   r   r   r    r"   r   maxr   
logical_or)rE   rN   
labeled_im
convex_obj
convex_imgis         r   r   r      s    F zzA~3446!@AAu<AFJ%++T2J%++T2J1jnn&*+ ;&zQ7
]]:z:
; r   )Tg|=T)__doc__	itertoolsr   numpyr   scipy.spatialr   r   measure.pnpolyr   _convex_hullr	   measure._labelr
   utilr   _shared.utilsr   __all__r   r2   r   r    r   r   <module>rc      sN       0 0 ' "    4
51j FJ`F /0 1r   