o
    oit                     @   s  d dl Z d dlmZmZmZ d dlZd dlmZ d dlm	Z	m
Z
mZmZ d dlmZ d dlmZ d dlmZ d dlmZ d	d
lmZmZmZmZmZ g dZde	de	fddZde	de	fddZde	de	de	fddZde	de	de	de	de	f
ddZde	de	fddZ de	de	de	fdd Z!d!e	de	fd"d#Z"	$	%	&dYde	d'e	d(e#d)e#d*e$de	fd+d,Z%	$	%	-dZde	d'e	d(e#d)e#d*e$de	fd.d/Z&		$	%	&d[de	de	dede	f d(e#d)e#d*e$de	fd0d1Z'		$	%	-d\de	de	de	de	dede	f d(e#d)e#d*e$de	fd2d3Z(	$	%	&dYde	de	d(e#d)e#d*e$de	fd4d5Z)		$	%	&d[de	d6e	dede	f d(e#d)e#d*e$de	fd7dZ*	$	%	-dZde	d!e	d(e#d)e#d*e$de	fd8d!Z+d]d:e,d;e-d<e#dee,e,f fd=d>Z.e	$		9	-d^d?e	d@ee,ee,e,f f dAe#d*ee$ d<e#dBe$de	fdCdDZ/	$		9	-d^d?e	dEe,dAe#d*ee$ d<e#dBe$de	fdFdGZ0	$		-d_d?e	dHee-ee-e-f f dAe#d*ee$ dBe$de	fdIdJZ1G dKdL dLeZ2G dMdN dNeZ3G dOdP dPeZ4G dQdR dReZ5G dSdT dTeZ6G dUdV dVeZ7G dWdX dXeZ8dS )`    N)OptionalTupleUnion)ImageModule)Tensorones	ones_likezeros)gaussian_blur2d)_extract_device_dtype)perform_keep_shape_image)eye_like   )get_affine_matrix2dget_projective_transformget_rotation_matrix2dwarp_affinewarp_affine3d)AffineRescaleResizeRotateScaleShear	Translateaffineaffine3drescaleresizeresize_to_be_divisiblerotaterotate3dscaleshear	translatetensorreturnc                 C   s|   dt | j  krdksn td| j d| jdd \}}t|d d }t|d d }tj||g| j| jd}|S )	zJCompute the center of tensor plane for (H, W), (C, H, W) and (B, C, H, W).      z-Must be a 3D tensor as HW, CHW and BCHW. Got .Nr   devicedtypelenshapeAssertionErrorfloattorchr%   r,   r-   )r%   heightwidthcenter_xcenter_ycenter r9   U/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/transform/affwarp.py_compute_tensor_center7   s   r;   c                 C   s   dt | j  krdksn td| j d| jdd \}}}t|d d }t|d d }t|d d }tj|||g| j| jd	}|S )
zSCompute the center of tensor plane for (D, H, W), (C, D, H, W) and (B, C, D, H, W).      z0Must be a 3D tensor as DHW, CDHW and BCDHW. Got r)   Nr   r'   r+   r.   )r%   depthr4   r5   r6   r7   center_zr8   r9   r9   r:   _compute_tensor_center3dB   s   rA   angler8   c                 C   s   t |}t|| |}|S )&Compute a pure affine rotation matrix.)r   r   )rB   r8   r"   matrixr9   r9   r:   _compute_rotation_matrixN   s   rE   yawpitchrollc                 C   s0  t | jt |j  krt |j  krdkr,n n| jdd} |jdd}|jdd}t | jt |j  krDt |j  krDdkrXn n| jdd} |jdd}|jdd}t | jt |j  krpt |j  krpdksn td| j d|j d|j dtj| ||gdd}t| }t|||}|S )rC   r   dimr   r'   z,Expected yaw, pitch, roll to be (B, 1). Got z, r)   )r/   r0   	unsqueezer1   r3   catr   r   )rF   rG   rH   r8   anglesscalesrD   r9   r9   r:   _compute_rotation_matrix3dU   s   442"rO   translationc                 C   sZ   t d| dd}tj| ddd\}}|ddddf  |7  < |dd	ddf  |7  < |S )
z&Compute affine matrix for translation.r<   Fshared_memoryr'   chunksrJ   .r   r   r   r3   chunk)rP   rD   dxdyr9   r9   r:   _compute_translation_matrixj   
   rZ   r"   c                 C   s,   t | jdd | j| jd}t||| }|S )z"Compute affine matrix for scaling.Nr   r+   )r	   r0   r,   r-   r   )r"   r8   rB   rD   r9   r9   r:   _compute_scaling_matrixt   s   r\   r#   c                 C   sZ   t d| dd}tj| ddd\}}|ddd	df  |7  < |dd	dd	f  |7  < |S )
