
    0-Phu>                         d dl mZ d dlmZ d dlZd dlmZ  ej	        g dg dg dgej
                  Z ej        dej
                  Zg d	Zg d
Z ej	        g d          ZddZddZddZd ZdS )    )sqrt)RealN)ndimage)r      r   )r   r   r   dtype)   r	   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r
   r   r   r   r   r   r   r   r
   r   (   r   r   r   r   r   r   r
   r   r   r   r
   r   r
   r
   r   r   r   r   r
   r   r
   r
   r   r   r   r   r   r   r
   r   r   r   r
   r   r   r   r   r   r
   r
   r   r   r   r   r
   r   r
   r
   r   r   r   r   r
   r   r   r   r
   r   r   r   r   r   r   r   r   r   r
   r   r   r   r
   r   r   r
   r   r   r
   r   r
   r   r   r
   r   r   r
   r   r   r   r   r   r
   r   r   r   r
   r   r   r   r   r   r   r
      r   r	   r   r	   r      r
   r   r   r
   r   r
   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   r   r   r
   r   r   r   r
   r   r
   r   r   r
   r   r   r
   r   r
   r   r   r	   r   r   r   r   r   r   r	   r   r   r   r
   r   r   r   r   r   r
   r   r   r
   r   r   r
   r   r
   r   r   r
   r   r   r	   r   r   r   r
   r   r   r   r   r   r   r   r   r   r
   r   r   r   r
   r   r   r   r   r
   r
   r   r
   r   r   r   r   r
   r	   r   r   r   r   r   r
   r   r   r   r
   r   r   r   r   r   r   r   r   r	   r
   r   r
   r   r   r   r   r
   r
   r   r
   r   r   r   r
   r   r   r   r   r   r   r   c                    | dk                         t                    } t          j        | dd          } || j        }| j        dk    r5t          j        g dg dg d	g          }|dk    rt          }nt          }d
}ng|dk    rt          d          t          j        g dg dg dgg dg dg d	gg dg dg dgg          }|dk    rt          ddd         }nt          }d}t          j        | |dd          }t          j        |                                |          }| j        dk    r||z  S t          d|z  |z            S )uq  Calculate the Euler characteristic in binary image.

    For 2D objects, the Euler number is the number of objects minus the number
    of holes. For 3D objects, the Euler number is obtained as the number of
    objects plus the number of holes, minus the number of tunnels, or loops.

    Parameters
    ----------
    image: (M, N[, P]) ndarray
        Input image. If image is not binary, all values greater than zero
        are considered as the object.
    connectivity : int, optional
        Maximum number of orthogonal hops to consider a pixel/voxel
        as a neighbor.
        Accepted values are ranging from  1 to input.ndim. If ``None``, a full
        connectivity of ``input.ndim`` is used.
        4 or 8 neighborhoods are defined for 2D images (connectivity 1 and 2,
        respectively).
        6 or 26 neighborhoods are defined for 3D images, (connectivity 1 and 3,
        respectively). Connectivity 2 is not defined.

    Returns
    -------
    euler_number : int
        Euler characteristic of the set of all objects in the image.

    Notes
    -----
    The Euler characteristic is an integer number that describes the
    topology of the set of all objects in the input image. If object is
    4-connected, then background is 8-connected, and conversely.

    The computation of the Euler characteristic is based on an integral
    geometry formula in discretized space. In practice, a neighborhood
    configuration is constructed, and a LUT is applied for each
    configuration. The coefficients used are the ones of Ohser et al.

    It can be useful to compute the Euler characteristic for several
    connectivities. A large relative difference between results
    for different connectivities suggests that the image resolution
    (with respect to the size of objects and holes) is too low.

    References
    ----------
    .. [1] S. Rivollier. Analyse d’image geometrique et morphometrique par
           diagrammes de forme et voisinages adaptatifs generaux. PhD thesis,
           2010. Ecole Nationale Superieure des Mines de Saint-Etienne.
           https://tel.archives-ouvertes.fr/tel-00560838
    .. [2] Ohser J., Nagel W., Schladitz K. (2002) The Euler Number of
           Discretized Sets - On the Choice of Adjacency in Homogeneous
           Lattices. In: Mecke K., Stoyan D. (eds) Morphology of Condensed
           Matter. Lecture Notes in Physics, vol 600. Springer, Berlin,
           Heidelberg.

    Examples
    --------
    >>> import numpy as np
    >>> SAMPLE = np.zeros((100,100,100));
    >>> SAMPLE[40:60, 40:60, 40:60]=1
    >>> euler_number(SAMPLE) # doctest: +ELLIPSIS
    1...
    >>> SAMPLE[45:55,45:55,45:55] = 0;
    >>> euler_number(SAMPLE) # doctest: +ELLIPSIS
    2...
    >>> SAMPLE = np.array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0],
    ...                    [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
    ...                    [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
    ...                    [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0],
    ...                    [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
    ...                    [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    ...                    [0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
    ...                    [1, 0, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 0],
    ...                    [0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1],
    ...                    [0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1]])
    >>> euler_number(SAMPLE)  # doctest:
    0
    >>> euler_number(SAMPLE, connectivity=1)  # doctest:
    2
    r   r   constant	pad_widthmodeNr   r   r   r   r   r   r   r   r         zJFor 3D images, Euler number is implemented for connectivities 1 and 3 only)r   r   @   )r          r
      r   cval	minlengthg      ?)astypeintnppadndimarrayEULER_COEFS2D_4EULER_COEFS2D_8NotImplementedErrorEULER_COEFS3D_26ndiconvolvebincountravel)imageconnectivityconfigcoefsbinsXFhs          b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/measure/_regionprops_utils.pyeuler_numberr8     s   d QYs##EF5AJ777E z
 zQ999iii;<<1#EE#E1%2  
 IIIyyy1IIIyyy1KKK6
 
 1$TTrT*EE$E
 
