Ë
    ÃÓ^jB  ã                  óü  — d Z ddlmZ 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 g d
¢Z	 	 	 	 	 	 	 	 	 	 dd„Z	 d	 	 	 	 	 	 	 	 	 dd„Z ed«      	 	 	 	 	 	 dd„«       Zdd„Zdd„Ze	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd„«       Z	 	 	 	 	 	 	 	 	 	 	 	 dd„Z ed«      	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 	 	 d d„«       Z	 	 	 	 	 	 	 	 	 	 	 	 d!d„Z	 	 	 	 	 	 	 	 	 	 	 	 d"d„Z	 d#	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d$d„Z y)%a—  Functional implementations of image cropping operations.

This module provides utility functions for performing various cropping operations on images,
bounding boxes, and keypoints. It includes functions to calculate crop coordinates, crop images,
and handle the corresponding transformations for bounding boxes and keypoints to maintain
consistency between different data types during cropping operations.
é    )Úannotations)ÚSequence)ÚAnyN)Úmaybe_process_in_chunksÚpreserve_channel_dim)Ú
functional)Úhandle_empty_array)Údenormalize_bboxesÚnormalize_bboxes)ÚcropÚcrop_and_padÚcrop_and_pad_bboxesÚcrop_and_pad_keypointsÚcrop_bboxes_by_coordsÚcrop_keypoints_by_coordsÚget_center_crop_coordsÚget_crop_coordsÚpad_along_axesÚvolume_crop_yxÚvolumes_crop_yxc                ó¾   — | dd \  }}t        |d   |«      }t        |d   |«      }t        ||z
  dz   |z  «      }||z   }	t        ||z
  dz   |z  «      }
|
|z   }|
|||	fS )aS  Get crop coordinates.

    This function gets the crop coordinates.

    Args:
        image_shape (tuple[int, int]): Original image shape.
        crop_shape (tuple[int, int]): Crop shape.
        h_start (float): Start height.
        w_start (float): Start width.

    Returns:
        tuple[int, int, int, int]: Crop coordinates.

    Né   r   é   )ÚminÚint)Úimage_shapeÚ
crop_shapeÚh_startÚw_startÚheightÚwidthÚcrop_heightÚ
crop_widthÚy_minÚy_maxÚx_minÚx_maxs               úx/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/albumentations/augmentations/crops/functional.pyr   r   %   sŠ   € ð2    �O�M€FˆEô �j ‘m VÓ,€KÜ�Z ‘] EÓ*€Jä�˜+Ñ%¨Ñ)¨WÑ4Ó5€EØ�KÑ€EÜ�˜Ñ# aÑ'¨7Ñ2Ó3€EØ�JÑ€EØ�%˜ Ð%Ð%ó    c                óœ  — | j                   s| S |r8t        | j                  «       j                  t        j
                  «      |«      }n-| j                  «       j                  t        j
                  «      }|dd \  }}|dd…ddgfxx   |z  cc<   |dd…ddgfxx   |z  cc<   |d   |d   z
  }|d   |d   z
  }||f}	|rt        ||	«      S |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
                             can be either normalized (in [0, 1]) if normalized_input=True or
                             absolute pixel values if normalized_input=False.
        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).
        normalized_input (bool): Whether input boxes are in normalized coordinates.
                               If True, assumes input is normalized [0,1] and returns normalized coordinates.
                               If False, assumes input is in absolute pixels and returns absolute coordinates.
                               Default: True for backward compatibility.

    Returns:
        np.ndarray: Array of cropped bounding boxes. Coordinates will be in the same format as input
                   (normalized if normalized_input=True, absolute pixels if normalized_input=False).

    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
   ÚcopyÚastypeÚnpÚfloat32r   )
