
    9iu>                     
   S SK Jr  S SKJr  S SKrS SKJr  \R                  " / SQ/ SQ/ SQ/\R                  S9r\R                  " S\R                  S9r/ S	Qr/ S
Qr\R                  " / SQ5      rSS jrSS jrSS jrS rg)    )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                    U S:  R                  [        5      n [        R                  " U SSS9n Uc  U R                  nU R                  S:X  a5  [        R
                  " / SQ/ SQ/ S	Q/5      nUS:X  a  [        nO[        nS
nO`US:X  a  [        S5      e[        R
                  " / SQ/ SQ/ SQ// SQ/ SQ/ S	Q// SQ/ SQ/ SQ//5      nUS:X  a  [        SSS2   nO[        nSn[        R                  " XSSS9n[        R                  " UR                  5       US9nU R                  S:X  a  X6-  $ [        SU-  U-  5      $ )uY  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/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/measure/_regionprops_utils.pyeuler_numberr8     s5   d QYs#EFF5AJ7E zz
 zzQ9i;<1#E#E1%2 
 Iy1Iy1K6
 1$TrT*E$E
 
e*1	=B
BHHJ$/AzzQy55=1$%%    c           	      (   U R                   S:w  a  [        S5      eUS:X  a  [        nO[        nU R	                  [
        R                  5      n [        R                  " XSS9nX-
  n[
        R                  " S[
        R                  S9nSU/ S	Q'   [        S5      US
S/'   S[        S5      -   S-  USS/'   [        R                  " U[
        R                  " / SQ/ SQ/ SQ/5      SSS9n[
        R                  " UR                  5       SS9nXu-  nU$ )a  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     s    H zzQ!"GHHqLL"E%%eCL'L2::601,-"&q'r2h#$tAw;!"3r2hll
+y+67	O ++o&;&;&=L)=Or9   c                 J   U R                   S:w  a  [        S5      eU S:  R                  [        R                  5      n [        R
                  " U SSS9n [        R                  " U [        R                  " / SQ/ SQ/ S	Q/5      SSS
9n[        R                  " UR                  5       SS9nUS:X  at  S[        R                  S-  SSS[        R                  S-  SS[        R                  S-  [        R                  SS[        R                  S-  [        R                  SS/nGOzS[        R                  S-  SS[        R                  " S5      -  -   -  [        R                  S[        R                  " S5      -  -  [        R                  S[        R                  " S5      -  -  S[        R                  S-  SS[        R                  " S5      -  -   -  S[        R                  S[        R                  " S5      -  -  [        R                  S-  [        R                  S-  [        R                  S[        R                  " S5      -  -  [        R                  S[        R                  " S5      -  -  [        R                  S-  [        R                  S-  SS/nXC-  nU$ )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 zzQ!"OPP QYrxx(EFF5AJ7E	rxxIy9:RS
B 	BHHJ"-A QEEAIEEAIEEAIEEEEAIEE!
( EEAIQ"''!*--.EEQ^$EEQ^$EEAIQ"''!*--.EEQ^$EEAIEEAIEEQ^$EEQ^$EEAIEEAI!
& iOr9   c                 x   [         R                  " U 5      n U R                  S:X  a  [         R                  " X4S9n O$U R                  U4:w  a  [	        SU4 SU  S35      e[        S U  5       5      (       d  [        SU  S35      e[        [         R                  " U 5      5      (       d  [	        SU  S35      eU $ )	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   B   #    U  H  n[        U[        5      v   M     g 7fN)
isinstancer   ).0ss     r7   	<genexpr>%_normalize_spacing.<locals>.<genexpr>n  s     4Gqz!T""Gs   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_spacingrm   N  s    2 hhwG}}//':	5(	"=uhZvgYVWX
 	
 4G444B7)1M
 	
 r{{7#$$J7)STU
 	
 Nr9   r`   )r   )mathr   numbersr   numpyr$   scipyr   r,   r'   rJ   rH   onesrI   r(   r)   r+   r8   rV   rZ   rm   r\   r9   r7   <module>rs      s         ((Iy)4BHH
E
''&
) DD88AC L@&FAHgT(r9   