o
    oi                     @  s   d dl mZ d dlmZ d dl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 d	d
lmZmZ 	ddddZdddZdS )    )annotations)OptionalN)stack)KORNIA_CHECK_SHAPE)transform_points)remap)create_meshgrid   )distort_pointstilt_projection   pointstorch.TensorKdistnew_KOptional[torch.Tensor]	num_itersintreturnc                 C  s  t | g d t |g d |  dk r$| jd dkr$td| j d|du r+|}nt |g d |jd dvrCtd	|jd  |jd d
k rYtjj|dd
|jd  g}|ddddf }|ddddf }|ddddf }|ddddf }| d | | }	| d | | }
t|d dkst|d dkrt	|d |d d}t
|t|	|
gddd\}	}
|	|
}}t|D ]}|	|	 |
|
  }d|dddf |  |dddf | |  |dddf |d   d|dddf |  |dddf | |  |dddf |d    }d|dddf  |	 |
 |dddf |d|	 |	    |dddf |  |dddf | |  }|dddf |d|
 |
   d|dddf  |	 |
  |dddf |  |dddf | |  }|| | }	|| | }
q|ddddf }|ddddf }|ddddf }|ddddf }||	 | }	||
 | }
t|	|
gdS )aM  Compensate for lens distortion a set of 2D image points.

    Radial :math:`(k_1, k_2, k_3, k_4, k_5, k_6)`,
    tangential :math:`(p_1, p_2)`, thin prism :math:`(s_1, s_2, s_3, s_4)`, and tilt :math:`(\tau_x, \tau_y)`
    distortion models are considered in this function.

    Args:
        points: Input image points with shape :math:`(*, N, 2)`.
        K: Intrinsic camera matrix with shape :math:`(*, 3, 3)`.
        dist: Distortion coefficients
            :math:`(k_1,k_2,p_1,p_2[,k_3[,k_4,k_5,k_6[,s_1,s_2,s_3,s_4[,\tau_x,\tau_y]]]])`. This is
            a vector with 4, 5, 8, 12 or 14 elements with shape :math:`(*, n)`.
        new_K: Intrinsic camera matrix of the distorted image. By default, it is the same as K but you may additionally
            scale and shift the result by using a different matrix. Shape: :math:`(*, 3, 3)`. Default: None.
        num_iters: Number of undistortion iterations. Default: 5.

    Returns:
        Undistorted 2D points with shape :math:`(*, N, 2)`.

    Example:
        >>> _ = torch.manual_seed(0)
        >>> x = torch.rand(1, 4, 2)
        >>> K = torch.eye(3)[None]
        >>> dist = torch.rand(1, 4)
        >>> undistort_points(x, K, dist)
        tensor([[[-0.1513, -0.1165],
                 [ 0.0711,  0.1100],
                 [-0.0697,  0.0228],
                 [-0.1843, -0.1606]]])

    )*N2)r   3r      zpoints shape is invalid. Got .N   r            /Invalid number of distortion coefficients. Got r!   r   .r	   .r   .r	   ).r    ).   T)dimr         r      r   	   
      r    )r   r&   shape
ValueErrortorchnn
functionalpadanyr   r   r   unbindrange)r   r   r   r   r   cxcyfxfyxyinv_tiltx0y0_r2inv_rad_polydeltaXdeltaYnew_cxnew_cynew_fxnew_fy rH   Y/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/calibration/undistort.pyundistort_points"   sf   "$ 
FFrJ   imagec              	   C  s  t | jdk rtd| j d|jdd dkr"td|j d|jd d	vr4td
|jd  d|  sAtd| j d| jdd |jdd ks]| jdd |jdd krt| jdd dk|jdd dk|jdd dkfstd| jdd  d|jdd  d|jdd  d| jdd \}}}|  || |  }t||d| j| j}|	dd}t
|||}	|	d 	|||}
|	d 	|||}t| 	|||||
|dd}|| S )a  Compensate an image for lens distortion.

    Radial :math:`(k_1, k_2, k_3, k_4, k_4, k_6)`,
    tangential :math:`(p_1, p_2)`, thin prism :math:`(s_1, s_2, s_3, s_4)`, and tilt :math:`(\tau_x, \tau_y)`
    distortion models are considered in this function.

    Args:
        image: Input image with shape :math:`(*, C, H, W)`.
        K: Intrinsic camera matrix with shape :math:`(*, 3, 3)`.
        dist: Distortion coefficients
            :math:`(k_1,k_2,p_1,p_2[,k_3[,k_4,k_5,k_6[,s_1,s_2,s_3,s_4[,\tau_x,\tau_y]]]])`. This is
            a vector with 4, 5, 8, 12 or 14 elements with shape :math:`(*, n)`.

    Returns:
        Undistorted image with shape :math:`(*, C, H, W)`.

    Example:
        >>> img = torch.rand(1, 3, 5, 5)
        >>> K = torch.eye(3)[None]
        >>> dist_coeff = torch.rand(1, 4)
        >>> out = undistort_image(img, K, dist_coeff)
        >>> out.shape
        torch.Size([1, 3, 5, 5])

    r)   zImage shape is invalid. Got: r   N)r)   r)   zK matrix shape is invalid. Got r   r   r"   z:Invalid input image data type. Input should be float. Got )r	   rH   zAInput shape is invalid. Input batch dimensions should match. Got z, Fr   r#   r$   T)align_corners)lenr-   r.   is_floating_pointdtypeallnumelr   devicereshaper
   r   view_as)rK   r   r   channelsrowscolsBxy_gridptsptsdmapxmapyoutrH   rH   rI   undistort_image   s8   88
ra   )Nr   )r   r   r   r   r   r   r   r   r   r   r   r   )rK   r   r   r   r   r   r   r   )
__future__r   typingr   r/   kornia.corer   kornia.core.checkr   kornia.geometry.linalgr   kornia.geometry.transformr   kornia.utilsr   distortr
   r   rJ   ra   rH   rH   rH   rI   <module>   s   h