
    9i                          S r SSKJr  SSKrSSKJrJr  SSKJ	r	  SSK
Jr  SS	KJr  SS
KJr  SSKJr  SS/rS rS r SS jrSS.S jrg)zConvex Hull.    )productN)
ConvexHull
QhullError   )grid_points_in_poly   )possible_hull)label)unique_rows)warnconvex_hull_imageconvex_hull_objectc                     [         R                  " SU -  U 45      n[        [        [	        U 5      S5      5       H  u  nu  p4XAX#4'   M     U$ )Nr   )g      g      ?)npzeros	enumerater   range)ndimoffsetsvertexaxisoffsets        ^/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/morphology/convex_hull.py_offsets_diamondr      sJ    hhD$'(G"+GE$K,M"N & #ON    c                    U R                   u  p4UR                   S   n[        R                  " U[        S9n[        R                  " U[        R
                  S9n[        R                  " U[        R
                  S9n[        R                  " U[        S9n	[        U5       HR  n
[        R                  " XSU24   XS9  [        R                  " XqXS24   US9  [        R                  " XU	S9  Xi-  nMT     U$ )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 %&
~5D5j):E
ye4.I
/AB, ' r   c           	      >   U R                   n[        R                  " U 5      S:X  a3  [        S[        5        [        R
                  " U R                  [        S9$ US:X  a-  [        [        R                  " U [        R                  S95      nOV[        R                  " [        R                  " U 5      5      nU(       a%   [        U5      nUR                  UR                      nU(       aB  [#        U R                   5      nUSS2[        R$                  SS24   U-   R'                  SU5      n[)        U5      n [        U5      n	U	R                  U	R                      n
US:X  a*  [+        U R                  U
SS	9nU(       a  US
:  nU$ US
:H  nU$ [        R&                  " [        R,                  [/        [1        [2        U R                  5      5         US45      n[5        XR6                  U5      n[        R&                  " XR                  5      nU$ ! [         a;  n[        SU 35        [        R
                  " U R                  [        S9s SnA$ SnAff = f! [         a;  n[        SU 35        [        R
                  " U R                  [        S9s SnA$ SnAff = f)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	!#$	

 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
.z>>9Uzz.++6KU  9He
 xx4889*  1@e	

 xx4001s<   ?H 8I 
I0I	II
J!0JJJ)connectivityc                   U R                   S:  a  [        S5      eUS;  a  [        S5      e[        XSS9n[        R                  " U R
                  [        S9n[        R                  " U R
                  [        S9n[        SUR                  5       S-   5       H&  n[        X%:H  5      n[        R                  " XC5      nM(     U$ )	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uAFJ%++T2J%++T2J1jnn&*+&z7
]]::
 , 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   