
    3jI                        S SK Jr  S SKrS SKJr  S SKJrJr  S SKJr  S\R                  S\\
   S\R                  4S	 jr   SS
\R                  S\R                  S\
S\S\S\S\R                  4S jjr " S S\R                  5      rg)    )ListN)nn)filter2d_separableget_gaussian_kernel1d)_compute_paddingimgcropping_shapereturnc                 |    [         R                  R                  R                  XS   * US   * US   * US   * 45      $ )z.Crop out the part of "valid" convolution area.      r      )torchr   
functionalpad)r   r	   s     M/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/metrics/ssim.py_cropr      sL    88""a  >!#4"4~a7H6H>Z[K\J\]     img1img2window_sizemax_valepspaddingc                    [        U [        R                  5      (       d  [        S[	        U 5       35      e[        U[        R                  5      (       d  [        S[	        U5       35      e[        U[
        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                  :X  d%  [        SU R                   SUR                   35      e[        US	U R                  U R                  S
9nSU-  S-  nSU-  S-  n[        XU5      n	[        XU5      n
/ nUS:X  a3  UR                  S   =p[        X/5      n[        X5      n	[        X5      n
OUS:X  a   U	S-  nU
S-  nX-  n[        U S-  Xf5      n[        US-  Xf5      n[        X-  Xf5      nUS:X  a%  [        UU5      n[        UU5      n[        UU5      nOUS:X  a   UU-
  nUU-
  nUU-
  nSU-  U-   SU-  U-   -  nX-   U-   UU-   U-   -  nUUU-   -  $ )a  Compute the Structural Similarity (SSIM) index map between two images.

Measures the (SSIM) index between each element in the input `x` and target `y`.

The index can be described as:

.. math::

  \text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)}
  {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}

where:
  - :math:`c_1=(k_1 L)^2` and :math:`c_2=(k_2 L)^2` are two variables to
    stabilize the division with weak denominator.
  - :math:`L` is the dynamic range of the pixel-values (typically this is
    :math:`2^{\#\text{bits per pixel}}-1`).

Args:
    img1: the first input image with shape :math:`(B, C, H, W)`.
    img2: the second input image with shape :math:`(B, C, H, W)`.
    window_size: the size of the gaussian kernel to smooth the images.
    max_val: the dynamic range of the images.
    eps: Small value for numerically stability when dividing.
    padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
     area to compute SSIM to match the MATLAB implementation of original SSIM paper.

Returns:
   The ssim index map with shape :math:`(B, C, H, W)`.

Examples:
    >>> input1 = torch.rand(1, 4, 5, 5)
    >>> input2 = torch.rand(1, 4, 5, 5)
    >>> ssim_map = ssim(input1, input2, 5)  # 1x4x5x5

z+Input img1 type is not a torch.Tensor. Got z+Input img2 type is not a torch.Tensor. Got z'Input max_val type is not a float. Got    z,Invalid img1 shape, we expect BxCxHxW. Got: z,Invalid img2 shape, we expect BxCxHxW. Got: z,img1 and img2 shapes must be the same. Got: z and g      ?)devicedtypeg{Gz?r   gQ?validsameg       @)
isinstancer   Tensor	TypeErrortypefloatlenshape
ValueErrorr   r   r   r   r   r   )r   r   r   r   r   r   kernelC1C2mu1mu2r	   heightwidthmu1_sqmu2_sqmu1_mu2
mu_img1_sq
mu_img2_sqmu_img1_img2	sigma1_sq	sigma2_sqsigma12numdens                            r   ssimr<   "   s   V dELL))Ed4j\RSSdELL))Ed4j\RSSgu%%A$w-QRRtzz?aG

|TUUtzz?aG

|TUU::#G

|SXY]YcYcXdeff 1c$++]a]g]ghF A%BA%B +4@C*4@C "N'b)))6/:C(C(	F	!VF!VFiG#D!GV<J#D!GV<J%dk6BL':~6
:~6
\>:	F	 V#IV#IW$G w+g0BCC2-)i2G"2LMC#)r   c                      ^  \ rS rSrSrSS\S\S\S\SS4
U 4S	 jjjrS
\	R                  S\	R                  S\	R                  4S jrSrU =r$ )SSIM   a  Create a module that computes the Structural Similarity (SSIM) index between two images.

Measures the (SSIM) index between each element in the input `x` and target `y`.

The index can be described as:

.. math::

  \text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)}
  {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}

where:
  - :math:`c_1=(k_1 L)^2` and :math:`c_2=(k_2 L)^2` are two variables to
    stabilize the division with weak denominator.
  - :math:`L` is the dynamic range of the pixel-values (typically this is
    :math:`2^{\#\text{bits per pixel}}-1`).

Args:
    window_size: the size of the gaussian kernel to smooth the images.
    max_val: the dynamic range of the images.
    eps: Small value for numerically stability when dividing.
    padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
     area to compute SSIM to match the MATLAB implementation of original SSIM paper.

Shape:
    - Input: :math:`(B, C, H, W)`.
    - Target :math:`(B, C, H, W)`.
    - Output: :math:`(B, C, H, W)`.

Examples:
    >>> input1 = torch.rand(1, 4, 5, 5)
    >>> input2 = torch.rand(1, 4, 5, 5)
    >>> ssim = SSIM(5)
    >>> ssim_map = ssim(input1, input2)  # 1x4x5x5

r   r   r   r   r
   Nc                 R   > [         TU ]  5         Xl        X l        X0l        X@l        g N)super__init__r   r   r   r   )selfr   r   r   r   	__class__s        r   rC   SSIM.__init__   s#     +%r   r   r   c                 p    [        XU R                  U R                  U R                  U R                  5      $ rA   )r<   r   r   r   r   )rD   r   r   s      r   forwardSSIM.forward   s'    D 0 0$,,$,,WWr   )r   r   r   r   g      ?g-q=r!   )__name__
__module____qualname____firstlineno____doc__intr&   strrC   r   r#   rH   __static_attributes____classcell__)rE   s   @r   r>   r>      sh    #JC % E \_ mq  XELL X X X Xr   r>   rJ   )typingr   r   r   kornia.filtersr   r   kornia.filters.filterr   r#   rP   r   r&   rQ   r<   Moduler>    r   r   <module>rY      s   $    D 2u|| T#Y 5<<  i
,,i
,,i i 	i
 
i i \\iX.X299 .Xr   