z#Compute affine matrix for shearing.r<   FrQ   r'   rS   rT   .r   r   rV   )r#   rD   shxshyr9   r9   r:   _compute_shear_matrix{   r[   r_   bilinearr	   TrD   modepadding_modealign_cornersc           	      C   sr   |   dk}|rtj| dd} || jd dd}| jd }| jd }t| |||f|||}|r7tj|dd}|S )a&  Apply an affine transformation to the image.

    .. image:: _static/img/warp_affine.png

    Args:
        tensor: The image tensor to be warped in shapes of
            :math:`(H, W)`, :math:`(D, H, W)` and :math:`(B, C, H, W)`.
        matrix: The 2x3 affine transformation matrix.
        mode: interpolation mode to calculate output values ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The warped image with the same shape as the input.

    Example:
        >>> img = torch.rand(1, 2, 3, 5)
        >>> aff = torch.eye(2, 3)[None]
        >>> out = affine(img, aff)
        >>> print(out.shape)
        torch.Size([1, 2, 3, 5])

    r<   r   rI   rS   r*   )
ndimensionr3   rK   expandr0   r   squeeze)	r%   rD   ra   rb   rc   is_unbatchedr4   r5   warpedr9   r9   r:   r      s    

r   Fc           
      C   s~   |   dk}|rtj| dd} || jd dd}| jd }| jd }| jd }t| ||||f|||}	|r=tj|	dd}	|	S )a  Apply an affine transformation to the 3d volume.

    Args:
        tensor: The image tensor to be warped in shapes of
            :math:`(D, H, W)`, :math:`(C, D, H, W)` and :math:`(B, C, D, H, W)`.
        matrix: The affine transformation matrix with shape :math:`(B, 3, 4)`.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
         `` 'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The warped image.

    Example:
        >>> img = torch.rand(1, 2, 4, 3, 5)
        >>> aff = torch.eye(3, 4)[None]
        >>> out = affine3d(img, aff)
        >>> print(out.shape)
        torch.Size([1, 2, 4, 3, 5])

    r(   r   rI   rS   r>   r*   )rd   r3   rK   re   r0   r   rf   )
r%   rD   ra   rb   rc   rg   r?   r4   r5   rh   r9   r9   r:   r      s   


r   c                 C   s   t | tstdt|  t |tstdt| |dur.t |ts.tdt| t| jdvr=td| j |du rEt| }|| jd }|| jd d}t	||}t
| |d	dd
ddf |||S )aW  Rotate the tensor anti-clockwise about the center.

    .. image:: _static/img/rotate.png

    Args:
        tensor: The image tensor to be warped in shapes of :math:`(B, C, H, W)`.
        angle: The angle through which to rotate. The tensor
          must have a shape of (B), where B is batch size.
        center: The center through which to rotate. The tensor
          must have a shape of (B, 2), where B is batch size and last
          dimension contains cx and cy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The rotated tensor with shape as input.

    .. note::
       See a working example `here <https://kornia.github.io/tutorials/nbs/rotate_affine.html>`__.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> angle = torch.tensor([90.])
        >>> out = rotate(img, angle)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    'Input tensor type is not a Tensor. Got z&Input angle type is not a Tensor. Got N'Input center type is not a Tensor. Got r<   r(   7Invalid tensor shape, we expect CxHxW or BxCxHxW. Got: r   rS   .r'   r<   )
isinstancer   	TypeErrortyper/   r0   
ValueErrorr;   re   rE   r   )r%   rB   r8   ra   rb   rc   rotation_matrixr9   r9   r:   r       s   
'

