+
    AiK                        ^ RI HtHt ^ RItRR lt]! RRRR7      R 4       t]! R	RRR7      R
 4       t]! RRRR7      R 4       t]! RRRR7      R 4       t	]! RRRR7      R 4       t
]! R	RRR7      R 4       t]! RRRR7      R 4       t]! RRRRR7      R 4       t]! RRRRR7      R 4       t]! RRRRR7      R 4       t]! RRRRR7      R 4       tR# )    )njitpndindexNFc                   V P                   \        P                  \        P                  39   g   Q RV P                    24       hVP                   \        P                  \        P                  39   g   Q RVP                    24       hVP                  V P                  R,          8X  g"   Q RV P                   RVP                   24       h\        V P                  4      ^8X  d   V P                  ^,          ^8X  g   Q RV P                   24       hVf4   ^V	,          ^,           ^,          R\        P                  ,          ,          pVf4   ^V	,          ^,           ^,          R\        P                  ,          ,          pV P                  \        P                  4      p VP                  \        P                  4      pVP                  4       pV^ 8  d   VR8  d   \        VVV VVR	7       WR
8H  ,          P                  ^ R7      pWR8H  ,          P                  ^ R7      p\        P                  ! V 4      V,           p\        P                  ! V 4      V,           p\        P                  ! V4      pVw  pppp\        VVVV VVVVVVVR7       \        P                  ! V 4      p\        P                  ! V 4      p\        P                  ! V4      p\        VVVVVV VVR7       \        P                  ! V 4      p\        P                  ! V 4      p\        P                  ! V4      p\        VVVVVVV VV	V
VVVVR7       V'       d   VVV3# V# )aA  
Estimate alpha from an input image and an input trimap using Shared Matting as proposed by :cite:`GastalOliveira2010SharedMatting`.

Parameters
----------
image: numpy.ndarray
    Image with shape :math:`h \times  w \times d` for which the alpha matte should be estimated
trimap: numpy.ndarray
    Trimap with shape :math:`h \times  w` of the image
return_foreground_background: numpy.ndarray
    Whether to return foreground and background estimate. They will be computed either way
trimap_expansion_radius: int
    How much to expand trimap.
trimap_expansion_threshold: float
    Which pixel colors are similar enough to expand trimap into
sample_gathering_angles: int
    In how many directions to search for new samples.
sample_gathering_weights: Tuple[float, float, float, float]
    Weights for various cost functions
sample_gathering_Np_radius: int
    Radius of Np function
sample_refinement_radius: int
    Search region for better neighboring samples
local_smoothing_radius1: int
    Radius for foreground/background smoothing
local_smoothing_radius2: int
    Radius for confidence computation
local_smoothing_radius3: int
    Radius for low frequency alpha computation
local_smoothing_sigma_sq1: float
    Squared sigma value for foreground/background smoothing
    Defaults to :code:`(2 * local_smoothing_radius1 + 1)**2 / (9 * pi)` if not given
local_smoothing_sigma_sq2: float
    Squared sigma value for confidence computation
local_smoothing_sigma_sq3: float
    Squared sigma value for low frequency alpha computation
    Defaults to :code:`(2 * local_smoothing_radius3 + 1)**2 / (9 * pi)` if not given

Returns
-------
alpha: numpy.ndarray
    Estimated alpha matte
foreground: numpy.ndarray
    Estimated foreground
background: numpy.ndarray
    Estimated background

