o
    oi~E                     @   s  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mZ d dlmZ d dlmZmZ ddlmZ dd	lmZmZ g d
Z			d+de	de	deeef dededede	fddZ			d+de	deeef dededede	fddZ				d,de	de	de	dedededede	fddZ			d+de	de	deeef dededede	fd d!Z				"	#d-de	de	deeeef  d$edee d%ed&ede	fd'd(ZG d)d* d*eZ dS ).    )OptionalTupleUnionN)Resample)ModuleTensor	as_tensorpadtensor)KORNIA_CHECK_SHAPE)infer_bbox_shapevalidate_bbox   )resize)get_perspective_transformwarp_affine)CenterCrop2Dcenter_cropcrop_and_resizecrop_by_boxescrop_by_indicescrop_by_transform_matbilinearzerosTinput_tensorboxessizemodepadding_modealign_cornersreturnc           
      C   s   t | tstdt|  t |tstdt| t |ttfs0t|dkr0td| t| jdkr@t	d| j d|\}}|
| }tddg|d	 dg|d	 |d	 gd|d	 ggg| j| jd
|jd dd}	t| ||	|||S )a  Extract crops from 2D images (4D tensor) and resize given a bounding box.

    Args:
        input_tensor: the 2D image tensor with shape (B, C, H, W).
        boxes : a tensor containing the coordinates of the bounding boxes to be extracted.
            The tensor must have the shape of Bx4x2, where each box is defined in the following (clockwise)
            order: top-left, top-right, bottom-right and bottom-left. The coordinates must be in the x, y order.
            The coordinates would compose a rectangle with a shape of (N1, N2).
        size: a tuple with the height and width that will be
            used to resize the extracted patches.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | 'reflection'.
        align_corners: mode for grid_generation.

    Returns:
        Tensor: tensor containing the patches with shape BxCxN1xN2.

    Example:
        >>> input = torch.tensor([[[
        ...     [1., 2., 3., 4.],
        ...     [5., 6., 7., 8.],
        ...     [9., 10., 11., 12.],
        ...     [13., 14., 15., 16.],
        ... ]]])
        >>> boxes = torch.tensor([[
        ...     [1., 1.],
        ...     [2., 1.],
        ...     [2., 2.],
        ...     [1., 2.],
        ... ]])  # 1x4x2
        >>> crop_and_resize(input, boxes, (2, 2), mode='nearest', align_corners=True)
        tensor([[[[ 6.,  7.],
                  [10., 11.]]]])

    'Input tensor type is not a Tensor. Got z&Input boxes type is not a Tensor. Got    1Input size must be a tuple/list of length 2. Got    3Only tensor with shape (B, C, H, W) supported. Got .r   r   devicedtype)
isinstancer   	TypeErrortypetuplelistlen
ValueErrorshapeAssertionErrortor
   r(   r)   expandr   )
r   r   r   r   r   r   dst_hdst_w
points_src
points_dst r:   T/home/ubuntu/.local/lib/python3.10/site-packages/kornia/geometry/transform/crop2d.pyr   (   s$   
-

,r   c                 C   sP  t | tstdt|  t |ttfs"t|dkr"td| t| jdkr2t	d| j d|\}}| jdd \}}|d }	|d }
|d }|d }||
 }||	 }|| d	 }|| d	 }t
||g||g||g||ggg| j| jd
}t
ddg|d	 dg|d	 |d	 gd|d	 ggg| j| jd
|jd dd}t| |||||S )a  Crop the 2D images (4D tensor) from the center.

    Args:
        input_tensor: the 2D image tensor with shape (B, C, H, W).
        size: a tuple with the expected height and width
          of the output patch.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: mode for grid_generation.

    Returns:
        the output tensor with patches.

    Examples:
        >>> input = torch.tensor([[[
        ...     [1., 2., 3., 4.],
        ...     [5., 6., 7., 8.],
        ...     [9., 10., 11., 12.],
        ...     [13., 14., 15., 16.],
        ...  ]]])
        >>> center_crop(input, (2, 4), mode='nearest', align_corners=True)
        tensor([[[[ 5.,  6.,  7.,  8.],
                  [ 9., 10., 11., 12.]]]])

    r!   r"   r#   r$   r%   r&   Nr   r'   r   r*   )r+   r   r,   r-   r.   r/   r0   r1   r2   r3   r
   r(   r)   r5   r   )r   r   r   r   r   r6   r7   src_hsrc_w
