
    #
3j                         S SK rS SKJrJr  \" SSSSS9S 5       r\" SSSSS9S 5       r\" S	SSS
9S 5       r      SS jrg)    N)njitprangezvoid(f4[:, :, :], f4[:, :, :])T)cachenogilparallelc           
      8   UR                   u  p#nU R                   u  pVn[        U5       Hm  n[        U5       H[  n[        S[	        US-
  X-  U-  5      5      n	[        S[	        US-
  Xr-  U-  5      5      n
[        U5       H  nXX4   XX4'   M     M]     Mo     g)a  
Internal method.

Resize image src to dst using nearest neighbors filtering.
Images must have multiple color channels, i.e. :code:`len(shape) == 3`.

Parameters
----------
dst: numpy.ndarray of type np.float32
    output image
src: numpy.ndarray of type np.float32
    input image
r      Nshaper   rangemaxmin)dstsrch_srcw_srcdepthh_dstw_dsty_dstx_dstx_srcy_srccs               e/home/wildlama/miniconda3/lib/python3.13/site-packages/pymatting/foreground/estimate_foreground_ml.py_resize_nearest_multichannelr      s     ))E%))E%5\E3uqy%-5*@ABE3uqy%-5*@ABE5\'*%?';5O$ "	 "     zvoid(f4[:, :], f4[:, :])c           
         UR                   u  p#U R                   u  pE[        U5       H[  n[        U5       HI  n[        S[	        US-
  Xs-  U-  5      5      n[        S[	        US-
  Xb-  U-  5      5      n	XU4   XU4'   MK     M]     g)a
  
Internal method.

Resize image src to dst using nearest neighbors filtering.
Images must be grayscale, i.e. :code:`len(shape) == 3`.

Parameters
----------
dst: numpy.ndarray of type np.float32
    output image
src: numpy.ndarray of type np.float32
    input image
r   r	   Nr
   )
r   r   r   r   r   r   r   r   r   r   s
             r   _resize_nearestr       s     99LE99LE5\E3uqy%-5*@ABE3uqy%-5*@ABE #5L 1Cu	 " r   zLTuple((f4[:, :, :], f4[:, :, :]))(f4[:, :, :], f4[:, :], f4, i4, i4, i4, f4))r   r   c                 t   U R                   u  pxn	[        R                  n
SnSn[        R                  " XS9n[        R                  " XS9nSnSn[	        U5       H  n[	        U5       Ho  nUUU4   S:  a*  [	        U	5       H  nUU==   U UUU4   -  ss'   M     US-  nUUU4   S:  d  ME  [	        U	5       H  nUU==   U UUU4   -  ss'   M     US-  nMq     M     XS-   -  nUUS-   -  n[        R                  " XU	4U
S9U-   n[        R                  " XU	4U
S9U-   n[        [        R                  " [        R                  " [        X5      5      5      5      n[	        US-   5       GH  n[        UUU-  -  5      n[        UUU-  -  5      n[        R                  " UUU	4U
S9n[        R                  " UU4U
S9n[        UU 5        [        UU5        [        R                  " UUU	4U
S9n[        R                  " UUU	4U
S9n[        UU5        [        UU5        UU::  a	  UU::  a  UnOUn[        R                  " SU	4U
S9n/ SQn / S	Qn![	        U5       GH  n"[        U5       GH  n[	        U5       GH  nUUU4   n#S
U#-
  n$U#U#-  n%U#U$-  n&U$U$-  n'[	        U	5       H#  nU#UUUU4   -  USU4'   U$UUUU4   -  USU4'   M%     [	        S5       H  n([        S[        US-
  UU U(   -   5      5      n)[        S[        US-
  UU!U(   -   5      5      n*[        U#UU*U)4   -
  5      n+X&U+-  -   n,U%U,-  n%U'U,-  n'[	        U	5       H3  nUSU4==   U,UU*U)U4   -  -  ss'   USU4==   U,UU*U)U4   -  -  ss'   M5     M     U%U'-  U&U&-  -
  n-S
U--  n.U.U'-  n/U.U&* -  n0U.U%-  n1[	        U	5       Hi  nU/USU4   -  U0USU4   -  -   n2U0USU4   -  U1USU4   -  -   n3[        S[        S
U25      5      n2[        S[        S
U35      5      n3U2UUUU4'   U3UUUU4'   Mk     GM     GM     GM     UnUnUnUnGM     WW4$ )Nr	   )dtyper   g?g?h㈵>   )r	   r   r   )r   r   r$   r	         ?   g        )r   npfloat32zerosr   intceillog2r   roundemptyr   r   r   r   abs)4input_imageinput_alpharegularizationn_small_iterationsn_big_iterations
small_sizegradient_weighth0w0r   r!   w_prevh_prevF_mean_colorB_mean_colorF_countB_countyxr   F_prevB_prevn_levelsi_levelwhimagealphaFBn_iterbdxdyi_itera0a1a00a01a11dx2y2gradientdadeterminantinv_detb00b01b11F_cB_cs4                                                       r   _estimate_fb_mlra   =   s     %%MBEJJEFF 88E/L88E/LGG2YrA1a4 3&uA O{1a7';;O &11a4 3&uA O{1a7';;O &1   dN"LGdN"L XXvu-U;lJFXXvu-U;lJF2772773r;/01HA&"8+,-"8+,-!Qe4!Qu-$UK8{+HHaE]%0HHaE]%0$Q/$Q/
?qJ'F%FHHaZu-FmFAYqAq!tBrBr'Cr'Cr'C"5\"$uQ1W~"5!Q$"$uQ1W~"5!Q$ * #1X CAq2a5y$9: CAq2a5y$9:#&rE"b&M'9#:+.HHr	r	!&uAadGrAb"aiL'88GadGrAb"aiL'88G ". & #&)cCi"7K!K/G!C-C!SD.C!C-C"5\!AadGmcAadGm;!AadGmcAadGm;!#s3}5!#s3}5%(!Q'
%(!Q'
 *I "  $` ] '` a4Kr   c           	          [        U R                  [        R                  5      UR                  [        R                  5      UUUUU5      u  pU(       a  X4$ U$ )a  Estimates the foreground of an image given its alpha matte.

See :cite:`germer2020multilevel` for reference.

Parameters
----------
image: numpy.ndarray
    Input image with shape :math:`h \times  w \times d`
alpha: numpy.ndarray
    Input alpha matte shape :math:`h \times  w`
regularization: float
    Regularization strength :math:`\epsilon`, defaults to :math:`10^{-5}`.
    Higher regularization results in smoother colors.
n_small_iterations: int
    Number of iterations performed on small scale, defaults to :math:`10`
n_big_iterations: int
    Number of iterations performed on large scale, defaults to :math:`2`
small_size: int
    Threshold that determines at which size `n_small_iterations` should be used
return_background: bool
    Whether to return the estimated background in addition to the foreground
gradient_weight: float
    Larger values enforce smoother foregrounds, defaults to :math:`1`

Returns
-------
F: numpy.ndarray
    Extracted foreground
B: numpy.ndarray
    Extracted background

Example
-------
>>> from pymatting import *
>>> image = load_image("data/lemur/lemur.png", "RGB")
>>> alpha = load_image("data/lemur/lemur_alpha.png", "GRAY")
>>> F = estimate_foreground_ml(image, alpha, return_background=False)
>>> F, B = estimate_foreground_ml(image, alpha, return_background=True)

See Also
--------
stack_images: This function can be used to place the foreground on a new background.
)ra   astyper'   r(   )
rG   rH   r2   r3   r4   r5   return_backgroundr6   
foreground
backgrounds
             r   estimate_foreground_mlrg      sU    l -RZZ RZZ J %%r   )r"   
   r#       Fr%   )	numpyr'   numbar   r   r   r   ra   rg    r   r   <module>rm      s      &d$N< O<4  D4H2 I28 T\`hlmy ny~ Cr   