eV*1	=	=	=B
BHHJJ$///AzQqy55=1$%%%    r   c           	      D   | j         dk    rt          d          |dk    rt          }nt          }|                     t
          j                  } t          j        | |d          }| |z
  }t          j	        dt
          j
                  }d|g d	<   t          d          |d
dg<   dt          d          z   dz  |ddg<   t          j        |t          j        g dg dg dg          dd          }t          j        |                                d          }||z  }|S )ac  Calculate total perimeter of all objects in binary image.

    Parameters
    ----------
    image : (M, N) ndarray
        Binary input image.
    neighborhood : 4 or 8, optional
        Neighborhood connectivity for border pixel determination. It is used to
        compute the contour. A higher neighborhood widens the border on which
        the perimeter is computed.

    Returns
    -------
    perimeter : float
        Total perimeter of all objects in binary image.

    References
    ----------
    .. [1] K. Benkrid, D. Crookes. Design and FPGA Implementation of
           a Perimeter Estimator. The Queen's University of Belfast.
           http://www.cs.qub.ac.uk/~d.crookes/webpubs/papers/perimeter.doc

    Examples
    --------
    >>> from skimage import data, util
    >>> from skimage.measure import label
    >>> # coins image (binary)
    >>> img_coins = data.coins() > 110
    >>> # total perimeter of all objects in the image
    >>> perimeter(img_coins, neighborhood=4)  # doctest: +ELLIPSIS
    7796.867...
    >>> perimeter(img_coins, neighborhood=8)  # doctest: +ELLIPSIS
    8806.268...

    r   z#`perimeter` supports 2D images onlyr   r   )border_value2   r   r   )                     !         )
   r   rG   )r   r   r   r   r   r    )r&   r*   STREL_4STREL_8r"   r$   uint8r,   binary_erosionzerosfloat64r   r-   r'   r.   r/   )	r0   neighborhoodstreleroded_imageborder_imageperimeter_weightsperimeter_imageperimeter_histogramtotal_perimeters	            r7   	perimeterrV     s9   H zQ!"GHHHqLL""E%eUCCCL<'L2:66601,,,-"&q''r2h#$tAww;!"3r2hl
