
    3j                     @   S SK Jr  S SKrS SKJs  Jr  S SKJr  S SKJrJ	r	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)KORNIA_CHECKKORNIA_CHECK_IS_TENSORKORNIA_CHECK_SHAPE)filter3dget_gaussian_kernel3d)_compute_paddingimgcropping_shapereturnc           
      l    [         R                  " U US   * US   * US   * US   * US   * US   * 45      $ )z.Crop out the part of "valid" convolution area.            r      )Fpad)r   r   s     O/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/metrics/ssim3d.py_cropr      s]    55AAAAAA	

 
    img1img2window_sizemax_valepspaddingc                 t   [        U 5        [        U5        [        U / SQ5        [        U/ SQ5        [        U R                  UR                  :H  SU R                   SUR                   35        [	        U[
        5      (       d  [        S[        U5       35      e[        X"U4S5      nSU-  S-  nSU-  S-  n[        X5      n	[        X5      n
/ nUS	:X  a6  UR                  S
S u  pn[        XU/5      n[        X5      n	[        X5      n
OUS:X  a   U	S-  nU
S-  nX-  n[        U S-  U5      n[        US-  U5      n[        X-  U5      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UU-   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)}

torch.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, D, H, W)`.
    img2: the second input image with shape :math:`(B, C, D, 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, D, H, W)`.

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

)BCDHWz,img1 and img2 shapes must be the same. Got: z and z'Input max_val type is not a float. Got )      ?r%   r%   g{Gz?r   gQ?validNsameg       @)r   r   r   shape
isinstancefloat	TypeErrortyper	   r   r
   r   )r   r   r   r   r   r   kernelC1C2mu1mu2r   depth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   ssim3drA   ,   s   V 4 4 t67t67tzz)-YZ^ZdZdYeejkokukujv+wxgu%%A$w-QRR 1+K1XZijF A%BA%B !.C .C "N'%||BC0u)5%*@AC(C(	F	!VF!VFiG$'6*J$'6*JDK0L':~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$ )SSIM3D   a  Create a module that computes the Structural Similarity (SSIM) index between two 3D 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)}

torch.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, D, H, W)`.
    - Target :math:`(B, C, D, H, W)`.
    - Output: :math:`(B, C, D, H, W)`.

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

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   rH   SSIM3D.__init__   s#     +%r   r   r   c                 p    [        XU R                  U R                  U R                  U R                  5      $ rF   )rA   r   r   r   r   )rI   r   r   s      r   forwardSSIM3D.forward   s'    d$"2"2DLL$((DLLYYr   )r   r   r   r   g      ?g-q=r(   )__name__
__module____qualname____firstlineno____doc__intr+   strrH   torchTensorrM   __static_attributes____classcell__)rJ   s   @r   rC   rC      sh    #JC % E \_ mq  ZELL Z Z Z Zr   rC   rO   )typingr   rW   torch.nn.functionalr   
functionalr   kornia.core.checkr   r   r   kornia.filtersr   r	   kornia.filters.filterr
   rX   rU   r   r+   rV   rA   ModulerC    r   r   <module>rc      s   $      V V : 2u|| T#Y 5<< & `
,,`
,,` ` 	`
 
` ` \\`F.ZRYY .Zr   