
    9i                     H    S SK rSSKJr  SSKJrJr  SSSSS.S	 jrS
 rS r	g)    N   )_supported_float_type   )apply_kernelapply_kernel_nand   F)radiuskernelnansafenum_threadsc                &   [         R                  " U 5      n [        U R                  5      nU R	                  USS9nUc  SnUc  [        XR                  5      nUR	                  U5      n[         R                  " UR                  5      nUS-  nU[        U5         n	X-
  n
[         R                  X[         R                  :H  '   U
R	                  UR                  5      n
U
R                  S5      n
[         R                  " XhSS2[         R                  4   [         R                  SS9nU(       a  [        O[        nU" UR                  S5      U
[         R                  " XR                  S	9R                  S5      [         R                   " U R                  [         R"                  S	9[         R                   " UR                  [         R"                  S	9UR	                  [         R"                  5      U5      nUR	                  U R                  SS9nU$ )
a  Estimate background intensity using the rolling-ball algorithm.

This function is a generalization of the rolling-ball algorithm [1]_ to
estimate the background intensity of an n-dimensional image. This is
typically useful for background subtraction in case of uneven exposure.
Think of the image as a landscape (where altitude is determined by
intensity), under which a ball of given radius is rolled. At each
position, the ball's apex gives the resulting background intensity.

Parameters
----------
image : ndarray
    The image to be filtered.
radius : int, optional
    Radius of the ball-shaped kernel to be rolled under the
    image landscape. Used only if `kernel` is ``None``.
kernel : ndarray, optional
    An alternative way to specify the rolling ball, as an arbitrary
    kernel. It must have the same number of axes as `image`.
nansafe: bool, optional
    If ``False`` (default), the function 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``, the function uses
    the OpenMP default value; typically, it is 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
-----
This implementation assumes that dark pixels correspond to the background. If
you have a bright background, invert the image before passing it to this
function, e.g., using :func:`skimage.util.invert`.

For this method to give meaningful results, the radius of the ball (or
typical size of the kernel, in the general case) should be larger than the
typical size of the image features of interest.

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.

This algorithm's complexity is polynomial in the radius, with degree equal
to the image dimensionality (a 2D image is N^2, a 3D image is N^3, etc.),
so it can take a long time as the radius grows beyond 30 or so ([2]_, [3]_).
It is an exact N-dimensional calculation; if all you need is an
approximation, faster options to consider are top-hat filtering [4]_ or
downscaling-then-upscaling to reduce the size of the input processed.

References
----------
.. [1] Sternberg, Stanley R. "Biomedical image processing." Computer 1
       (1983): 22-34. :DOI:`10.1109/MC.1983.1654163`
.. [2] https://github.com/scikit-image/scikit-image/issues/5193
.. [3] https://github.com/scikit-image/scikit-image/issues/7423
.. [4] https://forum.image.sc/t/59267/7

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

>>> import numpy as np
>>> import skimage as ski
>>> image = ski.data.coins()
>>> kernel = ski.restoration.ellipsoid_kernel((101, 101), 75)
>>> background = ski.restoration.rolling_ball(image, 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                a/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/skimage/restoration/_rolling_ball.pyrolling_ballr,      s   ` JJuE&u{{3J
,,z,
.C~VZZ0]]:&F::fll+L A%MeM23+4-/VV266)*/66syyA/77;
&&1bjj=)266
C  'LDB
e99-55b9
BGG,
"''*BGG$J ""5;;U";J    c                    [         R                  " [         R                  " [         R                  " U 5      /U-   Vs/ s H  n[         R                  " U* US-   5      PM      snSS06SS9n[         R
                  " US-  SS9n[         R                  " U5      n[         R                  " [         R                  " U S-  U-
  SS5      5      n[         R                  XeU :  '   U$ s  snf )	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
r   indexingijr   axisr   r   N)	r   stackmeshgridceilarangesumsqrtclipr   )r	   r   xkernel_coordssum_of_squaresdistance_from_centerr
   s          r+   r   r      s    , HH
-/WWV_,=,DE,DqbiiAE",DE	
PT	
 	M VVM1,26N77>2WWRWWVQY7DABF,.FFF&()M Fs   %C#c                    [         R                  " U 5      n [         R                  " U S-  SS5      n[         R                  " [         R                  " U Vs/ s H  n[         R
                  " U* US-   5      PM      snSS06SS9nS[         R                  " XB-  S-  SS9-
  nU[         R                  " [         R                  " USS5      5      -  n[         R                  XeS:  '   U$ s  snf )	a  Create an ellipoid kernel for restoration.rolling_ball.

Parameters
----------
shape : array-like
    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   Nr/   r0   r   r1   r   )	r   r   r9   r3   r4   r6   r7   r8   r   )r   	intensity	semi_axisr:   r;   intensity_scalingr
   s          r+   ellipsoid_kernelrB      s    . JJuE
At,IHH
I>IqbiiAE*I>NNUWM BFFM$=!#C"MM):At!DEEF$&FFFq !M ?s   %C()
numpyr   _shared.utilsr   _rolling_ball_cyr   r   r,   r   rB    r-   r+   <module>rG      s+     1 < #&dEt up"J"r-   