ÚbboxesÚcrop_coordsr   Únormalized_inputÚcropped_bboxesr&   r$   r"   r#   r   s
             r(   r   r   K   sÛ   € ð@ �;Š;Øˆñ Ü+¨F¯K©K«M×,@Ñ,@ÄÇÁÓ,LÈkÓZ‰àŸ™›×-Ñ-¬b¯j©jÓ9ˆà˜r �?�L€Eˆ5ð ’1�q˜!�f�9Ó Ñ&ÓØ’1�q˜!�f�9Ó Ñ&Óð ˜a‘. ;¨q¡>Ñ1€KØ˜Q‘ +¨a¡.Ñ0€JØ˜zÐ*€Jñ <LÔ˜N¨JÓ7Ð_ÐQ_Ð_r)   Ú	keypointsc                óz   — |dd \  }}| j                  «       }|dd…dfxx   |z  cc<   |dd…dfxx   |z  cc<   |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-   )r5   r2   Úx1Úy1Úcropped_keypointss        r(   r   r   ƒ   sN   € ð ˜˜!ˆ_�F€Bˆà!Ÿ™Ó(ÐØ’a˜�dÓ˜rÑ!ÓØ’a˜�dÓ˜rÑ!ÓàÐr)   c                ób   — | dd \  }}|dd \  }}||z
  dz  }||z   }||z
  dz  }||z   }	|||	|fS )a  Get center crop coordinates.

    This function gets the center crop coordinates.

    Args:
        image_shape (tuple[int, int]): Original image shape.
        crop_shape (tuple[int, int]): Crop shape.

    Returns:
        tuple[int, int, int, int]: Center crop coordinates.

    Nr   © )
r   r   r    r!   r"   r#   r$   r%   r&   r'   s
             r(   r   r   ›   sd   € ð    �O�M€FˆEØ(¨¨!˜nÑ€K�à�kÑ! aÑ'€EØ�KÑ€EØ�ZÑ AÑ%€EØ�JÑ€EØ�%˜ Ð%Ð%r)   c                óä   — | j                   dd \  }}||k  s||k  rt        d|› d|› d|› d|› d�	«      ‚|dk  s||kD  s
|dk  s||kD  rt        d	|› d|› d|› d|› d
|› d|› d�«      ‚| ||…||…f   S )aF  Crop an image.

    This function crops an image.

    Args:
        img (np.ndarray): Input image.
        x_min (int): Minimum x coordinate.
        y_min (int): Minimum y coordinate.
        x_max (int): Maximum x coordinate.
        y_max (int): Maximum y coordinate.

    Returns:
        np.ndarray: Cropped image.

    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)Úimgr&   r$   r'   r%   r    r!   s          r(   r   r   ²   sÌ   € ð  —I‘I˜b˜q�M�M€FˆEØ�‚~˜ %šÜðØ˜˜z¨%¨°
¸5¸'ÀÈEÈ7ÐRSðUó
ð 	
ð
 ˆq‚y�E˜E’M U¨Q¢Y°%¸&².ÜðØ�w˜j¨¨¨z¸%¸À
È5È'ð RØ�x˜z¨%¨°ð3ó
ð 	
ð ˆu�Uˆ{˜E %˜KÐ'Ñ(Ð(r)   c           	     ó  — |�t        d„ |D «       «      rt        | g|¢­Ž } |�:t        d„ |D «       «      r(t        j                  | |d   |d   |d   |d   ||¬«      } |r.|dd \  }}	t	        t
        j                  |	|f|¬	«      }
 |
| «      S | S )
aV  Crop and pad an image.

    This function crops and pads an image.

    Args:
        img (np.ndarray): Input image.
        crop_params (tuple[int, int, int, int] | None): Crop parameters.
        pad_params (tuple[int, int, int, int] | None): Pad parameters.
        pad_value (tuple[float, ...] | float | None): Pad value.
        image_shape (tuple[int, int]): Original image shape.
        interpolation (int): Interpolation method.
        pad_mode (int): Pad mode.
        keep_size (bool): Whether to keep the original size.

    Returns:
        np.ndarray: Cropped and padded image.

    Nc              3  ó&   K  — | ]	  }|d k7  –— Œ y­w©r   Nr;   ©Ú.0Úis     r(   ú	<genexpr>zcrop_and_pad.<locals>.<genexpr>ð   s   è ø€ Ò&C°! q¨A¥vÑ&Cùó   ‚c              3  ó&   K  — | ]	  }|d k7  –— Œ y­wrD   r;   rE   s     r(   rH   zcrop_and_pad.<locals>.<genexpr>ò   s   è ø€ Ò%A° a¨1¥fÑ%AùrI   r   r   r   r+   )Úborder_modeÚvalue)ÚdsizeÚinterpolation)Úanyr   Ú
