a
    de                     @   s   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
mZ dejejejeej eejd	d
dZejejejejdddZdS )    )OptionalN)transform_points)remap)create_meshgrid   )distort_pointstilt_projection   )pointsKdistnew_K	num_itersreturnc                 C   s  |   dk r,| jd dkr,td| j d|jdd dkrPtd|j d|du r^|}n$|jdd dkrtd	|j d|jd d
vrtd|jd  |jd dk rt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s`t|d dkrt|d |d d}t	|tj
|	|
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 ) aK  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]]])
       zpoints shape is invalid. Got .N   r   K matrix shape is invalid. Got z#new_K matrix shape is invalid. Got    r	            /Invalid number of distortion coefficients. Got r   r   .r   .r   .r   ).r   ).   T)dimr	         r   r   r   	   
      r   )r    shape
ValueErrortorchnnZ
functionalpadanyr   r   stackZunbindrange)r
   r   r   r   r   ZcxcyZfxZfyxyZinv_tiltZx0Zy0_Zr2Zinv_rad_polyZdeltaXZdeltaYZnew_cxZnew_cyZnew_fxZnew_fy r2   n/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/geometry/calibration/undistort.pyundistort_points   sh     ("
FFr4   )imager   r   r   c              	   C   s  t | jdk r td| j d|jdd dkrDtd|j d|jd d	vrhtd
|jd  d|  std| j d| jdd |jdd ks| jdd |jdd kr0t| jdd dk|jdd dk|jdd dkfs0t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   r   Nr   r   r   r   r   z:Invalid input image data type. Input should be float. Got )r   r2   zAInput shape is invalid. Input batch dimensions should match. Got z, Fr   r   r   T)Zalign_corners)lenr&   r'   Zis_floating_pointZdtypeallZnumelr   ZdeviceZreshaper   r   Zview_as)r5   r   r   ZchannelsrowscolsBZxy_gridZptsZptsdZmapxZmapyoutr2   r2   r3   undistort_images   s8    ::r=   )Nr	   )typingr   r(   Zkornia.geometry.linalgr   Zkornia.geometry.transformr   Zkornia.utilsr   Zdistortr   r   ZTensorintr4   r=   r2   r2   r2   r3   <module>   s    f