
    9i.                     V    S r SSKrSSKJr  S rSSSSSS.S jrSS	 jrSS
 jr	SS jr
g)z4Utility functions used in the morphology subpackage.    N)ndimagec                    Uc  Sn[         R                  " U5      (       a  [        R                  " X5      nO6[         R                  " U[
        5      nUR                  U :w  a  [        S5      eUc^  [        UR                   Vs/ s H
  oDS-  S:H  PM     sn5      (       a  [        S5      e[         R                  " UR                  5      S-  nX24$ s  snf )a  Convert any valid connectivity to a footprint and offset.

Parameters
----------
image_dim : int
    The number of dimensions of the input image.
connectivity : int, array, or None
    The neighborhood connectivity. An integer is interpreted as in
    ``scipy.ndimage.generate_binary_structure``, as the maximum number
    of orthogonal steps to reach a neighbor. An array is directly
    interpreted as a footprint and its shape is validated against
    the input image shape. ``None`` is interpreted as a connectivity of 1.
offset : tuple of int, or None
    The coordinates of the center of the footprint.

Returns
-------
c_connectivity : array of bool
    The footprint (structuring element) corresponding to the input
    `connectivity`.
offset : array of int
    The offset corresponding to the center of the footprint.

Raises
------
ValueError:
    If the image dimension and the connectivity or offset dimensions don't
    match.
   z,Connectivity dimension must be same as image   r   z2Connectivity array must have an unambiguous center)
npisscalarndigenerate_binary_structurearrayboolndim
ValueErroranyshape)	image_dimconnectivityoffsetc_connectivityxs        X/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/morphology/_util.py_validate_connectivityr      s    < 	{{<  66yO,5)+KLL~N$8$89$8qA
$89::TUU.../14!! :s   Cr   C)	footprintr   centerspacingorderc                   [        U 5      nUc  [        R                  " XbS9nUc  [        S UR                   5       5      nUR
                  Us=:X  a  [        U5      :X  d  O  [        S5      e[        R                  " [        [        R                  " U5      U5       VVs/ s H	  u  pxXx-
  PM     snnSS9n	US:X  a  U	SS2SSS24   n	U SSS2   n OUS:w  a  [        S	5      eU S
S S-   n
[        R                  " U
SSS2   5      SSS2   n
X-  R                  S
S9nUc  [        R                  " U5      nX-  n[        R                  " [        R                  " US-  S
S95      nU[        R                  " USS9   n[        R                   " USS9n[#        S [        XR                  5       5       5      (       a7  [        R$                  " USS9u  nn[        R                   " USS9nUU   nUU   nUS
S nUS
S nX4$ s  snnf )a  Compute offsets to neighboring pixels in raveled coordinate space.

This function also returns the corresponding distances from the center
pixel given a spacing (assumed to be 1 along each axis by default).

Parameters
----------
image_shape : tuple of int
    The shape of the image for which the offsets are being computed.
footprint : array of bool
    The footprint of the neighborhood, expressed as an n-dimensional array
    of 1s and 0s. If provided, the connectivity argument is ignored.
connectivity : {1, ..., ndim}
    The square connectivity of the neighborhood: the number of orthogonal
    steps allowed to consider a pixel a neighbor. See
    `scipy.ndimage.generate_binary_structure`. Ignored if footprint is
    provided.
center : tuple of int
    Tuple of indices to the center of the footprint. If not provided, it
    is assumed to be the center of the footprint, either provided or
    generated by the connectivity argument.
spacing : tuple of float
    The spacing between pixels/voxels along each axis.
order : 'C' or 'F'
    The ordering of the array, either C or Fortran ordering.

Returns
-------
raveled_offsets : ndarray
    Linear offsets to a samples neighbors in the raveled image, sorted by
    their distance from the center.
distances : ndarray
    The pixel distances corresponding to each offset.

Notes
-----
This function will return values even if `image_shape` contains a dimension
length that is smaller than `footprint`.

Examples
--------
>>> off, d = _raveled_offsets_and_distances(
...         (4, 5), footprint=np.ones((4, 3)), center=(1, 1)
...         )
>>> off
array([-5, -1,  1,  5, -6, -4,  4,  6, 10,  9, 11])
>>> d[0]
1.0
>>> d[-1]  # distance from (1, 1) to (3, 2)
2.236...
N)rankr   c              3   *   #    U  H	  oS -  v   M     g7f)r   N .0ss     r   	<genexpr>1_raveled_offsets_and_distances.<locals>.<genexpr>x   s     7!Av   zQnumber of dimensions in image shape, footprint and itscenter index does not match)axisFr   zorder must be 'C' or 'F'r   r   r   stable)kindc              3   .   #    U  H  u  pX:  v   M     g 7f)Nr    )r"   r   ys      r   r$   r%      s     
?>TQ15>s   T)return_index)lenr	   r
   tupler   r   r   r   stackzipnonzerocumprodsumonessqrtargsortsortr   unique)image_shaper   r   r   r   r   r   idxcoffsetsravel_factorsraveled_offsetsweighted_offsets	distancessorted_raveled_offsetssorted_distances_indicess                     r   _raveled_offsets_and_distancesrH   8   s	   x {D11tW	~7y77>>T0S[0*
 	

 hh!$RZZ	%:F!CD!Cvs#'!CD2G |!TrT'"!$B$'	#344  Od*MJJ}TrT23DbD9M.333;O ''$-(/2;<I,RZZ	-QRwwyx8 
?Soo>
???YY5DI
7'''1!7!@+G4 4AB7'+!33G 	Es   "H
c                      [        XX#S9S   nU$ )aT  Compute offsets to a samples neighbors if the image would be raveled.

Parameters
----------
image_shape : tuple
    The shape of the image for which the offsets are computed.
footprint : ndarray
    The footprint (structuring element) determining the neighborhood
    expressed as an n-D array of 1's and 0's.
center : tuple
    Tuple of indices to the center of `footprint`.
order : {"C", "F"}, optional
    Whether the image described by `image_shape` is in row-major (C-style)
    or column-major (Fortran-style) order.

Returns
-------
raveled_offsets : ndarray
    Linear offsets to a samples neighbors in the raveled image, sorted by
    their distance from the center.

Notes
-----
This function will return values even if `image_shape` contains a dimension
length that is smaller than `footprint`.

Examples
--------
>>> _offsets_to_raveled_neighbors((4, 5), np.ones((4, 3)), (1, 1))
array([-5, -1,  1,  5, -6, -4,  4,  6, 10,  9, 11])
>>> _offsets_to_raveled_neighbors((2, 3, 2), np.ones((3, 3, 3)), (1, 1, 1))
array([-6, -2, -1,  1,  2,  6, -8, -7, -5, -4, -3,  3,  4,  5,  7,  8, -9,
        9])
)r   r   r   r   )rH   )r<   r   r   r   rA   s        r   _offsets_to_raveled_neighborsrJ      s$    F 5	O     c                 l   U c  Uc  Un[         R                  " X!5      n U $ [        R                  " U [        S9n U R
                  U:w  a  [        S5      eU(       a,  [        S U R                   5       5      (       a  [        S5      e[        S U R                   5       5      (       a  [        S5      eU $ )ao  Validate or create a footprint (structuring element).

Depending on the values of `connectivity` and `footprint` this function
either creates a new footprint (`footprint` is None) using `connectivity`
or validates the given footprint (`footprint` is not None).

Parameters
----------
footprint : ndarray
    The footprint (structuring) element used to determine the neighborhood
    of each evaluated pixel (``True`` denotes a connected pixel). It must
    be a boolean array and have the same number of dimensions as `image`.
    If neither `footprint` nor `connectivity` are given, all adjacent
    pixels are considered as part of the neighborhood.
connectivity : int
    A number used to determine the neighborhood of each evaluated pixel.
    Adjacent pixels whose squared distance from the center is less than or
    equal to `connectivity` are considered neighbors. Ignored if
    `footprint` is not None.
ndim : int
    Number of dimensions `footprint` ought to have.
enforce_adjacency : bool
    A boolean that determines whether footprint must only specify direct
    neighbors.

Returns
-------
footprint : ndarray
    Validated or new footprint specifying the neighborhood.

Examples
--------
>>> _resolve_neighborhood(None, 1, 2)
array([[False,  True, False],
       [ True,  True,  True],
       [False,  True, False]])
>>> _resolve_neighborhood(None, None, 3).shape
(3, 3, 3)
)dtypez7number of dimensions in image and footprint do notmatchc              3   *   #    U  H	  oS :g  v   M     g7f)   Nr    r!   s     r   r$   (_resolve_neighborhood.<locals>.<genexpr>  s     $E_!V_r&   z$dimension size in footprint is not 3c              3   0   #    U  H  oS -  S:g  v   M     g7f)r   r   Nr    r!   s     r   r$   rP     s     7!a%1*s   z/footprint size must be odd along all dimensions)	r	   r
   r   asarrayr   r   r   r   r   )r   r   r   enforce_adjacencys       r   _resolve_neighborhoodrT      s    P L11$E	  JJy5	>>T!L  $EY__$E!E!ECDD7y777NOOrK   c                 .   [         R                  " U5      (       a  X"44U R                  -  nO$[        U5      U R                  :w  a  [	        S5      e[        U5       H  u  p4[        U5      S:w  a  [	        S5      eUu  pVXVs=:X  a  S:X  a  O  OM4  XVs=:X  a  S:X  a  O  O[        S5      4U-  S-   S-   nXU'   M^  US:  a$  [        S5      4U-  [        SU5      4-   S-   nXU'   US:  d  M  [        S5      4U-  [        U* S5      4-   S-   nXU'   M     g)	a   Set edge values along all axes to a constant value.

Parameters
----------
image : ndarray
    The array to modify inplace.
value : scalar
    The value to use. Should be compatible with `image`'s dtype.
border_width : int or sequence of tuples
    A sequence with one 2-tuple per axis where the first and second values
    are the width of the border at the start and end of the axis,
    respectively. If an int is provided, a uniform border width along all
    axes is used.

Examples
--------
>>> image = np.zeros((4, 5), dtype=int)
>>> _set_border_values(image, 1)
>>> image
array([[1, 1, 1, 1, 1],
       [1, 0, 0, 0, 1],
       [1, 0, 0, 0, 1],
       [1, 1, 1, 1, 1]])
>>> image = np.zeros((8, 8), dtype=int)
>>> _set_border_values(image, 1, border_width=((1, 1), (2, 3)))
>>> image
array([[1, 1, 1, 1, 1, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1, 1],
       [1, 1, 0, 0, 0, 1, 1, 1],
       [1, 1, 1, 1, 1, 1, 1, 1]])
z.length of `border_width` must match image.ndimr   z2each sequence in `border_width` must have length 2r   r   N))r   r'   ).)r   r   r   r0   r   	enumerateslice)imagevalueborder_widthr(   npadw_startw_endsls           r   _set_border_valuesr_     s   H 
{{<  %46C	\	ejj	(IJJ-
t9>TUU q ""+$&3f<B"IQ;+$&%7*;)==FB"I19+$&%*=)??&HB"I% .rK   )r   )Tr*   )__doc__numpyr   scipyr   r	   r   rH   rJ   rT   r_   r    rK   r   <module>rc      s=    :   ."h 
l4^'T:z:rK   