
    3j(                        S SK JrJr  S SKr SS\R                  S\R                  S\R                  S\S\\R                  \R                  4   4
S jjrSS	\R                  S
\R                  S\S\R                  4S jjrSS\R                  S\R                  S\R                  S\S\R                  4
S jjr SS\R                  S\R                  S\R                  S\R                  S\S\R                  4S jjr	        SS\R                  S\S\S\
S\\   S\\R                     S\S\S\S\\R                  \R                  4   4S jjrg)    )OptionalTupleNvaluesbinssigmaepsilonreturnc                 l   [        U [        R                  5      (       d  [        S[	        U 5       35      e[        U[        R                  5      (       d  [        S[	        U5       35      e[        U[        R                  5      (       d  [        S[	        U5       35      eU R                  5       S:X  d  [        SU R                   35      eUR                  5       S:X  d  [        SUR                   35      eUR                  5       S:X  d  [        S	UR                   35      eXR                  S5      R                  S5      -
  n[        R                  " S
XB-  R                  S5      -  5      n[        R                  " USS9n[        R                  " USS9R                  S5      U-   nXg-  nXe4$ )a  Calculate the marginal probability distribution function of the input based on the number of histogram bins.

Args:
    values: shape [BxNx1].
    bins: shape [NUM_BINS].
    sigma: shape [1], gaussian smoothing factor.
    epsilon: scalar, for numerical stability.

Returns:
    Tuple[torch.Tensor, torch.Tensor]:
      - torch.Tensor: shape [BxN].
      - torch.Tensor: shape [BxNxNUM_BINS].

z-Input values type is not a torch.Tensor. Got z+Input bins type is not a torch.Tensor. Got z,Input sigma type is not a torch.Tensor. Got    z/Input values must be a of the shape BxNx1. Got    z0Input bins must be a of the shape NUM_BINS. Got r   z*Input sigma must be a of the shape 1. Got          dim)
isinstancetorchTensor	TypeErrortyper   
ValueErrorshape	unsqueezeexppowmeansum)r   r   r   r   	residualskernel_valuespdfnormalizations           R/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/enhance/histogram.pymarginal_pdfr"      sh   " fell++GV~VWWdELL))Ed4j\RSSeU\\**FtE{mTUU::<1J6<<.YZZ88:?KDJJ<XYY99;!Eekk]STT*44Q77IIIdi&7%<%<Q%??@M
**]
*CIIcq)33A6@M

C    kernel_values1kernel_values2c                    [        U [        R                  5      (       d  [        S[	        U 5       35      e[        U[        R                  5      (       d  [        S[	        U5       35      eU R                  5       S:X  d  [        SU R                   35      eUR                  5       S:X  d  [        SUR                   35      eU R                  UR                  :w  a%  [        SU R                   SUR                   35      e[        R                  " U R                  SS	5      U5      n[        R                  " US
S9R                  SSS5      U-   nX4-  nU$ )a'  Calculate the joint probability distribution function of the input tensors based on the number of histogram bins.

Args:
    kernel_values1: shape [BxNxNUM_BINS].
    kernel_values2: shape [BxNxNUM_BINS].
    epsilon: scalar, for numerical stability.

Returns:
    shape [BxNUM_BINSxNUM_BINS].

z5Input kernel_values1 type is not a torch.Tensor. Got z5Input kernel_values2 type is not a torch.Tensor. Got r   z5Input kernel_values1 must be a of the shape BxN. Got z5Input kernel_values2 must be a of the shape BxN. Got zGInputs kernel_values1 and kernel_values2 must have the same shape. Got z and r   r   )r   r   r   )r   r   r   r   r   r   r   r   matmul	transposer   view)r$   r%   r   joint_kernel_valuesr    r   s         r!   	joint_pdfr,   D   sL    nell33OPTUcPdOefggnell33OPTUcPdOefgg1$PQ_QeQePfghh1$PQ_QeQePfghh~333"(()~/C/C.DF
 	

  ,,~'?'?1'E~VII1v>CCB1MPWWM

-CJr#   x	bandwidthc                 B    [        U R                  S5      XU5      u  pEU$ )a  Estimate the histogram of the input torch.Tensor.

The calculation uses kernel density estimation which requires a bandwidth (smoothing) parameter.

Args:
    x: Input torch.Tensor to compute the histogram with shape :math:`(B, D)`.
    bins: The number of bins to use the histogram :math:`(N_{bins})`.
    bandwidth: Gaussian smoothing factor with shape shape [1].
    epsilon: A scalar, for numerical stability.

Returns:
    Computed histogram of shape :math:`(B, N_{bins})`.

Examples:
    >>> x = torch.rand(1, 10)
    >>> bins = torch.torch.linspace(0, 255, 128)
    >>> hist = histogram(x, bins, bandwidth=torch.tensor(0.9))
    >>> hist.shape
    torch.Size([1, 128])

r   )r"   r   )r-   r   r.   r   r   _s         r!   	histogramr1   i   s!    , !++a.$7CFCJr#   x1x2c                     [        U R                  S5      X#U5      u  pV[        UR                  S5      X#U5      u  pW[        Xg5      nU$ )aA  Estimate the 2d histogram of the input torch.Tensor.

The calculation uses kernel density estimation which requires a bandwidth (smoothing) parameter.

Args:
    x1: Input torch.Tensor to compute the histogram with shape :math:`(B, D1)`.
    x2: Input torch.Tensor to compute the histogram with shape :math:`(B, D2)`.
    bins: The number of bins to use the histogram :math:`(N_{bins})`.
    bandwidth: Gaussian smoothing factor with shape shape [1].
    epsilon: A scalar, for numerical stability. Default: 1e-10.

Returns:
    Computed histogram of shape :math:`(B, N_{bins}), N_{bins})`.

Examples:
    >>> x1 = torch.rand(2, 32)
    >>> x2 = torch.rand(2, 32)
    >>> bins = torch.torch.linspace(0, 255, 128)
    >>> hist = histogram2d(x1, x2, bins, bandwidth=torch.tensor(0.9))
    >>> hist.shape
    torch.Size([2, 128, 128])

r   )r"   r   r,   )	r2   r3   r   r.   r   r0   r$   r%   r   s	            r!   histogram2dr5      sE    4 %R\\!_dwOA$R\\!_dwOA
N
3CJr#   imageminmaxn_binscenters
return_pdfkernelepsc	                 l   U b7  [        U [        R                  5      (       d  [        S[	        U 5       S35      eUb7  [        U[        R                  5      (       d  [        S[	        U5       S35      eUbF  [        UR                  5      S:  a-  UR                  5       S:w  a  [        SUR                   S35      e[        U[        5      (       d  [        S[	        U5       S35      e[        U[        5      (       d  [        S[	        U5       S35      e[        U[        5      (       d  [        S	[	        U5       S35      eUb-  [        U[        5      (       d  [        S
[	        U5       S35      e[        U[        5      (       d  [        S[	        U5       S35      eUc  X!-
  U-  nUc1  X[        R                  " X0R                  U R                  S9S-   -  -   nUR                  SSSSS5      n[        R                   " U R#                  S5      U-
  5      U-  n	US:X  a  [        R$                  " SU	S-  -  5      n
OWUS;   aB  U	S:*  R'                  U	R                  5      nUS:X  a	  SU	-
  U-  n
O$US:X  a  Un
OSU	S-  -
  U-  n
O[        SU S35      e[        R(                  " U
SS9R+                  SSS5      nU(       a  [        R(                  " USSS9U-   nX-  nU R                  5       S:X  a#  UR-                  5       nUR-                  5       nX4$ U R                  5       S:X  a"  UR-                  S5      nUR-                  S5      nX4$ U R                  5       S:X  a  UR-                  5       nO%U R                  5       S:X  a  UR-                  S5      nU[        R.                  " U5      4$ )a*  Estimate the histogram of the input image(s).

The calculation uses triangular kernel density estimation.

Args:
    image: Input torch.Tensor to compute the histogram with shape
      :math:`(H, W)`, :math:`(C, H, W)` or :math:`(B, C, H, W)`.
    min: Lower end of the interval (inclusive).
    max: Upper end of the interval (inclusive). Ignored when
      :attr:`centers` is specified.
    n_bins: The number of histogram bins. Ignored when
      :attr:`centers` is specified.
    bandwidth: Smoothing factor. If not specified or equal to -1,
      :math:`(bandwidth = (max - min) / n_bins)`.
    centers: Centers of the bins with shape :math:`(n_bins,)`.
      If not specified or empty, it is calculated as centers of
      equal width bins of [min, max] range.
    return_pdf: If True, also return probability densities for
      each bin.
    kernel: kernel to perform kernel density estimation
      ``(`triangular`, `gaussian`, `uniform`, `epanechnikov`)``.
    eps: epsilon for numerical stability.

Returns:
    Computed histogram of shape :math:`(bins)`, :math:`(C, bins)`,
      :math:`(B, C, bins)`.
    Computed probability densities of shape :math:`(bins)`, :math:`(C, bins)`,
      :math:`(B, C, bins)`, if return_pdf is ``True``. torch.Tensor of torch.zeros with shape
      of the histogram otherwise.

z,Input image type is not a torch.Tensor. Got .z.Bins' centers type is not a torch.Tensor. Got r   r   zABins' centers must be a torch.Tensor of the shape (n_bins,). Got z3Type of lower end of the range is not a float. Got z3Type of upper end of the range is not a float. Got z*Type of number of bins is not an int. Got z#Bandwidth type is not a float. Got z#Return_pdf type is not a bool. Got )devicedtypeg      ?r'   gaussianr   r   )
triangularuniformepanechnikovrC   g      ?rD   zJKernel must be 'triangular', 'gaussian', 'uniform' or 'epanechnikov'. Got )r'   r   T)r   keepdimr   )r   r   r   r   r   lenr   r   r   floatintboolaranger@   rA   reshapeabsr   r   tor   permutesqueeze
zeros_like)r6   r7   r8   r9   r.   r:   r;   r<   r=   ur   maskhistr    r   s                  r!   image_histogram2drV      s]   T E5<<!@!@FtE{mSTUVV:gu||#D#DHgWXYZZs7==1A5'++-1:L\]d]j]j\kklmnnc5!!MdSVi[XYZ[[c5!!MdSVi[XYZ[[fc""DT&\NRSTUUZ	5%A%A=d9o=NaPQQj$''=d:>N=OqQRRY&(	U\\&UZU`U`%adg%ghhoob!Q1-G		%//!$w./);A		$A+.	<	<Q{{177#\! 1W,My  M 1a4Z4/Meflemmnopp99]199!QBD		$B=C"99;!<<>D++-C y YY[A<<?D++a.Cyyy{a||~		||A!!$'''r#   )绽|=)g        g     o@   NNFrC   rW   )typingr   r   r   r   rI   r"   r,   r1   r5   rJ   rK   strrV    r#   r!   <module>r\      s  $ #  UZ*LL* %*5:\\*LQ*
5<<%&*Z"ell "ELL "SX "ejeqeq "J U\\ ell UZ glgsgs 8 gl,,.3llGL||^c
\\H !%&*k(<<k(	k( 
k( 	k(
 k( ell#k( k( k( 
k( 5<<%&k(r#   