
    9iHe                     <   S r SSKrSSKrSSKJr  SSKJrJ	r	J
r
  SSKJr  SSKJr  / S	QrS
 rS rS rS r1 Skr\SS\\4SSS.S jj5       r\SS\\4SSS.S jj5       r\SSSS.S jj5       r\SSSS.S jj5       r\SSSS.S jj5       r\SSSS.S jj5       rg)z$
Grayscale morphological operations
    N)ndimage   )_footprint_is_sequencemirror_footprintpad_footprint)default_footprint   )
DEPRECATED)erosiondilationopeningclosingwhite_tophatblack_tophatc           
          US   u  pgU " XX4US9  [        SU5       H  nU " UR                  5       XcXES9  M     USS  H-  u  pg[        U5       H  nU " UR                  5       XcXES9  M     M/     U$ )zHelper to call `gray_func` for each footprint in a sequence.

`gray_func` is a morphology function that accepts `footprint`, `output`,
`mode` and `cval` keyword arguments (e.g. `scipy.ndimage.grey_erosion`).
r   )	footprintoutputmodecvalr   N)rangecopy)		gray_funcimage
footprintsoutr   r   fpnum_iter_s	            W/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/morphology/gray.py_iterate_gray_funcr       sw     a=LBe#tD1h#((*TM  "12xAchhjBQ ! ' J    c                    [         R                  " U 5      n U R                  S:w  a  U $ U R                  u  p4US-  S:X  a^  [         R                  " SU4U R
                  5      nU(       a  [         R                  " X45      n O[         R                  " XP45      n US-  nUS-  S:X  aZ  [         R                  " US4U R
                  5      nU(       a  [         R                  " X45      n U $ [         R                  " X`45      n U $ )a  Shift the binary image `footprint` in the left and/or up.

This only affects 2D footprints with even number of rows
or columns.

Parameters
----------
footprint : 2D array, shape (M, N)
    The input footprint.
shift_x, shift_y : bool or None
    Whether to move `footprint` along each axis. If ``None``, the
    array is not modified along that dimension.

Returns
-------
out : 2D array, shape (M + int(shift_x), N + int(shift_y))
    The shifted footprint.
r	   r   r   )npasarrayndimshapezerosdtypevstackhstack)r   shift_xshift_ymn	extra_row	extra_cols          r   _shift_footprintr1   #   s    & 

9%I~~??DA1uzHHaVY__5			9"89I		9"89I	Q1uzHHaVY__5			9"89I  		9"89Ir!   c                    ^^ T[         L a  T[         L a  U $ Sn[        R                  " U[        SS9  [	        U 5      (       a  [        UU4S jU  5       5      $ [        U TT5      $ )zShifts the footprints, whether it's a single array or a sequence.

See `_shift_footprint`, which is called for each array in the sequence.
zThe parameters `shift_x` and `shift_y` are deprecated since v0.23 and will be removed in v0.26. Use `pad_footprint` or modify the footprintmanually instead.   )
stacklevelc              3   F   >#    U  H  u  p[        UTT5      U4v   M     g 7f)N)r1   ).0r   r.   r+   r,   s      r   	<genexpr>$_shift_footprints.<locals>.<genexpr>[   s"     XiUR&r7G<a@is   !)r
   warningswarnFutureWarningr   tupler1   )r   r+   r,   warning_msgs    `` r   _shift_footprintsr>   K   sa    
 *J!6	 
 MM+};i((XiXXXIw88r!   c                 :   US:X  a  Sn[         R                  " U [        5      (       a  SnX4$ [         R                  " U [         R                  5      (       a#  [         R                  " U 5      R
                  nX4$ [         R                  n X4$ US:X  a  Sn[         R                  " U [        5      (       a  SnX4$ [         R                  " U [         R                  5      (       a#  [         R                  " U 5      R                  nX4$ [         R                  * nX4$ )zDReplace 'max' and 'min' with appropriate 'cval' and 'constant' mode.maxconstantTminF)r#   
issubdtypeboolintegeriinfor@   infrB   )r(   r   r   s      r   _min_max_to_constant_moderH   _   s    u}==%%D : ]]5"**--88E?&&D : 66D : 
==%%D
 :	 ]]5"**--88E?&&D : FF7D:r!   >   r@   rB   wrapignoremirrornearestreflectrA   rM   g        r   r   c          	      8   Uc  [         R                  " U 5      nU[        ;  a  [        SU< 35      eUS:X  a  Sn[	        U R
                  XV5      u  pV[        XU5      n[        USS9n[        U5      (       d  US4/n[        [        R                  U UUUUS9nU$ )a  Return grayscale morphological erosion of an image.

Morphological erosion sets a pixel at (i,j) to the minimum over all pixels
in the neighborhood centered at (i,j). Erosion shrinks bright regions and
enlarges dark regions.

Parameters
----------
image : ndarray
    Image array.
footprint : ndarray or tuple, optional
    The neighborhood expressed as a 2-D array of 1's and 0's.
    If None, use a cross-shaped footprint (connectivity=1). The footprint
    can also be provided as a sequence of smaller footprints as described
    in the notes below.
out : ndarrays, optional
    The array to store the result of the morphology. If None is
    passed, a new array will be allocated.
mode : str, optional
    The `mode` parameter determines how the array borders are handled.
    Valid modes are: 'reflect', 'constant', 'nearest', 'mirror', 'wrap',
    'max', 'min', or 'ignore'.
    If 'max' or 'ignore', pixels outside the image domain are assumed
    to be the maximum for the image's dtype, which causes them to not
    influence the result. Default is 'reflect'.
cval : scalar, optional
    Value to fill past edges of input if `mode` is 'constant'. Default
    is 0.0.

    .. versionadded:: 0.23
        `mode` and `cval` were added in 0.23.

Returns
-------
eroded : array, same shape as `image`
    The result of the morphological erosion.

Other Parameters
----------------
shift_x, shift_y : DEPRECATED

    .. deprecated:: 0.23

Notes
-----
For ``uint8`` (and ``uint16`` up to a certain bit-depth) data, the
lower algorithm complexity makes the :func:`skimage.filters.rank.minimum`
function more efficient for larger images and footprints.

The footprint can also be a provided as a sequence of 2-tuples where the
first element of each 2-tuple is a footprint ndarray and the second element
is an integer describing the number of times it should be iterated. For
example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
computational cost. Most of the builtin footprints such as
:func:`skimage.morphology.disk` provide an option to automatically generate
a footprint sequence of this type.

For even-sized footprints, :func:`skimage.morphology.binary_erosion` and
this function produce an output that differs: one is shifted by one pixel
compared to the other.

Examples
--------
>>> # Erosion shrinks bright regions
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bright_square = np.array([[0, 0, 0, 0, 0],
...                           [0, 1, 1, 1, 0],
...                           [0, 1, 1, 1, 0],
...                           [0, 1, 1, 1, 0],
...                           [0, 0, 0, 0, 0]], dtype=np.uint8)
>>> erosion(bright_square, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

unsupported mode, got rJ   r@   Fpad_endr   r   r   r   r   r   r   )r#   
empty_like_SUPPORTED_MODES
ValueErrorrH   r(   r>   r   r   r    ndigrey_erosionr   r   r   r+   r,   r   r   s          r   r   r      s    x {mmE"##1$:;;x*5;;CJD!)g>Ii7I!),,^$	
""C Jr!   c          	      N   Uc  [         R                  " U 5      nU[        ;  a  [        SU< 35      eUS:X  a  Sn[	        U R
                  XV5      u  pV[        XU5      n[        USS9n[        U5      n[        U5      (       d  US4/n[        [        R                  U UUUUS9nU$ )aN  Return grayscale morphological dilation of an image.

Morphological dilation sets the value of a pixel to the maximum over all
pixel values within a local neighborhood centered about it. The values
where the footprint is 1 define this neighborhood.
Dilation enlarges bright regions and shrinks dark regions.

Parameters
----------
image : ndarray
    Image array.
footprint : ndarray or tuple, optional
    The neighborhood expressed as a 2-D array of 1's and 0's.
    If None, use a cross-shaped footprint (connectivity=1). The footprint
    can also be provided as a sequence of smaller footprints as described
    in the notes below.
out : ndarray, optional
    The array to store the result of the morphology. If None is
    passed, a new array will be allocated.
mode : str, optional
    The `mode` parameter determines how the array borders are handled.
    Valid modes are: 'reflect', 'constant', 'nearest', 'mirror', 'wrap',
    'max', 'min', or 'ignore'.
    If 'min' or 'ignore', pixels outside the image domain are assumed
    to be the maximum for the image's dtype, which causes them to not
    influence the result. Default is 'reflect'.
cval : scalar, optional
    Value to fill past edges of input if `mode` is 'constant'. Default
    is 0.0.

    .. versionadded:: 0.23
        `mode` and `cval` were added in 0.23.

Returns
-------
dilated : uint8 array, same shape and type as `image`
    The result of the morphological dilation.

Other Parameters
----------------
shift_x, shift_y : DEPRECATED

    .. deprecated:: 0.23

Notes
-----
For ``uint8`` (and ``uint16`` up to a certain bit-depth) data, the lower
algorithm complexity makes the :func:`skimage.filters.rank.maximum`
function more efficient for larger images and footprints.

The footprint can also be a provided as a sequence of 2-tuples where the
first element of each 2-tuple is a footprint ndarray and the second element
is an integer describing the number of times it should be iterated. For
example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
computational cost. Most of the builtin footprints such as
:func:`skimage.morphology.disk` provide an option to automatically generate
a footprint sequence of this type.

For non-symmetric footprints, :func:`skimage.morphology.binary_dilation`
and :func:`skimage.morphology.dilation` produce an output that differs:
`binary_dilation` mirrors the footprint, whereas `dilation` does not.

Examples
--------
>>> # Dilation enlarges bright regions
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bright_pixel = np.array([[0, 0, 0, 0, 0],
...                          [0, 0, 0, 0, 0],
...                          [0, 0, 1, 0, 0],
...                          [0, 0, 0, 0, 0],
...                          [0, 0, 0, 0, 0]], dtype=np.uint8)
>>> dilation(bright_pixel, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 1, 1, 1, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

rP   rJ   rB   FrQ   r   rS   )r#   rT   rU   rV   rH   r(   r>   r   r   r   r    rW   grey_dilationrY   s          r   r   r      s    z {mmE"##1$:;;x*5;;CJD!)g>Ii7I !+I!),,^$	
##C Jr!   c                X    [        USS9n[        XX4S9n[        U[        U5      X#US9nU$ )a
  Return grayscale morphological opening of an image.

The morphological opening of an image is defined as an erosion followed by
a dilation. Opening can remove small bright spots (i.e. "salt") and connect
small dark cracks. This tends to "open" up (dark) gaps between (bright)
features.

Parameters
----------
image : ndarray
    Image array.
footprint : ndarray or tuple, optional
    The neighborhood expressed as a 2-D array of 1's and 0's.
    If None, use a cross-shaped footprint (connectivity=1). The footprint
    can also be provided as a sequence of smaller footprints as described
    in the notes below.
out : ndarray, optional
    The array to store the result of the morphology. If None
    is passed, a new array will be allocated.
mode : str, optional
    The `mode` parameter determines how the array borders are handled.
    Valid modes are: 'reflect', 'constant', 'nearest', 'mirror', 'wrap',
    'max', 'min', or 'ignore'.
    If 'ignore', pixels outside the image domain are assumed
    to be the maximum for the image's dtype in the erosion, and minimum
    in the dilation, which causes them to not influence the result.
    Default is 'reflect'.
cval : scalar, optional
    Value to fill past edges of input if `mode` is 'constant'. Default
    is 0.0.

    .. versionadded:: 0.23
        `mode` and `cval` were added in 0.23.

Returns
-------
opening : array, same shape and type as `image`
    The result of the morphological opening.

Notes
-----
The footprint can also be a provided as a sequence of 2-tuples where the
first element of each 2-tuple is a footprint ndarray and the second element
is an integer describing the number of times it should be iterated. For
example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
computational cost. Most of the builtin footprints such as
:func:`skimage.morphology.disk` provide an option to automatically generate
a footprint sequence of this type.

Examples
--------
>>> # Open up gap between two bright regions (but also shrink regions)
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bad_connection = np.array([[1, 0, 0, 0, 1],
...                            [1, 1, 0, 1, 1],
...                            [1, 1, 1, 1, 1],
...                            [1, 1, 0, 1, 1],
...                            [1, 0, 0, 0, 1]], dtype=np.uint8)
>>> opening(bad_connection, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [1, 1, 0, 1, 1],
       [1, 1, 0, 1, 1],
       [1, 1, 0, 1, 1],
       [0, 0, 0, 0, 0]], dtype=uint8)

FrQ   rN   r   r   r   )r   r   r   r   )r   r   r   r   r   erodeds         r   r   r   n  s9    N i7IUD<F
6+I6CQU
VCJr!   c                X    [        USS9n[        XX4S9n[        U[        U5      X#US9nU$ )aS
  Return grayscale morphological closing of an image.

The morphological closing of an image is defined as a dilation followed by
an erosion. Closing can remove small dark spots (i.e. "pepper") and connect
small bright cracks. This tends to "close" up (dark) gaps between (bright)
features.

Parameters
----------
image : ndarray
    Image array.
footprint : ndarray or tuple, optional
    The neighborhood expressed as a 2-D array of 1's and 0's.
    If None, use a cross-shaped footprint (connectivity=1). The footprint
    can also be provided as a sequence of smaller footprints as described
    in the notes below.
out : ndarray, optional
    The array to store the result of the morphology. If None,
    a new array will be allocated.
mode : str, optional
    The `mode` parameter determines how the array borders are handled.
    Valid modes are: 'reflect', 'constant', 'nearest', 'mirror', 'wrap',
    'max', 'min', or 'ignore'.
    If 'ignore', pixels outside the image domain are assumed
    to be the maximum for the image's dtype in the erosion, and minimum
    in the dilation, which causes them to not influence the result.
    Default is 'reflect'.
cval : scalar, optional
    Value to fill past edges of input if `mode` is 'constant'. Default
    is 0.0.

    .. versionadded:: 0.23
        `mode` and `cval` were added in 0.23.

Returns
-------
closing : array, same shape and type as `image`
    The result of the morphological closing.

Notes
-----
The footprint can also be a provided as a sequence of 2-tuples where the
first element of each 2-tuple is a footprint ndarray and the second element
is an integer describing the number of times it should be iterated. For
example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
computational cost. Most of the builtin footprints such as
:func:`skimage.morphology.disk` provide an option to automatically generate
a footprint sequence of this type.

Examples
--------
>>> # Close a gap between two bright lines
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> broken_line = np.array([[0, 0, 0, 0, 0],
...                         [0, 0, 0, 0, 0],
...                         [1, 1, 0, 1, 1],
...                         [0, 0, 0, 0, 0],
...                         [0, 0, 0, 0, 0]], dtype=np.uint8)
>>> closing(broken_line, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

FrQ   rN   r]   )r   r   r   r   )r   r   r   r   r   dilateds         r   r   r     s9    N i7Iud>G
'+I6CQU
VCJr!   c                r   X L aQ  [        XX4S9n[        R                  " UR                  [        5      (       a  [        R
                  " X%US9  U$ X%-  nU$ [        XX#US9n[        R                  " UR                  [        5      (       a  [        R
                  " XUS9  U$ [        R                  " XUS9  U$ )a	  Return white top hat of an image.

The white top hat of an image is defined as the image minus its
morphological opening. This operation returns the bright spots of the image
that are smaller than the footprint.

Parameters
----------
image : ndarray
    Image array.
footprint : ndarray or tuple, optional
    The neighborhood expressed as a 2-D array of 1's and 0's.
    If None, use a cross-shaped footprint (connectivity=1). The footprint
    can also be provided as a sequence of smaller footprints as described
    in the notes below.
out : ndarray, optional
    The array to store the result of the morphology. If None
    is passed, a new array will be allocated.
mode : str, optional
    The `mode` parameter determines how the array borders are handled.
    Valid modes are: 'reflect', 'constant', 'nearest', 'mirror', 'wrap',
    'max', 'min', or 'ignore'. See :func:`skimage.morphology.opening`.
    Default is 'reflect'.
cval : scalar, optional
    Value to fill past edges of input if `mode` is 'constant'. Default
    is 0.0.

    .. versionadded:: 0.23
        `mode` and `cval` were added in 0.23.

Returns
-------
out : array, same shape and type as `image`
    The result of the morphological white top hat.

Notes
-----
The footprint can also be a provided as a sequence of 2-tuples where the
first element of each 2-tuple is a footprint ndarray and the second element
is an integer describing the number of times it should be iterated. For
example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
computational cost. Most of the builtin footprints such as
:func:`skimage.morphology.disk` provide an option to automatically generate
a footprint sequence of this type.

See Also
--------
black_tophat

References
----------
.. [1] https://en.wikipedia.org/wiki/Top-hat_transform

Examples
--------
>>> # Subtract gray background from bright peak
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> bright_on_gray = np.array([[2, 3, 3, 3, 2],
...                            [3, 4, 5, 4, 3],
...                            [3, 5, 9, 5, 3],
...                            [3, 4, 5, 4, 3],
...                            [2, 3, 3, 3, 2]], dtype=np.uint8)
>>> white_tophat(bright_on_gray, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 1, 5, 1, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

rN   r   r]   )r   r#   rC   r(   rD   logical_xorsubtract)r   r   r   r   r   openeds         r   r   r     s    V |@==t,,NN3C0 
 MC
 %T
BC	}}SYY%%
us+ J 	EC(Jr!   c                   X L ab  [        XX4S9n[        R                  " UR                  [        5      (       a  [        R
                  " XRUS9  U$ [        R                  " XRUS9  U$ [        XX#US9n[        R                  " UR                  [        R                  5      (       a  [        R
                  " X US9  U$ X -  nU$ )aY
  Return black top hat of an image.

The black top hat of an image is defined as its morphological closing minus
the original image. This operation returns the dark spots of the image that
are smaller than the footprint. Note that dark spots in the
original image are bright spots after the black top hat.

Parameters
----------
image : ndarray
    Image array.
footprint : ndarray or tuple, optional
    The neighborhood expressed as a 2-D array of 1's and 0's.
    If None, use a cross-shaped footprint (connectivity=1). The footprint
    can also be provided as a sequence of smaller footprints as described
    in the notes below.
out : ndarray, optional
    The array to store the result of the morphology. If None
    is passed, a new array will be allocated.
mode : str, optional
    The `mode` parameter determines how the array borders are handled.
    Valid modes are: 'reflect', 'constant', 'nearest', 'mirror', 'wrap',
    'max', 'min', or 'ignore'. See :func:`skimage.morphology.closing`.
    Default is 'reflect'.
cval : scalar, optional
    Value to fill past edges of input if `mode` is 'constant'. Default
    is 0.0.

    .. versionadded:: 0.23
        `mode` and `cval` were added in 0.23.

Returns
-------
out : array, same shape and type as `image`
    The result of the morphological black top hat.

Notes
-----
The footprint can also be a provided as a sequence of 2-tuples where the
first element of each 2-tuple is a footprint ndarray and the second element
is an integer describing the number of times it should be iterated. For
example ``footprint=[(np.ones((9, 1)), 1), (np.ones((1, 9)), 1)]``
would apply a 9x1 footprint followed by a 1x9 footprint resulting in a net
effect that is the same as ``footprint=np.ones((9, 9))``, but with lower
computational cost. Most of the builtin footprints such as
:func:`skimage.morphology.disk` provide an option to automatically generate
a footprint sequence of this type.

See Also
--------
white_tophat

References
----------
.. [1] https://en.wikipedia.org/wiki/Top-hat_transform

Examples
--------
>>> # Change dark peak to bright peak and subtract background
>>> import numpy as np
>>> from skimage.morphology import footprint_rectangle
>>> dark_on_gray = np.array([[7, 6, 6, 6, 7],
...                          [6, 5, 4, 5, 6],
...                          [6, 4, 0, 4, 6],
...                          [6, 5, 4, 5, 6],
...                          [7, 6, 6, 6, 7]], dtype=np.uint8)
>>> black_tophat(dark_on_gray, footprint_rectangle((3, 3)))
array([[0, 0, 0, 0, 0],
       [0, 0, 1, 0, 0],
       [0, 1, 5, 1, 0],
       [0, 0, 1, 0, 0],
       [0, 0, 0, 0, 0]], dtype=uint8)

rN   rb   r]   )r   r#   rC   r(   rD   rc   rd   bool_)r   r   r   r   r   closeds         r   r   r   e  s    X |@==t,,NN6C0 
 KK-

%T
BC	}}SYY))
ss+ J 	Jr!   )NN)__doc__r9   numpyr#   scipyr   rW   r   r   r   r   miscr   _shared.utilsr
   __all__r    r1   r>   rH   rU   r   r   r   r   r   r    r!   r   <module>rp      s/       O O # & X"%P9(*	   q 
	q qh  u 
	u up IYS I IX IYS I IX Y)# Y Yx Y)# Y Yr!   