"r    c           	      C   s4  t | tstdt|  t |tstdt| t |ts*tdt| t |ts8tdt| |durJt |tsJtdt| t| jdvrYtd| j |du rat| }|| jd	 }|| jd	 }|| jd	 }|| jd	 d
}t	||||}t
| |dddddf |||S )a  Rotate 3D the tensor anti-clockwise about the centre.

    Args:
        tensor: The image tensor to be warped in shapes of :math:`(B, C, D, H, W)`.
        yaw: The yaw angle through which to rotate. The tensor
          must have a shape of (B), where B is batch size.
        pitch: The pitch angle through which to rotate. The tensor
          must have a shape of (B), where B is batch size.
        roll: The roll angle through which to rotate. The tensor
          must have a shape of (B), where B is batch size.
        center: The center through which to rotate. The tensor
          must have a shape of (B, 2), where B is batch size and last
          dimension contains cx and cy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        Tensor: The rotated tensor with shape as input.

    ri   zyaw is not a Tensor. Got zpitch is not a Tensor. Got zroll is not a Tensor. Got Nrj   )r(   r=   z;Invalid tensor shape, we expect CxDxHxW or BxCxDxHxW. Got: r   rS   .r<   r(   )rm   r   rn   ro   r/   r0   rp   rA   re   rO   r   )	r%   rF   rG   rH   r8   ra   rb   rc   rq   r9   r9   r:   r!   6  s(   
!


"r!   c                 C      t | tstdt|  t |tstdt| t| jdvr+td| j t|}t| |dddddf |||S )	a  Translate the tensor in pixel units.

    .. image:: _static/img/translate.png

    Args:
        tensor: The image tensor to be warped in shapes of :math:`(B, C, H, W)`.
        translation: tensor containing the amount of pixels to
          translate in the x and y direction. The tensor must have a shape of
          (B, 2), where B is batch size, last dimension contains dx dy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The translated tensor with shape as input.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> translation = torch.tensor([[1., 0.]])
        >>> out = translate(img, translation)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    ri   z,Input translation type is not a Tensor. Got rk   rl   .Nr'   r<   )	rm   r   rn   ro   r/   r0   rp   rZ   r   )r%   rP   ra   rb   rc   translation_matrixr9   r9   r:   r$   y     
!
"r$   scale_factorc                 C   s   t | tstdt|  t |tstdt| t|jdkr)|dd}|du r1t| }|| jd d}|| jd d}t	||}t
| |ddddd	f |||S )
af  Scale the tensor by a factor.

    .. image:: _static/img/scale.png

    Args:
        tensor: The image tensor to be warped in shapes of :math:`(B, C, H, W)`.
        scale_factor: The scale factor apply. The tensor
          must have a shape of (B) or (B, 2), where B is batch size.
          If (B), isotropic scaling will perform.
          If (B, 2), x-y-direction specific scaling will perform.
        center: The center through which to scale. The tensor
          must have a shape of (B, 2), where B is batch size and last
          dimension contains cx and cy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The scaled tensor with the same shape as the input.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> scale_factor = torch.tensor([[2., 2.]])
        >>> out = scale(img, scale_factor)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    ri   z-Input scale_factor type is not a Tensor. Got r   r'   Nr   rS   .r<   )rm   r   rn   ro   r/   r0   repeatr;   re   r\   r   )r%   ru   r8   ra   rb   rc   scaling_matrixr9   r9   r:   r"     s   
&