dst_h_half
dst_w_half
src_h_half
src_w_halfstart_xstart_yend_xend_yr8   r9   r:   r:   r;   r   s   s:   
",r   src_boxdst_boxvalidate_boxesc                 C   s   |r
t | t | t| jdkrtd| j dt|| || }t|}|d |d d k rA|d |d d k sPtd|d  d|d  dt|d d 	 }	t|d d 	 }
t
| ||	|
f|||dS )	a&	  Perform crop transform on 2D images (4D tensor) given two bounding boxes.

    Given an input tensor, this function selected the interested areas by the provided bounding boxes (src_box).
    Then the selected areas would be fitted into the targeted bounding boxes (dst_box) by a perspective transformation.
    So far, the ragged tensor is not supported by PyTorch right now. This function hereby requires the bounding boxes
    in a batch must be rectangles with same width and height.

    Args:
        input_tensor: the 2D image tensor with shape (B, C, H, W).
        src_box: a tensor with shape (B, 4, 2) containing the coordinates of the bounding boxes
            to be extracted. The tensor must have the shape of Bx4x2, where each box is defined in the clockwise
            order: top-left, top-right, bottom-right and bottom-left. The coordinates must be in x, y order.
        dst_box: a tensor with shape (B, 4, 2) containing the coordinates of the bounding boxes
            to be placed. The tensor must have the shape of Bx4x2, where each box is defined in the clockwise
            order: top-left, top-right, bottom-right and bottom-left. The coordinates must be in x, y order.
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode: padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: mode for grid_generation.
        validate_boxes: flag to perform validation on boxes.

    Returns:
        Tensor: the output tensor with patches.

    Examples:
        >>> input = torch.arange(16, dtype=torch.float32).reshape((1, 1, 4, 4))
        >>> src_box = torch.tensor([[
        ...     [1., 1.],
        ...     [2., 1.],
        ...     [2., 2.],
        ...     [1., 2.],
        ... ]])  # 1x4x2
        >>> dst_box = torch.tensor([[
        ...     [0., 0.],
        ...     [1., 0.],
        ...     [1., 1.],
        ...     [0., 1.],
        ... ]])  # 1x4x2
        >>> crop_by_boxes(input, src_box, dst_box, align_corners=True)
        tensor([[[[ 5.0000,  6.0000],
                  [ 9.0000, 10.0000]]]])

    Note:
        If the src_box is smaller than dst_box, the following error will be thrown.
        RuntimeError: solve_cpu: For batch 0: U(2,2) is zero, singular U.

    r$   r%   r&   r   r   zKCropping height, width and depth must be exact same in a batch. Got height z and width r   r   r   )r   r0   r2   r3   r   r4   r   allintitemr   )r   rG   rH   r   r   r   rI   dst_trans_srcbboxh_outw_outr:   r:   r;   r      s    90r   	transformout_sizec                 C   sP   t || jd dd| j| jd}t| |ddddddf ||||d}|S )a~  Perform crop transform on 2D images (4D tensor) given a perspective transformation matrix.

    Args:
        input_tensor: the 2D image tensor with shape (B, C, H, W).
        transform: a perspective transformation matrix with shape (B, 3, 3).
        out_size: size of the output image (height, width).
        mode: interpolation mode to calculate output values
          ``'bilinear'`` | ``'nearest'``.
        padding_mode (str): padding mode for outside grid values
          ``'zeros'`` | ``'border'`` | ``'reflection'``.
        align_corners: mode for grid_generation.

    Returns:
        the output tensor with patches.

    r   r*   r'   Nr"   rJ   )r   r5   r2   r(   r)   r   )r   rR   rS   r   r   r   rN   patchesr:   r:   r;   r     s   	r   Fr   interpolation	antialiasshape_compensationc              	   C   sf  t | g d t |g d | j\}}}	}	t|| jtjd}