Example
-------
>>> from pymatting import *
>>> image = load_image("data/lemur/lemur.png", "RGB")
>>> trimap = load_image("data/lemur/lemur_trimap.png", "GRAY")
>>> alpha, foreground, background = estimate_alpha_sm(
...     image,
...     trimap,
...     return_foreground_background=True,
...     sample_gathering_angles=4)
z1image.dtype should be float32 or float64, but is z2trimap.dtype should be float32 or float64, but is N   NzPimage height and width should match trimap height and width, but image shape is z and trimap shape is z"image should be RGB, but shape is g      "@        )k_ik_c      ?axis)
num_angleseNeAefeb	Np_radius)radius)radius1radius2radius3	sigma_sq1	sigma_sq2	sigma_sq3)dtypenpfloat32float64shapelenpiastypecopyexpand_trimapmean
zeros_likesample_gatheringsample_refinementlocal_smoothing)imagetrimapreturn_foreground_backgroundtrimap_expansion_radiustrimap_expansion_thresholdsample_gathering_anglessample_gathering_weightssample_gathering_Np_radiussample_refinement_radiuslocal_smoothing_radius1local_smoothing_radius2local_smoothing_radius3local_smoothing_sigma_sq1local_smoothing_sigma_sq2local_smoothing_sigma_sq3expanded_trimapavg_Favg_Bgathering_Fgathering_Bgathering_alphar   r   r   r   	refined_F	refined_Brefined_alphafinal_Ffinal_Bfinal_alphas   &&&&&&&&&&&&&&&                _/var/www/html/photoedit/myenv/lib/python3.14/site-packages/pymatting/alpha/estimate_alpha_sm.pyestimate_alpha_smrF      s'   X ;;2::rzz22u6ghmhshsgt4uu2<<BJJ