"c                 C   rr   )	a}  Shear the tensor.

    .. image:: _static/img/shear.png

    Args:
        tensor: The image tensor to be skewed with shape of :math:`(B, C, H, W)`.
        shear: tensor containing the angle to shear
          in the x and y direction. The tensor must have a shape of
          (B, 2), where B is batch size, last dimension contains shx shy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The skewed tensor with shape same as the input.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> shear_factor = torch.tensor([[0.5, 0.0]])
        >>> out = shear(img, shear_factor)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    ri   z&Input shear type is not a Tensor. Got rk   rl   .Nr'   r<   )	rm   r   rn   ro   r/   r0   rp   r_   r   )r%   r#   ra   rb   rc   shear_matrixr9   r9   r:   r#     rt   short	side_sizeaspect_ratiosidec                 C   sx   |dvrt d| d|dkr| t| | fS |dkr$t| | | fS |dk|dk A r4| t| | fS t| | | fS )N)ry   longverthorzz=side can be one of 'short', 'long', 'vert', and 'horz'. Got ''r~   r   ry         ?)rp   int)rz   r{   r|   r9   r9   r:   _side_to_image_size  s   r   inputsizeinterpolation	antialiasc                 C   s  t | tstdt|  t| jdk rtdt| j | jdd  }\}}t |trEtj	
 r;tjddd || }	t||	|}tj	
 sP||krP| S ||d	  ||d  f}
|oct|
dk}|rt|
d	 d
 d dt|
d d
 d df}ttd|d	  dttd|d  df}|d	 d d	kr|d	 d |d f}|d d d	kr|d	 |d d f}t| ||} tjjj| |||d}|S )a  Resize the input Tensor to the given size.

    .. image:: _static/img/resize.png

    Args:
        input: The image tensor to be skewed with shape of :math:`(..., H, W)`.
            `...` means there can be any number of dimensions.
        size: Desired output size. If size is a sequence like (h, w),
            output size will be matched to this. If size is an int, smaller edge of the image will
            be matched to this number. i.e, if height > width, then image will be rescaled
            to (size * height / width, size)
        interpolation:  algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` |
            'bicubic' | 'trilinear' | 'area'.
        align_corners: interpolation flag.
        side: Corresponding side if ``size`` is an integer. Can be one of ``'short'``, ``'long'``, ``'vert'``,
            or ``'horz'``.
        antialias: if True, then image will be filtered with Gaussian before downscaling.
            No effect for upscaling.

    Returns:
        The resized tensor with the shape as the specified size.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> out = resize(img, (6, 8))
        >>> print(out.shape)
        torch.Size([1, 3, 6, 8])

    ri   r'   z4Input tensor must have at least two dimensions. Got r*   NzPPlease pass the size with a tuple when exporting to ONNX to correct the tracing.r   )
stacklevelr   r   g       @gMbP?g      @r<   )r   ra   rc   )rm   r   rn   ro   r/   r0   rp   r   r3   onnxis_in_onnx_exportwarningswarnr   maxr
   nn
functionalinterpolate)r   r   r   rc   r|   r   
input_sizehwr{   factorssigmasksoutputr9   r9   r:   r   %  s6   
&


,,r   divisible_factorc                 C   s   t | trt| jdkr| jd | jd }}t | tr.t| jdkr.| jd | jd }}t|| | }t|| | }t| ||f||||S )a  Resize the input tensor to be divisible by a certain factor.

    Args:
        input (Tensor): Input tensor to be resized.
        divisible_factor (int): The factor to which the image should be divisible.
        interpolation (str, optional): Interpolation flag. Defaults to "bilinear".
        align_corners (Optional[bool], optional):
            whether to align the corners of the input and output. Defaults to None.
        side (str, optional): Side to resize. Defaults to "short".
        antialias (bool, optional):
            If True, then image will be filtered with Gaussian before downscaling. Defaults to False.

    Returns:
        Tensor: The resized tensor.

    r(   r'   r<   r   )rm   r   r/   r0   roundr   )r   r   r   rc   r|   r   r4   r5   r9   r9   r:   r   {  s   r   factorc           
      C   sZ   t |tr
| }}n|\}}|  dd \}}t|| t|| f}	t| |	|||dS )a  Rescale the input Tensor with the given factor.

    .. image:: _static/img/rescale.png

    Args:
        input: The image tensor to be scale with shape of :math:`(B, C, H, W)`.
        factor: Desired scaling factor in each direction. If scalar, the value is used
            for both the x- and y-direction.
        interpolation:  algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` |
            ``'bicubic'`` | ``'trilinear'`` | ``'area'``.
        align_corners: interpolation flag.
        side: Corresponding side if ``size`` is an integer. Can be one of ``'short'``, ``'long'``, ``'vert'``,
            or ``'horz'``.
        antialias: if True, then image will be filtered with Gaussian before downscaling.
            No effect for upscaling.

    Returns:
        The rescaled tensor with the shape as the specified size.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> out = rescale(img, (2, 3))
        >>> print(out.shape)
        torch.Size([1, 3, 8, 12])

    r*   N)r   rc   r   )rm   r2   r   r   r   )