fgeometricÚpad_with_paramsr   Úcv2Úresize)rA   Úcrop_paramsÚ
pad_paramsÚ	pad_valuer   rN   Úpad_modeÚ	keep_sizeÚrowsÚcolsÚ	resize_fns              r(   r   r   Ó   s¨   € ð: Ð¤3Ñ&C°{Ô&CÔ#CÜ�3Ð%˜Ò%ˆØÐ¤#Ñ%A°jÔ%AÔ"AÜ×(Ñ(ØØ�q‰MØ�q‰MØ�q‰MØ�q‰MØ Øô
ˆñ Ø   !�_‰
ˆˆdÜ+¬C¯J©J¸tÀT¸lÐZgÔhˆ	Ù˜‹~Ðà€Jr)   c                ó  — t        | «      dk(  r| S t        | |«      }|�.|dd \  }}|dd…ddgfxx   |z  cc<   |dd…ddgfxx   |z  cc<   |�-|\  }}	}
}	|dd…ddgfxx   |
z  cc<   |dd…ddgfxx   |z  cc<   t        ||«      S )aì  Crop and pad bounding boxes.

    This function crops and pads bounding boxes.

    Args:
        bboxes (np.ndarray): Array of bounding boxes.
        crop_params (tuple[int, int, int, int] | None): Crop parameters.
        pad_params (tuple[int, int, int, int] | None): Pad parameters.
        image_shape (tuple[int, int]): Original image shape.
        result_shape (tuple[int, int]): Result image shape.

    Returns:
        np.ndarray: Array of cropped and padded bounding boxes.

    r   Nr   r   r+   )Úlenr
   r   )r1   rT   rU   r   Úresult_shapeÚdenormalized_bboxesÚcrop_xÚcrop_yÚtopÚ_Úlefts              r(   r   r     s¼   € ô, ˆ6ƒ{�aÒØˆô -¨V°[ÓAÐàÐØ$ R a˜‰ˆ�àšA  1˜v˜IÓ&¨&Ñ0Ó&ØšA  1˜v˜IÓ&¨&Ñ0Ó&àÐØ$‰ˆˆQ��aàšA  1˜v˜IÓ&¨$Ñ.Ó&ØšA  1˜v˜IÓ&¨#Ñ-Ó&ô Ð/°Ó>Ð>r)   c                óN  — | j                  «       }|�*|dd \  }}|dd…dfxx   |z  cc<   |dd…dfxx   |z  cc<   |�)|\  }	}
}}
|dd…dfxx   |z  cc<   |dd…dfxx   |	z  cc<   |dd \  }}|dd \  }}|r+||k7  s||k7  r!||z  }||z  }t        j                  |||«      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-   rP   Úkeypoints_scale)r5   rT   rU   r   r^   rX   Útransformed_keypointsÚcrop_x1Úcrop_y1rb   rc   rd   rY   rZ   Úresult_rowsÚresult_colsÚscale_xÚscale_ys                     r(   r   r   1  së   € ð. &ŸN™NÓ,ÐàÐØ& r¨˜?Ñˆ�Øša ˜dÓ# wÑ.Ó#Øša ˜dÓ# wÑ.Ó#àÐØ$‰ˆˆQ��aØša ˜dÓ# tÑ+Ó#Øša ˜dÓ# sÑ*Ó#à˜R˜a��J€Dˆ$Ø+¨B¨QÐ/Ñ€K�á�k TÒ)¨[¸DÒ-@Ø˜Ñ$ˆØ˜Ñ$ˆÜ×)Ñ)Ð*?ÀÈ'ÓRÐRà Ð r)   c                óþ   — | j                   dd \  }}}||k  s||k  rt        d|› d|› d|› d|› d�	«      ‚|dk  s|dk  s
||kD  s||kD  r't        d	|› d|› d|› d|› d
| j                   dd › �
«      ‚| dd…||…||…f   S )aè  Crop a single volume along Y (height) and X (width) axes only.

    Args:
        volume (np.ndarray): Input volume with shape (D, H, W) or (D, H, W, C).
        x_min (int): Minimum width coordinate.
        y_min (int): Minimum height coordinate.
        x_max (int): Maximum width coordinate.
        y_max (int): Maximum height coordinate.

    Returns:
        np.ndarray: Cropped volume (D, H_new, W_new, [C]).

    Raises:
        ValueError: If crop coordinates are invalid.

    Nr+   ú5Crop coordinates must satisfy min < max. Got: (x_min=ú, y_min=ú, x_max=ú, y_max=r=   r   úECrop coordinates must be within image dimensions (H, W). Got: (x_min=ú) for volume shape r>   )Úvolumer&   r$   r'   r%   rc   r    r!   s           r(   r   r   _  sÕ   € ð. —|‘| B QÐ'Ñ€A€vˆuØ�‚~˜ %šÜðØ�W˜H U G¨8°E°7¸(À5À'ÈðLó
ð 	
ð
 ˆq‚y�E˜A’I ¨¢°%¸&².ÜðØ�W˜H U G¨8°E°7¸(À5À'ð J Ø &§¡¨R¨aÐ 0Ð1ð3ó
ð 	
ð ’!�U˜5�[ %¨ +Ð-Ñ.Ð.r)   c                óN  — d| j                   cxk  rdk  sn t        d| j                   › �«      ‚| j                  dd \  }}}||k  s||k  rt        d|› d|› d|› d|› d	�	«      ‚|d
k  s|d
k  s
||kD  s||kD  rt        d|› d|› d|› d|› d|||f› �
«      ‚| dd…dd…||…||…f   S )a'  Crop a batch of volumes along Y (height) and X (width) axes only.

    Args:
        volumes (np.ndarray): Input batch of volumes with shape (B, D, H, W) or (B, D, H, W, C).
        x_min (int): Minimum width coordinate.
        y_min (int): Minimum height coordinate.
        x_max (int): Maximum width coordinate.
        y_max (int): Maximum height coordinate.

    Returns:
        np.ndarray: Cropped batch of volumes (B, D, H_new, W_new, [C]).

    Raises:
        ValueError: If crop coordinates are invalid or volumes shape is incorrect.

    é   é   z1Input volumes should have 4 or 5 dimensions, got r   ro   rp   rq   rr   r=   r   rs   rt   N)Úndimr@   r?   )Úvolumesr&   r$   r'   r%   Údepthr    r!   s           r(   r   r   ˆ  sû   € ð. �—‘Ô! Ô!ÜÐLÈWÏ\É\ÈNÐ[Ó\Ð\à"Ÿ=™=¨¨1Ð-Ñ€Eˆ6�5Ø�‚~˜ %šÜðØ�W˜H U G¨8°E°7¸(À5À'ÈðLó
