
    0-Phq%                     @    d dl ZddlmZ d dlmZ dZ	 d
dddZd	 ZdS )    N   )_get_contour_segments)deque)highlowr   maskc                    |t           vrt          d          |t           vrt          d          | j        d         dk     s| j        d         dk     rt          d          | j        dk    rt          d          |p|j        | j        k    rt          d	          t	          j        |j        t          d
          st          d          |	                    t          j
        d          }|,t	          j        |           t	          j        |           z   dz  }t          | 	                    t          j                  t          |          |dk    |          }t!          |          }|dk    rd |D             }|S )a7  Find iso-valued contours in a 2D array for a given level value.

    Uses the "marching squares" method to compute the iso-valued contours of
    the input 2D array for a particular level value. Array values are linearly
    interpolated to provide better precision for the output contours.

    Parameters
    ----------
    image : (M, N) ndarray of double
        Input image in which to find contours.
    level : float, optional
        Value along which to find contours in the array. By default, the level
        is set to (max(image) + min(image)) / 2

        .. versionchanged:: 0.18
            This parameter is now optional.
    fully_connected : str, {'low', 'high'}
         Indicates whether array elements below the given level value are to be
         considered fully-connected (and hence elements above the value will
         only be face connected), or vice-versa. (See notes below for details.)
    positive_orientation : str, {'low', 'high'}
         Indicates whether the output contours will produce positively-oriented
         polygons around islands of low- or high-valued elements. If 'low' then
         contours will wind counter-clockwise around elements below the
         iso-value. Alternately, this means that low-valued elements are always
         on the left of the contour. (See below for details.)
    mask : (M, N) ndarray of bool or None
        A boolean mask, True where we want to draw contours.
        Note that NaN values are always excluded from the considered region
        (``mask`` is set to ``False`` wherever ``array`` is ``NaN``).

    Returns
    -------
    contours : list of (K, 2) ndarrays
        Each contour is a ndarray of ``(row, column)`` coordinates along the contour.

    See Also
    --------
    skimage.measure.marching_cubes

    Notes
    -----
    The marching squares algorithm is a special case of the marching cubes
    algorithm [1]_.  A simple explanation is available here:

    https://users.polytech.unice.fr/~lingrand/MarchingCubes/algo.html

    There is a single ambiguous case in the marching squares algorithm: when
    a given ``2 x 2``-element square has two high-valued and two low-valued
    elements, each pair diagonally adjacent. (Where high- and low-valued is
    with respect to the contour value sought.) In this case, either the
    high-valued elements can be 'connected together' via a thin isthmus that
    separates the low-valued elements, or vice-versa. When elements are
    connected together across a diagonal, they are considered 'fully
    connected' (also known as 'face+vertex-connected' or '8-connected'). Only
    high-valued or low-valued elements can be fully-connected, the other set
    will be considered as 'face-connected' or '4-connected'. By default,
    low-valued elements are considered fully-connected; this can be altered
    with the 'fully_connected' parameter.

    Output contours are not guaranteed to be closed: contours which intersect
    the array edge or a masked-off region (either where mask is False or where
    array is NaN) will be left open. All other contours will be closed. (The
    closed-ness of a contours can be tested by checking whether the beginning
    point is the same as the end point.)

    Contours are oriented. By default, array values lower than the contour
    value are to the left of the contour and values greater than the contour
    value are to the right. This means that contours will wind
    counter-clockwise (i.e. in 'positive orientation') around islands of
    low-valued pixels. This behavior can be altered with the
    'positive_orientation' parameter.

    The order of the contours in the output list is determined by the position
    of the smallest ``x,y`` (in lexicographical order) coordinate in the
    contour.  This is a side effect of how the input array is traversed, but
    can be relied upon.

    .. warning::

       Array coordinates/values are assumed to refer to the *center* of the
       array element. Take a simple example input: ``[0, 1]``. The interpolated
       position of 0.5 in this array is midway between the 0-element (at
       ``x=0``) and the 1-element (at ``x=1``), and thus would fall at
       ``x=0.5``.

    This means that to find reasonable contours, it is best to find contours
    midway between the expected "light" and "dark" values. In particular,
    given a binarized array, *do not* choose to find contours at the low or
    high value of the array. This will often yield degenerate contours,
    especially around structures that are a single array element wide. Instead,
    choose a middle value, as above.

    References
    ----------
    .. [1] Lorensen, William and Harvey E. Cline. Marching Cubes: A High
           Resolution 3D Surface Construction Algorithm. Computer Graphics
           (SIGGRAPH 87 Proceedings) 21(4) July 1987, p. 163-170).
           :DOI:`10.1145/37401.37422`

    Examples
    --------
    >>> a = np.zeros((3, 3))
    >>> a[0, 0] = 1
    >>> a
    array([[1., 0., 0.],
           [0., 0., 0.],
           [0., 0., 0.]])
    >>> find_contours(a, 0.5)
    [array([[0. , 0.5],
           [0.5, 0. ]])]
    z<Parameters "fully_connected" must be either "high" or "low".zAParameters "positive_orientation" must be either "high" or "low".r      r   z!Input array must be at least 2x2.zOnly 2D arrays are supported.Nz3Parameters "array" and "mask" must have same shape.safe)castingz(Parameter "mask" must be a binary array.F)copyg       @r   r   c                 $    g | ]}|d d d         S )N ).0cs     ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/measure/_find_contours.py
<listcomp>z!find_contours.<locals>.<listcomp>   s"    ...AdddG...    )_param_options
ValueErrorshapendimnpcan_castdtypebool	TypeErrorastypeuint8nanminnanmaxr   float64float_assemble_contours)imagelevelfully_connectedpositive_orientationr	   segmentscontourss          r   find_contoursr-   
   s   f n,,M
 
 	
 >11R
 
 	
 {1~U[^a//<===zQ8999:$$UVVV{4:tV<<< 	HFGGG{{28%{00}5!!BIe$4$44;$RZ  %,,60IPT  H "(++Hv%%..X...Or   c                 x   d}i }i }i }| D ]\  }}||k    r|                     |d          \  }}|                     |d          \  }	}
||	||	u r|	                    |           ]||
k    rF|	                    |           |                     |d            |	|
f||	d         <   |	|
f||	d         <   |                    t	          |	                     |                     |	d         d            |                     |
d            ||f||d         <   ||f||d         <   |-|	+t          ||f          }|||<   ||f||<   ||f||<   |dz  }H|	|                    |           ||f||<   h|	                    |           |	|
f||<   d t          |                                          D             S )Nr   )NNr   r   c                 <    g | ]\  }}t          j        |          S r   )r   array)r   _contours      r   r   z&_assemble_contours.<locals>.<listcomp>   s&    III*!WBHWIIIr   )	popappendextend
extendleftreversedr   
appendleftsorteditems)r+   current_indexr,   startsends
from_pointto_pointtailtail_numheadhead_numnew_contours               r   r&   r&      s   MHFD ( 8. 8.
H !!Hl;;h*l;;h 0t||H%%%%
 h&&KK%%%LL4000'+X&6F47O&*H%5DbNN OOHTNN333JJtAw---LL4000'+X&6F47O&*H%5DbNN\dlX 677K&1H]#"-}!=F:)=9DNQMM\ OOJ'''"&!1F: KK!!!"H-DNNIIx~~7G7G0H0HIIIIr   )Nr   r   )	numpyr   _find_contours_cyr   collectionsr   r   r-   r&   r   r   r   <module>rH      s        4 4 4 4 4 4        DINRVN N N N Nb?J ?J ?J ?J ?Jr   