r   r   r   rc   r   factor_vertfactor_horzr4   r5   r   r9   r9   r:   r     s   
!
r   c                       sj   e Zd ZdZ				ddeeeeef f dedee	 d	ed
e	ddf fddZ
dedefddZ  ZS )r   av  Resize the input Tensor to the given size.

    Args:
        size: Desired output size. If size is a sequence like (h, w),
            output size will be matched to this. If size is an int, smaller edge of the image will
            be matched to this number. i.e, if height > width, then image will be rescaled
            to (size * height / width, size)
        interpolation:  algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` |
            'bicubic' | 'trilinear' | 'area'.
        align_corners: interpolation flag.
        side: Corresponding side if ``size`` is an integer. Can be one of ``'short'``, ``'long'``, ``'vert'``,
            or ``'horz'``.
        antialias: if True, then image will be filtered with Gaussian before downscaling.
            No effect for upscaling.

    Returns:
        The resized tensor with the shape of the given size.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> out = Resize((6, 8))(img)
        >>> print(out.shape)
        torch.Size([1, 3, 6, 8])

    .. raw:: html

        <gradio-app src="kornia/kornia-resize-antialias"></gradio-app>

    r`   Nry   Fr   r   rc   r|   r   r&   c                    ,   t    || _|| _|| _|| _|| _d S N)super__init__r   r   rc   r|   r   )selfr   r   rc   r|   r   	__class__r9   r:   r        

zResize.__init__r   c                 C   s   t || j| j| j| j| jdS )N)rc   r|   r   )r   r   r   rc   r|   r   r   r   r9   r9   r:   forward  s   zResize.forwardr`   Nry   F)__name__
__module____qualname____doc__r   r   r   strr   boolr   r   r   __classcell__r9   r9   r   r:   r     s(    !r   c                       s~   e Zd ZdZ								ddee dee dee d	ee d
ee dedededdf fddZdedefddZ	  Z
S )r   aa  Apply multiple elementary affine transforms simultaneously.

    Args:
        angle: Angle in degrees for counter-clockwise rotation around the center. The tensor
            must have a shape of (B), where B is the batch size.
        translation: Amount of pixels for translation in x- and y-direction. The tensor must
            have a shape of (B, 2), where B is the batch size and the last dimension contains dx and dy.
        scale_factor: Factor for scaling. The tensor must have a shape of (B), where B is the
            batch size.
        shear: Angles in degrees for shearing in x- and y-direction around the center. The
            tensor must have a shape of (B, 2), where B is the batch size and the last dimension contains sx and sy.
        center: Transformation center in pixels. The tensor must have a shape of (B, 2), where
            B is the batch size and the last dimension contains cx and cy. Defaults to the center of image to be
            transformed.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Raises:
        RuntimeError: If not one of ``angle``, ``translation``, ``scale_factor``, or ``shear`` is set.

    Returns:
        The transformed tensor with same shape as input.

    Example:
        >>> img = torch.rand(1, 2, 3, 5)
        >>> angle = 90. * torch.rand(1)
        >>> out = Affine(angle)(img)
        >>> print(out.shape)
        torch.Size([1, 2, 3, 5])

    Nr`   r	   TrB   rP   ru   r#   r8   ra   rb   rc   r&   c	                    s   dd ||||fD }	|	sd}