ð 	
ð
 ˆq‚y�E˜A’I ¨¢°%¸&².ÜðØ�W˜H U G¨8°E°7¸(À5À'ð J Ø!&¨°Ð 6Ð7ð9ó
ð 	
ð ’1’a˜˜u˜ e¨E kÐ1Ñ2Ð2r)   c	           
     óì  — | j                   }	d|cxk  r|	k  rn nd|cxk  r|	k  sn t        d|› d|› d|	› d�«      ‚||k(  rt        d|› d|› d�«      ‚t        j                  d	t        j                  d
t        j
                  dt        j                  dt        j                  di}
||
vrt        d|› �«      ‚|
|   }dg|	z  }||f||<   ||f||<   d|i}|d	k(  r||d<   t        j                  | |fi |¤ŽS )a  Pad an array along specified height (H) and width (W) axes using np.pad.

    Args:
        arr (np.ndarray): Input array.
        pad_top (int): Padding added to the top (start of H axis).
        pad_bottom (int): Padding added to the bottom (end of H axis).
        pad_left (int): Padding added to the left (start of W axis).
        pad_right (int): Padding added to the right (end of W axis).
        h_axis (int): Index of the height axis (Y).
        w_axis (int): Index of the width axis (X).
        border_mode (int): OpenCV border mode.
        pad_value (float | Sequence[float]): Value for constant padding.

    Returns:
        np.ndarray: Padded array.

    Raises:
        ValueError: If border_mode is unsupported or axis indices are out of bounds.

    r   zAxis indices z, z" are out of bounds for array with z dimensions.zHeight axis z and width axis z cannot be the same.ÚconstantÚedgeÚreflectÚ	symmetricÚwrapzUnsupported border_mode: ©r   r   ÚmodeÚconstant_values)
