
    hڕ                    .   S r SSKJr  SSKJrJr  SSKrSSKrSSK	J
r
JrJrJrJrJr  SSKJr  SSKJr  SSKJr  / S	Qr\ S#       S$S
 jj5       r        S%S jr\S&S j5       rS'S jrS(S jrS)S jr          S*S jr          S+S jr          S,S jr          S-S jr           S.S jr!          S/S jr"\" S5      S0S j5       r#\" S5            S1S j5       r$            S2S jr%          S3S jr&              S4S jr'\" S5                  S5S j5       r(\" S5            S6S j5       r)S7S8S jjr*            S9S  jr+        S:S! jr,              S;S" jr-g)<a  Functional implementations of dropout operations for image augmentation.

This module provides low-level functions for various dropout techniques used in image
augmentation, including channel dropout, grid dropout, mask dropout, and coarse dropout.
These functions create and apply dropout patterns to images, masks, bounding boxes, and
keypoints, with support for different filling methods and hole generation strategies.
    )annotations)LiteralcastN)MAX_VALUES_BY_DTYPENUM_MULTI_CHANNEL_DIMENSIONSget_num_channelsis_grayscale_imagepreserve_channel_dimuint8_io)split_uniform_grid)handle_empty_array)MONO_CHANNEL_DIMENSIONS)calculate_grid_dimensionschannel_dropoutcutoutfilter_bboxes_by_holesfilter_keypoints_in_holesgenerate_grid_holesgenerate_random_fillc                l    [        U 5      (       a  Sn[        U5      eU R                  5       n X SU4'   U $ )aH  Drop channels from an image.

This function drops channels from an image.

Args:
    img (np.ndarray): Input image.
    channels_to_drop (int | tuple[int, ...] | np.ndarray): Channels to drop.
    fill (tuple[float, ...] | float): Value to fill the dropped channels with.

Returns:
    np.ndarray: Image with channels dropped.

z0Only one channel. ChannelDropout is not defined..)r	   NotImplementedErrorcopy)imgchannels_to_dropfillmsgs       i/var/www/fran/franai/venv/lib/python3.13/site-packages/albumentations/augmentations/dropout/functional.pyr   r   '   s=    & #@!#&&
((*C!%J    c                >   [         U    n[        R                  " U [        R                  5      (       a  UR	                  SUS-   XS9$ [        R                  " U [        R
                  5      (       a  UR                  SX1S9R                  U 5      $ [        SU  35      e)a  Generate a random fill array based on the given dtype and target shape.

This function creates a numpy array filled with random values. The range and type of these values
depend on the input dtype. For integer dtypes, it generates random integers. For floating-point
dtypes, it generates random floats.

Args:
    dtype (np.dtype): The data type of the array to be generated.
    shape (tuple[int, ...]): The shape of the array to be generated.
    random_generator (np.random.Generator): The random generator to use for generating values.
        If None, the default numpy random generator is used.

Returns:
    np.ndarray: A numpy array of the specified shape and dtype, filled with random values.

Raises:
    ValueError: If the input dtype is neither integer nor floating-point.

Examples:
    >>> import numpy as np
    >>> random_state = np.random.RandomState(42)
    >>> result = generate_random_fill(np.dtype('uint8'), (2, 2), random_state)
    >>> print(result)
    [[172 251]
     [ 80 141]]

r      )sizedtyper!   zUnsupported dtype: )	r   np
issubdtypeintegerintegersfloatinguniformastype
ValueError)r"   shaperandom_generator	max_values       r   r   r   C   s    @ $E*I	}}UBJJ''((IM(SS	}}UBKK((''9'AHHOO
*5'2
33r   c                   [        U 5      n[        R                  " U R                  SS [        R                  S9nU H  u  pVpxSXFU2XW24'   M     US:X  a  [
        R                  O[
        R                  n	US:X  a  U R                  [        :X  a  U R                  5       n [
        R                  " U [
        R                  5      n
[
        R                  " XSU	5      nU[        :X  a(  [
        R                  " U[
        R                  5      S   $ [
        R                  " U[
        R                  5      $ [
        R                  " XSU	5      $ )	ao  Apply OpenCV inpainting to fill the holes in the image.

Args:
    img (np.ndarray): Input image (grayscale or BGR)
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    method (Literal["inpaint_telea", "inpaint_ns"]): Inpainting method to use

Returns:
    np.ndarray: Inpainted image

Raises:
    NotImplementedError: If image has more than 3 channels

N   r"      inpaint_telear       ).N)r   r$   zerosr,   uint8cv2INPAINT_TELEA
INPAINT_NSndimr   squeezecvtColorCOLOR_GRAY2BGRinpaintCOLOR_BGR2GRAY)r   holesmethodnum_channelsmaskx_miny_minx_maxy_maxinpaint_methodimg_3chresults               r   apply_inpaintingrK   k   s     $C(L88CIIbqM2D&+"e),5[%+%& ', +1O*CS&&N q8833++-C,,sC$6$67WA~> ;; LL!3!34Y?	
 fc&8&89	
 ;;s!^44r   c                .    U H  u  p4pVX XF2X524'   M     U $ )zFill holes with a constant value.

Args:
    img (np.ndarray): Input image
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    fill (np.ndarray): Value to fill the holes with

 )r   r@   r   rD   rE   rF   rG   s          r   fill_holes_with_valuerN      s)     ',"e(,EK$% ',Jr   c                4    U H  u  p4pVX SS2XF2X524'   M     U $ )zFill holes in a volume with a constant value.

Args:
    volume (np.ndarray): Input volume
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    fill (np.ndarray): Value to fill the holes with

NrM   )volumer@   r   rD   rE   rF   rG   s          r   fill_volume_holes_with_valuerQ      s+     ',"e.2q%+u{*+ ',Mr   c                :    U H  u  p4pVX SS2SS2XF2X524'   M     U $ )zFill holes in a batch of volumes with a constant value.

Args:
    volumes (np.ndarray): Input batch of volumes
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    fill (np.ndarray): Value to fill the holes with

NrM   )volumesr@   r   rD   rE   rF   rG   s          r   fill_volumes_holes_with_valuerT      s-     ',"e261ek5;./ ',Nr   c                    U Hu  u  pEpgU(       a  SOXu-
  Xd-
  4nU R                   [        :w  a,  U(       a  SU R                  S   4O/ UQU R                  S   P7n[        U R                  X5      n	XXW2XF24'   Mw     U $ )a  Fill holes with random values.

Args:
    img (np.ndarray): Input image
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    random_generator (np.random.Generator): Random number generator
    uniform (bool): If True, use same random value for entire hole

)r    r    r0   )r:   r   r,   r   r"   )
r   r@   r-   r)   rD   rE   rF   rG   r,   random_fills
             r   fill_holes_with_randomrW      s     ',"eemU]%C88..)0Q		!%6L6Lsyy|6LE*399eN(3EK$% ', Jr   c                T   U H  u  pEpgU(       a  U R                   S   SS4OU R                   S   Xu-
  Xd-
  4nU R                  S:w  a;  U(       a   U R                   S   SSU R                   S   4O/ UQU R                   S   P7n[        U R                  X5      n	XSS2XW2XF24'   M     U $ )aP  Fill holes in a volume with random values.

Args:
    volume (np.ndarray): Input volume of shape (D, H, W, C) or (D, H, W)
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    random_generator (np.random.Generator): Random number generator
    uniform (bool): If True, use same random value for entire hole in each image.

r   r    r4   Nr,   r:   r   r"   )
rP   r@   r-   r)   rD   rE   rF   rG   r,   rV   s
             r   fill_volume_holes_with_randomrZ      s     ',"e+2a!Q'a%-Y^Yf8g;;!@GV\\!_aFLLO<MfuMfV\VbVbcdVeMfE*6<<Q.9q%+u{*+ ', Mr   c                   U H  u  pEpgU(       a   U R                   S   U R                   S   SS4O#U R                   S   U R                   S   Xu-
  Xd-
  4nU R                  S:w  aI  U(       a.  U R                   S   U R                   S   SSU R                   S   4O/ UQU R                   S   P7n[        U R                  X5      n	XSS2SS2XW2XF24'   M     U $ )aa  Fill holes in a batch of volumes with random values.

Args:
    volumes (np.ndarray): Input volume of shape (N, D, H, W, C) or (N, D, H, W)
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    random_generator (np.random.Generator): Random number generator
    uniform (bool): If True, use same random value for entire hole for each image

r   r       NrY   )
rS   r@   r-   r)   rD   rE   rF   rG   r,   rV   s
             r   fill_volumes_holes_with_randomr]      s     ',"e  ]]1w}}Q/A6--"GMM!$4emU]S 	
 <<1PWq!7==#3Q7==;KL]w_d]wfmfsfstufv]w  +7==%R2=1ek5;./ ', Nr   c                   U R                  5       n [        U[        5      (       aL  US;   a  [        X[	        SU5      5      $ US:X  a  [        XUSS9$ US:X  a  [        XUSS9$ [        SU 35      e[        U[        [        45      (       a*  [        R                  " X R                  S	9n[        XU5      $ [        R                  " X R                  S	9nU R                  [        :X  aU  UR                  5       nUR                   U R"                  S
   :w  a(  [        SUR                    SU R"                  S
    35      e[        XU5      $ )a4  Apply cutout augmentation to the image by cutting out holes and filling them.

Args:
    img (np.ndarray): The image to augment
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    fill (tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]):
        Value to fill holes with. Can be:
        - number (int/float): Will be broadcast to all channels
        - sequence (tuple/list/ndarray): Must match number of channels
        - "random": Different random values for each pixel
        - "random_uniform": Same random value for entire hole
        - "inpaint_telea"/"inpaint_ns": OpenCV inpainting methods
    random_generator (np.random.Generator): Random number generator for random fills

Raises:
    ValueError: If fill length doesn't match number of channels

   
inpaint_nsr3   &Literal['inpaint_telea', 'inpaint_ns']randomFr)   random_uniformTUnsupported string fill: r1   r0   ;Fill value must have same number of channels as image. Got , expected )r   
isinstancestrrK   r   rW   r+   intfloatr$   arrayr"   rN   r:   r   ravelr!   r,   )r   r@   r   r-   
fill_arrays        r   r   r     s9   0 ((*C $22#C5]_c0dee8)#6FPUVV##)#6FPTUU4TF;<< $e%%XXd))4
$S<< $ii0J xx//%%'
??ciil*!'{399Q<.B 
 !Z88r   c                   U R                  5       n [        U[        5      (       a  US;   aW  U  Vs/ s H  n[        XA[	        SU5      5      PM     nn[
        R                  " U5      nUR                  U R                  5      $ US:X  a  [        XUSS9$ US:X  a  [        XUSS9$ [        SU 35      e[        U[        [        45      (       a*  [
        R                  " X R                  S	9n[        XU5      $ [
        R                  " X R                  S	9nU R                  S
:X  aU  UR!                  5       nUR"                  U R                  S   :w  a(  [        SUR"                   SU R                  S    35      e[        XU5      $ s  snf )az  Apply cutout augmentation to a volume of shape (D, H, W) or (D, H, W, C) by cutting out holes and filling them.

Args:
    volume (np.ndarray): The volume to augment
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    fill (tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]):
        Value to fill holes with. Can be:
        - number (int/float): Will be broadcast to all channels
        - sequence (tuple/list/ndarray): Must match number of channels
        - "random": Different random values for each pixel
        - "random_uniform": Same random value for entire hole, different values across images
        - "inpaint_telea"/"inpaint_ns": OpenCV inpainting methods
    random_generator (np.random.Generator): Random number generator for random fills

Raises:
    ValueError: If fill length doesn't match number of channels

r_   ra   rb   Frc   rd   Tre   r1   r\   r4   rf   rg   )r   rh   ri   rK   r   r$   rl   reshaper,   rZ   r+   rj   rk   r"   rQ   r:   rm   r!   )rP   r@   r   r-   r   processed_imagesrJ   rn   s           r   cutout_on_volumerr   D  s{   0 [[]F $22nt ntgj T2Z\`-abnt    XX./F>>&,,//80@PZ_``##0@PZ^__4TF;<< $e%%XXd,,7
+F:FF $ll3J {{a%%'
??fll1o-!'{6<<?2CE 
 (zBB; s    Fc                2   U R                  5       n [        U[        5      (       a  US;   ac  U  VVs/ s H#  nU  H  n[        XQ[	        SU5      5      PM     M%     nnn[
        R                  " U5      nUR                  U R                  5      $ US:X  a  [        XUSS9$ US:X  a  [        XUSS9$ [        SU 35      e[        U[        [        45      (       a*  [
        R                  " X R                  S	9n[        XU5      $ [
        R                  " X R                  S	9nU R                  S
:X  aU  UR!                  5       nUR"                  U R                  S   :w  a(  [        SUR"                   SU R                  S    35      e[        XU5      $ s  snnf )ac  Apply cutout augmentation to a batch of volumes of shape (N, D, H, W) or (N, D, H, W, C)

Args:
    volumes (np.ndarray): The image to augment
    holes (np.ndarray): Array of [x1, y1, x2, y2] coordinates
    fill (tuple[float, ...] | float | Literal["random", "random_uniform", "inpaint_telea", "inpaint_ns"]):
        Value to fill holes with. Can be:
        - number (int/float): Will be broadcast to all channels
        - sequence (tuple/list/ndarray): Must match number of channels
        - "random": Different random values for each pixel
        - "random_uniform": Same random value for entire hole, different values across images
        - "inpaint_telea"/"inpaint_ns": OpenCV inpainting methods
    random_generator (np.random.Generator): Random number generator for random fills

Raises:
    ValueError: If fill length doesn't match number of channels

r_   ra   rb   Frc   rd   Tre   r1      r\   rf   rg   )r   rh   ri   rK   r   r$   rl   rp   r,   r]   r+   rj   rk   r"   rT   r:   rm   r!   )	rS   r@   r   r-   rP   r   rq   rJ   rn   s	            r   cutout_on_volumesru     s   0 llnG $22 & %F!C !T2Z\`-ab! c%   
 XX./F>>'--0081'BR\abb##1'BR\`aa4TF;<< $e%%XXd--8
,WZHH $mm4J ||q%%'
??gmmA..!'{7==3C2DF 
 )DD? s   *F	keypointsc                   U SS2S4   SS2[         R                  4   nU SS2S4   SS2[         R                  4   nUSS2S4   nUSS2S4   nUSS2S4   nUSS2S4   nX$:  X&:  -  X5:  -  X7:  -  n[         R                  " USS9) n	X	   $ )a  Filter out keypoints that are inside any of the holes.

Args:
    keypoints (np.ndarray): Array of keypoints with shape (num_keypoints, 2+).
                            The first two columns are x and y coordinates.
    holes (np.ndarray): Array of holes with shape (num_holes, 4).
                        Each hole is represented as [x1, y1, x2, y2].

Returns:
    np.ndarray: Array of keypoints that are not inside any hole.

Nr   r    r0   r4   axis)r$   newaxisany)
rv   r@   kp_xkp_yhole_x1hole_y1hole_x2hole_y2inside_holevalid_keypointss
             r   r   r     s     QT?1bjj=)DQT?1bjj=)DAqDkGAqDkGAqDkGAqDkG ?t~6$/Jdn]K vvk22O%%r   bboxesc                   U SS2S4   R                  [        5      nU SS2S4   R                  [        5      nU SS2S4   R                  [        5      nU SS2S4   R                  [        5      n/ n[        [        U 5      5       Vs/ s H  oqX7   XW   2X'   XG   24   PM     nnU V	s/ s H  n	SU	-
  PM
     n
n	[	        [        X5      5       H  u  nu  pUR                  5       (       d  M  UR                  SS9nUR                  SS9n[        R                  " U5      S   n[        R                  " U5      S   nUR                  5       nX'   US   -   US'   X7   US   -   US'   X'   US   -   S-   US'   X7   US   -   S-   US'   UR                  U5        M     U(       a  [        R                  " U5      $ [        R                  " SU R                  S   4U R                  S9$ s  snf s  sn	f )	z:Resize boxes to their largest visible rectangular regions.Nr   r    r0   r4   rx   r1   )r*   rj   rangelen	enumeratezipr{   r$   nonzeror   appendrl   r5   r,   r"   )boxes	hole_maskx1y1x2y2	new_boxesiregionsregionvisible_areasvisiblebox	y_visible	x_visibley_coordsx_coordsnew_boxs                     r   resize_boxes_to_visible_arear     s    
q!t		C	 B	q!t		C	 B	q!t		C	 B	q!t		C	 B #%I@Ec%j@QR@Q156@QGR.56gFQZgM6&s='@A>G{{}} KKQK'	KKQK'	::i(+::i(+ ((*UXa[(
UXa[(
UXb\)A-
UXb\)A-
!% B, #,288Ia1ekk!n:MUZU`U`1aa3 S6s   	G)+G.c                   [        U 5      S:X  d  [        U5      S:X  a  U $ [        R                  " U[        R                  S9nU H$  nUR	                  [
        5      u  pxpSXXU
2Xy24'   M&     U R	                  [
        5      nUSS2S4   USS2S4   -
  USS2S4   USS2S4   -
  -  n[        R                  " USS2SS24    VVVVs/ s H$  u  pnn[        R                  " X^U2X24   5      PM&     snnnn5      nUU-
  nUU-  nUU:  UU:  -  US:  -  nU U   n[        U5      S:X  a%  [        R                  " SU R                  S   45      $ [        UU5      $ s  snnnnf )a  Filter bounding boxes by holes.

This function filters bounding boxes by holes.

Args:
    bboxes (np.ndarray): Array of bounding boxes.
    holes (np.ndarray): Array of holes.
    image_shape (tuple[int, int]): Shape of the image.
    min_area (float): Minimum area of a bounding box.
    min_visibility (float): Minimum visibility of a bounding box.

Returns:
    np.ndarray: Filtered bounding boxes.

r   r1   r    Nr0   r4   r\   )r   r$   r5   r6   r*   rj   rl   sumemptyr,   r   )r   r@   image_shapemin_areamin_visibilityr   holerD   rE   rF   rG   
bboxes_int	box_areasxyr   r   intersection_areasremaining_areasvisibility_ratiosrC   valid_boxess                         r   r   r     s}   , 6{a3u:? BHH5I%)[[%5"e./	+u{*+ 
 s#JAqD!Jq!t$44AqD9IJWXZ[W[L\9\]IU_`acedece`eUf"gUf\Q2r266)bD!$J*?#@Uf"gh"44O')3x',=,OPTcfgTghD,K
;1xxFLLO,-- (Y?? #hs   
+Ec                    U SS u  pEUb2  US   [        U SS 5      :  a  [        S5      eUR                  " U6 nXf4$ U(       a  Uu  pxXW-  n	XH-  n
X4$ [        SUS-  5      n	[        SUS-  5      n
X4$ )aP  Calculate the dimensions of grid units for GridDropout.

This function determines the size of grid units based on the input parameters.
It supports three modes of operation:
1. Using a range of unit sizes
2. Using a specified number of holes in x and y directions
3. Falling back to a default calculation

Args:
    image_shape (tuple[int, int]): The shape of the image as (height, width).
    unit_size_range (tuple[int, int] | None, optional): A range of possible unit sizes.
        If provided, a random size within this range will be chosen for both height and width.
    holes_number_xy (tuple[int, int] | None, optional): The number of holes in the x and y directions.
        If provided, the grid dimensions will be calculated to fit this number of holes.
    random_generator (np.random.Generator): The random generator to use for generating random values.

Returns:
    tuple[int, int]: The calculated grid unit dimensions as (unit_height, unit_width).

Raises:
    ValueError: If the upper limit of unit_size_range is greater than the shortest image edge.

Notes:
    - If both unit_size_range and holes_number_xy are None, the function falls back to a default calculation,
      where the grid unit size is set to max(2, image_dimension // 10) for both height and width.
    - The function prioritizes unit_size_range over holes_number_xy if both are provided.
    - When using holes_number_xy, the actual number of holes may be slightly different due to integer division.

Examples:
    >>> image_shape = (100, 200)
    >>> calculate_grid_dimensions(image_shape, unit_size_range=(10, 20))
    (15, 15)  # Random value between 10 and 20

    >>> calculate_grid_dimensions(image_shape, holes_number_xy=(5, 10))
    (20, 20)  # 100 // 5 and 200 // 10

    >>> calculate_grid_dimensions(image_shape)
    (10, 20)  # Default calculation: max(2, dimension // 10)

Nr0   r    z8Grid size limits must be within the shortest image edge.
   )minr+   r'   max)r   unit_size_rangeholes_number_xyr-   heightwidth	unit_sizeholes_number_xholes_number_y
unit_widthunit_heights              r   r   r   :  s    \  OMF"1KO 44WXX$--?	##)8&,
.&& Q$Ja2&K""r   c                8   U SS u  pg[        XU5      nUSS2S4   USS2S4   -
  n	USS2S4   USS2S4   -
  n
[        R                  " X-  SU	S-
  5      R                  [        5      n[        R                  " X-  SU
S-
  5      R                  [        5      nX-
  nX-
  nU(       a+  UR                  SUS-   5      nUR                  SUS-   5      nO2[        R                  " XS   5      n[        R                  " XS   5      n[        R                  " USS2S4   U-   SX|-
  5      n[        R                  " USS2S4   U-   SXk-
  5      n[        R                  " UU-   U5      n[        R                  " UU-   U5      n[        R                  " UUUU45      $ )a  Generate a list of holes for GridDropout using a uniform grid.

This function creates a grid of holes for use in the GridDropout augmentation technique.
It allows for customization of the grid size, hole size ratio, and positioning of holes.

Args:
    image_shape (tuple[int, int]): The shape of the image as (height, width).
    grid (tuple[int, int]): The grid size as (rows, columns). This determines the number of cells
        in the grid, where each cell may contain a hole.
    ratio (float): The ratio of the hole size to the grid cell size. Should be between 0 and 1.
        A ratio of 1 means the hole will fill the entire grid cell.
    random_offset (bool): If True, applies random offsets to each hole within its grid cell.
        If False, uses the global shift specified by shift_xy.
    shift_xy (tuple[int, int]): The global shift to apply to all holes as (shift_x, shift_y).
        Only used when random_offset is False.
    random_generator (np.random.Generator): The random generator for generating random offsets
        and shuffling. If None, a new Generator will be created.

Returns:
    np.ndarray: An array of hole coordinates, where each hole is represented as
        [x1, y1, x2, y2]. The shape of the array is (n_holes, 4), where n_holes
        is determined by the grid size.

Notes:
    - The function first creates a uniform grid based on the image shape and specified grid size.
    - Hole sizes are calculated based on the provided ratio and grid cell sizes.
    - If random_offset is True, each hole is randomly positioned within its grid cell.
    - If random_offset is False, all holes are shifted by the global shift_xy value.
    - The function ensures that all holes remain within the image boundaries.

Examples:
    >>> image_shape = (100, 100)
    >>> grid = (5, 5)
    >>> ratio = 0.5
    >>> random_offset = True
    >>> random_state = np.random.RandomState(42)
    >>> shift_xy = (0, 0)
    >>> holes = generate_grid_holes(image_shape, grid, ratio, random_offset, random_state, shift_xy)
    >>> print(holes.shape)
    (25, 4)
    >>> print(holes[0])  # Example output: [x1, y1, x2, y2] of the first hole
    [ 1 21 11 31]

Nr0   r   r4   r    )	r   r$   clipr*   rj   r'   	full_likeminimumcolumn_stack)r   gridratiorandom_offsetshift_xyr-   r   r   cellscell_heightscell_widthshole_heightshole_widthsmax_offset_ymax_offset_xoffset_yoffset_xrD   rE   rF   rG   s                        r   r   r   |  s   h  OMF {2BCE A;q!t,L1+ad+K77</L14DELLSQL''+-q+/BII#NK  .L,L#,,Qq0@A#,,Qq0@A <<qk:<<qk: GGE!Q$K(*Au/BCEGGE!Q$K(*Av/DEEJJu{*E2EJJu|+V4E??E5%788r   c                t   Uu  pVUR                   S:  ac  UR                  S   S:X  a  UR                  S5      nO>UR                  S   S::  a  [        R                  " USS9nO[        R                  " USS9n[        R
                  SU2SU24   u  pxUSSS24   U SS2SSS4   :  USSS24   U SS2SSS4   :*  -  USSS24   U SS2SSS4   :  -  USSS24   U SS2SSS4   :*  -  n	U SS2S4   U SS2S4   -
  U SS2S4   U SS2S4   -
  -  n
[        R                  " X) -  S	S9nX-  nX:  X:  -  nX   $ )
a  Filter and resize bounding boxes based on dropout mask.

Args:
    bboxes (np.ndarray): Array of bounding boxes with shape (num_boxes, 4+)
    dropout_mask (np.ndarray): Binary mask indicating dropped areas
    image_shape (tuple[int, int]): Shape of the image (height, width)
    min_area (float): Minimum area of a bounding box to keep
    min_visibility (float): Minimum visibility ratio of a bounding box to keep

Returns:
    np.ndarray: Filtered and resized bounding boxes

r0   r   r    r   r\   rx   Nr4   )r    r0   )r:   r,   r;   r$   r{   ogridr   )r   dropout_maskr   r   r   r   r   r   r   	box_masksr   r   visibility_ratio	keep_masks                 r   mask_dropout_bboxesr     s   *  MF 1a A%'//2L#q(66,R8L66,Q7L 88GVGVeVO$DA	
47vaD$./	/T1W:1dD 011	3T1W:1dD 011	3 T1W:1dD 011	3  1q!t,1q!t1LMI FF9}46BM %0 */?/QRIr   c                2   UR                   S:  ac  UR                  S   S:X  a  UR                  S5      nO>UR                  S   S::  a  [        R                  " USS9nO[        R                  " USS9nU SS2SS24   R                  [        5      nUSS2S4   S:  USS2S4   UR                  S   :  -  USS2S4   S:  -  USS2S4   UR                  S   :  -  n[        R                  " U5      (       a  X#   nXSS2S4   USS2S4   4   ) X3'   X   $ )zFilter keypoints based on dropout mask.

Args:
    keypoints (np.ndarray): Array of keypoints with shape (num_keypoints, 2+)
    dropout_mask (np.ndarray): Binary mask indicating dropped areas

Returns:
    np.ndarray: Filtered keypoints

r0   r   r    r   r\   rx   N)r:   r,   r;   r$   r{   r*   rj   )rv   r   coords
valid_maskvalid_coordss        r   mask_dropout_keypointsr   	  s?     1a A%'//2L#q(66,R8L66,Q7L q"1"u$$S)F 
1	!Q$<,,,Q//	1!Q$<1	 !Q$<,,,Q//	1  
vvj)".AqD/A<PQSTPTCU/U"V!V
  r   c                t   [         R                  " U [         R                  S9n[         R                  " X S:g     5      nSnU Hb  nX:H  R	                  [         R
                  5      nUS:X  a  SOSn[        R                  " XxS9u  p[        SU	5       H  nXSX:H  '   US-  nM     Md     US-
  n	U(       a  X94$ U$ )a  Label connected regions of an integer array.

This function uses OpenCV's connectedComponents under the hood but mimics
the behavior of scikit-image's label function.

Args:
    mask (np.ndarray): The array to label. Must be of integer type.
    return_num (bool): If True, return the number of labels (default: False).
    connectivity (int): Maximum number of orthogonal hops to consider a pixel/voxel
                        as a neighbor. Accepted values are 1 or 2. Default is 2.

Returns:
    np.ndarray | tuple[np.ndarray, int]: Labeled array, where all connected regions are
    assigned the same integer value. If return_num is True, it also returns the number of labels.

r1   r   r    r\      )connectivity)	r$   
zeros_likeint32uniquer*   r6   r7   connectedComponentsr   )rC   
return_numr   labeledunique_values
next_labelvaluebinary_maskcv2_connectivity
num_labelslabelsr   s               r   labelr   4  s    $ mmD1G IId19o.M J},,RXX6 !- 11q !44[`
 q*%A#-FK !OJ &  aJ$.G ;G;r   c                   [        U 5      nU SS2S4   U SS2S4   -
  nU SS2S4   U SS2S4   -
  nUR                  US   US   XQ4S9USS2S4   -  R                  [        R                  5      nUR                  US   US   XQ4S9USS2S4   -  R                  [        R                  5      n	UR                  SSXQ4S9USS2S4   U	-
  -  n
UR                  SSXQ4S9USS2S4   U-
  -  nU SS2SS4   U
-   nU SS2SS4   U-   nX-   nX-   n[        R
                  " XX/SS9R                  [        R                  5      R                  SS	5      $ )
z'Generate holes based on bounding boxes.Nr0   r   r4   r    r#   r   rx   r\   )r   r)   r*   r$   r   stackrp   )target_boxesnum_holes_per_boxhole_height_rangehole_width_ranger-   	num_boxes
box_widthsbox_heightsr   r   	x_offsets	y_offsetsrD   rE   rF   rG   s                   r   get_holes_from_boxesr   `  s    L!I ad#l1a4&88Jq!t$|AqD'99K 	  a a / 	! 	

 ag
	 fRXX  	  QQ/ 	! 	

 QW
	 fRXX  !((AY4R(S1d7k)I !((AY4R(SAtG|+I
 At$y0EAt$y0EE E88U50r:AA"((KSSTVXYZZr   c                >   [         R                  " U R                  [        R                  5      5      u  p4US:X  a  g/ n/ n[        SU5       H  n[        R                  " XG:H  5      n[        U5      S:X  a  M,  [        R                  " [        U5      5      n	UR                  [        U5      USS9n
X   nUR                  USS2SSS24   5        UR                  U	/U-  5        M     U(       a,  [        R                  " U5      [        R                  " U5      4$ S$ )aN  Sample points from connected components in a mask.

Args:
    mask (np.ndarray): Binary mask
    num_points (int): Number of points to sample
    random_generator (np.random.Generator): Random number generator

Returns:
    tuple[np.ndarray, np.ndarray] | None: Tuple of (x_coordinates, y_coordinates) or None if no valid components

r    Nr   T)r!   replacer   )r7   r   r*   r$   r6   r   argwherer   sqrtchoiceextendrl   )rC   
num_pointsr-   r   r   centers	obj_sizesr   pointsobj_sizeindicessampled_pointss               r   sample_points_from_componentsr    s      00RXX1FGJQGIq*%V_-v;! 773v;' #))#f+JPT)U~a2g./(j01 &  8?BHHWrxx	23HDHr   c                   [         R                  " U [         R                  " U5      5      n[         R                  " U5      (       d2  [         R                  " / [         R                  S9R                  S5      $ [        XaU5      nUc2  [         R                  " / [         R                  S9R                  S5      $ Uu  p[        U5      n
U R                  SS u  pUR                  US   US   U
S9U	-  nUR                  US   US   U
S9U	-  nUS-  nUS-  n[         R                  " USS2S4   U-
  USS2S4   U-
  USS2S4   U-   USS2S4   U-   /5      R                  [         R                  5      n[         R                  " USS2S4   SUS-
  5      USS2S4'   [         R                  " USS2S4   SUS-
  5      USS2S4'   [         R                  " USS2S4   SU5      USS2S4'   [         R                  " USS2S4   SU5      USS2S4'   USS2S4   USS2S4   -
  S:  USS2S4   USS2S4   -
  S:  -  nUU   $ )	z*Generate holes based on segmentation mask.r1   )r   r\   Nr0   r   r    r#   r4   )r$   isinrl   r{   r   rp   r  r   r,   r)   r   r*   r   )rC   num_holes_per_objmask_indicesr   r   r-   r   rJ   r   r   num_centersr   r   r   r   half_heightshalf_widthsr@   valid_holess                      r   get_holes_from_maskr    sh    ''$ 67K66+xx"((+33F;;*;K[\F~xx"((+33F;;Gg,KJJrNMF 	  a a  	! 	

 	  	  QQ 	! 	

 	   1$L"KOOAqDMK'AqDML(AqDMK'AqDML(		
 fRXX 
 ''%1+q%!)4E!Q$K''%1+q&1*5E!Q$K''%1+q%0E!Q$K''%1+q&1E!Q$K A;q!t,q0U1a4[5A;5NQR5RSKr   )r   )r   
np.ndarrayr   z"int | tuple[int, ...] | np.ndarrayr   ztuple[float, ...] | floatreturnr  )r"   znp.dtyper,   ztuple[int, ...]r-   np.random.Generatorr  r  )r   r  r@   r  rA   ra   r  r  )r   r  r@   r  r   r  r  r  )rP   r  r@   r  r   r  r  r  )rS   r  r@   r  r   r  r  r  )
r   r  r@   r  r-   r  r)   boolr  r  )
rP   r  r@   r  r-   r  r)   r  r  r  )
rS   r  r@   r  r-   r  r)   r  r  r  )
r   r  r@   r  r   ^tuple[float, ...] | float | Literal['random', 'random_uniform', 'inpaint_telea', 'inpaint_ns']r-   r  r  r  )
rP   r  r@   r  r   r  r-   r  r  r  )
rS   r  r@   r  r   r  r-   r  r  r  )rv   r  r@   r  r  r  )r   r  r   r  r  r  )r   r  r@   r  r   tuple[int, int]r   rk   r   rk   r  r  )
r   r  r   tuple[int, int] | Noner   r  r-   r  r  r  )r   r  r   r  r   rk   r   r  r   r  r-   r  r  r  )r   r  r   r  r   r  r   rk   r   rk   r  r  )rv   r  r   r  r  r  )Fr0   )rC   r  r   r  r   rj   r  z#np.ndarray | tuple[np.ndarray, int])r   r  r   rj   r   tuple[float, float]r   r  r-   r  r  r  )rC   r  r   rj   r-   r  r  z$tuple[np.ndarray, np.ndarray] | None)rC   r  r  rj   r  z	list[int]r   r  r   r  r-   r  r  r  ).__doc__
__future__r   typingr   r   r7   numpyr$   albucorer   r   r   r	   r
   r   1albumentations.augmentations.geometric.functionalr   "albumentations.augmentations.utilsr   $albumentations.core.type_definitionsr   __all__r   r   rK   rN   rQ   rT   rW   rZ   r]   r   rr   ru   r   r   r   r   r   r   r   r   r   r  r  rM   r   r   <module>r     sf   #   
   Q A H  '(	8 $ 	 6%4%4%4 *%4 	%4P 
#5 
#5L	 * 	
 2 * 	
 2 * 	
 <59	5959 i59 *	59
 59p:C:C:C i:C *	:C
 :Cz<E<E<E i<E *	<E
 <E~ K & !&> H'b'b'b 'b 'bT,@,@,@ !,@ 	,@
 ,@ ,@^?# ?#+?# ,?# *	?#
 ?#DR9 R9
R9 R9 	R9
 R9 *R9 R9j H444 !4 	4
 4 4 4n K '!'!'! '! !'!T)<X/[/[/[ +/[ *	/[
 */[ /[d'I
'I'I *'I *	'IT=
== = +	=
 *= *= =r   