t |
|	d  t fdd|	dd  D s-t d|	  | _t   t|||g\}}|d u rIt ||d	}|| _|d u rXt d
||d	}|| _|d u rgt	 d
||d	}|| _
|| _|| _|| _|| _|| _d S )Nc                 S   s    g | ]}|d ur|  d qS )Nr   )r   ).0argr9   r9   r:   
<listcomp>0  s     z#Affine.__init__.<locals>.<listcomp>zzAffine was created without any affine parameter. At least one of angle, translation, scale_factor, or shear has to be set.r   c                 3   s    | ]}| kV  qd S r   r9   )r   other
batch_sizer9   r:   	<genexpr>9  s    z"Affine.__init__.<locals>.<genexpr>r   z3The batch sizes of the affine parameters mismatch: r+   r'   )RuntimeErrorall_batch_sizer   r   r   r	   rB   rP   r   ru   r#   r8   ra   rb   rc   )r   rB   rP   ru   r#   r8   ra   rb   rc   batch_sizesmsgr,   r-   r   r   r:   r   %  s2   

zAffine.__init__r   c                 C   s   | j d u r
d  }}n| j d | j d }}| jd u r't|| d d}n| j}t| j|| j| j ||d}t	||dd dd df | j
| j| jS )	N).r   ).r   r   rS   )sxsy.r'   r<   )r#   r8   r;   re   r   r   rP   ru   rB   r   ra   rb   rc   )r   r   r   r   r8   rD   r9   r9   r:   r   S  s   


(zAffine.forward)NNNNNr`   r	   T)r   r   r   r   r   r   r   r   r   r   r   r9   r9   r   r:   r     s<    %	
.r   c                       s`   e Zd ZdZ			ddeeeeef f dededed	d
f
 fddZ	de
d	e
fddZ  ZS )r   a  Rescale the input Tensor with the given factor.

    Args:
        factor: Desired scaling factor in each direction. If scalar, the value is used
            for both the x- and y-direction.
        interpolation:  algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` |
            ``'bicubic'`` | ``'trilinear'`` | ``'area'``.
        align_corners: interpolation flag.
        side: Corresponding side if ``size`` is an integer. Can be one of ``'short'``, ``'long'``, ``'vert'``,
            or ``'horz'``.
        antialias: if True, then image will be filtered with Gaussian before downscaling.
            No effect for upscaling.

    Returns:
        The rescaled tensor with the shape according to the given factor.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> out = Rescale((2, 3))(img)
        >>> print(out.shape)
        torch.Size([1, 3, 8, 12])

    r`   TFr   r   rc   r   r&   Nc                    &   t    || _|| _|| _|| _d S r   )r   r   r   r   rc   r   )r   r   r   rc   r   r   r9   r:   r   {  s
   

zRescale.__init__r   c                 C   s   t || j| j| j| jdS )N)rc   r   )r   r   r   rc   r   r   r9   r9   r:   r     s   zRescale.forward)r`   TF)r   r   r   r   r   r2   r   r   r   r   r   r   r   r9   r9   r   r:   r   b  s"    r   c                       ^   e Zd ZdZ				ddededef ded	ed
eddf fddZdedefddZ	  Z
S )r   az  Rotate the tensor anti-clockwise about the centre.

    Args:
        angle: The angle through which to rotate. The tensor
          must have a shape of (B), where B is batch size.
        center: The center through which to rotate. The tensor
          must have a shape of (B, 2), where B is batch size and last
          dimension contains cx and cy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The rotated tensor with the same shape as the input.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> angle = torch.tensor([90.])
        >>> out = Rotate(angle)(img)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    Nr`   r	   TrB   r8   ra   rb   rc   r&   c                    r   r   )r   r   rB   r8   ra   rb   rc   )r   rB   r8   ra   rb   rc   r   r9   r:   r     r   zRotate.__init__r   c                 C      t || j| j| j| j| jS r   )r    rB   r8   ra   rb   rc   r   r9   r9   r:   r        zRotate.forwardNr`   r	   Tr   r   r   r   r   r   r   r   r   r   r   r9   r9   r   r:   r     s(    
r   c                       L   e Zd ZdZ	ddedededed	d
f
 fddZded	efddZ  Z	S )r   a3  Translate the tensor in pixel units.

    Args:
        translation: tensor containing the amount of pixels to
          translate in the x and y direction. The tensor must have a shape of
          (B, 2), where B is batch size, last dimension contains dx dy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The translated tensor with the same shape as the input.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> translation = torch.tensor([[1., 0.]])
        >>> out = Translate(translation)(img)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    r`   r	   TrP   ra   rb   rc   r&   Nc                    r   r   )r   r   rP   ra   rb   rc   )r   rP   ra   rb   rc   r   r9   r:   r     
   

