
    0-Ph)                     p    d Z ddlZddlmZ ddlmZmZ ddlm	Z	m
Z
mZ ddlmZ dddd	d
dZdddddZdS )zflood_fill.py - in place flood fill algorithm

This module provides a function to fill all equal (or within tolerance) values
connected to a given seed point with a different value.
    N   )crop   )_flood_fill_equal_flood_fill_tolerance)_offsets_to_raveled_neighbors_resolve_neighborhood_set_border_values)numeric_dtype_min_maxF)	footprintconnectivity	tolerancein_placec                d    t          | ||||          }|s|                                 } || |<   | S )a  Perform flood filling on an image.

    Starting at a specific `seed_point`, connected points equal or within
    `tolerance` of the seed value are found, then set to `new_value`.

    Parameters
    ----------
    image : ndarray
        An n-dimensional array.
    seed_point : tuple or int
        The point in `image` used as the starting point for the flood fill.  If
        the image is 1D, this point may be given as an integer.
    new_value : `image` type
        New value to set the entire fill.  This must be chosen in agreement
        with the dtype of `image`.
    footprint : ndarray, optional
        The footprint (structuring element) used to determine the neighborhood
        of each evaluated pixel. It must contain only 1's and 0's, have the
        same number of dimensions as `image`. If not given, all adjacent pixels
        are considered as part of the neighborhood (fully connected).
    connectivity : int, optional
        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.
    tolerance : float or int, optional
        If None (default), adjacent values must be strictly equal to the
        value of `image` at `seed_point` to be filled.  This is fastest.
        If a tolerance is provided, adjacent points with values within plus or
        minus tolerance from the seed point are filled (inclusive).
    in_place : bool, optional
        If True, flood filling is applied to `image` in place.  If False, the
        flood filled result is returned without modifying the input `image`
        (default).

    Returns
    -------
    filled : ndarray
        An array with the same shape as `image` is returned, with values in
        areas connected to and equal (or within tolerance of) the seed point
        replaced with `new_value`.

    Notes
    -----
    The conceptual analogy of this operation is the 'paint bucket' tool in many
    raster graphics programs.

    Examples
    --------
    >>> from skimage.morphology import flood_fill
    >>> image = np.zeros((4, 7), dtype=int)
    >>> image[1:3, 1:3] = 1
    >>> image[3, 0] = 1
    >>> image[1:3, 4:6] = 2
    >>> image[3, 6] = 3
    >>> image
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 1, 1, 0, 2, 2, 0],
           [0, 1, 1, 0, 2, 2, 0],
           [1, 0, 0, 0, 0, 0, 3]])

    Fill connected ones with 5, with full connectivity (diagonals included):

    >>> flood_fill(image, (1, 1), 5)
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [5, 0, 0, 0, 0, 0, 3]])

    Fill connected ones with 5, excluding diagonal points (connectivity 1):

    >>> flood_fill(image, (1, 1), 5, connectivity=1)
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [1, 0, 0, 0, 0, 0, 3]])

    Fill with a tolerance:

    >>> flood_fill(image, (0, 0), 5, tolerance=1)
    array([[5, 5, 5, 5, 5, 5, 5],
           [5, 5, 5, 5, 2, 2, 5],
           [5, 5, 5, 5, 2, 2, 5],
           [5, 5, 5, 5, 5, 5, 3]])
    r   r   r   )floodcopy)image
seed_point	new_valuer   r   r   r   masks           ^/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/skimage/morphology/_flood_fill.py
flood_fillr      sM    ~ !  D  

E$KL    r   c          	         t          j        |           } | j        j        du rd}n'| j        j        du rd}nt          j        |           } d}d| j        v r t          j        | j        t                    S 	 t          |           n# t          $ r |f}Y nw xY w| |         }t          t          j        |          | j        z            }t          ||| j        d          }t          d |j        D                       }d	 t          t          j        |          |          D             }t          j        | |d
|                                           }	t          j        d t          ||          D             |	j        |          }
t'          |	j        |||          }t          j        |	j        t           j        |          }t+          |d|           	 |t-          |          }t/          |j                  \  }}t3          |                                |                                |z
            }t#          |                                |                                |z             }t7          |	                    |          |                    |          ||