ry   r@   rR   ÚBORDER_CONSTANTÚBORDER_REPLICATEÚBORDER_REFLECTÚBORDER_REFLECT_101ÚBORDER_WRAPr/   Úpad)ÚarrÚpad_topÚ
pad_bottomÚpad_leftÚ	pad_rightÚh_axisÚw_axisrK   rV   ry   Úmode_mapÚnp_modeÚ	pad_widthÚkwargss                 r(   r   r   ´  s1  € ð> �8‰8€DØ�Ô˜$Õ 1¨Ô#5°Ô#5Ü˜=¨¨°°6°(Ð:\Ð]aÐ\bÐbnÐoÓpÐpØ�ÒÜ˜<¨ xÐ/?À¸xÐG[Ð\Ó]Ð]ô 	×Ñ˜ZÜ×Ñ˜fÜ×Ñ˜IÜ×Ñ Ü�‰˜ð€Hð ˜(Ñ"ÜÐ4°[°MÐBÓCÐCØ�{Ñ#€Gà�˜4‘€IØ  *Ð-€IˆfÑØ! 9Ð-€IˆfÑð % gÐ.€Fà�*ÒØ$-ˆÐ Ñ!ä�6‰6�#�yÑ+ FÑ+Ð+r)   )
r   útuple[int, int]r   r–   r   Úfloatr   r—   Úreturnútuple[int, int, int, int])T)
r1   ú
np.ndarrayr2   r™   r   r–   r3   Úboolr˜   rš   )r5   rš   r2   r™   r˜   rš   )r   r–   r   r–   r˜   r™   )rA   rš   r&   r   r$   r   r'   r   r%   r   r˜   rš   )rA   rš   rT   ú tuple[int, int, int, int] | NonerU   rœ   rV   z tuple[float, ...] | float | Noner   r–   rN   r   rW   r   rX   r›   r˜   rš   )r1   rš   rT   rœ   rU   rœ   r   r–   r^   r–   r˜   rš   )NNr‚   r‚   F)r5   rš   rT   rœ   rU   rœ   r   r–   r^   r–   rX   r›   r˜   rš   )ru   rš   r&   r   r$   r   r'   r   r%   r   r˜   rš   )rz   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   rK   r   rV   zfloat | Sequence[float]r˜   rš   )!Ú__doc__Ú
__future__r   Úcollections.abcr   Útypingr   rR   Únumpyr/   Úalbucorer   r   Ú&albumentations.augmentations.geometricr   rP   Ú"albumentations.augmentations.utilsr	   Úalbumentations.core.bbox_utilsr
   r   Ú__all__r   r   r   r   r   r   r   r   r   r   r   r;   r)   r(   ú<module>r§      sÛ  ðñõ #å $Ý ã 
Û ß Bå KÝ Aß Oò€ð#&Ø ð#&àð#&ð ð#&ð ð	#&ð
 ó#&ðT "ð	5`Øð5`à*ð5`ð !ð5`ð ð	5`ð
 ó5`ñp �KÓ ðØðà*ðð òó !ðó.&ó.)ðB ð.Ø	ð.à1ð.ð 1ð.ð 0ð	.ð
 !ð.ð ð.ð ð.ð ð.ð ò.ó ð.ðb)?Øð)?à1ð)?ð 1ð)?ð !ð	)?ð
 "ð)?ð ó)?ñX �KÓ ð 59Ø37Ø#)Ø$*Øð*!Øð*!à1ð*!ð 1ð*!ð !ð	*!ð
 "ð*!ð ð*!ð ò*!ó !ð*!ðZ&/Øð&/àð&/ð ð&/ð ð	&/ð
 ð&/ð ó&/ðR)3Øð)3àð)3ð ð)3ð ð	)3ð
 ð)3ð ó)3ðj *+ð:,Ø	ð:,àð:,ð ð:,ð ð	:,ð
 ð:,ð ð:,ð ð:,ð ð:,ð 'ð:,ð ô:,r)   