
    a6d                     H    d dl ZddlmZ ddlmZmZ dddddd	Zd
 Zd Z	dS )    N   )_supported_float_type   )apply_kernelapply_kernel_nand   F)radiuskernelnansafenum_threadsc          
         t          j        |           } t          | j                  }|                     |d          }|d}|t          || j                  }|                    |          }t          j        |j                  }|dz  }|t          |                   }	|	|z
  }
t           j	        |
|t           j	        k    <   |
                    |j                  }
|

                    d          }
t          j        ||ddt           j        f         t           j	        d          }|rt          nt          } ||
                    d          |
t          j        | |j        	          
                    d          t          j        | j        t           j        	          t          j        |j        t           j        	          |                    t           j                  |          }|                    | j        d          }|S )
a`  Estimate background intensity by rolling/translating a kernel.

    This rolling ball algorithm estimates background intensity for a
    ndimage in case of uneven exposure. It is a generalization of the
    frequently used rolling ball algorithm [1]_.

    Parameters
    ----------
    image : ndarray
        The image to be filtered.
    radius : int, optional
        Radius of a ball shaped kernel to be rolled/translated in the image.
        Used if ``kernel = None``.
    kernel : ndarray, optional
        The kernel to be rolled/translated in the image. It must have the
        same number of dimensions as ``image``. Kernel is filled with the
        intensity of the kernel at that position.
    nansafe: bool, optional
        If ``False`` (default) assumes that none of the values in ``image``
        are ``np.nan``, and uses a faster implementation.
    num_threads: int, optional
        The maximum number of threads to use. If ``None`` use the OpenMP
        default value; typically equal to the maximum number of virtual cores.
        Note: This is an upper limit to the number of threads. The exact number
        is determined by the system's OpenMP library.

    Returns
    -------
    background : ndarray
        The estimated background of the image.

    Notes
    -----
    For the pixel that has its background intensity estimated (without loss
    of generality at ``center``) the rolling ball method centers ``kernel``
    under it and raises the kernel until the surface touches the image umbra
    at some ``pos=(y,x)``. The background intensity is then estimated
    using the image intensity at that position (``image[pos]``) plus the
    difference of ``kernel[center] - kernel[pos]``.

    This algorithm assumes that dark pixels correspond to the background. If
    you have a bright background, invert the image before passing it to the
    function, e.g., using `utils.invert`. See the gallery example for details.

    This algorithm is sensitive to noise (in particular salt-and-pepper
    noise). If this is a problem in your image, you can apply mild
    gaussian smoothing before passing the image to this function.

    References
    ----------
    .. [1] Sternberg, Stanley R. "Biomedical image processing." Computer 1
           (1983): 22-34. :DOI:`10.1109/MC.1983.1654163`

    Examples
    --------
    >>> import numpy as np
    >>> from skimage import data
    >>> from skimage.restoration import rolling_ball
    >>> image = data.coins()
    >>> background = rolling_ball(data.coins())
    >>> filtered_image = image - background


    >>> import numpy as np
    >>> from skimage import data
    >>> from skimage.restoration import rolling_ball, ellipsoid_kernel
    >>> image = data.coins()
    >>> kernel = ellipsoid_kernel((101, 101), 75)
    >>> background = rolling_ball(data.coins(), kernel=kernel)
    >>> filtered_image = image - background
    F)copyNr   r   constant)constant_valuesmode)dtype)npasarrayr   r   astypeball_kernelndimshapetupleInfreshapepadnewaxisr   r   
zeros_likearrayintp)imager	   r
   r   r   
float_typeimgkernel_shapekernel_centercenter_intensityintensity_differencefunc
backgrounds                @lib/python3.11/site-packages/skimage/restoration/rolling_ball.pyrolling_ballr,      s   T JuE&u{33J
,,z,
.
.C~VUZ00]]:&&F:fl++L!Q&MeM223+f4-/V26)*/66syAA/77;;
&mAAArzM2!#j: : :C  '8LDB
e39---55b99
BG,,,
"'***BG$$ J ""5;U";;J    c                 l   t          j        t          j        d t          j        |           g|z  D             ddid          }t          j        |dz  d          }t          j        |          }t          j        t          j        | dz  |z
  dd                    }t           j        ||| k    <   |S )	a  Create a ball kernel for restoration.rolling_ball.

    Parameters
    ----------
    radius : int
        Radius of the ball.
    ndim : int
        Number of dimensions of the ball. ``ndim`` should match the
        dimensionality of the image the kernel will be applied to.

    Returns
    -------
    kernel : ndarray
        The kernel containing the surface intensity of the top half
        of the ellipsoid.

    See Also
    --------
    rolling_ball
    c                 @    g | ]}t          j        | |d z             S r   r   arange.0xs     r+   
<listcomp>zball_kernel.<locals>.<listcomp>   s*    EEEqbiAE""EEEr-   indexingijr   axisr   r   N)r   stackmeshgridceilsumsqrtclipr   )r	   r   kernel_coordssum_of_squaresdistance_from_centerr
   s         r+   r   r   x   s    , H
EERWV__,=,DEEE	
	
 	
   M VMQ.R888N7>22WRWVq[>91dCCDDF,.FF&()Mr-   c                 x   t          j        |           } t          j        | dz  dd          }t          j        t          j        d |D             ddid          }dt          j        ||z  dz  d          z
  }|t          j        t          j        |d	d                    z  }t           j        ||d	k     <   |S )
a/  Create an ellipoid kernel for restoration.rolling_ball.

    Parameters
    ----------
    shape : arraylike
        Length of the principal axis of the ellipsoid (excluding
        the intensity axis). The kernel needs to have the same
        dimensionality as the image it will be applied to.
    intensity : int
        Length of the intensity axis of the ellipsoid.

    Returns
    -------
    kernel : ndarray
        The kernel containing the surface intensity of the top half
        of the ellipsoid.

    See Also
    --------
    rolling_ball
    r   r   Nc                 @    g | ]}t          j        | |d z             S r0   r1   r3   s     r+   r6   z$ellipsoid_kernel.<locals>.<listcomp>   s*    666qbiAE""666r-   r7   r8   r   r9   r   )r   r   r@   r;   r<   r>   r?   r   )r   	intensity	semi_axisrA   intensity_scalingr
   s         r+   ellipsoid_kernelrI      s    . JuE
At,,IH
66I666	
	
 	
   M BFMI$=!#C"MMMM):At!D!DEEEF$&FFq !Mr-   )
numpyr   _shared.utilsr   _rolling_ball_cyr   r   r,   r   rI    r-   r+   <module>rN      s        1 1 1 1 1 1 < < < < < < < < #&dDn n n n nb# # #L% % % % %r-   