U
    h(!                     @  sH  d dl mZ d dlmZ d dlZd dlZd dlmZm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZ d	d
ddddddgZdddddddd	Zddddddd
ZeddddddZddddddZdddddddddZe	dd d d!dddd"dd#	d$dZdd%d%dddd&d'dZed,dd%d%ddd"dd*d+dZdS )-    )annotations)SequenceN)maybe_process_in_chunkspreserve_channel_dim)
functional)handle_empty_array)denormalize_bboxesnormalize_bboxes)	ColorTypeget_crop_coordscrop_bboxes_by_coordscrop_keypoints_by_coordsget_center_crop_coordscropcrop_and_padcrop_and_pad_bboxescrop_and_pad_keypointsztuple[int, int]floatztuple[int, int, int, int])image_shape
crop_shapeh_startw_startreturnc                 C  sd   | d d \}}|d d \}}t || d | }|| }	t || d | }
|
| }|
|||	fS )N      )int)r   r   r   r   heightwidthcrop_height
crop_widthy_miny_maxx_minx_max r$   Q/tmp/pip-unpacked-wheel-e8onvpoz/albumentations/augmentations/crops/functional.pyr      s    z
np.ndarray)bboxescrop_coordsr   r   c           	      C  s   | j s
| S t|  tj|}|dd \}}|ddddgf  |8  < |ddddgf  |8  < |d |d  }|d |d  }||f}t||S )a  Crop bounding boxes based on given crop coordinates.

    This function adjusts bounding boxes to fit within a cropped image.

    Args:
        bboxes (np.ndarray): Array of bounding boxes with shape (N, 4+) where each row is
                             [x_min, y_min, x_max, y_max, ...]. The bounding box coordinates
                             should be normalized (in the range [0, 1]).
        crop_coords (tuple[int, int, int, int]): Crop coordinates (x_min, y_min, x_max, y_max)
                                                 in absolute pixel values.
        image_shape (tuple[int, int]): Original image shape (height, width).

    Returns:
        np.ndarray: Array of cropped bounding boxes, normalized to the new crop size.

    Note:
        Bounding boxes that fall completely outside the crop area will be removed.
        Bounding boxes that partially overlap with the crop area will be adjusted to fit within it.
    Nr   r   r      )sizer   copyZastypenpZfloat32r	   )	r&   r'   r   Zcropped_bboxesr"   r    r   r   r   r$   r$   r%   r   0   s    )	keypointsr'   r   c                 C  sL   |dd \}}|   }|dddf  |8  < |dddf  |8  < |S )a  Crop keypoints using the provided coordinates of bottom-left and top-right corners in pixels.

    Args:
        keypoints (np.ndarray): An array of keypoints with shape (N, 4+) where each row is (x, y, angle, scale, ...).
        crop_coords (tuple): Crop box coords (x1, y1, x2, y2).

    Returns:
        np.ndarray: An array of cropped keypoints with the same shape as the input.
    Nr   r   r   )r*   )r,   r'   x1y1Zcropped_keypointsr$   r$   r%   r   \   s
    )r   r   r   c           
      C  sT   | d d \}}|d d \}}|| d }|| }|| d }|| }	|||	|fS )Nr   r$   )
r   r   r   r   r   r   r    r!   r"   r#   r$   r$   r%   r   s   s    r   )imgr"   r    r#   r!   r   c                 C  s   | j d d \}}||ks"||krDtd| d| d| d| d	|dk sd||ksd|dk sd||krtd| d| d| d| d	| d
| d| ||||f S )Nr   zDWe should have x_min < x_max and y_min < y_max. But we got (x_min = z
, y_min = z
, x_max = z
, y_max = )r   zUValues for crop should be non negative and equal or smaller than image sizes(x_min = z, height = z
, width = )shape
ValueError)r/   r"   r    r#   r!   r   r   r$   r$   r%   r   ~   s     (zSequence[int] | NonezColorType | Nonebool)	r/   crop_params
pad_params	pad_valuer   interpolationpad_mode	keep_sizer   c              	   C  s   |d k	r(t dd |D r(t| f| } |d k	rjt dd |D rjtj| |d |d |d |d ||d} |r|d d \}}	ttj|	|f|d	}
|
| S | S )
Nc                 s  s   | ]}|d kV  qdS r   Nr$   .0ir$   r$   r%   	<genexpr>   s     zcrop_and_pad.<locals>.<genexpr>c                 s  s   | ]}|d kV  qdS r:   r$   r;   r$   r$   r%   r>      s     r   r   r   r(   )Zborder_modevalue)Zdsizer7   )anyr   
fgeometricZpad_with_paramsr   cv2resize)r/   r4   r5   r6   r   r7   r8   r9   rowscolsZ	resize_fnr$   r$   r%   r      s"    
z tuple[int, int, int, int] | None)r&   r4   r5   r   result_shaper   c                 C  s   t | dkr| S t| |}|d k	rj|d d \}}|d d ddgf  |8  < |d d ddgf  |8  < |d k	r|\}}	}
}	|d d ddgf  |
7  < |d d ddgf  |7  < t||S )Nr   r   r   r(   )lenr   r	   )r&   r4   r5   r   rF   Zdenormalized_bboxesZcrop_xZcrop_ytop_leftr$   r$   r%   r      s    
r   r   F)r,   r4   r5   r   rF   r9   r   c                 C  s   |   }|dk	rP|dd \}}|dddf  |8  < |dddf  |8  < |dk	r|\}	}
}}
|dddf  |7  < |dddf  |	7  < |dd \}}|dd \}}|r||ks||kr|| }|| }t|||S |S )a  Crop and pad multiple keypoints simultaneously.

    Args:
        keypoints (np.ndarray): Array of keypoints with shape (N, 4+) where each row is (x, y, angle, scale, ...).
        crop_params (Sequence[int], optional): Crop parameters [crop_x1, crop_y1, ...].
        pad_params (Sequence[int], optional): Pad parameters [top, bottom, left, right].
        image_shape (Tuple[int, int]): Original image shape (rows, cols).
        result_shape (Tuple[int, int]): Result image shape (rows, cols).
        keep_size (bool): Whether to keep the original size.

    Returns:
        np.ndarray: Array of transformed keypoints with the same shape as input.
    Nr   r   r   )r*   rA   Zkeypoints_scale)r,   r4   r5   r   rF   r9   Ztransformed_keypointsZcrop_x1Zcrop_y1rH   rI   rJ   rD   rE   Zresult_rowsZresult_colsZscale_xZscale_yr$   r$   r%   r      s     )NNrK   rK   F)
__future__r   typingr   rB   Znumpyr+   Zalbucorer   r   Z&albumentations.augmentations.geometricr   rA   Z"albumentations.augmentations.utilsr   Zalbumentations.core.bbox_utilsr   r	   Zalbumentations.core.typesr
   __all__r   r   r   r   r   r   r   r   r$   r$   r$   r%   <module>   sB   ,      