+++yyy+++677	  O +o&;&;&=&=LLL),==Or9   c                    | j         dk    rt          d          | dk                        t          j                  } t          j        | dd          } t          j        | t          j        g dg dg d	g          dd
          }t          j	        |
                                d          }|dk    r\dt          j        dz  dddt          j        dz  ddt          j        dz  t          j        ddt          j        dz  t          j        ddg}n@dt          j        dz  ddt          j        d          z  z   z  t          j        dt          j        d          z  z  t          j        dt          j        d          z  z  dt          j        dz  ddt          j        d          z  z   z  dt          j        dt          j        d          z  z  t          j        dz  t          j        dz  t          j        dt          j        d          z  z  t          j        dt          j        d          z  z  t          j        dz  t          j        dz  ddg}||z  }|S )u,  Calculate total Crofton perimeter of all objects in binary image.

    Parameters
    ----------
    image : (M, N) ndarray
        Input image. If image is not binary, all values greater than zero
        are considered as the object.
    directions : 2 or 4, optional
        Number of directions used to approximate the Crofton perimeter. By
        default, 4 is used: it should be more accurate than 2.
        Computation time is the same in both cases.

    Returns
    -------
    perimeter : float
        Total perimeter of all objects in binary image.

    Notes
    -----
    This measure is based on Crofton formula [1], which is a measure from
    integral geometry. It is defined for general curve length evaluation via
    a double integral along all directions. In a discrete
    space, 2 or 4 directions give a quite good approximation, 4 being more
    accurate than 2 for more complex shapes.

    Similar to :func:`~.measure.perimeter`, this function returns an
    approximation of the perimeter in continuous space.

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Crofton_formula
    .. [2] S. Rivollier. Analyse d’image geometrique et morphometrique par
           diagrammes de forme et voisinages adaptatifs generaux. PhD thesis,
           2010.
           Ecole Nationale Superieure des Mines de Saint-Etienne.
           https://tel.archives-ouvertes.fr/tel-00560838

    Examples
    --------
    >>> from skimage import data, util
    >>> from skimage.measure import label
    >>> # coins image (binary)
    >>> img_coins = data.coins() > 110
    >>> # total perimeter of all objects in the image
    >>> perimeter_crofton(img_coins, directions=2)  # doctest: +ELLIPSIS
    8144.578...
    >>> perimeter_crofton(img_coins, directions=4)  # doctest: +ELLIPSIS
    7837.077...
    r   z+`perimeter_crofton` supports 2D images onlyr   r   r   r   r   r   r   r   r   r    r   )r&   r*   r"   r$   rJ   r%   r,   r-   r'   r.   r/   pir   )r0   
directionsr5   r6   r3   rU   s         r7   perimeter_croftonrZ     s   d zQ!"OPPP QYrx((EF5AJ777E	rxIIIyyy9::RS
 
 
B 	BHHJJ"---A QEAIEAIEAIEEAIE!
( EAIQ"'!**--.EQ^$EQ^$EAIQ"'!**--.EQ^$EAIEAIEQ^$EQ^$EAIEAI!
& aiOr9   c                 z   t          j        |           } | j        dk    rt          j        | |f          } n#| j        |fk    rt	          d|f d|  d          t          d | D                       st          d|  d          t          t          j        |                     st	          d|  d          | S )	a  Normalize spacing parameter.

    The `spacing` parameter should be a sequence of numbers matching
    the image dimensions. If `spacing` is a scalar, assume equal
    spacing along all dimensions.

    Parameters
    ---------
    spacing : Any
        User-provided `spacing` keyword.
    ndims : int
        Number of image dimensions.

    Returns
    -------
    spacing : array
        Corrected spacing.

    Raises
    ------
    ValueError
        If `spacing` is invalid.

     )shapez/spacing isn't a scalar nor a sequence of shape z, got .c              3   @   K   | ]}t          |t                    V  d S N)
isinstancer   ).0ss     r7   	<genexpr>z%_normalize_spacing.<locals>.<genexpr>n  s,      44qz!T""444444r9   z4Element of spacing isn't float or integer type, got z<Invalid spacing parameter. All elements must be finite, got )r$   r'   r]   broadcast_to
ValueErrorall	TypeErrorisfinite)spacingndimss     r7   _normalize_spacingrl   N  s    2 hwG}/'%:::	5(	"	"XuhXXgXXX
 
 	
 44G44444 
M7MMM
 
 	
 r{7##$$ 
U7UUU
 
 	
 Nr9   r`   )r   )mathr   numbersr   numpyr$   scipyr   r,   r'   rJ   rH   onesrI   r(   r)   r+   r8   rV   rZ   rl   r\   r9   r7   <module>rr      s2                               "(IIIyyy)))4BH
E
E
E
"'&
)
)
) DCCDDD28A A AC C L@& @& @& @&FA A A AHg g g gT( ( ( ( (r9   