33x7ijpjvjviw5xx3<<5;;r?*  p.~  @E  @K  @K  L  La  bh  bn  bn  ao  -p  p*u{{q U[[^q%8l<^_d_j_j^k:ll8 (%&)@%@1%Dq$HCRTRWRWK$X! (%&)@%@1%Dq$HCRTRWRWK$X! LL$E]]2::&FkkmO"'AC'G'*	, S()..A.6ES()..A.6E--&.K--&.KmmF+O-NBB*,. e$Ie$IMM&)M') mmE"GmmE"G--'K'''+++-  $GW,,    zf4(f4[::1], f4[::1], f4[::1])T)cachenogilc                    V^ ,          V^ ,          ,
          pV^,          V^,          ,
          pV^,          V^,          ,
          pV ^ ,          V^ ,          ,
          pV ^,          V^,          ,
          pV ^,          V^,          ,
          pW3,          WD,          ,           WU,          ,           R,           p	Wc,          Wt,          ,           W,          ,           V	,          p
\        R\        RV
4      4      p
V
# )r   h㈵>r   r   )maxmin)IFBfb0fb1fb2ib0ib1ib2denomalphas   &&&        rE   estimate_alpharY      s    
A$1+C
A$1+C
A$1+C
A$1+C
A$1+C
A$1+CI	!CI-4EY"SY.%7ESe_%ELrG   zf4(f4[::1], f4[::1])c                 x    R p\        \        V 4      4       F  pW V,          W,          ,          ,          pK!  	  V# r   )ranger    )absis   &&  rE   innerra      s0    A3q6]	qTAD[ HrG   c                    \        WV4      pW1^ ,          ,          RV,
          V^ ,          ,          ,           V ^ ,          ,
          pW1^,          ,          RV,
          V^,          ,          ,           V ^,          ,
          pW1^,          ,          RV,
          V^,          ,          ,           V ^,          ,
          pWD,          WU,          ,           Wf,          ,           # )r   r   )rY   )rN   rO   rP   r]   d0d1d2s   &&&    rE   Mp2rf      s    qQA	
qTS1W!$	$qt	+B	
qTS1W!$	$qt	+B	
qTS1W!$	$qt	+B7RWrw&&rG   z/f4(f4[:, :, ::1], i8, i8, f4[::1], f4[::1], i8)c           	      v   V P                   R ,          w  rgRp\        W%,
          W%,           ^,           4       F~  p	\        ^ \        V^,
          V	4      4      p	\        W,
          W,           ^,           4       F;  p
\        ^ \        V^,
          V
4      4      p
V\	        W	V
3,          W44      ,          pK=  	  K  	  V# )r   r   )r   r\   rL   rM   rf   )r*   xyrO   rP   rhwresulty2x2s   &&&&&&     rE   Nprp      s    ;;r?DAFAE1519%CArN#quqy)BQAE2'Bc%B-..F * &
 MrG   z!f4(f4[:, :, ::1], i8, i8, i8, i8)c           	      (   R pW1,
          pWB,
          p\         P                  ! Wg4      pVR 8X  d   R # \        \         P                  ! V4      4      p	\	        ^\        ^
V	4      4      p	Wi,          p
Wy,          pV P                  R,          w  r\        V	^,           4       EFo  p\        V\        V4      V
,          ,           4      p\        V\        V4      V
,          ,           4      p\	        ^ \        V^,
          V^,
          4      4      p\	        ^ \        V^,
          V^,           4      4      p\	        ^ \        V^,
          V^,           4      4      p\	        ^ \        V^,
          V^,
          4      4      p\	        ^ \        V^,
          V4      4      p\	        ^ \        V^,
          V4      4      pRV VV^ 3,          V VV^ 3,          ,
          ,          pRV VV^3,          V VV^3,          ,
          ,          pRV VV^3,          V VV^3,          ,
          ,          pRV VV^ 3,          V VV^ 3,          ,
          ,          pRV VV^3,          V VV^3,          ,
          ,          pRV VV^3,          V VV^3,          ,
          ,          pV
V,          VV,          ,           pV
V,          VV,          ,           pV
V,          VV,          ,           pV\         P                  ! VV,          VV,          ,           VV,          ,           4      ,          pEKr  	  V# )r   r   g      ?)
r   hypotintceilrL   rM   r   r\   floatsqrt)r*   pxpysxsyrm   spxspyd	num_stepsstep_xstep_yrk   rl   r`   qxqyq_lq_rq_uq_dIx0Ix1Ix2Iy0Iy1Iy2v0v1v2s   &&&&&                         rE   Epr      s   F
'C
'C
ACxBGGAJIAs2y)*I_F_F;;r?DA9q=!eAh''(eAh''(!SQQ'(!SQQ'(!SQQ'(!SQQ'(CArN#CArN#U2sA:&r3z)::;U2sA:&r3z)::;U2sA:&r3z)::;U3A:&sBz)::;U3A:&sBz)::;U3A:&sBz)::;c\FSL(c\FSL(c\FSL("''"r'BG+b2g566/ "2 MrG   c                     R p\        V P                  ^ ,          4       F&  pW V,          W,          ,
          ^,          ,          pK(  	  \        P                  ! V4      # r[   )r\   r   r   rv   )r]   r^   re   r`   s   &&  rE   distr     sC    	B1771:
tad{q   772;rG   zf4(f4[::1])c                 @    \         P                  ! \        W 4      4      # )N)r   rv   ra   )r]   s   &rE   lengthr   
  s    775;rG   z3void(f4[:, ::1], f4[:, ::1], f4[:, :, ::1], i8, f4))rH   parallelrI   c                 r   VP                   w  rV\        WV34       EF  w  rxWV3,          ^ 8X  g   WV3,          ^8X  d   K&  \        P                  p	\	        Ws,
          Ws,           ^,           4       EF=  p
\	        W,
          W,           ^,           4       EF  pV^ 8  g   W8  g   V
^ 8  g   W8  d   K   WV3,          ^ 8w  d   WV3,          ^8w  d   K@  W'V^ 3,          W*V^ 3,          ,
          pW'V^3,          W*V^3,          ,
          pW'V^3,          W*V^3,          ,
          p\        P
                  ! W,          W,          ,           W,          ,           4      p\        P                  ! W,
          Wz,
          4      pW8  d   K  VV8  d   K  VV	8  g   EK  Tp	WV3,          WV3&   EK  	  EK@  	  EK  	  R# )r   N)r   r   r   infr\   rv   rr   )r9   r+   r*   r	   r
   rk   rl   ri   rh   closestrn   ro   drdgdbcolor_distancespatial_distances   &&&&&            rE   r$   r$     sc    <<DA! Q$<1!t 18&&1-BAGQWq[16RWQ"'8b&>Q&6b&>Q+>a7^eAI&66a7^eAI&66a7^eAI&66!#27):RW)D!E#%88AFAF#; !'#c)8#g-.G,2r6NOqD)# 2 . !rG   zavoid(f4[:, :, ::1], f4[:, :, ::1], f4[:, ::1], f4[:, :, ::1], f4[:, ::1], i8, f4, f4, f4, f4, i8)c                 p
   VP                   w  r^\        W4      ,          p\        W34       EF  w  r\        P                  ! V^3\        P
                  R7      p\        P                  ! V^3\        P                  R7      p\        P                  ! V^3\        P
                  R7      p\        P                  ! V^3\        P                  R7      pW>V3,          pWNV3,          W.V3&   WNV3,          ^ 8X  d	   VWV3&   K  WNV3,          ^8X  d	   VWV3&   K  ^pVV,          VV,          V,          ,           pVR,          VV,          ,          pR\        P                  ,          VV,          ,          V,          p^ p^ p\        V4       EFX  pR\        P                  ,          V,          V,          V,           p\        P                  ! V4      p\        P                  ! V4      pRpRp\        V4       F  pV'       d   V'       d    K  \        VVV,          ,           4      p \        VVV,          ,           4      p!V ^ 8  g   V!^ 8  g   V V8  g   V!V8  d    K  V'       g8   VV!V 3,          ^8X  d(   VV!V 3,          VV&   V VV^ 3&   V!VV^3&   V^,          pRpV'       d   K  VV!V 3,          ^ 8X  g   K  VV!V 3,          VV&   V VV^ 3&   V!VV^3&   V^,          pRpK  	  EK[  	  V^ 8X  d%   WV3,          VV&   VVV^ 3&   VVV^3&   V^,          pV^ 8X  d%   WV3,          VV&   VVV^ 3&   VVV^3&   V^,          p\        P                  p"\        P                  p#\        V4       F/  p\        W?VVV^ 3,          VV^3,          4      p$\        V"V$4      p"K1  	  \        V4       F/  p%\        W?VVV%^ 3,          VV%^3,          4      p&\        V#V&4      p#K1  	  V#V"V#,           R,           ,          p'\        P                  p(\        V4       EF   p\        V4       EF  p%VV,          p)VV%,          p*\        VV)V*4      p+V'RRV',          ,
          V+,          ,           p,\        P                   ! VVV^ 3,          ,
          VVV^3,          ,
          4      p-\        P                   ! VVV%^ 3,          ,
          VVV%^3,          ,
          4      p.\#        W?VV)V*V
4      V,          V,V,          ,          V-V,          ,          V.V	,          ,          p/V(V/8  g   K  T/p(V+W.V3&   V)WV3&   V*WV3&   EK  	  EK#  	  EK  	  R# )	r   r   i        @FTrK   r   N)r   rL   r   r   zerosr   int32r!   r\   cossinrs   r   r   rM   rY   rr   rp   )0r<   r=   r>   r*   r+   r   r   r   r   r   r   rk   rl   	max_stepsri   rh   
fg_samplesfg_samples_xy
bg_samplesbg_samples_xyC_pnr`   start_anglenum_fg_samplesnum_bg_samplesanglecr_   has_fghas_bgstepro   rn   min_Ep_fmin_Ep_bEp_fjEp_bPF_pmin_costrO   rP   alpha_pApDp_fDp_bg_ps0   &&&&&&&&&&&                                     rE   r'   r'   -  s    <<DACII! XXz1oRZZ@
*aAXXz1oRZZ@
*aAqDk &!t1Q$<1 #K1Q$<1 #K1 Uq1uk!Y1q5!BEEkQU+a/z"A"%%K*,q0;>EuAuAFFi(feTAX&TAX&6R!VrQw"'5&R.A"516r2vJ~.79M.!"3479M.!"34"a'N!Fv&R.A"516r2vJ~.79M.!"3479M.!"34"a'N!F) ) #> Q)4T):J~&/0M.!+,/0M.!+,aNQ)4T):J~&/0M.!+,/0M.!+,aN6666~&Ae=A#6ad8KLD8T*H '
 ~&Ae=A#6ad8KLD8T*H '
 8h.5666 ~&A>*qMqM(a3S3:-88xxM!Q$$7 7]1a4=P9PQxxM!Q$$7 7]1a4=P9PQ uAq)4b8F"H "H  c>"H,3OqD)()K1%()K1%+ + 's !rG   zkvoid(f4[:, :, ::1], f4[:, :, ::1], f4[:, ::1], f4[:, :, ::1], f4[:, :, ::1], f4[:, :, ::1], f4[:, ::1], i8)c                    VP                   w  rW0R &   WAR &   WbR &   \        W34       EF  w  rWZV3,          pWjV3,          ^ 8X  g   WjV3,          ^8X  d   K0  ^p\        P                  ! V^3\        P                  R7      p\        P                  ! V^3\        P                  R7      p\        P                  ! V\        P                  R7      p\        P
                  VR &   \        V) V^,           4       F  p\        V) V^,           4       F  pVV,           pV
V,           p^ Tu;8:  d
   V	8  g   K&  M K*  ^ Tu;8:  d
   V8  g   K;  M K?  VVV3,          pVVV3,          p\        VVV4      p\        P                  ! V4      pVVV,          8  g   K  VVV&   VVV&   VVV&   K  	  K  	  VP                  ^ R7      V,          pVP                  ^ R7      V,          pVW
V3&   VWV3&   \        VVV4      W*V3&   EK  	  R# )NNNr   r   N)r   r   r   r   r   r   r\   rf   argmaxsumrY   )r?   r@   rA   r<   r=   r*   r+   r   rk   rl   ri   rh   r   max_samplessample_Fsample_Bsample_costdydxro   rn   F_qB_qcostr`   F_meanB_means   &&&&&&&&                   rE   r(   r(     s    <<DAaLaL!! qDkQ$<1!t 188[!,BJJ?88[!,BJJ?hh{"**=A!,BVGVaZ0VV;Q;;1;Q;;%b"f-C%b"f-CsC-D		+.Ak!n,)-A&)&) 1 -$ 1%31%3 	Q$ 	Q$,S&&AdI !rG   zvoid(f4[:, :, ::1], f4[:, :, ::1], f4[:, ::1], f4[:, :, ::1], f4[:, :, ::1], f4[:, ::1], f4[:, :, ::1], f4[:, ::1], i8, i8, i8, f4, f4, f4)c                    VP                   w  r\        P                  ! W3\        P                  R 7      p\        P                  ! W3\        P                  R 7      p\        P                  ! W3\        P                  R 7      pW0R&   WAR&   \	        W34       EFj  w  ppVVV3,          pVVV3,          ^ 8X  g   VVV3,          ^8X  d   K4  \        P                  ! ^\        P                  R 7      p\        P                  ! ^\        P                  R 7      pRpRpVVV3,          p\        V) V^,           4       EF{  p\        V) V^,           4       EF^  pVV,           pVV,           p^ Tu;8:  d
   V8  g   K'  M K+  ^ Tu;8:  d
   V8  g   K<  M K@  \        P                  ! RV,          VV,          VV,          ,           ,          4      pVV8w  g   VV8w  d,   V\        VVV3,          VVV3,          ,
          4      ,          pVVV3,          p VV ,          p!VRV ,
          ,          p"\        V!R4      p!\        V"R4      p"VV!,          pVV",          p\        ^4       FO  p#VV#;;,          V!VVVV#3,          ,          ,          uu&   VV#;;,          V"VVVV#3,          ,          ,          uu&   KQ  	  EKa  	  EK~  	  VV,          pVV,          pVV VV3&   VVVV3&   \        VVV4      VVV3&   VRV,
          ,          VVV3&   EKm  	  \	        W34       EF  w  ppVVV3,          pVVV3,          ^ 8X  g   VVV3,          ^8X  d   VVV3,          VVV3&   KD  Rp$Rp%\        V	) V	^,           4       F  p\        V	) V	^,           4       F  pVV,           pVV,           p^ Tu;8:  d
   V8  g   K&  M K*  ^ Tu;8:  d
   V8  g   K;  M K?  V%VVV3,          ,          p%V$VVV3,          \        V VV3,          VVV3,          4      ,          ,          p$K  	  K  	  V$V%R,           ,          p$V$R,          p$\        V VV3,          VVV3,          4      p&V VV3,          pVVV3,          p\        RV&V$,          4      \        P                  ! RV,          \        P                  ! \        VVV4      4      ,          4      ,          VVV3&   EK  	  \	        W34       EF  w  ppVVV3,          ^ 8X  g   VVV3,          ^8X  d   VVV3,          VVV3&   K9  Rp'Rp%\        V
) V
^,           4       F  p\        V
) V
^,           4       F  pVV,           pVV,           p^ Tu;8:  d
   V8  g   K&  M K*  ^ Tu;8:  d
   V8  g   K;  M K?  VV,          VV,          ,           p(VVV3,          ^ 8H  ;'       g    VVV3,          ^8H  p)\        P                  ! RV,          VV,          VV,          ,           ,          4      V),           p*V'V*VVV3,          ,          ,          p'V%V*,          p%K  	  K  	  V'V%,          VVV3&   VVV3,          VVV3,          ,          RVVV3,          ,
          VVV3,          ,          ,           VVV3&   EK  	  R# )r   r   r   r   rK   Ng      )r   r   r   r   r   r\   expabsrL   rY   r   rM   rv   rf   )+rB   rC   rD   r?   r@   rA   r*   r+   r   r   r   r   r   r   rk   rl   final_confidenceW_FBlow_frequency_alphari   rh   r   F_pB_psum_Fsum_Br   r   r   ro   rn   Wc_pqalpha_qW_FW_Br   D_FB
weight_sumFB_dist	alpha_sumD_image_squaredis_knownW_alphas+   &&&&&&&&&&&&&&                             rE   r)   r)     s   " <<DAxxbjj988QF"**-D((A6<AJAJ! 1AqDk!Q$<1q!t 1hhq

+hhq

+1%'A+.BWHgk2VV;Q;;1;Q;;FF4)#3rBwb7H#IJEBw!r']1a4%8=R;P%P!QQ+BF3G'/C3=1Cc4.Cc4.CSLESLE"1XA#	"b!)(<"<<A#	"b!)(<"<< &- 3 /6 	uu11*3S9AqD g.QT
e !h ! 1AqDk!Q$<1q!t 1%+AqD\QT"
'A+.BWHgk2VV;Q;;1;Q;;$r2v,.JDRL4BR+QQQD 3 / 	
T!!wq!t}gadm4admadm!$S'D.!9BFF4)CSVXV]V]^abegjlo^pVqCq<r!rA3 !6 ! 1!Q$<1q!t 1 &q!tK1	
'A+.BWHgk2VV;Q;;1;Q;;&(2gR&7O%b"f~2IIfRVn6IH ffTI%5b279J%KLxWG=R+@!@@I')J 3 / %.
$:AqD!,QT2[A5FF#P`abdeaePfJfj}~  BC  C  kD  JD  DAqD+ !rG   )F
   g{Gz?   )g      @r   r   g      @      r   r   r   Ng?N)numbar   r   numpyr   rF   rY   ra   rf   rp   r   r   r   r$   r'   r(   r)    rG   rE   <module>r      s     aF %T> ?" D5 6 %T>' ?' 7t4P Q )TB+ C+Z D5 6 m4t,  -  ;4RV^bc; d;< iqu  AE  MQ  R@* R@*D s{  KP  X\  ]4B ]4Bl   T  \`  ko  w{  |}D |}DrG   