
    9i":                        S SK Jr  S SKrS SKJr  SSKJr  / SQr	 " S S5      r
\R                  " S	5      \R                  " / S
Q/S	-  5      \R                  " \R                  " S	5      5      \R                  " / S
Q/S	-  5      /r\" S5       V s/ s H  n \R#                  S5      PM     sn rS\S    SS2SS2S4'   S\S   SS2SSS24'   S\S   SSS2SS24'   S\S	   SS2/ SQ/ SQ4'   S\S   SS2/ SQ/ SQ4'   S\S   / SQSS2/ SQ4'   S\S   / SQSS2/ SQ4'   S\S   / SQ/ SQSS24'   S\S   / SQ/ SQSS24'   S rS r\
" S S /5      rS rS rSSS.S jrS&S jrS'S jrSSSSS  4S! jrS"SS#S S$ 4S% jrgs  sn f )(    )cycleN)ndimage   )check_nD)morphological_chan_vese%morphological_geodesic_active_contourinverse_gaussian_gradientdisk_level_setcheckerboard_level_setc                        \ rS rSrS rS rSrg)_fcycle   c                 $    [        U5      U l        g)z8Call functions from the iterable each time it is called.N)r   funcs)selfiterables     `/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/segmentation/morphsnakes.py__init___fcycle.__init__   s    8_
    c                 <    [        U R                  5      nU" U0 UD6$ N)nextr   )r   argskwargsfs       r   __call___fcycle.__call__   s     $!&!!r   )r   N)__name__
__module____qualname____firstlineno__r   r   __static_attributes__ r   r   r   r      s    %"r   r      )r      r   	   )r%   r%   r%   r&   )r   r&   r      )r   r&   r               c                 ~   [         R                  " U 5      S:X  a  [        nO,[         R                  " U 5      S:X  a  [        nO[	        S5      e/ nU HE  nUR                  [        R                  " X5      R                  [         R                  5      5        MG     [         R                  " USS9R                  S5      $ )zSI operator.r   r%   8u has an invalid number of dimensions (should be 2 or 3)r   axis)npndim_P2_P3
ValueErrorappendndibinary_erosionastypeint8stackmax)uPerosionsP_is       r   sup_infrA   /   s     
wwqzQ	qVWWH**1299"''BC  88H1%))!,,r   c                 ~   [         R                  " U 5      S:X  a  [        nO,[         R                  " U 5      S:X  a  [        nO[	        S5      e/ nU HE  nUR                  [        R                  " X5      R                  [         R                  5      5        MG     [         R                  " USS9R                  S5      $ )zIS operator.r   r%   r.   r   r/   )r1   r2   r3   r4   r5   r6   r7   binary_dilationr9   r:   r;   min)r=   r>   	dilationsr@   s       r   inf_suprF   @   s     
wwqzQ	qVWWI,,Q4;;BGGDE  88IA&**1--r   c                 *    [        [        U 5      5      $ r   )rA   rF   r=   s    r   <lambda>rI   R   s    wwqz"r   c                 *    [        [        U 5      5      $ r   )rF   rA   rH   s    r   rI   rI   R   s    ggaj.Ar   c                     [        U SS/5        [        U R                  5      [        UR                  5      :w  a  [        S5      eg)z8Check that shapes of `image` and `init_level_set` match.r   r%   zQThe dimensions of the initial level set do not match the dimensions of the image.N)r   lenshaper5   )imageinit_level_sets     r   _check_inputrP   V   sC    UQF
5;;3~33441
 	
 5r   c                     [        U [        5      (       a1  U S:X  a  [        U5      nU$ U S:X  a  [        U5      nU$ [	        S5      eU nU$ )zzAuxiliary function for initializing level sets with a string.

If `init_level_set` is not a string, it is returned as is.
checkerboarddiskz0`init_level_set` not in ['checkerboard', 'disk'])
isinstancestrr   r
   r5   )rO   image_shaperess      r   _init_level_setrX   a   s]    
 .#&&^+(5C J v% -C
 J RSSJr   )centerradiusc                   Uc  [        S U  5       5      nUc  [        U 5      S-  S-  n[        R                  U  Vs/ s H  n[	        U5      PM     sn   nUR
                  U-
  R
                  nU[        R                  " [        R                  " US-  S5      5      -
  n[        R                  " US:  5      nU$ s  snf )aM  Create a disk level set with binary values.

Parameters
----------
image_shape : tuple of positive integers
    Shape of the image
center : tuple of positive integers, optional
    Coordinates of the center of the disk given in (row, column). If not
    given, it defaults to the center of the image.
radius : float, optional
    Radius of the disk. If not given, it is set to the 75% of the
    smallest image dimension.

Returns
-------
out : array with shape `image_shape`
    Binary level set of the disk with the given `radius` and `center`.

See Also
--------
checkerboard_level_set
c              3   *   #    U  H	  oS -  v   M     g7f)r   Nr$   ).0is     r   	<genexpr>!disk_level_set.<locals>.<genexpr>   s     3{!Av{s   g      @g       @r   r   )	tuplerD   r1   mgridsliceTsqrtsumr:   )rV   rY   rZ   r^   gridphirW   s          r   r
   r
   r   s    0 ~3{33~[!C'#-88{3{!U1X{34DFFVOD
2772664A+q12
2C
''#'
CJ	 4s   B<c                     [         R                  U  Vs/ s H  n[        U5      PM     sn   nX1-  nUS-  n[         R                  R	                  USS9n[         R
                  " U5      nU$ s  snf )ai  Create a checkerboard level set with binary values.

Parameters
----------
image_shape : tuple of positive integers
    Shape of the image.
square_size : int, optional
    Size of the squares of the checkerboard. It defaults to 5.

Returns
-------
out : array with shape `image_shape`
    Binary level set of the checkerboard.

See Also
--------
disk_level_set
r&   r   r/   )r1   rb   rc   bitwise_xorreducer:   )rV   square_sizer^   rg   rR   rW   s         r   r   r      si    ( 88{3{!U1X{34DD !8D>>((A(6L
'',
CJ 4s   A-c                 h    [         R                  " XSS9nS[        R                  " SX-  -   5      -  $ )a  Inverse of gradient magnitude.

Compute the magnitude of the gradients in the image and then inverts the
result in the range [0, 1]. Flat areas are assigned values close to 1,
while areas close to borders are assigned values close to 0.

This function or a similar one defined by the user should be applied over
the image as a preprocessing step before calling
`morphological_geodesic_active_contour`.

Parameters
----------
image : (M, N) or (L, M, N) array
    Grayscale image or volume.
alpha : float, optional
    Controls the steepness of the inversion. A larger value will make the
    transition between the flat areas and border areas steeper in the
    resulting array.
sigma : float, optional
    Standard deviation of the Gaussian filter applied over the image.

Returns
-------
gimage : (M, N) or (L, M, N) array
    Preprocessed image (or volume) suitable for
    `morphological_geodesic_active_contour`.
nearest)modeg      ?)r7   gaussian_gradient_magnituder1   re   )rN   alphasigmagradnorms       r   r	   r	      s2    8 ..u)LHu//000r   rR   c                     g r   r$   xs    r   rI   rI          Dr   c                    [        X R                  5      n[        X5        [        R                  " US:  5      nU" U5        [        U5       H  nU SU-
  -  R                  5       [        SU-
  R                  5       S-   5      -  n	X-  R                  5       [        UR                  5       S-   5      -  n
[        R                  " U5      n[        R                  " U5      R                  S5      nXX
-
  S-  -  XPU	-
  S-  -  -
  -  nSX}S:  '   SX}S:  '   [        U5       H  n[        U5      nM     U" U5        M     U$ )uD  Morphological Active Contours without Edges (MorphACWE)

Active contours without edges implemented with morphological operators. It
can be used to segment objects in images and volumes without well defined
borders. It is required that the inside of the object looks different on
average than the outside (i.e., the inner area of the object should be
darker or lighter than the outer area on average).

Parameters
----------
image : (M, N) or (L, M, N) array
    Grayscale image or volume to be segmented.
num_iter : uint
    Number of num_iter to run
init_level_set : str, (M, N) array, or (L, M, N) array
    Initial level set. If an array is given, it will be binarized and used
    as the initial level set. If a string is given, it defines the method
    to generate a reasonable initial level set with the shape of the
    `image`. Accepted values are 'checkerboard' and 'disk'. See the
    documentation of `checkerboard_level_set` and `disk_level_set`
    respectively for details about how these level sets are created.
smoothing : uint, optional
    Number of times the smoothing operator is applied per iteration.
    Reasonable values are around 1-4. Larger values lead to smoother
    segmentations.
lambda1 : float, optional
    Weight parameter for the outer region. If `lambda1` is larger than
    `lambda2`, the outer region will contain a larger range of values than
    the inner region.
lambda2 : float, optional
    Weight parameter for the inner region. If `lambda2` is larger than
    `lambda1`, the inner region will contain a larger range of values than
    the outer region.
iter_callback : function, optional
    If given, this function is called once per iteration with the current
    level set as the only argument. This is useful for debugging or for
    plotting intermediate results during the evolution.

Returns
-------
out : (M, N) or (L, M, N) array
    Final segmentation (i.e., the final level set)

See Also
--------
disk_level_set, checkerboard_level_set

Notes
-----
This is a version of the Chan-Vese algorithm that uses morphological
operators instead of solving a partial differential equation (PDE) for the
evolution of the contour. The set of morphological operators used in this
algorithm are proved to be infinitesimally equivalent to the Chan-Vese PDE
(see [1]_). However, morphological operators are do not suffer from the
numerical stability issues typically found in PDEs (it is not necessary to
find the right time step for the evolution), and are computationally
faster.

The algorithm and its theoretical derivation are described in [1]_.

References
----------
.. [1] A Morphological Approach to Curvature-based Evolution of Curves and
       Surfaces, Pablo Márquez-Neila, Luis Baumela, Luis Álvarez. In IEEE
       Transactions on Pattern Analysis and Machine Intelligence (PAMI),
       2014, :DOI:`10.1109/TPAMI.2013.106`
r   r&   g:0yE>r   )rX   rM   rP   r1   r:   rangerf   floatgradientabs_curvop)rN   num_iterrO   	smoothinglambda1lambda2iter_callbackr=   _c0c1duabs_duauxs                 r   r   r      s#   Z %^[[AN'
"#A!8_ q1uo""$ua!e[[]T-A'BBi__quuw~!66 [[^"5:!"33gPQ@Q6QQR'
'
 y!A
A " 	a% ( Hr   rS   autoc                     g r   r$   ru   s    r   rI   rI   I  rw   r   c                 L   U n[        X'R                  5      n[        Xr5        US:X  a  [        R                  " US5      n[        R
                  " S[        UR                  5      -  [        R                  S9n[        R                  " U5      n	US:w  a  Xt[        R                  " U5      -  :  n
[        R                  " US:  5      nU" U5        [        U5       H  nUS:  a  [        R                  " X5      nOUS:  a  [        R                  " X5      nUS:w  a  WW
   X'   [        R                  " U5      n[        R                  " U5      n[        X5       H  u  nnXU-  -  nM     SXS:  '   SXS:  '   [        U5       H  n[!        U5      nM     U" U5        M     U$ )u  Morphological Geodesic Active Contours (MorphGAC).

Geodesic active contours implemented with morphological operators. It can
be used to segment objects with visible but noisy, cluttered, broken
borders.

Parameters
----------
gimage : (M, N) or (L, M, N) array
    Preprocessed image or volume to be segmented. This is very rarely the
    original image. Instead, this is usually a preprocessed version of the
    original image that enhances and highlights the borders (or other
    structures) of the object to segment.
    :func:`morphological_geodesic_active_contour` will try to stop the contour
    evolution in areas where `gimage` is small. See
    :func:`inverse_gaussian_gradient` as an example function to
    perform this preprocessing. Note that the quality of
    :func:`morphological_geodesic_active_contour` might greatly depend on this
    preprocessing.
num_iter : uint
    Number of num_iter to run.
init_level_set : str, (M, N) array, or (L, M, N) array
    Initial level set. If an array is given, it will be binarized and used
    as the initial level set. If a string is given, it defines the method
    to generate a reasonable initial level set with the shape of the
    `image`. Accepted values are 'checkerboard' and 'disk'. See the
    documentation of `checkerboard_level_set` and `disk_level_set`
    respectively for details about how these level sets are created.
smoothing : uint, optional
    Number of times the smoothing operator is applied per iteration.
    Reasonable values are around 1-4. Larger values lead to smoother
    segmentations.
threshold : float, optional
    Areas of the image with a value smaller than this threshold will be
    considered borders. The evolution of the contour will stop in these
    areas.
balloon : float, optional
    Balloon force to guide the contour in non-informative areas of the
    image, i.e., areas where the gradient of the image is too small to push
    the contour towards a border. A negative value will shrink the contour,
    while a positive value will expand the contour in these areas. Setting
    this to zero will disable the balloon force.
iter_callback : function, optional
    If given, this function is called once per iteration with the current
    level set as the only argument. This is useful for debugging or for
    plotting intermediate results during the evolution.

Returns
-------
out : (M, N) or (L, M, N) array
    Final segmentation (i.e., the final level set)

See Also
--------
inverse_gaussian_gradient, disk_level_set, checkerboard_level_set

Notes
-----
This is a version of the Geodesic Active Contours (GAC) algorithm that uses
morphological operators instead of solving partial differential equations
(PDEs) for the evolution of the contour. The set of morphological operators
used in this algorithm are proved to be infinitesimally equivalent to the
GAC PDEs (see [1]_). However, morphological operators are do not suffer
from the numerical stability issues typically found in PDEs (e.g., it is
not necessary to find the right time step for the evolution), and are
computationally faster.

The algorithm and its theoretical derivation are described in [1]_.

References
----------
.. [1] A Morphological Approach to Curvature-based Evolution of Curves and
       Surfaces, Pablo Márquez-Neila, Luis Baumela, Luis Álvarez. In IEEE
       Transactions on Pattern Analysis and Machine Intelligence (PAMI),
       2014, :DOI:`10.1109/TPAMI.2013.106`
r   (   )r%   )dtyper   r&   )rX   rM   rP   r1   
percentileonesrL   r:   r{   r|   ry   r7   rC   r8   
zeros_likezipr}   )gimager~   rO   r   	thresholdballoonr   rN   	structuredimagethreshold_mask_balloonr=   r   r   r   el1el2s                    r   r   r   B  sm   l E$^[[AN'FMM%,	s5;;//rww?I[[F!|!&RVVG_)D!D
"#A!8_Q;%%a3Cq[$$Q2Ca<(+,B(CA% mmE"[[^FHC9C ('
'
 y!A
A " 	a+ . Hr   )r)   )g      Y@g      @)	itertoolsr   numpyr1   scipyr   r7   _shared.utilsr   __all__r   eyearrayflipudrot90r3   ry   zerosr4   rA   rF   r}   rP   rX   r
   r   r	   r   r   )r^   s   0r   <module>r      s       $" " FF1IHHi[1_IIbffQiHHi[1_	 %*!H-Hqrxx	H-Aq!QwAq!QwAq!Qw"#Aq)Y "#Aq)Y "#Ay!Y "#Ay!Y "#Ay)Q "#Ay)Q -"." "$AB

" +/t "J>1F " i^  A	 .s   F