|||           n9t;          |	                    |          |                    |          ||
|           n3# t          $ r& |	j        t           j        k    rt          d           w xY wt?          ||d                               t                    S )a/  Mask corresponding to a flood fill.

    Starting at a specific `seed_point`, connected points equal or within
    `tolerance` of the seed value are found.

    Parameters
    ----------
    image : ndarray
        An n-dimensional array.
    seed_point : tuple or int
        The point in `image` used as the starting point for the flood fill.  If
        the image is 1D, this point may be given as an integer.
    footprint : ndarray, optional
        The footprint (structuring element) used to determine the neighborhood
        of each evaluated pixel. It must contain only 1's and 0's, have the
        same number of dimensions as `image`. If not given, all adjacent pixels
        are considered as part of the neighborhood (fully connected).
    connectivity : int, optional
        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.
    tolerance : float or int, optional
        If None (default), adjacent values must be strictly equal to the
        initial value of `image` at `seed_point`.  This is fastest.  If a value
        is given, a comparison will be done at every point and if within
        tolerance of the initial value will also be filled (inclusive).

    Returns
    -------
    mask : ndarray
        A Boolean array with the same shape as `image` is returned, with True
        values for areas connected to and equal (or within tolerance of) the
        seed point.  All other values are False.

    Notes
    -----
    The conceptual analogy of this operation is the 'paint bucket' tool in many
    raster graphics programs.  This function returns just the mask
    representing the fill.

    If indices are desired rather than masks for memory reasons, the user can
    simply run `numpy.nonzero` on the result, save the indices, and discard
    this mask.

    Examples
    --------
    >>> from skimage.morphology import flood
    >>> image = np.zeros((4, 7), dtype=int)
    >>> image[1:3, 1:3] = 1
    >>> image[3, 0] = 1
    >>> image[1:3, 4:6] = 2
    >>> image[3, 6] = 3
    >>> image
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 1, 1, 0, 2, 2, 0],
           [0, 1, 1, 0, 2, 2, 0],
           [1, 0, 0, 0, 0, 0, 3]])

    Fill connected ones with 5, with full connectivity (diagonals included):

    >>> mask = flood(image, (1, 1))
    >>> image_flooded = image.copy()
    >>> image_flooded[mask] = 5
    >>> image_flooded
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [5, 0, 0, 0, 0, 0, 3]])

    Fill connected ones with 5, excluding diagonal points (connectivity 1):

    >>> mask = flood(image, (1, 1), connectivity=1)
    >>> image_flooded = image.copy()
    >>> image_flooded[mask] = 5
    >>> image_flooded
    array([[0, 0, 0, 0, 0, 0, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [0, 5, 5, 0, 2, 2, 0],
           [1, 0, 0, 0, 0, 0, 3]])

    Fill with a tolerance:

    >>> mask = flood(image, (0, 0), tolerance=1)
    >>> image_flooded = image.copy()
    >>> image_flooded[mask] = 5
    >>> image_flooded
    array([[5, 5, 5, 5, 5, 5, 5],
           [5, 5, 5, 5, 2, 2, 5],
           [5, 5, 5, 5, 2, 2, 5],
           [5, 5, 5, 5, 5, 5, 3]])
    TFCr   )dtypeF)enforce_adjacencyc              3       K   | ]	}|d z  V  
dS )r   N ).0ss     r   	<genexpr>zflood.<locals>.<genexpr>   s&      33a16333333r   c                 n    g | ]2\  }}t          j        t          j        ||z
                      fd z  3S )r   )npmaxabs)r"   idxcs      r   
<listcomp>zflood.<locals>.<listcomp>   sG       +13sQw	 	 "Q&  r   constant)modeconstant_valuesc                 $    g | ]\  }\  }}||z   S r!   r!   )r"   i	pad_startpad_ends       r   r+   zflood.<locals>.<listcomp>  s&    QQQ212y'YQQQr   )order)centerr3   )r   r3   r   )valueborder_widthNzLdtype of `image` is float16 which is not supported, try upcasting to float32)r   )!r&   asarrayflagsf_contiguousc_contiguousascontiguousarrayshapezerosbooliter	TypeErrortupler	   ndimzipnonzeropadminravel_multi_indexr   uint8r
   r(   r   r   r'   itemr   ravelr   float16r   view)r   r   r   r   r   r3   
seed_valuer4   	pad_widthworking_imageravelled_seed_idxneighbor_offsetsr8   	min_value	max_valuelow_tolhigh_tols                    r   r   r      s   | JuE{4''		!T	)	)$U++ 	EKx40000#Z # # # ]


# z"Jrz*--;<<J%<u  I 339?33333F 58I9N9NPV5W5W  I
 Fyz599;;  M ,QQc*i6P6PQQQ  
 5YvU  
 H](FFFEuAI>>>>" II $99I#J#J Iy)..**JOO,=,=	,IJJG9>>++Z__->->-JKKH!##E**E"" !    ##E**E"" !      "*,,6  
  yu---224888s   7B BBDK( (0L)__doc__numpyr&   utilr   _flood_fill_cyr   r   _utilr   r	   r
   _shared.dtyper   r   r   r!   r   r   <module>r\      s               D D D D D D D D         
 2 1 1 1 1 1 k k k k k\ +/TT u9 u9 u9 u9 u9 u9 u9r   