|
ddddf }|
ddddf d }|
ddddf }|
ddddf d }t|jdd	t|jdd	  krrt|jdd	  krrt|jdd	  krrdkrn n2| d
t|d t|d t|d t|d f }|dur|jdd |krt	||||d|dS |du rt
|
\}}|jdd	|jdd	f}tj||g|R | j| jd}t|D ]`\}}	| ||d ddt|| t|| t|| t|| f }|jdd |kr,|dkrt	||||d|d||< qt|d|d |jd  d|d |jd  g||< q|||< q|S )a\  Crop tensors with naive indices.

    Args:
        input_tensor: the 2D image tensor with shape (B, C, H, W).
        src_box: a tensor with shape (B, 4, 2) containing the coordinates of the bounding boxes
            to be extracted. The tensor must have the shape of Bx4x2, where each box is defined in the clockwise
            order: top-left, top-right, bottom-right and bottom-left. The coordinates must be in x, y order.
        size: output size. An auto resize or pad will be performed according to ``shape_compensation``
            if the cropped slice sizes are not exactly align `size`.
            If None, will auto-infer from src_box.
        interpolation:  algorithm used for upsampling: ``'nearest'`` | ``'linear'`` | ``'bilinear'`` |
            'bicubic' | 'trilinear' | 'area'.
        align_corners: interpolation flag.
        antialias: if True, then image will be filtered with Gaussian before downscaling.
            No effect for upscaling.
        shape_compensation: if the cropped slice sizes are not exactly align `size`, the image can either be padded
            or resized.

    )BCHW)rX   42r'   Nr   r      F)sorted.r<   short)rU   r   siderV   r   r*   )r   r2   r   r(   torchlongr0   uniquerL   r   r   emptyr)   	enumerater	   )r   rG   r   rU   r   rV   rW   rX   rY   _srcx1x2y1y2outhwi_outr:   r:   r;   r   <  sR   
6 D
4	
r   c                       sn   e Zd ZdZdejjdfdeee	eef f de
deeeef dedd	f
 fd
dZdedefddZ  ZS )r   aF  Center crop the input tensor.

    Args:
        size: Size (h, w) in pixels of the resized region or just one side.
        align_corners: interpolation flag.
        resample: Resampling mode.
        cropping_mode: Cropping mode, "resample" or "slice".

    Note:
        For JIT, the cropping mode must be "resample".
    Tslicer   r   resamplecropping_moder    Nc                    s   t    t|tr|d |d f| _nt|tr||f| _n
tdt| d| j\}}tj	ddg|d dg|d |d gd|d gggtj
d}| d|  | d| t|||| jdd	| _d S )
Nr   r   z8Invalid size type. Expected (int, tuple(int, int). Got: r&   )r)   r8   r9   r   )rs   rt   r   r   r   )super__init__r+   r.   r   rL   	Exceptionr-   rb   r
   rc   register_buffercloner   getflags)selfr   r   rs   rt   r6   r7   r9   	__class__r:   r;   rv     s    



:zCenterCrop2D.__init__inputc                 C   s  |j d }| j\}}|j dd  \}}|d |d }}|d |d }	}
t|
| t|	| }}|| d || d }}|| jd< || jd< || jd< || jd< || jd	< || jd
< || jd< || jd< | jd dkrt| j|dd|| j|dd|}||dd}t	||d d d dd d f | j| jd j
 d| jd S | jd dkrt|| j|dd|| jd S td| jd  d)Nr   r<   r"   r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   )r   r"   r   )r   r"   r   )r   r^   r   )r   r^   r   rt   rs   r*   r   r   rr   r   zNot supported type: r&   )r2   r   rL   r8   r{   r   r5   r4   r9   r   namelowerr   NotImplementedError)r|   r   
batch_sizer6   r7   r=   r>   r?   r@   rA   rB   rC   rD   rE   rF   rR   r:   r:   r;   forward  s@   









	$zCenterCrop2D.forward)__name__
__module____qualname____doc__r   BILINEARr   r   rL   r   boolstrrv   r   r   __classcell__r:   r:   r}   r;   r     s"    r   )r   r   T)r   r   TT)Nr   NFr   )!typingr   r   r   rb   kornia.constantsr   kornia.corer   r   r   r	   r
   kornia.core.checkr   kornia.geometry.bboxr   r   affwarpr   imgwarpr   r   __all__rL   r   r   r   r   r   r   r   r   r:   r:   r:   r;   <module>   s   

N

R
V

,
K