zTranslate.__init__r   c                 C   s   t || j| j| j| jS r   )r$   rP   ra   rb   rc   r   r9   r9   r:   r        zTranslate.forwardr`   r	   T
r   r   r   r   r   r   r   r   r   r   r9   r9   r   r:   r         	r   c                       r   )r   a  Scale the tensor by a factor.

    Args:
        scale_factor: The scale factor apply. The tensor
          must have a shape of (B) or (B, 2), where B is batch size.
          If (B), isotropic scaling will perform.
          If (B, 2), x-y-direction specific scaling will perform.
        center: The center through which to scale. The tensor
          must have a shape of (B, 2), where B is batch size and last
          dimension contains cx and cy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The scaled tensor with the same shape as the input.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> scale_factor = torch.tensor([[2., 2.]])
        >>> out = Scale(scale_factor)(img)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    Nr`   r	   Tru   r8   ra   rb   rc   r&   c                    r   r   )r   r   ru   r8   ra   rb   rc   )r   ru   r8   ra   rb   rc   r   r9   r:   r     r   zScale.__init__r   c                 C   r   r   )r"   ru   r8   ra   rb   rc   r   r9   r9   r:   r     r   zScale.forwardr   r   r9   r9   r   r:   r     s(    
r   c                       r   )r   a	  Shear the tensor.

    Args:
        shear: tensor containing the angle to shear
          in the x and y direction. The tensor must have a shape of
          (B, 2), where B is batch size, last dimension contains shx shy.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: interpolation flag.

    Returns:
        The skewed tensor with the same shape as the input.

    Example:
        >>> img = torch.rand(1, 3, 4, 4)
        >>> shear_factor = torch.tensor([[0.5, 0.0]])
        >>> out = Shear(shear_factor)(img)
        >>> print(out.shape)
        torch.Size([1, 3, 4, 4])

    r`   r	   Tr#   ra   rb   rc   r&   Nc                    r   r   )r   r   r#   ra   rb   rc   )r   r#   ra   rb   rc   r   r9   r:   r   +  r   zShear.__init__r   c                 C   s   t || j | j| j| jS r   )r#   ra   rb   rc   r   r9   r9   r:   r   4  r   zShear.forwardr   r   r9   r9   r   r:   r     r   r   r   )r`   r	   Fr   )Nr`   r	   F)ry   r   )r`   NF)9r   typingr   r   r   r3   kornia.corer   Moduler   r   r   r	   kornia.filtersr
   kornia.utilsr   kornia.utils.imager   kornia.utils.miscr   imgwarpr   r   r   r   r   __all__r;   rA   rE   rO   rZ   r\   r_   r   r   r   r   r    r!   r$   r"   r#   r   r2   r   r   r   r   r   r   r   r   r   r   r   r9   r9   r9   r:   <module>   s  

7
;

F
	
F
4

A
$1X
%
+9a,.&0