Ë
    îì�iä   ã                   ó’   — 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/ITT-ServiceAPI-Services/ITT-Document-Scan/mrzscanner/mrzenv/lib/python3.12/site-packages/skimage/morphology/convex_hull.pyÚ_offsets_diamondr      sT   € Ü�h‰h˜˜D™ $Ð'Ó(€GÜ"+¬G´E¸$³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ã   € ð@  ×%Ñ%�N€Dˆ(Ø%×+Ñ+¨AÑ.ÐÜ—W‘W˜X¬TÔ2€Nô —‘˜¬¯©Ô4€IÜ—X‘X˜h¬b¯j©jÔ9€NÜŸ™ (´$Ô7Ðô Ð%Ó&ò -ˆä
�‰ˆ~˜c 5 D 5˜jÑ)¨:¸9ÕEÜ
�‰ˆy˜.¨¨d©e¨Ñ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:
NéÿÿÿÿF)Ú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Ü	×Ñ˜Ó !Ò#Üð$äô	
ô
 �x‰x˜Ÿ™¬4Ô0Ð0ð ˆq‚yÜœr×3Ñ3°EÄÇÁÔJÓK‰ä—‘œbŸj™j¨Ó/Ó0ˆÙð9Ü" 6Ó*�ð —\‘\ %§.¡.Ñ1ˆFñ Ü" 5§:¡:Ó.ˆØšœBŸJ™JªÐ)Ñ*¨WÑ4×=Ñ=¸bÀ$ÓGˆô ˜Ó €Fð1Ü˜&Ó!ˆð �{‰{˜4Ÿ=™=Ñ)€Hð ˆq‚yÜ$ U§[¡[°(ÀUÔKˆñ .ˆv˜‰{ˆð €Kð 4:¸Q±;ˆð €Kô —Z‘Z¤§¡¬¬s´5¸%¿+¹+Ó/FÓ)GÑ HÈ4ÐQSÈ*ÓUˆ
ä.¨z¸4¿>¹>È9ÓUˆÜ�z‰z˜.¨%¯+©+Ó6ˆà€KøôU ò 9ÜðHà�eðôô
 —x‘x §¡´4Ô8Õ8ûð9ûô* ò 1Üð@àˆeðô	
ô
 �x‰x˜Ÿ™¬4Ô0Õ0ûð1ús<   Â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 ‡z�z�A‚~ÜÐ3Ó4Ð4à˜6Ñ!ÜÐ@ÓAÐAä�u¨<ÀAÔF€JÜ—‘˜%Ÿ+™+¬TÔ2€JÜ—‘˜%Ÿ+™+¬TÔ2€Jä�1�j—n‘nÓ&¨Ñ*Ó+ò ;ˆÜ& z°Q¡Ó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   