o
    oi'                     @   s  d dl mZmZ d dlZd dlmZ d'dedededed	eeef f
d
dZd'dededed	efddZd'dedededed	ef
ddZ	d'dededededed	efddZ
								d(dededed edee d!ee d"ed#ed$ed	eeef fd%d&ZdS ))    )OptionalTupleN)Tensor绽|=valuesbinssigmaepsilonreturnc                 C   s
  t | tstdt|  t |tstdt| t |ts*tdt| |  dks8td| j | dksFtd|j | dksTtd	|j | |dd }t	d
|| 
d }tj|dd}tj|ddd| }|| }||fS )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[Tensor, Tensor]:
          - Tensor: shape [BxN].
          - Tensor: shape [BxNxNUM_BINS].

    z'Input values type is not a Tensor. Got z%Input bins type is not a Tensor. Got z&Input sigma type is not a 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)
isinstancer   	TypeErrortyper   
ValueErrorshape	unsqueezetorchexppowmeansum)r   r   r   r	   	residualskernel_valuespdfnormalization r    L/home/ubuntu/.local/lib/python3.10/site-packages/kornia/enhance/histogram.pymarginal_pdf   s$   


r"   kernel_values1kernel_values2c                 C   s   t | tstdt|  t |tstdt| |  dks*td| j | dks8td|j | j|jkrJtd| j d|j t| 	dd	|}tj
|d
dddd| }|| }|S )aC  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].

    z/Input kernel_values1 type is not a Tensor. Got z/Input kernel_values2 type is not a 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   r    r    r!   	joint_pdfD   s(   

r*   x	bandwidthc                 C   s   t | d|||\}}|S )a  Estimate the histogram of the input tensor.

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

    Args:
        x: Input 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   _r    r    r!   	histogrami   s   r.   x1x2c           	      C   s>   t | d|||\}}t |d|||\}}t||}|S )as  Estimate the 2d histogram of the input tensor.

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

    Args:
        x1: Input tensor to compute the histogram with shape :math:`(B, D1)`.
        x2: Input 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*   )	r/   r0   r   r,   r	   r-   r#   r$   r   r    r    r!   histogram2d   s   
r1                o@   F
triangularimageminmaxn_binscenters
return_pdfkernelepsc	                 C   s  | durt | tstdt|  d|dur&t |ts&tdt| d|dur@t|jdkr@| dkr@td|j dt |tsOtdt| dt |ts^td	t| dt |t	smtd
t| d|durt |tstdt| dt |t
stdt| d|du r|| | }|du r||tj|| j| jdd   }|ddddd}t| d| | }	|dkrtd|	d  }
n/|dv r|	dk|	j}|dkrd|	 | }
n|dkr|}
nd|	d  | }
ntd| dtj|
ddddd}|rDtj|ddd| }|| }|  dkr/| }| }||fS |  dkr@|d}|d}||fS |  dkrP| }n|  dkr\|d}|t|fS )a  Estimate the histogram of the input image(s).

    The calculation uses triangular kernel density estimation.

    Args:
        image: Input 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``. Tensor of zeros with shape
          of the histogram otherwise.

    Nz&Input image type is not a Tensor. Got .z(Bins' centers type is not a Tensor. Got r   r   z;Bins' centers must be a 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   )r5   uniformepanechnikovr5   g      ?rB   zJKernel must be 'triangular', 'gaussian', 'uniform' or 'epanechnikov'. Got )r%   r   T)r   keepdimr   )r   r   r   r   lenr   r   r   floatintboolr   aranger?   r@   reshapeabsr   r   tor   permutesqueeze
zeros_like)r6   r7   r8   r9   r,   r:   r;   r<   r=   ur   maskhistr   r   r    r    r!   image_histogram2d   sb   *"



 



rT   )r   )r2   r3   r4   NNFr5   r   )typingr   r   r   kornia.corer   rG   r"   r*   r.   r1   rH   rI   strrT   r    r    r    r!   <module>   sH   (+ %$"	

