a
    dw&                     @   s   d dl mZmZ d dlZdejejejeeejejf dddZdejejeejddd	Zdejejejeejd
ddZdejejejejeejdddZ	dejeee
ee eej eeeeejejf d
ddZdS )    )OptionalTupleN绽|=)valuesbinssigmaepsilonreturnc                 C   s  t | tjstdt|  t |tjs<tdt| t |tjsZtdt| |  dksvtd| j| dkstd|j| dkst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 tensor 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    z1Input values must be a of the shape BxNx1. Got {}   z1Input 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formatshape	unsqueezeexppowmeansum)r   r   r   r   Z	residualskernel_valuespdfnormalization r    a/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/enhance/histogram.pymarginal_pdf   s$    r"   )kernel_values1kernel_values2r   r	   c                 C   s   t | tjstdt|  t |tjs<tdt| |  dksXtd| j| dksttd|j| j|jkrtd| j|jt	| 
dd|}tj|d	d
ddd| }|| }|S )aF  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
   z7Input kernel_values1 must be a of the shape BxN. Got {}z7Input kernel_values2 must be a of the shape BxN. Got {}zPInputs kernel_values1 and kernel_values2 must have the same shape. Got {} and {}r   r   )r   r   r   )r   r   r   r   r   r   r   r   r   matmulZ	transposer   view)r#   r$   r   Zjoint_kernel_valuesr   r   r    r    r!   	joint_pdf4   s$    r(   )xr   	bandwidthr   r	   c                 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!   	histogramZ   s    r,   )x1x2r   r*   r   r	   c           	      C   s>   t | d|||\}}t |d|||\}}t||}|S )ar  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-   r.   r   r*   r   r+   r#   r$   r   r    r    r!   histogram2du   s    
r/                o@   F
triangular)
imageminmaxn_binsr*   centers
return_pdfkernelepsr	   c	                 C   s  | dur(t | tjs(tdt|  d|durPt |tjsPtdt| d|durt|jdkr| dkrtd|j dt |t	stdt| dt |t	std	t| dt |t
std
t| d|durt |t	stdt| dt |ts(tdt| d|du r>|| | }|du rh||tj|| j| jdd   }|ddddd}t| d| | }	|dkrtd|	d  }
nn|dv r|	dk|	j}|dkrd|	 | }
n*|dkrt|	| }
nd|	d  | }
ntd| dtj|
ddddd}|rtj|ddd| }|| }|  dkrx| }| }n"|  dkr|d}|d}||fS |  dkr| }n|  dkr|d}|t|fS )aQ  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`)``.

    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 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%   Zgaussianr   r   )r3   uniformZepanechnikovr3   g      ?r?   zJKernel must be 'triangular', 'gaussian', 'uniform' or 'epanechnikov'. Got )r%   r   T)r   Zkeepdimr
   )r   r   r   r   r   lenr   r   r   floatintboolZaranger=   r>   Zreshapeabsr   r   toZ	ones_liker   ZpermuteZsqueezeZ
zeros_like)r4   r5   r6   r7   r*   r8   r9   r:   r;   ur   maskhistr   r   r    r    r!   image_histogram2d   s`    ("




 








rJ   )r   )r   )r   )r   )r0   r1   r2   NNFr3   r   )typingr   r   r   r   rB   r"   r(   r,   r/   rC   rD   strrJ   r    r    r    r!   <module>   sB    .&  $        