o
    e                    @   s  d Z ddlmZmZmZ ddlZddlZddlZddl	m
Z
 ddlmZ ddlZ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 ddlmZ ddlmZ ddlm Z! ddlm"Z# h dZ$h dZ%ej&ej'ej(ej(ej(dZ)ej*ej+ej,ej-ej.dZ/dd Z0dd Z1dd Z2		dVddZ3dd Z4d d! Z5d"d# Z6d$d% Z7d&d' Z8	(dWd)d*Z9G d+d, d,e:Z;dXd.d/Z<G d0d1 d1ej=Z>G d2d3 d3e>Z?G d4d5 d5e>Z@G d6d7 d7e>ZAG d8d9 d9e>ZBG d:d; d;e>ZCG d<d= d=e>ZDG d>d? d?e>ZEG d@dA dAej=ZFG dBdC dCe:ZGG dDdE dEej=ZHG dFdG dGe:ZIG dHdI dIej=ZJG dJdK dKe:ZKG dLdM dMej=ZLG dNdO dOej=ZMG dPdQ dQej=ZNG dRdS dSej=ZOG dTdU dUe:ZPdS )Ya  Augmenters that apply affine or similar transformations.

List of augmenters:

    * :class:`Affine`
    * :class:`ScaleX`
    * :class:`ScaleY`
    * :class:`TranslateX`
    * :class:`TranslateY`
    * :class:`Rotate`
    * :class:`ShearX`
    * :class:`ShearY`
    * :class:`AffineCv2`
    * :class:`PiecewiseAffine`
    * :class:`PerspectiveTransform`
    * :class:`ElasticTransformation`
    * :class:`Rot90`
    * :class:`WithPolarWarping`
    * :class:`Jigsaw`

    )print_functiondivisionabsolute_importN)ndimage)	transform)_normalize_cv2_input_arr_)_ConcavePolygonRecoverer   )meta)blur)size   )
parameters)dtypes)random>	   float16uint8boolint16int8float64float32int32uint16>   r   r   r   r   r   r   r   r   )r   r	   r         constantedgeZ	symmetricreflectwrapc                 C   s4  | t jkr|dv rtg dS tg dS t | rBd|   kr&dks.n J d| f |dkr=| dv s=J d| f t| S t| trtd	d
 | D s^J dt	dd
 | D f tdd
 | D spJ dt	| f |dkrtdd
 | D sJ d| f t| S t| tj
r| S tdt| f )N)autocv2r   r	   r   )r   r	   r   r      r   r$   zDExpected order's integer value to be in the interval [0, 5], got %d.r"   zWBackend "cv2" and order=%d was chosen, but cv2 backend can only handle order 0, 1 or 3.c                 S      g | ]}t |qS  )iais_single_integer.0valr&   r&   KD:\Projects\ConvertPro\env\Lib\site-packages\imgaug/augmenters/geometric.py
<listcomp>k       z%_handle_order_arg.<locals>.<listcomp>z;Expected order list to only contain integers, got types %s.c                 S      g | ]}t |qS r&   typer)   r&   r&   r,   r-   m       c                 S   s$   g | ]}d |  kodkn  qS r   r$   r&   r)   r&   r&   r,   r-   n   s   $ zJExpected all of order's integer values to be in range 0 <= x <= 5, got %s.c                 S   s   g | ]}|d v qS )r#   r&   r)   r&   r&   r,   r-   r   r2   zBcv2 backend can only handle order 0, 1 or 3. Got order list of %s.zQExpected order to be imgaug.ALL, int, list of int or StochasticParameter, got %s.)r'   ALLiapChoicer(   Deterministic
isinstancelistallstrStochasticParameter	Exceptionr1   )orderbackendr&   r&   r,   _handle_order_argQ   sT   






r@   c                 C   s*   | t jkrtddS tj| dd dddS )Nr      cvalTvalue_rangetuple_to_uniformlist_to_choice)r'   r4   r5   ZUniformhandle_continuous_param)rB   r&   r&   r,   _handle_cval_arg}   s   
rH   c                 C   s   | t jkrtg dS t | rt| S t| tr7tdd | D s2J dd	dd | D  t| S t| tj
r?| S tdt| f )Nr   c                 S   r%   r&   r'   	is_stringr)   r&   r&   r,   r-      r.   z$_handle_mode_arg.<locals>.<listcomp>z<Expected list of modes to only contain strings, got types %s, c                 S      g | ]}t t|qS r&   r;   r1   r*   vr&   r&   r,   r-          [Expected mode to be imgaug.ALL, a string, a list of strings or StochasticParameter, got %s.)r'   r4   r5   r6   rJ   r7   r8   r9   r:   joinr<   r=   r1   )moder&   r&   r,   _handle_mode_arg   s$   




rT   r   r!   c                 C   s   t |r|gt| jd  }| jdkr| S t| j\}}}	|dv}
|dkr.| jjt	v}n| jjt
v}|
p7|}|dkpA|dkoA|}|r\|d }tt||	|}t| |||||d}|S |riJ | d| jf ft| |tdd	 |D |||d}|S )
Nr   r   r#   skimager!   )rB   rS   r>   output_shapezicv2 backend in Affine got a dtype %s, which it cannot handle. Try using a different dtype or set order=0.c                 S   r/   r&   intrN   r&   r&   r,   r-      r2   z$_warp_affine_arr.<locals>.<listcomp>)r'   r(   lenshaper   iadtget_value_range_of_dtypedtypename_VALID_DTYPES_CV2_ORDER_0_VALID_DTYPES_CV2_ORDER_NOT_0maxmin_warp_affine_arr_skimage_warp_affine_arr_cv2tuple)arrmatrixr>   rS   rB   rV   r?   	min_value_center_value	max_valueZcv2_bad_orderZcv2_bad_dtypeZcv2_impossibleZuse_skimageimage_warpedr&   r&   r,   _warp_affine_arr   s`   



rl   c              	   C   s`   t j| g dg dd d | j}tj| |j|||d|d}|tjkr(|dk}|S t ||}|S )N
r   r   r   uint32r   r   r   r   r   r   	uint64uint128uint256int64int128int256float96float128float256allowedZ
disallowedZ	augmenterTr>   rS   rB   preserve_rangerV         ?)	r[   gate_dtypesr]   tfwarpZinversenpbool_restore_dtypes_)rf   rg   rB   rS   r>   rV   input_dtyperk   r&   r&   r,   rc      s*   
rc   c           	         sj  t j g dg dd d dkr jjdksJ df  j}|tjtjfv r0 tj n|tj	kr?dkr? tj
 tt|d tt|d ftt jd }|d	krtjt jd d
 d}|jd
kr|dtjf }n fddt|D }tj|dd}|jdkr|dk}|S |jdv rt ||}|S )N	r   r   r   r   r   r   r   r   r   
rn   rp   rq   rr   rs   rt   ru   rv   rw   rx   ry   r   r   zkAffine only supports cv2-based transformations of int32 arrays when using order=0, but order was set to %d.r	   r   r   dsizeflags
borderModeborderValue.c                    sL   g | ]"}t jt d d d d |f jd d td gdqS )Nr   r   r   )r"   
warpAffiner   paramsre   r*   crf   rB   r   rg   rS   r>   r&   r,   r-   .  s    	z(_warp_affine_arr_cv2.<locals>.<listcomp>axisr   r}   )r   r   )r[   r~   r]   r^   r   r   r   astyper   r   r   rX   round_AFFINE_MODE_SKIMAGE_TO_CV2get*_AFFINE_INTERPOLATION_ORDER_SKIMAGE_TO_CV2rZ   r"   r   r   r   ndimnewaxissmxrangestackr   )	rf   rg   rB   rS   r>   rV   r   nb_channelsrk   r&   r   r,   rd      s\   


	


rd   c                 C   s8  |d d \}}|dks|dkr| |fS t ddgd|d g|d |d g|d dgg}| |}|d d df  }|d d df  }|d d df  }|d d df  }|| d }	|| d }
t|dkrvt |	|
|d f}nt |	|
f}tdd | D }| | f}tj	|d}| | } | |fS )Nr   r   r	   r   c                 S   r/   r&   rW   rN   r&   r&   r,   r-   Z  r2   z5_compute_affine_warp_output_shape.<locals>.<listcomp>translation)
r   arrayrb   ra   rY   ceilre   tolistr   SimilarityTransform)rg   Zinput_shapeheightwidthZcornersmincZminrmaxcZmaxrZ
out_heightZ	out_widthrV   r   Zmatrix_to_fitr&   r&   r,   !_compute_affine_warp_output_shapeB  s0   

r   c                 C   sh  |j dd \}}| jdksJ d| j| j f | j d | dks,J d| j d |f | j d | dks@J d| j d |f | j d | }| j d | }t| ||f\}}t| }d}	t|D ]K}
t|D ]C}||	 ||	 }}|
| }|| }|| }|| }|| }|| }|| }|| }| ||||f }||||||f< |	d7 }	qmqf|S )a  Move cells of an image similar to a jigsaw puzzle.

    This function will split the image into ``rows x cols`` cells and
    move each cell to the target index given in `destinations`.

    Added in 0.4.0.

    **Supported dtypes**:

        * ``uint8``: yes; fully tested
        * ``uint16``: yes; fully tested
        * ``uint32``: yes; fully tested
        * ``uint64``: yes; fully tested
        * ``int8``: yes; fully tested
        * ``int16``: yes; fully tested
        * ``int32``: yes; fully tested
        * ``int64``: yes; fully tested
        * ``float16``: yes; fully tested
        * ``float32``: yes; fully tested
        * ``float64``: yes; fully tested
        * ``float128``: yes; fully tested
        * ``bool``: yes; fully tested

    Parameters
    ----------
    arr : ndarray
        Array with at least two dimensions denoting height and width.

    destinations : ndarray
        2-dimensional array containing for each cell the id of the destination
        cell. The order is expected to a flattened c-order, i.e. row by row.
        The height of the image must be evenly divisible by the number of
        rows in this array. Analogous for the width and columns.

    Returns
    -------
    ndarray
        Modified image with cells moved according to `destinations`.

    r   r   zFExpected array with at least two dimensions, but got %d with shape %s.zExpected image height to by divisible by number of rows, but got height %d and %d rows. Use cropping or padding to modify the image height or change the number of rows.r	   zExpected image width to by divisible by number of columns, but got width %d and %d columns. Use cropping or padding to modify the image width or change the number of columns.)rZ   r   r   unravel_indexflatten
zeros_likearange)rf   destinationsnb_rowsnb_colscell_height
cell_width	dest_rows	dest_colsresulti
source_row
source_coldest_rowdest_col	source_y1Z	source_y2	source_x1Z	source_x2dest_y1Zdest_y2dest_x1Zdest_x2sourcer&   r&   r,   apply_jigsawc  sN   ,


r   c                 C   s  |j dd \}}|dd \}}|| }|| }t| ||f\}	}
t| }t| D ]Y\}\}}|dk p;||k}|dk pC||k}|sH|rIq.t|| }t|| }|| | }|	| }|
| }|| }|| }|| }|| }|||  ||df< |||  ||df< q.|S )a7  Move coordinates on an image similar to a jigsaw puzzle.

    This is the same as :func:`apply_jigsaw`, but moves coordinates within
    the cells.

    Added in 0.4.0.

    Parameters
    ----------
    coords : ndarray
        ``(N, 2)`` array denoting xy-coordinates.

    destinations : ndarray
        See :func:`apply_jigsaw`.

    image_shape : tuple of int
        ``(height, width, ...)`` shape of the image on which the
        coordinates are placed. Only height and width are required.

    Returns
    -------
    ndarray
        Moved coordinates.

    r   r   r	   )rZ   r   r   r   copy	enumeraterX   )coordsr   image_shaper   r   r   r   r   r   r   r   r   r   xyZooi_xZooi_yr   r   Zsource_cell_idxr   r   r   r   r   r   r&   r&   r,   apply_jigsaw_to_coords  s2   
r   r   c                 C   s  |dv sJ d|f t |}|jd|| |fdd}|jd|| ||fdd}t| | | |f}t|D ]}	|dddd|	f }
t| D ]}t|D ]}|||f dkr|d |d f|d |d f|d |d f|d |d f|d |d f|d |d f|d |d f|d |d fd	|
||f  \}}tt|| d d}tt||d d}|||f }||f||fkr|dkr|||f }|||f }||||f< ||||f< |||f  d8  < |||f  d8  < qQqJq8|S )
af  Generate a destination pattern for :func:`apply_jigsaw`.

    Added in 0.4.0.

    Parameters
    ----------
    nb_rows : int
        Number of rows to split the image into.

    nb_cols : int
        Number of columns to split the image into.

    max_steps : int
        Maximum number of cells that each cell may be moved.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState
        Seed value or alternatively RNG to use.
        If ``None`` the global RNG will be used.

    connectivity : int, optional
        Whether a diagonal move of a cell counts as one step
        (``connectivity=8``) or two steps (``connectivity=4``).

    Returns
    -------
    ndarray
        2-dimensional array containing for each cell the id of the target
        cell.

    )r      z(Expected connectivity of 4 or 8, got %d.r   T)r   ZendpointFNr	   )r   r	   r   r   r   r$         )iarandomZRNGZintegersr   r   reshapera   rb   )r   r   	max_stepsseedZconnectivityrandom_stateZstepsZ
directionsr   stepZdirections_stepr   r   Zy_targetZx_targetZtarget_stepsZsource_destZtarget_destr&   r&   r,   generate_jigsaw_destinations  sT   
 

	
r   c                   @   s@   e Zd Z		dddZdd Z	ddd	ZdddZdd ZdS )_AffineSamplingResultNpxc	           	      C   s4   || _ || _|| _|| _|| _|| _|| _|| _d S Nscale	translatetranslate_moderotateshearrB   rS   r>   )	selfr   r   r   r   r   rB   rS   r>   r&   r&   r,   __init__J  s   
z_AffineSamplingResult.__init__c                 C   s   | j d | }| j d | }| jd | }| jd | }| jdv s)J d| jf | jdkr5||d  }n
||d  |d  }| jdkrK||d  }	n
||d  |d  }	| j| }
| jd | }| jd | }t|
||g\}}}||||	||||
||d
S )Nr	   r   )r   percentz%Expected 'px' or 'percent', got '%s'.r   )
scale_yscale_xtranslate_y_pxtranslate_x_px
rotate_radshear_y_radshear_x_rad
rotate_degshear_y_degshear_x_deg)r   r   r   r   r   r   Zdeg2rad)r   idx	arr_shaper   r   r   translate_ytranslate_xr   r   r   r   r   r   r   r   r&   r&   r,   get_affine_parametersV  s<   



z+_AffineSamplingResult.get_affine_parametersr}   r}   c                 C   s   d|v r
t  |fS |dd \}}| j|||d}|d |d  }	|d |d  }
t j|
 |	 gd}t jdd}t j|d	 d
}t jdd}t j|d |d f|d |d f|d |d d}t j|
|	gd}|| | | | | }|r|t||S ||fS )Nr   r   )r   r          @r	   r   gz !)rotationr   )r   gz !?r   r   r   r   r   r   r   r   r   r   )r   AffineTransformr   r   r   )r   r   r   r   
fit_output	shift_addr   r   r   shift_yshift_xmatrix_to_topleftZmatrix_shear_y_rotZmatrix_shear_yZmatrix_shear_y_rot_invmatrix_transformsmatrix_to_centerrg   r&   r&   r,   	to_matrix|  sL   

z_AffineSamplingResult.to_matrix        r   c                 C   s   |  |||||S r   )r   )r   r   r   r   r   r&   r&   r,   to_matrix_cba     z#_AffineSamplingResult.to_matrix_cbac              
   C   s(   t | j| j| j| j| j| j| j| jdS )Nr   )	r   r   r   r   r   r   rB   rS   r>   r   r&   r&   r,   r     s   z_AffineSamplingResult.copy)NNr   NNNNN)r   )r   )__name__
__module____qualname__r   r   r   r   r   r&   r&   r&   r,   r   I  s    
'

(r   -C6?c                 C   s6   t g dg dg dg}t t || j |kS )N)r	   r   r   )r   r	   r   )r   r   r	   )r   r   Zaverageabsr   )rg   Zepsidentityr&   r&   r,   _is_identity_matrix  s   r  c                       s~   e Zd ZdZ					d fd	d
	Zedd Zedd Zedd Zdd Z			dddZ
dd Zdd Zdd Z  ZS )Affinea U  
    Augmenter to apply affine transformations to images.

    This is mostly a wrapper around the corresponding classes and functions
    in OpenCV and skimage.

    Affine transformations involve:

        - Translation ("move" image on the x-/y-axis)
        - Rotation
        - Scaling ("zoom" in/out)
        - Shear (move one side of the image, turning a square into a trapezoid)

    All such transformations can create "new" pixels in the image without a
    defined content, e.g. if the image is translated to the left, pixels
    are created on the right.
    A method has to be defined to deal with these pixel values. The
    parameters `cval` and `mode` of this class deal with this.

    Some transformations involve interpolations between several pixels
    of the input image to generate output pixel values. The parameter `order`
    deals with the method of interpolation used for this.

    .. note::

        While this augmenter supports segmentation maps and heatmaps that
        have a different size than the corresponding image, it is strongly
        recommended to use the same aspect ratios. E.g. for an image of
        shape ``(200, 100, 3)``, good segmap/heatmap array shapes also follow
        a ``2:1`` ratio and ideally are ``(200, 100, C)``, ``(100, 50, C)`` or
        ``(50, 25, C)``. Otherwise, transformations involving rotations or
        shearing will produce unaligned outputs.
        For performance reasons, there is no explicit validation of whether
        the aspect ratios are similar.

    **Supported dtypes**:

    if (backend="skimage", order in [0, 1]):

        * ``uint8``: yes; tested
        * ``uint16``: yes; tested
        * ``uint32``: yes; tested (1)
        * ``uint64``: no (2)
        * ``int8``: yes; tested
        * ``int16``: yes; tested
        * ``int32``: yes; tested  (1)
        * ``int64``: no (2)
        * ``float16``: yes; tested
        * ``float32``: yes; tested
        * ``float64``: yes; tested
        * ``float128``: no (2)
        * ``bool``: yes; tested

        - (1) scikit-image converts internally to float64, which might
              affect the accuracy of large integers. In tests this seemed
              to not be an issue.
        - (2) results too inaccurate

    if (backend="skimage", order in [3, 4]):

        * ``uint8``: yes; tested
        * ``uint16``: yes; tested
        * ``uint32``: yes; tested (1)
        * ``uint64``: no (2)
        * ``int8``: yes; tested
        * ``int16``: yes; tested
        * ``int32``: yes; tested  (1)
        * ``int64``: no (2)
        * ``float16``: yes; tested
        * ``float32``: yes; tested
        * ``float64``: limited; tested (3)
        * ``float128``: no (2)
        * ``bool``: yes; tested

        - (1) scikit-image converts internally to float64, which might
              affect the accuracy of large integers. In tests this seemed
              to not be an issue.
        - (2) results too inaccurate
        - (3) ``NaN`` around minimum and maximum of float64 value range

    if (backend="skimage", order=5]):

            * ``uint8``: yes; tested
            * ``uint16``: yes; tested
            * ``uint32``: yes; tested (1)
            * ``uint64``: no (2)
            * ``int8``: yes; tested
            * ``int16``: yes; tested
            * ``int32``: yes; tested  (1)
            * ``int64``: no (2)
            * ``float16``: yes; tested
            * ``float32``: yes; tested
            * ``float64``: limited; not tested (3)
            * ``float128``: no (2)
            * ``bool``: yes; tested

            - (1) scikit-image converts internally to ``float64``, which
                  might affect the accuracy of large integers. In tests
                  this seemed to not be an issue.
            - (2) results too inaccurate
            - (3) ``NaN`` around minimum and maximum of float64 value range

    if (backend="cv2", order=0):

        * ``uint8``: yes; tested
        * ``uint16``: yes; tested
        * ``uint32``: no (1)
        * ``uint64``: no (2)
        * ``int8``: yes; tested
        * ``int16``: yes; tested
        * ``int32``: yes; tested
        * ``int64``: no (2)
        * ``float16``: yes; tested (3)
        * ``float32``: yes; tested
        * ``float64``: yes; tested
        * ``float128``: no (1)
        * ``bool``: yes; tested (3)

        - (1) rejected by cv2
        - (2) changed to ``int32`` by cv2
        - (3) mapped internally to ``float32``

    if (backend="cv2", order=1):

        * ``uint8``: yes; fully tested
        * ``uint16``: yes; tested
        * ``uint32``: no (1)
        * ``uint64``: no (2)
        * ``int8``: yes; tested (3)
        * ``int16``: yes; tested
        * ``int32``: no (2)
        * ``int64``: no (2)
        * ``float16``: yes; tested (4)
        * ``float32``: yes; tested
        * ``float64``: yes; tested
        * ``float128``: no (1)
        * ``bool``: yes; tested (4)

        - (1) rejected by cv2
        - (2) causes cv2 error: ``cv2.error: OpenCV(3.4.4)
              (...)imgwarp.cpp:1805: error:
              (-215:Assertion failed) ifunc != 0 in function 'remap'``
        - (3) mapped internally to ``int16``
        - (4) mapped internally to ``float32``

    if (backend="cv2", order=3):

        * ``uint8``: yes; tested
        * ``uint16``: yes; tested
        * ``uint32``: no (1)
        * ``uint64``: no (2)
        * ``int8``: yes; tested (3)
        * ``int16``: yes; tested
        * ``int32``: no (2)
        * ``int64``: no (2)
        * ``float16``: yes; tested (4)
        * ``float32``: yes; tested
        * ``float64``: yes; tested
        * ``float128``: no (1)
        * ``bool``: yes; tested (4)

        - (1) rejected by cv2
        - (2) causes cv2 error: ``cv2.error: OpenCV(3.4.4)
              (...)imgwarp.cpp:1805: error:
              (-215:Assertion failed) ifunc != 0 in function 'remap'``
        - (3) mapped internally to ``int16``
        - (4) mapped internally to ``float32``


    Parameters
    ----------
    scale : number or tuple of number or list of number or imgaug.parameters.StochasticParameter or dict {"x": number/tuple/list/StochasticParameter, "y": number/tuple/list/StochasticParameter}, optional
        Scaling factor to use, where ``1.0`` denotes "no change" and
        ``0.5`` is zoomed out to ``50`` percent of the original size.

            * If a single number, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the interval ``[a, b]``. That value will be
              used identically for both x- and y-axis.
            * If a list, then a random value will be sampled from that list
              per image (again, used for both x- and y-axis).
            * If a ``StochasticParameter``, then from that parameter a value
              will be sampled per image (again, used for both x- and y-axis).
            * If a dictionary, then it is expected to have the keys ``x``
              and/or ``y``. Each of these keys can have the same values as
              described above. Using a dictionary allows to set different
              values for the two axis and sampling will then happen
              *independently* per axis, resulting in samples that differ
              between the axes.

    translate_percent : None or number or tuple of number or list of number or imgaug.parameters.StochasticParameter or dict {"x": number/tuple/list/StochasticParameter, "y": number/tuple/list/StochasticParameter}, optional
        Translation as a fraction of the image height/width (x-translation,
        y-translation), where ``0`` denotes "no change" and ``0.5`` denotes
        "half of the axis size".

            * If ``None`` then equivalent to ``0.0`` unless `translate_px` has
              a value other than ``None``.
            * If a single number, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the interval ``[a, b]``. That sampled fraction
              value will be used identically for both x- and y-axis.
            * If a list, then a random value will be sampled from that list
              per image (again, used for both x- and y-axis).
            * If a ``StochasticParameter``, then from that parameter a value
              will be sampled per image (again, used for both x- and y-axis).
            * If a dictionary, then it is expected to have the keys ``x``
              and/or ``y``. Each of these keys can have the same values as
              described above. Using a dictionary allows to set different
              values for the two axis and sampling will then happen
              *independently* per axis, resulting in samples that differ
              between the axes.

    translate_px : None or int or tuple of int or list of int or imgaug.parameters.StochasticParameter or dict {"x": int/tuple/list/StochasticParameter, "y": int/tuple/list/StochasticParameter}, optional
        Translation in pixels.

            * If ``None`` then equivalent to ``0`` unless `translate_percent`
              has a value other than ``None``.
            * If a single int, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the discrete interval ``[a..b]``. That number
              will be used identically for both x- and y-axis.
            * If a list, then a random value will be sampled from that list
              per image (again, used for both x- and y-axis).
            * If a ``StochasticParameter``, then from that parameter a value
              will be sampled per image (again, used for both x- and y-axis).
            * If a dictionary, then it is expected to have the keys ``x``
              and/or ``y``. Each of these keys can have the same values as
              described above. Using a dictionary allows to set different
              values for the two axis and sampling will then happen
              *independently* per axis, resulting in samples that differ
              between the axes.

    rotate : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Rotation in degrees (**NOT** radians), i.e. expected value range is
        around ``[-360, 360]``. Rotation happens around the *center* of the
        image, not the top left corner as in some other frameworks.

            * If a number, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the interval ``[a, b]`` and used as the rotation
              value.
            * If a list, then a random value will be sampled from that list
              per image.
            * If a ``StochasticParameter``, then this parameter will be used to
              sample the rotation value per image.

    shear : number or tuple of number or list of number or imgaug.parameters.StochasticParameter or dict {"x": int/tuple/list/StochasticParameter, "y": int/tuple/list/StochasticParameter}, optional
        Shear in degrees (**NOT** radians), i.e. expected value range is
        around ``[-360, 360]``, with reasonable values being in the range
        of ``[-45, 45]``.

            * If a number, then that value will be used for all images as
              the shear on the x-axis (no shear on the y-axis will be done).
            * If a tuple ``(a, b)``, then two value will be uniformly sampled
              per image from the interval ``[a, b]`` and be used as the
              x- and y-shear value.
            * If a list, then two random values will be sampled from that list
              per image, denoting x- and y-shear.
            * If a ``StochasticParameter``, then this parameter will be used
              to sample the x- and y-shear values per image.
            * If a dictionary, then similar to `translate_percent`, i.e. one
              ``x`` key and/or one ``y`` key are expected, denoting the
              shearing on the x- and y-axis respectively. The allowed datatypes
              are again ``number``, ``tuple`` ``(a, b)``, ``list`` or
              ``StochasticParameter``.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        Interpolation order to use. Same meaning as in ``skimage``:

            * ``0``: ``Nearest-neighbor``
            * ``1``: ``Bi-linear`` (default)
            * ``2``: ``Bi-quadratic`` (not recommended by skimage)
            * ``3``: ``Bi-cubic``
            * ``4``: ``Bi-quartic``
            * ``5``: ``Bi-quintic``

        Method ``0`` and ``1`` are fast, ``3`` is a bit slower, ``4`` and
        ``5`` are very slow. If the backend is ``cv2``, the mapping to
        OpenCV's interpolation modes is as follows:

            * ``0`` -> ``cv2.INTER_NEAREST``
            * ``1`` -> ``cv2.INTER_LINEAR``
            * ``2`` -> ``cv2.INTER_CUBIC``
            * ``3`` -> ``cv2.INTER_CUBIC``
            * ``4`` -> ``cv2.INTER_CUBIC``

        As datatypes this parameter accepts:

            * If a single ``int``, then that order will be used for all images.
            * If a list, then a random value will be sampled from that list
              per image.
            * If ``imgaug.ALL``, then equivalant to list ``[0, 1, 3, 4, 5]``
              in case of ``backend=skimage`` and otherwise ``[0, 1, 3]``.
            * If ``StochasticParameter``, then that parameter is queried per
              image to sample the order value to use.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        The constant value to use when filling in newly created pixels.
        (E.g. translating by 1px to the right will create a new 1px-wide
        column of pixels on the left of the image).  The value is only used
        when `mode=constant`. The expected value range is ``[0, 255]`` for
        ``uint8`` images. It may be a float value.

            * If this is a single number, then that value will be used
              (e.g. 0 results in black pixels).
            * If a tuple ``(a, b)``, then three values (for three image
              channels) will be uniformly sampled per image from the
              interval ``[a, b]``.
            * If a list, then a random value will be sampled from that list
              per image.
            * If ``imgaug.ALL`` then equivalent to tuple ``(0, 255)`.
            * If a ``StochasticParameter``, a new value will be sampled from
              the parameter per image.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        Method to use when filling in newly created pixels.
        Same meaning as in ``skimage`` (and :func:`numpy.pad`):

            * ``constant``: Pads with a constant value
            * ``edge``: Pads with the edge values of array
            * ``symmetric``: Pads with the reflection of the vector mirrored
              along the edge of the array.
            * ``reflect``: Pads with the reflection of the vector mirrored on
              the first and last values of the vector along each axis.
            * ``wrap``: Pads with the wrap of the vector along the axis.
              The first values are used to pad the end and the end values
              are used to pad the beginning.

        If ``cv2`` is chosen as the backend the mapping is as follows:

            * ``constant`` -> ``cv2.BORDER_CONSTANT``
            * ``edge`` -> ``cv2.BORDER_REPLICATE``
            * ``symmetric`` -> ``cv2.BORDER_REFLECT``
            * ``reflect`` -> ``cv2.BORDER_REFLECT_101``
            * ``wrap`` -> ``cv2.BORDER_WRAP``

        The datatype of the parameter may be:

            * If a single string, then that mode will be used for all images.
            * If a list of strings, then a random mode will be picked
              from that list per image.
            * If ``imgaug.ALL``, then a random mode from all possible modes
              will be picked.
            * If ``StochasticParameter``, then the mode will be sampled from
              that parameter per image, i.e. it must return only the above
              mentioned strings.

    fit_output : bool, optional
        Whether to modify the affine transformation so that the whole output
        image is always contained in the image plane (``True``) or accept
        parts of the image being outside the image plane (``False``).
        This can be thought of as first applying the affine transformation
        and then applying a second transformation to "zoom in" on the new
        image so that it fits the image plane,
        This is useful to avoid corners of the image being outside of the image
        plane after applying rotations. It will however negate translation
        and scaling.
        Note also that activating this may lead to image sizes differing from
        the input image sizes. To avoid this, wrap ``Affine`` in
        :class:`~imgaug.augmenters.size.KeepSizeByResize`,
        e.g. ``KeepSizeByResize(Affine(...))``.

    backend : str, optional
        Framework to use as a backend. Valid values are ``auto``, ``skimage``
        (scikit-image's warp) and ``cv2`` (OpenCV's warp).
        If ``auto`` is used, the augmenter will automatically try
        to use ``cv2`` whenever possible (order must be in ``[0, 1, 3]``). It
        will silently fall back to skimage if order/dtype is not supported by
        cv2. cv2 is generally faster than skimage. It also supports RGB cvals,
        while skimage will resort to intensity cvals (i.e. 3x the same value
        as RGB). If ``cv2`` is chosen and order is ``2`` or ``4``, it will
        automatically fall back to order ``3``.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.Affine(scale=2.0)

    Zoom in on all images by a factor of ``2``.

    >>> aug = iaa.Affine(translate_px=16)

    Translate all images on the x- and y-axis by 16 pixels (towards the
    bottom right) and fill up any new pixels with zero (black values).

    >>> aug = iaa.Affine(translate_percent=0.1)

    Translate all images on the x- and y-axis by ``10`` percent of their
    width/height (towards the bottom right). The pixel values are computed
    per axis based on that axis' size. Fill up any new pixels with zero
    (black values).

    >>> aug = iaa.Affine(rotate=35)

    Rotate all images by ``35`` *degrees*. Fill up any new pixels with zero
    (black values).

    >>> aug = iaa.Affine(shear=15)

    Shear all images by ``15`` *degrees*. Fill up any new pixels with zero
    (black values).

    >>> aug = iaa.Affine(translate_px=(-16, 16))

    Translate all images on the x- and y-axis by a random value
    between ``-16`` and ``16`` pixels (to the bottom right) and fill up any new
    pixels with zero (black values). The translation value is sampled once
    per image and is the same for both axis.

    >>> aug = iaa.Affine(translate_px={"x": (-16, 16), "y": (-4, 4)})

    Translate all images on the x-axis by a random value
    between ``-16`` and ``16`` pixels (to the right) and on the y-axis by a
    random value between ``-4`` and ``4`` pixels to the bottom. The sampling
    happens independently per axis, so even if both intervals were identical,
    the sampled axis-wise values would likely be different.
    This also fills up any new pixels with zero (black values).

    >>> aug = iaa.Affine(scale=2.0, order=[0, 1])

    Same as in the above `scale` example, but uses (randomly) either
    nearest neighbour interpolation or linear interpolation. If `order` is
    not specified, ``order=1`` would be used by default.

    >>> aug = iaa.Affine(translate_px=16, cval=(0, 255))

    Same as in the `translate_px` example above, but newly created pixels
    are now filled with a random color (sampled once per image and the
    same for all newly created pixels within that image).

    >>> aug = iaa.Affine(translate_px=16, mode=["constant", "edge"])

    Similar to the previous example, but the newly created pixels are
    filled with black pixels in half of all images (mode ``constant`` with
    default `cval` being ``0``) and in the other half of all images using
    ``edge`` mode, which repeats the color of the spatially closest pixel
    of the corresponding image edge.

    >>> aug = iaa.Affine(shear={"y": (-45, 45)})

    Shear images only on the y-axis. Set `shear` to ``shear=(-45, 45)`` to
    shear randomly on both axes, using for each image the same sample for
    both the x- and y-axis. Use ``shear={"x": (-45, 45), "y": (-45, 45)}``
    to get independent samples per axis.

    Nr	   r   r   Fr!   
deprecatedc                    s8  t t| j||||d |||||g}tdd |D r.ddd}ddd}d}ddd}n|d ur4|nd	}|d ur<|nd
}|d urD|nd
}|
dv sQJ d|
f |
| _t||
| _t|| _t	|| _
| || _| ||| _tj|dd ddd| _| |\| _| _|	| _d| _d| _d| _d| _d| _d| _d S )Nr   r^   r   deterministicc                 S   s   g | ]}|d u qS r   r&   )r*   pr&   r&   r,   r-     r2   z#Affine.__init__.<locals>.<listcomp>)g?g?r   r   )gg?)i   )i
         ?r   )r!   rU   r"   z<Expected 'backend' to be "auto", "skimage" or "cv2", got %s.r   TrC   r   r   r   )superr  r   r:   r?   r@   r>   rH   rB   rT   rS   _handle_scale_argr   _handle_translate_argr   r5   rG   r   _handle_shear_argr   _shear_param_typer   _order_heatmaps_order_segmentation_maps_mode_heatmaps_mode_segmentation_maps_cval_heatmaps_cval_segmentation_maps)r   r   translate_percenttranslate_pxr   r   r>   rB   rS   r   r?   r   r^   r   r	  r   	__class__r&   r,   r     sL   






zAffine.__init__c                 C   sv   t |tr1d|v sd|v sJ d|dd}|dd}tj|dddddtj|d	ddddfS tj|d
ddddS )Nr   r   XExpected scale dictionary to contain at least key "x" or key "y". Found neither of them.r  
scale['x']r  NTrC   
scale['y']r   )r8   dictr   r5   rG   )clsr   r   r   r&   r&   r,   r    s&   
zAffine._handle_scale_argc              	   C   s6  |d u r
|d u r
d}|d u s|d u sJ d|d urYt |trLd|v s+d|v s+J d|dd}|dd}tj|dd dddtj|d	d dddd
fS tj|dd dddd d
fS t |trd|v sjd|v sjJ d|dd}|dd}tj|dd ddddtj|dd dddddfS tj|dd ddddd dfS )Nr   ZExpected either translate_percent or translate_px to be provided, but neither of them was.r   r   dExpected translate_percent dictionary to contain at least key "x" or key "y". Found neither of them.translate_percent['x']TrC   translate_percent['y']r   r  _Expected translate_px dictionary to contain at least key "x" or key "y". Found neither of them.translate_px['x']FrD   rE   rF   Zallow_floatstranslate_px['y']r   r  )r8   r"  r   r5   rG   handle_discrete_param)r#  r  r  r   r   r&   r&   r,   r    sp   



zAffine._handle_translate_argc                 C   s   t |tr3d|v sd|v sJ d|dd}|dd}tj|dd dddtj|dd dddfd	fS d
}t|r<d}tj|dd ddd|fS )Nr   r   zXExpected shear dictionary to contain at least key "x" or key "y". Found neither of them.r   z
shear['x']TrC   z
shear['y']r"  othersingle-numberr   )r8   r"  r   r5   rG   r'   Zis_single_number)r#  r   r   r   Z
param_typer&   r&   r,   r    s4   
	
zAffine._handle_shear_argc              	   C   sN  |  |j|}|jd ur| |j||_|jd ur)| |j|d| j| j| jd|_|j	d ur>| |j	|d| j
| j| jd|_	dD ]d}t||}|d urt|D ]T\}}	|||	j| j\}
}t|
s|	jsd|	jdd vr|dkr|	 }| }t||
j}||}|	|}	n|	 }t||
j}|	|}	||	_|	||< qOq@|S )	Narr_0to1r   rf   r   	keypointsbounding_boxespolygonsline_stringsr   r   r2  )_draw_samplesr   images_augment_images_by_samplesheatmaps_augment_maps_by_samplesr  r  r  segmentation_mapsr  r  r  getattrr   r   rZ   r   r  emptyZto_keypoints_on_imageto_xy_arrayr   matrix_transformr   fill_from_xy_array_Zinvert_to_keypoints_on_image_)r   batchr   parentshookssamples	augm_name
augm_valuer   cbaoirg   rV   kpsoir   
coords_augr&   r&   r,   _augment_batch_4  s\   








zAffine._augment_batch_c              
   C   s  t |}t|}|sd n|j}g }|rd g| }	t|D ]M}
||
 }|d u r+|jn||
 }||
|j|| j\}}|j	|
 }|j
|
 }|j|
 }t|s`t||||||| jd}|| n|| |rk||	|
< q|rt dd |D }|dkrt||}|r||	f}|S )N)r>   rS   rB   rV   r?   c                 S      h | ]}|j qS r&   rZ   )r*   imager&   r&   r,   	<setcomp>      z4Affine._augment_images_by_samples.<locals>.<setcomp>r	   )rY   r'   is_np_arrayr]   r   r   rZ   r   r   rB   rS   r>   r  rl   r?   appendr   r   )r   r6  rC  image_shapesreturn_matrices	nb_imagesinput_was_arrayr   r   matricesr   rL  r   rg   rV   rB   rS   r>   rk   Z	nb_shapesr&   r&   r,   r7  h  sH   






z!Affine._augment_images_by_samplesc                    s  t |}| }|d urtj|df||d|_|d ur!|g| |_|d ur+|g| |_ fdd|D }	dd |D }
| j|	||
dd\}}t||||j}|D ]7\}}}}d|j	v r\qP|d	krot
|tjrotj|d
d|d}t| | | jrt||j	\}}n|j	}||_	qP|S )Nr	   r]   c                       g | ]}t | qS r&   r;  r*   augmentablearr_attr_namer&   r,   r-     s    z3Affine._augment_maps_by_samples.<locals>.<listcomp>c                 S      g | ]}|j qS r&   rK  rY  r&   r&   r,   r-     rN  T)rQ  rR  r   r   r   r  out)rY   r   r   fullrB   rS   r>   r7  ziprZ   r8   r'   HeatmapsOnImageclipsetattrr   r   )r   augmentablesrC  r\  rB   rS   r>   Z
cval_dtyperS  arrsrQ  arrs_augrU  genaugmentable_iarr_augrg   Zorder_i_Zoutput_shape_ir&   r[  r,   r9    s:   



zAffine._augment_maps_by_samplesc              
   C   s  | d}t| jtr&| jd j|f|d d| jd j|f|d df}n| jj|f|d d}||f}| jd d urW| jd j|f|d d| jd j|f|d df}n| jd j|f|d d}||f}| jj|f|d	 d}| jd
kr| jd j|f|d d| jd j|f|d df}n'| jdkr| jj|f|d d}|t	
|f}n| jj|f|d d}||f}| jj|df|d d}| jj|f|d d}	| jj|f|d d}
t||| jd ||||	|
dS )N   r   r   r	   r   r   r   r$   r   r"  r   r   r.  	   r     r   )	duplicater8   r   re   draw_samplesr   r   r  r   r   r   rB   rS   r>   r   )r   
nb_samplesr   rngsscale_samplestranslate_samplesrotate_samplesshear_samplescval_samplesmode_samplesorder_samplesr&   r&   r,   r5    sp   










zAffine._draw_samplesc              	   C   s(   | j | j| j| j| j| j| j| j| jg	S z=See :func:`~imgaug.augmenters.meta.Augmenter.get_parameters`.)	r   r   r   r   r>   rB   rS   r?   r   r   r&   r&   r,   get_parameters  s   zAffine.get_parameters)NNNNNr	   r   r   Fr!   NNr  r  NF)r   r   r  __doc__r   classmethodr  r  r  rI  r7  r9  r5  r|  __classcell__r&   r&   r  r,   r    s0       S6

?
5
0+;r  c                       *   e Zd ZdZ					d fd
d	Z  ZS )ScaleXa  Apply affine scaling on the x-axis to input data.

    This is a wrapper around :class:`Affine`.

    Added in 0.4.0.

    **Supported dtypes**:

    See :class:`~imgaug.augmenters.geometric.Affine`.

    Parameters
    ----------
    scale : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Analogous to ``scale`` in :class:`Affine`, except that this scale
        value only affects the x-axis. No dictionary input is allowed.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    fit_output : bool, optional
        See :class:`Affine`.

    backend : str, optional
        See :class:`Affine`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.ScaleX((0.5, 1.5))

    Create an augmenter that scales images along the width to sizes between
    ``50%`` and ``150%``. This does not change the image shape (i.e. height
    and width), only the pixels within the image are remapped and potentially
    new ones are filled in.

    r}   g      ?r	   r   r   Fr!   Nr  c                    ,   t t| jd|i||||||||	|
d
 d S )Nr   
r   r>   rB   rS   r   r?   r   r^   r   r	  )r  r  r   r   r   r>   rB   rS   r   r?   r   r^   r   r	  r  r&   r,   r   C     

zScaleX.__init__
r  r	   r   r   Fr!   NNr  r  r   r   r  r~  r   r  r&   r&   r  r,   r        =r  c                       r  )ScaleYa  Apply affine scaling on the y-axis to input data.

    This is a wrapper around :class:`Affine`.

    Added in 0.4.0.

    **Supported dtypes**:

    See :class:`~imgaug.augmenters.geometric.Affine`.

    Parameters
    ----------
    scale : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Analogous to ``scale`` in :class:`Affine`, except that this scale
        value only affects the y-axis. No dictionary input is allowed.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    fit_output : bool, optional
        See :class:`Affine`.

    backend : str, optional
        See :class:`Affine`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.ScaleY((0.5, 1.5))

    Create an augmenter that scales images along the height to sizes between
    ``50%`` and ``150%``. This does not change the image shape (i.e. height
    and width), only the pixels within the image are remapped and potentially
    new ones are filled in.

    r  r	   r   r   Fr!   Nr  c                    r  )Nr   r  )r  r  r   r  r  r&   r,   r     r  zScaleY.__init__r  r  r&   r&   r  r,   r  R  r  r  c                       *   e Zd ZdZ				d fd	d
	Z  ZS )
TranslateXa
  Apply affine translation on the x-axis to input data.

    This is a wrapper around :class:`Affine`.

    Added in 0.4.0.

    **Supported dtypes**:

    See :class:`~imgaug.augmenters.geometric.Affine`.

    Parameters
    ----------
    percent : None or number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Analogous to ``translate_percent`` in :class:`Affine`, except that
        this translation value only affects the x-axis. No dictionary input
        is allowed.

    px : None or int or tuple of int or list of int or imgaug.parameters.StochasticParameter or dict {"x": int/tuple/list/StochasticParameter, "y": int/tuple/list/StochasticParameter}, optional
        Analogous to ``translate_px`` in :class:`Affine`, except that
        this translation value only affects the x-axis. No dictionary input
        is allowed.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    fit_output : bool, optional
        See :class:`Affine`.

    backend : str, optional
        See :class:`Affine`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.TranslateX(px=(-20, 20))

    Create an augmenter that translates images along the x-axis by
    ``-20`` to ``20`` pixels.

    >>> aug = iaa.TranslateX(percent=(-0.1, 0.1))

    Create an augmenter that translates images along the x-axis by
    ``-10%`` to ``10%`` (relative to the x-axis size).

    Nr	   r   r   Fr!   r  c                    ^   |d u r
|d u r
d}t t| j|d urd|ind |d ur d|ind |||||||	|
|d d S )Ng      пg      ?r   r  r  r>   rB   rS   r   r?   r   r^   r   r	  )r  r  r   r   r   r   r>   rB   rS   r   r?   r   r^   r   r	  r  r&   r,   r        

zTranslateX.__init__NNr	   r   r   Fr!   NNr  r  r  r&   r&   r  r,   r        Fr  c                       r  )
TranslateYa
  Apply affine translation on the y-axis to input data.

    This is a wrapper around :class:`Affine`.

    Added in 0.4.0.

    **Supported dtypes**:

    See :class:`~imgaug.augmenters.geometric.Affine`.

    Parameters
    ----------
    percent : None or number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Analogous to ``translate_percent`` in :class:`Affine`, except that
        this translation value only affects the y-axis. No dictionary input
        is allowed.

    px : None or int or tuple of int or list of int or imgaug.parameters.StochasticParameter or dict {"x": int/tuple/list/StochasticParameter, "y": int/tuple/list/StochasticParameter}, optional
        Analogous to ``translate_px`` in :class:`Affine`, except that
        this translation value only affects the y-axis. No dictionary input
        is allowed.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    fit_output : bool, optional
        See :class:`Affine`.

    backend : str, optional
        See :class:`Affine`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.TranslateY(px=(-20, 20))

    Create an augmenter that translates images along the y-axis by
    ``-20`` to ``20`` pixels.

    >>> aug = iaa.TranslateY(percent=(-0.1, 0.1))

    Create an augmenter that translates images along the y-axis by
    ``-10%`` to ``10%`` (relative to the y-axis size).

    Nr	   r   r   Fr!   r  c                    r  )Nr  r   r  )r  r  r   r  r  r&   r,   r   B  r  zTranslateY.__init__r  r  r&   r&   r  r,   r    r  r  c                       r  )Rotatea  Apply affine rotation on the y-axis to input data.

    This is a wrapper around :class:`Affine`.
    It is the same as ``Affine(rotate=<value>)``.

    Added in 0.4.0.

    **Supported dtypes**:

    See :class:`~imgaug.augmenters.geometric.Affine`.

    Parameters
    ----------
    rotate : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    fit_output : bool, optional
        See :class:`Affine`.

    backend : str, optional
        See :class:`Affine`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.Rotate((-45, 45))

    Create an augmenter that rotates images by a random value between ``-45``
    and ``45`` degress.

    i   r	   r   r   Fr!   Nr  c                    s(   t t| j|||||||||	|
d
 d S )N)
r   r>   rB   rS   r   r?   r   r^   r   r	  )r  r  r   )r   r   r>   rB   rS   r   r?   r   r^   r   r	  r  r&   r,   r     s   

zRotate.__init__
r  r	   r   r   Fr!   NNr  r  r  r&   r&   r  r,   r  U      ;r  c                       r  )ShearXaM  Apply affine shear on the x-axis to input data.

    This is a wrapper around :class:`Affine`.

    Added in 0.4.0.

    **Supported dtypes**:

    See :class:`~imgaug.augmenters.geometric.Affine`.

    Parameters
    ----------
    shear : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Analogous to ``shear`` in :class:`Affine`, except that this shear
        value only affects the x-axis. No dictionary input is allowed.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    fit_output : bool, optional
        See :class:`Affine`.

    backend : str, optional
        See :class:`Affine`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.ShearX((-20, 20))

    Create an augmenter that shears images along the x-axis by random amounts
    between ``-20`` and ``20`` degrees.

    r  r	   r   r   Fr!   Nr  c                    r  )Nr   
r   r>   rB   rS   r   r?   r   r^   r   r	  )r  r  r   r   r   r>   rB   rS   r   r?   r   r^   r   r	  r  r&   r,   r     r  zShearX.__init__r  r  r&   r&   r  r,   r    r  r  c                       r  )ShearYaM  Apply affine shear on the y-axis to input data.

    This is a wrapper around :class:`Affine`.

    Added in 0.4.0.

    **Supported dtypes**:

    See :class:`~imgaug.augmenters.geometric.Affine`.

    Parameters
    ----------
    shear : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Analogous to ``shear`` in :class:`Affine`, except that this shear
        value only affects the y-axis. No dictionary input is allowed.

    order : int or iterable of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :class:`Affine`.

    fit_output : bool, optional
        See :class:`Affine`.

    backend : str, optional
        See :class:`Affine`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.ShearY((-20, 20))

    Create an augmenter that shears images along the y-axis by random amounts
    between ``-20`` and ``20`` degrees.

    r  r	   r   r   Fr!   Nr  c                    r  )Nr   r  )r  r  r   r  r  r&   r,   r   '  r  zShearY.__init__r  r  r&   r&   r  r,   r    r  r  c                       s   e Zd ZdZdddddejdejddddf fdd	Zd	d
 Ze	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS )	AffineCv2az:  
    **Deprecated.** Augmenter to apply affine transformations to images using
    cv2 (i.e. opencv) backend.

    .. warning::

        This augmenter is deprecated since 0.4.0.
        Use ``Affine(..., backend='cv2')`` instead.

    Affine transformations
    involve:

        - Translation ("move" image on the x-/y-axis)
        - Rotation
        - Scaling ("zoom" in/out)
        - Shear (move one side of the image, turning a square into a trapezoid)

    All such transformations can create "new" pixels in the image without a
    defined content, e.g. if the image is translated to the left, pixels
    are created on the right.
    A method has to be defined to deal with these pixel values. The
    parameters `cval` and `mode` of this class deal with this.

    Some transformations involve interpolations between several pixels
    of the input image to generate output pixel values. The parameter `order`
    deals with the method of interpolation used for this.

    Deprecated since 0.4.0.

    **Supported dtypes**:

        * ``uint8``: yes; fully tested
        * ``uint16``: ?
        * ``uint32``: ?
        * ``uint64``: ?
        * ``int8``: ?
        * ``int16``: ?
        * ``int32``: ?
        * ``int64``: ?
        * ``float16``: ?
        * ``float32``: ?
        * ``float64``: ?
        * ``float128``: ?
        * ``bool``: ?

    Parameters
    ----------
    scale : number or tuple of number or list of number or imgaug.parameters.StochasticParameter or dict {"x": number/tuple/list/StochasticParameter, "y": number/tuple/list/StochasticParameter}, optional
        Scaling factor to use, where ``1.0`` denotes "no change" and
        ``0.5`` is zoomed out to ``50`` percent of the original size.

            * If a single number, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the interval ``[a, b]``. That value will be
              used identically for both x- and y-axis.
            * If a list, then a random value will be sampled from that list
              per image (again, used for both x- and y-axis).
            * If a ``StochasticParameter``, then from that parameter a value
              will be sampled per image (again, used for both x- and y-axis).
            * If a dictionary, then it is expected to have the keys ``x``
              and/or ``y``. Each of these keys can have the same values as
              described above. Using a dictionary allows to set different
              values for the two axis and sampling will then happen
              *independently* per axis, resulting in samples that differ
              between the axes.

    translate_percent : number or tuple of number or list of number or imgaug.parameters.StochasticParameter or dict {"x": number/tuple/list/StochasticParameter, "y": number/tuple/list/StochasticParameter}, optional
        Translation as a fraction of the image height/width (x-translation,
        y-translation), where ``0`` denotes "no change" and ``0.5`` denotes
        "half of the axis size".

            * If ``None`` then equivalent to ``0.0`` unless `translate_px` has
              a value other than ``None``.
            * If a single number, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the interval ``[a, b]``. That sampled fraction
              value will be used identically for both x- and y-axis.
            * If a list, then a random value will be sampled from that list
              per image (again, used for both x- and y-axis).
            * If a ``StochasticParameter``, then from that parameter a value
              will be sampled per image (again, used for both x- and y-axis).
            * If a dictionary, then it is expected to have the keys ``x``
              and/or ``y``. Each of these keys can have the same values as
              described above. Using a dictionary allows to set different
              values for the two axis and sampling will then happen
              *independently* per axis, resulting in samples that differ
              between the axes.

    translate_px : int or tuple of int or list of int or imgaug.parameters.StochasticParameter or dict {"x": int/tuple/list/StochasticParameter, "y": int/tuple/list/StochasticParameter}, optional
        Translation in pixels.

            * If ``None`` then equivalent to ``0`` unless `translate_percent`
              has a value other than ``None``.
            * If a single int, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the discrete interval ``[a..b]``. That number
              will be used identically for both x- and y-axis.
            * If a list, then a random value will be sampled from that list
              per image (again, used for both x- and y-axis).
            * If a ``StochasticParameter``, then from that parameter a value
              will be sampled per image (again, used for both x- and y-axis).
            * If a dictionary, then it is expected to have the keys ``x``
              and/or ``y``. Each of these keys can have the same values as
              described above. Using a dictionary allows to set different
              values for the two axis and sampling will then happen
              *independently* per axis, resulting in samples that differ
              between the axes.

    rotate : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Rotation in degrees (**NOT** radians), i.e. expected value range is
        around ``[-360, 360]``. Rotation happens around the *center* of the
        image, not the top left corner as in some other frameworks.

            * If a number, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the interval ``[a, b]`` and used as the rotation
              value.
            * If a list, then a random value will be sampled from that list
              per image.
            * If a ``StochasticParameter``, then this parameter will be used to
              sample the rotation value per image.

    shear : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Shear in degrees (**NOT** radians), i.e. expected value range is
        around ``[-360, 360]``.

            * If a number, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a value will be uniformly sampled
              per image from the interval ``[a, b]`` and be used as the
              rotation value.
            * If a list, then a random value will be sampled from that list
              per image.
            * If a ``StochasticParameter``, then this parameter will be used
              to sample the shear value per image.

    order : int or list of int or str or list of str or imaug.ALL or imgaug.parameters.StochasticParameter, optional
        Interpolation order to use. Allowed are:

            * ``cv2.INTER_NEAREST`` (nearest-neighbor interpolation)
            * ``cv2.INTER_LINEAR`` (bilinear interpolation, used by default)
            * ``cv2.INTER_CUBIC`` (bicubic interpolation over ``4x4`` pixel
                neighborhood)
            * ``cv2.INTER_LANCZOS4``
            * string ``nearest`` (same as ``cv2.INTER_NEAREST``)
            * string ``linear`` (same as ``cv2.INTER_LINEAR``)
            * string ``cubic`` (same as ``cv2.INTER_CUBIC``)
            * string ``lanczos4`` (same as ``cv2.INTER_LANCZOS``)

        ``INTER_NEAREST`` (nearest neighbour interpolation) and
        ``INTER_NEAREST`` (linear interpolation) are the fastest.

            * If a single ``int``, then that order will be used for all images.
            * If a string, then it must be one of: ``nearest``, ``linear``,
              ``cubic``, ``lanczos4``.
            * If an iterable of ``int``/``str``, then for each image a random
              value will be sampled from that iterable (i.e. list of allowed
              order values).
            * If ``imgaug.ALL``, then equivalant to list ``[cv2.INTER_NEAREST,
              cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_LANCZOS4]``.
            * If ``StochasticParameter``, then that parameter is queried per
              image to sample the order value to use.

    cval : number or tuple of number or list of number or imaug.ALL or imgaug.parameters.StochasticParameter, optional
        The constant value to use when filling in newly created pixels.
        (E.g. translating by 1px to the right will create a new 1px-wide
        column of pixels on the left of the image).  The value is only used
        when `mode=constant`. The expected value range is ``[0, 255]`` for
        ``uint8`` images. It may be a float value.

            * If this is a single number, then that value will be used
              (e.g. 0 results in black pixels).
            * If a tuple ``(a, b)``, then three values (for three image
              channels) will be uniformly sampled per image from the
              interval ``[a, b]``.
            * If a list, then a random value will be sampled from that list
              per image.
            * If ``imgaug.ALL`` then equivalent to tuple ``(0, 255)`.
            * If a ``StochasticParameter``, a new value will be sampled from
              the parameter per image.

    mode : int or str or list of str or list of int or imgaug.ALL or imgaug.parameters.StochasticParameter,
           optional
        Method to use when filling in newly created pixels.
        Same meaning as in OpenCV's border mode. Let ``abcdefgh`` be an image's
        content and ``|`` be an image boundary after which new pixels are
        filled in, then the valid modes and their behaviour are the following:

            * ``cv2.BORDER_REPLICATE``: ``aaaaaa|abcdefgh|hhhhhhh``
            * ``cv2.BORDER_REFLECT``: ``fedcba|abcdefgh|hgfedcb``
            * ``cv2.BORDER_REFLECT_101``: ``gfedcb|abcdefgh|gfedcba``
            * ``cv2.BORDER_WRAP``: ``cdefgh|abcdefgh|abcdefg``
            * ``cv2.BORDER_CONSTANT``: ``iiiiii|abcdefgh|iiiiiii``,
               where ``i`` is the defined cval.
            * ``replicate``: Same as ``cv2.BORDER_REPLICATE``.
            * ``reflect``: Same as ``cv2.BORDER_REFLECT``.
            * ``reflect_101``: Same as ``cv2.BORDER_REFLECT_101``.
            * ``wrap``: Same as ``cv2.BORDER_WRAP``.
            * ``constant``: Same as ``cv2.BORDER_CONSTANT``.

        The datatype of the parameter may be:

            * If a single ``int``, then it must be one of the ``cv2.BORDER_*``
              constants.
            * If a single string, then it must be one of: ``replicate``,
              ``reflect``, ``reflect_101``, ``wrap``, ``constant``.
            * If a list of ``int``/``str``, then per image a random mode will
              be picked from that list.
            * If ``imgaug.ALL``, then a random mode from all possible modes
              will be picked.
            * If ``StochasticParameter``, then the mode will be sampled from
              that parameter per image, i.e. it must return only the above
              mentioned strings.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.AffineCv2(scale=2.0)

    Zoom in on all images by a factor of ``2``.

    >>> aug = iaa.AffineCv2(translate_px=16)

    Translate all images on the x- and y-axis by 16 pixels (towards the
    bottom right) and fill up any new pixels with zero (black values).

    >>> aug = iaa.AffineCv2(translate_percent=0.1)

    Translate all images on the x- and y-axis by ``10`` percent of their
    width/height (towards the bottom right). The pixel values are computed
    per axis based on that axis' size. Fill up any new pixels with zero
    (black values).

    >>> aug = iaa.AffineCv2(rotate=35)

    Rotate all images by ``35`` *degrees*. Fill up any new pixels with zero
    (black values).

    >>> aug = iaa.AffineCv2(shear=15)

    Shear all images by ``15`` *degrees*. Fill up any new pixels with zero
    (black values).

    >>> aug = iaa.AffineCv2(translate_px=(-16, 16))

    Translate all images on the x- and y-axis by a random value
    between ``-16`` and ``16`` pixels (to the bottom right) and fill up any new
    pixels with zero (black values). The translation value is sampled once
    per image and is the same for both axis.

    >>> aug = iaa.AffineCv2(translate_px={"x": (-16, 16), "y": (-4, 4)})

    Translate all images on the x-axis by a random value
    between ``-16`` and ``16`` pixels (to the right) and on the y-axis by a
    random value between ``-4`` and ``4`` pixels to the bottom. The sampling
    happens independently per axis, so even if both intervals were identical,
    the sampled axis-wise values would likely be different.
    This also fills up any new pixels with zero (black values).

    >>> aug = iaa.AffineCv2(scale=2.0, order=[0, 1])

    Same as in the above `scale` example, but uses (randomly) either
    nearest neighbour interpolation or linear interpolation. If `order` is
    not specified, ``order=1`` would be used by default.

    >>> aug = iaa.AffineCv2(translate_px=16, cval=(0, 255))

    Same as in the `translate_px` example above, but newly created pixels
    are now filled with a random color (sampled once per image and the
    same for all newly created pixels within that image).

    >>> aug = iaa.AffineCv2(translate_px=16, mode=["constant", "replicate"])

    Similar to the previous example, but the newly created pixels are
    filled with black pixels in half of all images (mode ``constant`` with
    default `cval` being ``0``) and in the other half of all images using
    ``replicate`` mode, which repeats the color of the spatially closest pixel
    of the corresponding image edge.

    r  Nr   r   r  c              	      s  t t| j|	|
||d tjddd tjtjtjtj	gg d|tj
kr-t| _nt|rG|v s@J dt|f t|| _nlt|ra|v sZJ dt|f t|| _nRt|trtdd	 |D }|sJ d
tdd	 |D f tfdd	|D }|sJ d t|f t|| _nt|tjr|| _n	tdt|f |tj
krtdd| _ntj|dddddd| _tjtjtjtjtjg g d|tj
krt | _ nt|r| v sJ dt |f t|| _ ntt|r|v sJ dt|f t|| _ nXt|trctdd	 |D }|s?J dtdd	 |D f t fdd	|D }|s\J dt  t|f t|| _ nt|tjrn|| _ n	tdt|f t|t!rd|v sd|v sJ d |"dd!}|"dd!}tj#|d"d#ddd$tj#|d%d#ddd$f| _$ntj#|d&d#ddd$| _$|d u r|d u rd}|d u s|d u sJ d'|d urt|t!rd|v sd|v sJ d(|"dd}|"dd}tj#|d)d ddd$tj#|d*d ddd$f| _%nPtj#|d+d ddd$| _%nDt|t!rQd|v s-d|v s-J d,|"dd}|"dd}tj|d-d ddd.dtj|d/d ddd.df| _%ntj|d0d ddd.d| _%tj#|d1d ddd$| _&tj#|d2d ddd$| _'d S )3Nr  z\AffineCv2 is deprecated. Use imgaug.augmenters.geometric.Affine(..., backend='cv2') instead.r   )
stacklevelnearestZlinearZcubicZlanczos4z3Expected order's integer value to be in %s, got %d.z#Expected order to be in %s, got %s.c                 S       g | ]}t |pt |qS r&   r'   r(   rJ   r)   r&   r&   r,   r-   	      z&AffineCv2.__init__.<locals>.<listcomp>zCExpected order list to only contain integers/strings, got types %s.c                 S   r/   r&   r0   r)   r&   r&   r,   r-   	  r2   c                       g | ]}|  v qS r&   r&   r)   )available_ordersavailable_orders_strr&   r,   r-   	      z.Expected all order values to be in %s, got %s.zaExpected order to be imgaug.ALL, int, string, a list ofint/string or StochasticParameter, got %s.r   rA   rB   )r   rA   Tr*  	replicater   Zreflect_101r    r   "Expected mode to be in %s, got %d."Expected mode to be in %s, got %s.c                 S   r  r&   r  r)   r&   r&   r,   r-   	  s    BExpected mode list to only contain integers/strings, got types %s.c                 S   r/   r&   r0   r)   r&   r&   r,   r-   	  r2   c                    r  r&   r&   r)   available_modesavailable_modes_strr&   r,   r-   	  s    -Expected all mode values to be in %s, got %s.gExpected mode to be imgaug.ALL, an int, a string, a list of int/strings or StochasticParameter, got %s.r   r   r  r  r  r   rC   r!  r   r$  r%  r&  r'  r  r(  r)  Fr+  r  r   r   )(r  r  r   r'   Zwarn_deprecatedr"   INTER_NEARESTINTER_LINEARINTER_CUBICINTER_LANCZOS4r4   r5   r6   r>   r(   r;   r7   rJ   r8   r9   r:   r<   r=   r1   ZDiscreteUniformrB   r,  BORDER_REPLICATEBORDER_REFLECTBORDER_REFLECT_101BORDER_WRAPBORDER_CONSTANTrS   r"  r   rG   r   r   r   r   )r   r   r  r  r   r   r>   rB   rS   r   r^   r   r	  valid_typesZvalid_ordersZall_valid_typesZall_valid_modesr   r   r  )r  r  r  r  r,   r   `	  sn  









	
	
zAffineCv2.__init__c              
   C   s>   t |}| ||\}}}}	}
}}| |||||	|
||}|S r   )rY   r5  r7  )r   r6  r   rA  rB  rS  rt  ru  rv  rw  rx  ry  rz  r   r&   r&   r,   _augment_images
  s   
zAffineCv2._augment_imagesc	           #   
   C   s\  t jt jt jt jd}	t jt jt jt jt j	d}
t
|}|}t|D ]}|| jd || jd }}|d d }|d d }|d | |d | }}|d | }|d | }t|rntt||| jd  }n|}t|rtt||| jd  }n|}|| }|| }|| }|| }|| }t|r|n|
| }t|r|n|	| }|dkp|dkp|dkp|dkp|dkp|dk}|r%tj| | gd}tj||f||ft|t|d	}tj||gd} || |  }!t jt|| |!jd d
 ||f||tdd |D d}"|"jd
kr |"dtjf }"|"||< q#|| ||< q#|S )Nr  r  r   r	   r   r}   r  r   r   r   c                 S   r/   r&   rW   rN   r&   r&   r,   r-   h
  r2   z8AffineCv2._augment_images_by_samples.<locals>.<listcomp>r   .)r"   r  r  r  r  r  r  r  r  r  rY   r   r   rZ   r'   is_single_floatrX   r   r   r(   r   r   r   mathradiansr   r   r   re   r   r   )#r#  r6  rt  ru  rv  rw  rx  ry  rz  Zorder_str_to_intZmode_str_to_intrS  r   r   r   r   r   r   r   r   r   r   r   r   r   r   rB   rS   r>   
any_changer   r   r   rg   rk   r&   r&   r,   r7  
  s   





z$AffineCv2._augment_images_by_samplesc              
   C   s   t |}| ||\}}}}	}
}}tj|
jd dftjd}
dgt | }dd |D }| |||||	|
||}t||D ]\}}||_q=|S )Nr   r	   rV  r   c                 S   r]  r&   )r/  )r*   	heatmap_ir&   r&   r,   r-   }
  rN  z/AffineCv2._augment_heatmaps.<locals>.<listcomp>)	rY   r5  r   zerosrZ   r   r7  ra  r/  )r   r8  r   rA  rB  rS  rt  ru  rv  rw  rx  ry  rz  rf  rg  r  rj  r&   r&   r,   _augment_heatmapsv
  s    
zAffineCv2._augment_heatmapsc              
   C   s   t |}| ||\}}}}	}
}}tj|
jd dftjd}
dgt | }dgt | }dd |D }| |||||	|
||}t||D ]\}}||_qD|S )Nr   r	   rV  r   c                 S   r]  r&   )rf   )r*   	segmaps_ir&   r&   r,   r-   
  rN  z8AffineCv2._augment_segmentation_maps.<locals>.<listcomp>)	rY   r5  r   r  rZ   r   r7  ra  rf   )r   Zsegmapsr   rA  rB  rS  rt  ru  rv  rw  rx  ry  rz  rf  rg  r  rj  r&   r&   r,   _augment_segmentation_maps
  s"   
z$AffineCv2._augment_segmentation_mapsc           $      C   s  g }t |}| ||\}}}	}
}}}t|D ]\}}|js$|| q|j|j}}|d d }|d d }|d | |d | }}|d | }|d | }t|rbt	t
||jd  }n|}t|rvt	t
||jd  }n|}|	| }|
| }|dkp|dkp|dkp|dkp|dkp|dk}|rtj| | gd}tj||f||ft|t|d}tj||gd}|| | } | }!t|!| j}"dd	 t|j|"D }#||j|#|jd
 q|| q|S )Nr   r}   r   r	   r  r   r   c                 S   s&   g | ]\}}|j |d  |d dqS )r   r	   r  )deepcopy)r*   kpr   r&   r&   r,   r-   
  s    z0AffineCv2._augment_keypoints.<locals>.<listcomp>)r1  rZ   )rY   r5  r   r1  rP  r   r   r'   r  rX   r   r   rZ   r   r   r   r  r  r=  r>  r   ra  r  )$r   keypoints_on_imagesr   rA  rB  r   rS  rt  ru  rv  rw  Z_cval_samplesZ_mode_samplesZ_order_samplesr   Zkeypoints_on_imager   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   rg   r   rH  Zkps_newr&   r&   r,   _augment_keypoints
  s   






zAffineCv2._augment_keypointsc                 C      |  ||||S r   )Z_augment_polygons_as_keypoints)r   Zpolygons_on_imagesr   rA  rB  r&   r&   r,   _augment_polygons
     zAffineCv2._augment_polygonsc                 C   r  r   )Z"_augment_line_strings_as_keypoints)r   Zline_strings_on_imagesr   rA  rB  r&   r&   r,   _augment_line_strings
  r  zAffineCv2._augment_line_stringsc                 C   r  r   )Z$_augment_bounding_boxes_as_keypoints)r   Zbounding_boxes_on_imagesr   rA  rB  r&   r&   r,   _augment_bounding_boxes
  r  z!AffineCv2._augment_bounding_boxesc                 C       | j | j| j| j| j| j| jgS r{  )r   r   r   r   r>   rB   rS   r   r&   r&   r,   r|  
  s   zAffineCv2.get_parametersc                 C   s  | d}t| jtr&| jd j|f|d d| jd j|f|d df}n| jj|f|d d}||f}t| jtrV| jd j|f|d d| jd j|f|d df}n| jj|f|d d}||f}g d	}tdD ]}|| jj	|v sJ d
t
||| jj	f qn| jj|f|d d}| jj|f|d d}	| jj|df|d d}
| jj|f|d d}| jj|f|d d}||||	|
||fS )Nro  r   rm  r	   r   r   r   r$   )r   rs   r   r   z;Expected translate_samples to have any dtype of %s. Got %s.r   r   r   rn  r  )rp  r8   r   re   rq  r   r   r   r]   r^   r;   r   r   rB   rS   r>   )r   rr  r   rs  rt  ru  Z	valid_dtsr   rv  rw  rx  ry  rz  r&   r&   r,   r5  
  sd   






zAffineCv2._draw_samples)r   r   r  r~  r"   r  r  r   r  r  r7  r  r  r  r  r  r  r|  r5  r  r&   r&   r  r,   r  6  s,      +
 0

\Cr  c                   @   s   e Zd Zdd Zdd ZdS )_PiecewiseAffineSamplingResultc                 C   s(   || _ || _|| _|| _|| _|| _d S r   )r   r   r>   jitterrB   rS   )r   r   r   r  r>   rB   rS   r&   r&   r,   r         
z'_PiecewiseAffineSamplingResult.__init__c                 C   s.   t |\}}}| j| }tt|||}|S r   )r[   r\   rB   ra   rb   )r   r   r]   rh   rk  rj   rB   r&   r&   r,   get_clipped_cval(  s   
z/_PiecewiseAffineSamplingResult.get_clipped_cvalN)r   r   r  r   r  r&   r&   r&   r,   r    s    r  c                       sd   e Zd ZdZ						d fd
d	Zdd Zdd Zdd Zdd Zdd Z	dd Z
dd Z  ZS )PiecewiseAffinea  
    Apply affine transformations that differ between local neighbourhoods.

    This augmenter places a regular grid of points on an image and randomly
    moves the neighbourhood of these point around via affine transformations.
    This leads to local distortions.

    This is mostly a wrapper around scikit-image's ``PiecewiseAffine``.
    See also ``Affine`` for a similar technique.

    .. note::

        This augmenter is very slow. See :ref:`performance`.
        Try to use ``ElasticTransformation`` instead, which is at least 10x
        faster.

    .. note::

        For coordinate-based inputs (keypoints, bounding boxes, polygons,
        ...), this augmenter still has to perform an image-based augmentation,
        which will make it significantly slower for such inputs than other
        augmenters. See :ref:`performance`.

    **Supported dtypes**:

        * ``uint8``: yes; fully tested
        * ``uint16``: yes; tested (1)
        * ``uint32``: yes; tested (1) (2)
        * ``uint64``: no (3)
        * ``int8``: yes; tested (1)
        * ``int16``: yes; tested (1)
        * ``int32``: yes; tested (1) (2)
        * ``int64``: no (3)
        * ``float16``: yes; tested (1)
        * ``float32``: yes; tested (1)
        * ``float64``: yes; tested (1)
        * ``float128``: no (3)
        * ``bool``: yes; tested (1) (4)

        - (1) Only tested with `order` set to ``0``.
        - (2) scikit-image converts internally to ``float64``, which might
              introduce inaccuracies. Tests showed that these inaccuracies
              seemed to not be an issue.
        - (3) Results too inaccurate.
        - (4) Mapped internally to ``float64``.

    Parameters
    ----------
    scale : float or tuple of float or imgaug.parameters.StochasticParameter, optional
        Each point on the regular grid is moved around via a normal
        distribution. This scale factor is equivalent to the normal
        distribution's sigma. Note that the jitter (how far each point is
        moved in which direction) is multiplied by the height/width of the
        image if ``absolute_scale=False`` (default), so this scale can be
        the same for different sized images.
        Recommended values are in the range ``0.01`` to ``0.05`` (weak to
        strong augmentations).

            * If a single ``float``, then that value will always be used as
              the scale.
            * If a tuple ``(a, b)`` of ``float`` s, then a random value will
              be uniformly sampled per image from the interval ``[a, b]``.
            * If a list, then a random value will be picked from that list
              per image.
            * If a ``StochasticParameter``, then that parameter will be
              queried to draw one value per image.

    nb_rows : int or tuple of int or imgaug.parameters.StochasticParameter, optional
        Number of rows of points that the regular grid should have.
        Must be at least ``2``. For large images, you might want to pick a
        higher value than ``4``. You might have to then adjust scale to lower
        values.

            * If a single ``int``, then that value will always be used as the
              number of rows.
            * If a tuple ``(a, b)``, then a value from the discrete interval
              ``[a..b]`` will be uniformly sampled per image.
            * If a list, then a random value will be picked from that list
              per image.
            * If a StochasticParameter, then that parameter will be queried to
              draw one value per image.

    nb_cols : int or tuple of int or imgaug.parameters.StochasticParameter, optional
        Number of columns. Analogous to `nb_rows`.

    order : int or list of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :func:`~imgaug.augmenters.geometric.Affine.__init__`.

    cval : int or float or tuple of float or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :func:`~imgaug.augmenters.geometric.Affine.__init__`.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        See :func:`~imgaug.augmenters.geometric.Affine.__init__`.

    absolute_scale : bool, optional
        Take `scale` as an absolute value rather than a relative value.

    polygon_recoverer : 'auto' or None or imgaug.augmentables.polygons._ConcavePolygonRecoverer, optional
        The class to use to repair invalid polygons.
        If ``"auto"``, a new instance of
        :class`imgaug.augmentables.polygons._ConcavePolygonRecoverer`
        will be created.
        If ``None``, no polygon recoverer will be used.
        If an object, then that object will be used and must provide a
        ``recover_from()`` method, similar to
        :class:`~imgaug.augmentables.polygons._ConcavePolygonRecoverer`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.PiecewiseAffine(scale=(0.01, 0.05))

    Place a regular grid of points on each image and then randomly move each
    point around by ``1`` to ``5`` percent (with respect to the image
    height/width). Pixels between these points will be moved accordingly.

    >>> aug = iaa.PiecewiseAffine(scale=(0.01, 0.05), nb_rows=8, nb_cols=8)

    Same as the previous example, but uses a denser grid of ``8x8`` points
    (default is ``4x4``). This can be useful for large images.

    r   g{Gz?r   r   r	   r   r   FNr  c                    s   t t| j|	|
||d tj|ddddd| _tjd| jd| _tj|dd	ddd
d| _	tj|dd	ddd
d| _
t|dd| _t|| _t|| _|| _|| _|dkrWt | _d| _d| _d| _d| _d| _d| _d S )Nr  r   r   NTrC   r   locr   r   )r   NFr*  r   rU   )r?   r!   r   r   )r  r  r   r5   rG   r   Normalr  r,  r   r   r@   r>   rH   rB   rT   rS   absolute_scalepolygon_recovererr   r  r  r  r  r  r  )r   r   r   r   r>   rB   rS   r  r  r   r^   r   r	  r  r&   r,   r     s<   



zPiecewiseAffine.__init__c           
      C   s   |  |j|}|jd ur| |j||_|jd ur(| |jd|| j| j| j|_|j	d ur<| |j	d|| j
| j| j|_	|jd urTtj| j|d}| j|j|| jd|_dD ]}t||}|d urutj| j|d}| ||}	t|||	 qV|S )Nr/  rf   )rC  Z	recovererr1  r2  r4  )r5  r   r6  r7  r8  r9  r  r  r  r:  r  r  r  r3  	functoolspartial_augment_keypoints_by_samples_apply_to_polygons_as_keypointsr  r;  _apply_to_cbaois_as_keypointsrd  )
r   r@  r   rA  rB  rC  funcrD  rE  cbaoisr&   r&   r,   rI    sF   





zPiecewiseAffine._augment_batch_c           	   
   C   s   t j|g dg d| d |}t|D ]Y\}}| |j|j|j| |j| |j| }|d url|j}|jj	dkr>|
tj}tj|||j| |j| |||jd|| jd}|j	dkrb|dk}nt ||}|||< q|S )Nrm   ro   ry   bTr{   r}   )r[   r~   r   _get_transformerrZ   r   r   r  r]   kindr   r   r   r   r   r>   rS   r  r   )	r   r6  rC  r   r   rL  transformerr   rk   r&   r&   r,   r7    sB   


z*PiecewiseAffine._augment_images_by_samplesc              
   C   s   |}t |D ]h\}}	t|	|}
| |
j|	j|j| |j| |j| }|d urntj|
||d ur1|n|j	| |d ur;|n|j
| |d urE|n|j| d|
jd}||
j}|dkrht|	tjrhtj|dd|d}t|	|| q|S )NTr{   r   r   r  r^  )r   r;  r  rZ   r   r   r  r   r   r>   rS   rB   r   r]   r8   r'   rb  r   rc  rd  )r   re  r\  rC  rB   rS   r>   r   r   rZ  rf   r  
arr_warpedr&   r&   r,   r9  ;  s.   

z(PiecewiseAffine._augment_maps_by_samplesc              
   C   sN  g }t |D ]\}}|jdd \}}| |j|j|j| |j| |j| }|d u s2t|jdkr8|| q	 |j	dd}	t
j|	|dd|jd |jd t|jfd}
tjj|
ddddd	t|jd
k rhd n|jd d}t|j|jD ](\}}d|j  ko|k n  od|j  ko|k n  }|r|j|_|j|_qv|| q|S )Nr   r   T)invertedr	   )r>   r|   rV   g{Gz?r   r  r   )r  	thresholdZif_not_found_coordsr   )r   rZ   r  r   r   r  rY   r1  rP  Zto_distance_mapsr   r   r'   ZKeypointsOnImageZfrom_distance_mapsra  r   r   )r   kpsoisrC  r   r   rG  hwr  Z	dist_mapsZdist_maps_warpedZkps_augr  Zkp_augZwithin_imager&   r&   r,   r  d  sF   		4z-PiecewiseAffine._augment_keypoints_by_samplesc                 C   s  | d}| jj|f|d d}| jj|f|d d}| jj|f|d d}| jj|f|d d}| jj|f|d d}t|dd }t|dd }|| }	| j	jt
t|	df|d	 d}
g }d
}|	D ]}|
||| d d f }|| ||7 }qet||||||dS )Nr   irm  r   r   r   )r   r   r  r>   rB   rS   )rp  r   rq  r   r>   rB   rS   r   rc  r  rX   sumrP  r  )r   rS  r   rssZnb_rows_samplesZnb_cols_samplesrz  rx  ry  Znb_cellsr  Zjitter_by_imagecounterZ
nb_cells_i
jitter_imgr&   r&   r,   r5    sB   







zPiecewiseAffine._draw_samplesc                 C   s  t d|d |}t d|d |}t ||\}}	t |	j|jgd }
t |dk}|s0d S t |}| jrz|d dkrQ|d d df |d  |d d df< nd|d d df< |d dkrr|d d df |d  |d d df< nd|d d df< |d d df |d  |d d df< |d d df |d  |d d df< t |
}|d d df |d d df  |d d df< |d d df |d d df  |d d df< t |d d df d|d d |d d df< t |d d df d|d d |d d df< tdd |dd D }|d uot	|dko|d dkp.|d uo.t	|dko.|d dk}|s5|r7d S t
 }||
d d d d df |d d d d df  |S )	Nr   r	   r   c                 S   s   g | ]}|d kqS )r	   r&   )r*   r   r&   r&   r,   r-     r2   z4PiecewiseAffine._get_transformer.<locals>.<listcomp>r   r   r   )r   ZlinspacemeshgridZdstackZflatanyr   r  rc  rY   r   ZPiecewiseAffineTransformestimate)r   Zaugmentable_shaper   r   r   r  r   r   Zxx_srcZyy_srcZ
points_srcZany_nonzeroZpoints_destZhas_low_axisZhas_zero_channelsrg   r&   r&   r,   r    sT   
&&$$
,,





0z PiecewiseAffine._get_transformerc                 C   r  r{  )r   r   r   r>   rB   rS   r  r   r&   r&   r,   r|    s   zPiecewiseAffine.get_parameters)r  r  r  r	   r   r   FNNNr  r  )r   r   r  r~  r   rI  r7  r9  r  r5  r  r|  r  r&   r&   r  r,   r  /  s      *&/)H Pr  c                   @      e Zd Zdd ZdS )#_PerspectiveTransformSamplingResultc                 C   s"   || _ || _|| _|| _|| _d S r   )rU  max_heights
max_widthscvalsmodes)r   rU  r  r   r  r  r&   r&   r,   r   $  s
   
z,_PerspectiveTransformSamplingResult.__init__Nr   r   r  r   r&   r&   r&   r,   r  #      r  c                       s   e Zd ZdZejejdZ					
d fdd	Ze	dd Z
dd Zdd Zdd Zdd Zdd Ze	dd Ze	dd Zdd Z  ZS ) PerspectiveTransformak  
    Apply random four point perspective transformations to images.

    Each of the four points is placed on the image using a random distance from
    its respective corner. The distance is sampled from a normal distribution.
    As a result, most transformations don't change the image very much, while
    some "focus" on polygons far inside the image.

    The results of this augmenter have some similarity with ``Crop``.

    Code partially from
    http://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/

    **Supported dtypes**:

    if (keep_size=False):

        * ``uint8``: yes; fully tested
        * ``uint16``: yes; tested
        * ``uint32``: no (1)
        * ``uint64``: no (2)
        * ``int8``: yes; tested (3)
        * ``int16``: yes; tested
        * ``int32``: no (2)
        * ``int64``: no (2)
        * ``float16``: yes; tested (4)
        * ``float32``: yes; tested
        * ``float64``: yes; tested
        * ``float128``: no (1)
        * ``bool``: yes; tested (4)

        - (1) rejected by opencv
        - (2) leads to opencv error: cv2.error: ``OpenCV(3.4.4)
              (...)imgwarp.cpp:1805: error: (-215:Assertion failed)
              ifunc != 0 in function 'remap'``.
        - (3) mapped internally to ``int16``.
        - (4) mapped intenally to ``float32``.

    if (keep_size=True):

        minimum of (
            ``imgaug.augmenters.geometric.PerspectiveTransform(keep_size=False)``,
            :func:`~imgaug.imgaug.imresize_many_images`
        )

    Parameters
    ----------
    scale : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Standard deviation of the normal distributions. These are used to
        sample the random distances of the subimage's corners from the full
        image's corners. The sampled values reflect percentage values (with
        respect to image height/width). Recommended values are in the range
        ``0.0`` to ``0.1``.

            * If a single number, then that value will always be used as the
              scale.
            * If a tuple ``(a, b)`` of numbers, then a random value will be
              uniformly sampled per image from the interval ``(a, b)``.
            * If a list of values, a random value will be picked from the
              list per image.
            * If a ``StochasticParameter``, then that parameter will be
              queried to draw one value per image.

    keep_size : bool, optional
        Whether to resize image's back to their original size after applying
        the perspective transform. If set to ``False``, the resulting images
        may end up having different shapes and will always be a list, never
        an array.

    cval : number or tuple of number or list of number or imaug.ALL or imgaug.parameters.StochasticParameter, optional
        The constant value used to fill up pixels in the result image that
        didn't exist in the input image (e.g. when translating to the left,
        some new pixels are created at the right). Such a fill-up with a
        constant value only happens, when `mode` is ``constant``.
        The expected value range is ``[0, 255]`` for ``uint8`` images.
        It may be a float value.

            * If this is a single int or float, then that value will be used
              (e.g. 0 results in black pixels).
            * If a tuple ``(a, b)``, then a random value is uniformly sampled
              per image from the interval ``[a, b]``.
            * If a list, then a random value will be sampled from that list
              per image.
            * If ``imgaug.ALL``, then equivalent to tuple ``(0, 255)``.
            * If a ``StochasticParameter``, a new value will be sampled from
              the parameter per image.

    mode : int or str or list of str or list of int or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        Parameter that defines the handling of newly created pixels.
        Same meaning as in OpenCV's border mode. Let ``abcdefgh`` be an image's
        content and ``|`` be an image boundary, then:

            * ``cv2.BORDER_REPLICATE``: ``aaaaaa|abcdefgh|hhhhhhh``
            * ``cv2.BORDER_CONSTANT``: ``iiiiii|abcdefgh|iiiiiii``, where
              ``i`` is the defined cval.
            * ``replicate``: Same as ``cv2.BORDER_REPLICATE``.
            * ``constant``: Same as ``cv2.BORDER_CONSTANT``.

        The datatype of the parameter may be:

            * If a single ``int``, then it must be one of ``cv2.BORDER_*``.
            * If a single string, then it must be one of: ``replicate``,
              ``reflect``, ``reflect_101``, ``wrap``, ``constant``.
            * If a list of ints/strings, then per image a random mode will be
              picked from that list.
            * If ``imgaug.ALL``, then a random mode from all possible modes
              will be picked per image.
            * If ``StochasticParameter``, then the mode will be sampled from
              that parameter per image, i.e. it must return only the above
              mentioned strings.

    fit_output : bool, optional
        If ``True``, the image plane size and position will be adjusted
        to still capture the whole image after perspective transformation.
        (Followed by image resizing if `keep_size` is set to ``True``.)
        Otherwise, parts of the transformed image may be outside of the image
        plane.
        This setting should not be set to ``True`` when using large `scale`
        values as it could lead to very large images.

        Added in 0.4.0.

    polygon_recoverer : 'auto' or None or imgaug.augmentables.polygons._ConcavePolygonRecoverer, optional
        The class to use to repair invalid polygons.
        If ``"auto"``, a new instance of
        :class`imgaug.augmentables.polygons._ConcavePolygonRecoverer`
        will be created.
        If ``None``, no polygon recoverer will be used.
        If an object, then that object will be used and must provide a
        ``recover_from()`` method, similar to
        :class:`~imgaug.augmentables.polygons._ConcavePolygonRecoverer`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.PerspectiveTransform(scale=(0.01, 0.15))

    Apply perspective transformations using a random scale between ``0.01``
    and ``0.15`` per image, where the scale is roughly a measure of how far
    the perspective transformation's corner points may be distanced from the
    image's corner points. Higher scale values lead to stronger "zoom-in"
    effects (and thereby stronger distortions).

    >>> aug = iaa.PerspectiveTransform(scale=(0.01, 0.15), keep_size=False)

    Same as in the previous example, but images are not resized back to
    the input image size after augmentation. This will lead to smaller
    output images.

    )r  r   r   gQ?r   r   TFr!   Nr  c                    s   t t| j|||	|
d tj|ddddd| _tjd| jd| _d| _d| _	t
|| _| || _|| _|| _|| _|d	krBt | _tj| _tj| _tj| _tj| _d| _d| _d S )
Nr  r   r  TrC   r   r  r   r!   )r  r  r   r5   rG   r   r  r  	min_width
min_heightrH   rB   rT   rS   	keep_sizer   r  r   r"   r  r  r  r  r  r  r  r  r  )r   r   rB   rS   r	  r   r  r   r^   r   r	  r  r&   r,   r     s0   


zPerspectiveTransform.__init__c                    s*  t jt jg ddg|tjkrt S t|r,| v s'J dt |f t	|S t
|rD|v s?J dt|f t	|S t|trtdd |D }|scJ ddd	d |D f t fd
d|D }|sJ dt  t|f t|S t|tjr|S tdt|f )Nr  r   r  r  c                 S   r  r&   r  r)   r&   r&   r,   r-     r  z9PerspectiveTransform._handle_mode_arg.<locals>.<listcomp>r  rK   c                 S   rL   r&   rM   r)   r&   r&   r,   r-     rP   c                    r  r&   r&   r)   r  r&   r,   r-     r  r  r  )r"   r  r  r'   r4   r5   r6   r(   r;   r7   rJ   r8   r9   r:   rR   r<   r=   r1   )r#  rS   r  Zvalid_modesr&   r  r,   rT     sZ   









z%PerspectiveTransform._handle_mode_argc              	   C   s:  |   | | | }|jd ur| |j||_|jd ur>| dd |jD | }| |jd||| j| j	| j
|_|jd ura| dd |jD | }| |jd||| j| j| j|_|jd urytj| j|d}| j|j|| jd|_dD ]}t||}	|	d urtj| j|d}| |	|}
t|||
 q{|S )	Nc                 S      g | ]}|j jqS r&   )r/  rZ   rY  r&   r&   r,   r-   ;      z8PerspectiveTransform._augment_batch_.<locals>.<listcomp>r/  c                 S   r
  r&   )rf   rZ   rY  r&   r&   r,   r-   E  r  rf   )samples_imagesr  r  )Zadvance_r5  get_rowwise_shapesr   r6  r7  r8  r9  r  r  r  r:  r  r  r  r3  r  r  r  r  r  r;  r  rd  )r   r@  r   rA  rB  r  rC  r  rD  rE  r  r&   r&   r,   rI  +  sd   








z$PerspectiveTransform._augment_batch_c                    s  t j|g dg d| d |}| jst|}tt||j|j|j|j	|j
}|D ]\}\ j}|jdv rAtjn|jdv rLtjjd }jdk}|r[}	n=|dkrtjtf d	}	|	jdkr|| jd
krt|	d}	n fddt|D }	tj|	dd}	| jr|sjdd \}
}t|	|
|f}	|jdkr|	dk}	n|	jj|jkrt |	|}	|	||< q(|S )N)r   r   r   r   r   r   r   r   )rn   rp   rq   rr   r   rs   rt   ru   rv   rw   rx   ry   )r   )r   r   r   r   r   )r   r   r   c                    sF   g | ]}t jtd |f f t|t d  t jdqS ).r	   r   r   r   )r"   warpPerspectiver   rb   rY   r  r   rB   rL  rg   
max_height	max_widthrS   r&   r,   r-     s    	zCPerspectiveTransform._augment_images_by_samples.<locals>.<listcomp>r   r   r   r}   )r[   r~   r	  r9   r   ra  rU  r  r   r  r  r]   r^   r   r   r   r   rZ   r   r"   r  r   r   Zexpand_dimsr   r   r   r'   imresize_single_imager   )r   r6  rC  r   rh  r   r   r   Zhas_zero_sized_axiswarpedr  r  r&   r  r,   r7  d  s\   



	



z/PerspectiveTransform._augment_images_by_samplesc                    sD  |}| j r|j}	|j}
n|j}	|j}
tt||j|j|j}|D ]\}\}t|| ||d u r8|j| ||d u rC|j|  j	d }d|j	v } j
dk}|s|su fddt|D }tj|dd}t||| | j r j	dd \}}|||f}n|	| |
| f|j	dd   }||_	|||< q |S )Nr   r   c              
      s2   g | ]}t jt d |f fdqS ).r  )r"   r  r   r   rf   Zcval_ir   rg   r  r  Zmode_ir&   r,   r-     s    	zAPerspectiveTransform._augment_maps_by_samples.<locals>.<listcomp>r   r   )r	  r  r   r   ra  rU  r;  r  r  rZ   r   r   r   r   r   rd  resize)r   re  r\  rC  r  rB   rS   r   r   Zmax_heights_imgsZmax_widths_imgsrh  r   ri  r   image_has_zero_sized_axisZmap_has_zero_sized_axisr  r  r  Z	new_shaper&   r  r,   r9    sL   





	z-PerspectiveTransform._augment_maps_by_samplesc                 C   s   |}t t||j|j|j}|D ]Y\}\}}}}	d|jv }
|
si|j}||	f|jdd   }||_|js]| }t	t
j|gt
jd|}|d }t|j|D ]\}}|d |_|d |_qN| jre||}|||< q|S )Nr   r   rV  r	   )r   ra  rU  r  r   rZ   r<  r=  r"   perspectiveTransformr   r   r   r1  r   r   r	  on_)r   r  r  r   rh  r   rG  rg   r  r  r  
shape_origZ	shape_newZkps_arrr  r  r   r&   r&   r,   r    s4   


z2PerspectiveTransform._augment_keypoints_by_samplesc           "      C   s:  g }g }g }t |}|d}| jj|df|d d}| jj|f|d d}	| jj|ddf|d d}
| }|	jjdvrM| j	
 D ]
\}}||	|	|k< qBtt|
d}d|d d ddf  |d d ddf< d|d d ddf  |d d ddf< d|d d ddf  |d d ddf< d|d d ddf  |d d ddf< t||D ]j\}}|dd \}}|d d df  |9  < |d d df  |9  < | |}|\}}}}d }d }|d u s|| jk rSt|d |d  d |d |d  d  }t|d |d  d |d |d  d  }tt||}tt||}|| jk rJ| j| d }|d  |8  < |d  |7  < |d  |8  < |d  |7  < |d u s|| jk sd }d }|d u sb|| jk rt|d |d  d |d |d  d  }t|d |d  d |d |d  d  }tt||}tt||}|| jk r| j| d }|d  |8  < |d  |8  < |d  |7  < |d  |7  < |d u sb|| jk sbtjddg|dg||gd|ggtjd	} t|| }!| jr| |!||f\}!}}||! || || q|	tj}	t|||||	S )
Nr   r   rm  r	   r   r   )r   ur  rV  ) rY   rp  rB   rq  rS   r  r   r]   r  _BORDER_MODE_STR_TO_INTitemsr   modr  ra  _order_pointsr  sqrtrX   ra   rb   r  r   r   r"   getPerspectiveTransformr   _expand_transformrP  r   r   r  )"r   shapesr   rU  r  r   rS  rs  rx  ry  r  Zcval_samples_cv2rS   Zmapped_modeZpointsrZ   Zpoints_ir  r  tltrbrblr  r  Z	width_topZwidth_bottomZ	step_sizer  r  Zheight_rightZheight_leftdstmr&   r&   r,   r5  
  s   

$$$$
....

z"PerspectiveTransform._draw_samplesc                 C   sv   t jdt jd}|jdd}|t | |d< |t | |d< t j|dd}|t | |d< |t | |d< |S )N)r   r   rV  r	   r   r   r   r   )r   r  r   r  ZargminZargmaxdiff)r#  ZptsZpts_orderedZpointwise_sumr*  r&   r&   r,   r  ~  s   z"PerspectiveTransform._order_pointsc           
      C   s   |\}}t jddg|dg||gd|ggt jd}tt |g|d }||jddd8 }t j|dd}t||}|jdd\}}	|t	|t	|	fS )Nr   rV  T)r   Zkeepdims)Zdecimalsr   )
r   r   r   r"   r  rb   Zaroundr!  ra   rX   )
r#  rg   rZ   r   r   rectr(  Zmatrix_expandedr  r  r&   r&   r,   r"    s   z&PerspectiveTransform._expand_transformc                 C      | j | j| j| j| jgS r{  )r  r	  rB   rS   r   r   r&   r&   r,   r|    s   z#PerspectiveTransform.get_parameters)
r  r   r   TFr!   NNr  r  )r   r   r  r~  r"   r  r  r  r   r  rT   rI  r7  r9  r  r5  r  r"  r|  r  r&   r&   r  r,   r  -  s.     *+
$9H@t

r  c                   @   r  )$_ElasticTransformationSamplingResultc                 C   s(   || _ || _|| _|| _|| _|| _d S r   )random_statesalphassigmasordersr  r  )r   r.  r/  r0  r1  r  r  r&   r&   r,   r     r  z-_ElasticTransformationSamplingResult.__init__Nr  r&   r&   r&   r,   r-    r  r-  c                       s   e Zd ZdZdZdZdZdZej	ej
ejejdZejejejejejejdZ			
			d+ fdd	Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zed&d' Zed,d)d*Z   Z!S )-ElasticTransformationad"  
    Transform images by moving pixels locally around using displacement fields.

    The augmenter has the parameters ``alpha`` and ``sigma``. ``alpha``
    controls the strength of the displacement: higher values mean that pixels
    are moved further. ``sigma`` controls the smoothness of the displacement:
    higher values lead to smoother patterns -- as if the image was below water
    -- while low values will cause indivdual pixels to be moved very
    differently from their neighbours, leading to noisy and pixelated images.

    A relation of 10:1 seems to be good for ``alpha`` and ``sigma``, e.g.
    ``alpha=10`` and ``sigma=1`` or ``alpha=50``, ``sigma=5``. For ``128x128``
    a setting of ``alpha=(0, 70.0)``, ``sigma=(4.0, 6.0)`` may be a good
    choice and will lead to a water-like effect.

    Code here was initially inspired by
    https://gist.github.com/chsasank/4d8f68caf01f041a6453e67fb30f8f5a

    For a detailed explanation, see ::

        Simard, Steinkraus and Platt
        Best Practices for Convolutional Neural Networks applied to Visual
        Document Analysis
        in Proc. of the International Conference on Document Analysis and
        Recognition, 2003

    .. note::

        For coordinate-based inputs (keypoints, bounding boxes, polygons,
        ...), this augmenter still has to perform an image-based augmentation,
        which will make it significantly slower for such inputs than other
        augmenters. See :ref:`performance`.

    **Supported dtypes**:

        * ``uint8``: yes; fully tested (1)
        * ``uint16``: yes; tested (1)
        * ``uint32``: yes; tested (2)
        * ``uint64``: limited; tested (3)
        * ``int8``: yes; tested (1) (4) (5)
        * ``int16``: yes; tested (4) (6)
        * ``int32``: yes; tested (4) (6)
        * ``int64``: limited; tested (3)
        * ``float16``: yes; tested (1)
        * ``float32``: yes; tested (1)
        * ``float64``: yes; tested (1)
        * ``float128``: no
        * ``bool``: yes; tested (1) (7)

        - (1) Always handled by ``cv2``.
        - (2) Always handled by ``scipy``.
        - (3) Only supported for ``order != 0``. Will fail for ``order=0``.
        - (4) Mapped internally to ``float64`` when ``order=1``.
        - (5) Mapped internally to ``int16`` when ``order>=2``.
        - (6) Handled by ``cv2`` when ``order=0`` or ``order=1``, otherwise by
              ``scipy``.
        - (7) Mapped internally to ``float32``.

    Parameters
    ----------
    alpha : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Strength of the distortion field. Higher values mean that pixels are
        moved further with respect to the distortion field's direction. Set
        this to around 10 times the value of `sigma` for visible effects.

            * If number, then that value will be used for all images.
            * If tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the interval ``[a, b]``.
            * If a list, then for each image a random value will be sampled
              from that list.
            * If ``StochasticParameter``, then that parameter will be used to
              sample a value per image.

    sigma : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
        Standard deviation of the gaussian kernel used to smooth the distortion
        fields. Higher values (for ``128x128`` images around 5.0) lead to more
        water-like effects, while lower values (for ``128x128`` images
        around ``1.0`` and lower) lead to more noisy, pixelated images. Set
        this to around 1/10th of `alpha` for visible effects.

            * If number, then that value will be used for all images.
            * If tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the interval ``[a, b]``.
            * If a list, then for each image a random value will be sampled
              from that list.
            * If ``StochasticParameter``, then that parameter will be used to
              sample a value per image.

    order : int or list of int or imaug.ALL or imgaug.parameters.StochasticParameter, optional
        Interpolation order to use. Same meaning as in
        :func:`scipy.ndimage.map_coordinates` and may take any integer value
        in the range ``0`` to ``5``, where orders close to ``0`` are faster.

            * If a single int, then that order will be used for all images.
            * If a tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the interval ``[a, b]``.
            * If a list, then for each image a random value will be sampled
              from that list.
            * If ``imgaug.ALL``, then equivalant to list
              ``[0, 1, 2, 3, 4, 5]``.
            * If ``StochasticParameter``, then that parameter is queried per
              image to sample the order value to use.

    cval : number or tuple of number or list of number or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        The constant intensity value used to fill in new pixels.
        This value is only used if `mode` is set to ``constant``.
        For standard ``uint8`` images (value range ``0`` to ``255``), this
        value may also should also be in the range ``0`` to ``255``. It may
        be a ``float`` value, even for images with integer dtypes.

            * If this is a single number, then that value will be used
              (e.g. ``0`` results in black pixels).
            * If a tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the interval ``[a, b]``.
            * If a list, then a random value will be picked from that list per
              image.
            * If ``imgaug.ALL``, a value from the discrete range ``[0..255]``
              will be sampled per image.
            * If a ``StochasticParameter``, a new value will be sampled from
              the parameter per image.

    mode : str or list of str or imgaug.ALL or imgaug.parameters.StochasticParameter, optional
        Parameter that defines the handling of newly created pixels.
        May take the same values as in :func:`scipy.ndimage.map_coordinates`,
        i.e. ``constant``, ``nearest``, ``reflect`` or ``wrap``.

            * If a single string, then that mode will be used for all images.
            * If a list of strings, then per image a random mode will be picked
              from that list.
            * If ``imgaug.ALL``, then a random mode from all possible modes
              will be picked.
            * If ``StochasticParameter``, then the mode will be sampled from
              that parameter per image, i.e. it must return only the above
              mentioned strings.

    polygon_recoverer : 'auto' or None or imgaug.augmentables.polygons._ConcavePolygonRecoverer, optional
        The class to use to repair invalid polygons.
        If ``"auto"``, a new instance of
        :class`imgaug.augmentables.polygons._ConcavePolygonRecoverer`
        will be created.
        If ``None``, no polygon recoverer will be used.
        If an object, then that object will be used and must provide a
        ``recover_from()`` method, similar to
        :class:`~imgaug.augmentables.polygons._ConcavePolygonRecoverer`.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.ElasticTransformation(alpha=50.0, sigma=5.0)

    Apply elastic transformations with a strength/alpha of ``50.0`` and
    smoothness of ``5.0`` to all images.

    >>> aug = iaa.ElasticTransformation(alpha=(0.0, 70.0), sigma=5.0)

    Apply elastic transformations with a strength/alpha that comes
    from the interval ``[0.0, 70.0]`` (randomly picked per image) and
    with a smoothness of ``5.0``.

    r   r  g?r   r  r   r    r   r	   r   r   r   r$   r   g      D@g      @g       @r   r   r!   Nr  c                    s   t t| j|||	|
d tj|ddddd| _tj|ddddd| _| || _t	|| _
| || _|| _|dkr>t | _d| _d	| _d
| _d
| _d| _d	| _d S )Nr  alphar  TrC   sigmar!   r   r   r   r   )r  r2  r   r5   rG   r7  r8  r@   r>   rH   rB   rT   rS   r  r   r  r  r  r  r  r  )r   r7  r8  r>   rB   rS   r  r   r^   r   r	  r  r&   r,   r     s0   


zElasticTransformation.__init__c                 C   s.   |t jkrtg dS tj|ddddddS )Nr4  r>   r3   TFr*  )r'   r4   r5   r6   r,  )r#  r>   r&   r&   r,   r@     s   
z'ElasticTransformation._handle_order_argc                 C   s   |t jkrtg dS t |rt|S t |r8tdd |D s3J dddd |D f t|S t	|tj
r@|S tdt|f )Nr3  c                 S   r%   r&   rI   r)   r&   r&   r,   r-     r.   z:ElasticTransformation._handle_mode_arg.<locals>.<listcomp>z9Expected mode list to only contain strings, got types %s.rK   c                 S   rL   r&   rM   r)   r&   r&   r,   r-     rP   rQ   )r'   r4   r5   r6   rJ   r7   is_iterabler:   rR   r8   r<   r=   r1   )r#  rS   r&   r&   r,   rT     s&   




z&ElasticTransformation._handle_mode_argc           	      C   s   | |d }| jj|f|d d}| jj|f|d d}| jj|f|d d}| jj|f|d d}| jj|f|d d}t|dd |||||S )	Nr$   r  rm  r  r  r  r   r   )rp  r7  rq  r8  r>   rB   rS   r-  )	r   rS  r   r  r/  r0  r1  r  r  r&   r&   r,   r5    s   z#ElasticTransformation._draw_samplesc                 C   s  |j d urtj|j g dg d| d | }| t||}t|D ]\}}| j|dd |j| |j	| |j
| d\}	}
|j d urS| |j | |||	|
|j |< |jd urn| |j| |||	|
d| j| j| j	|j|< |jd ur| |j| |||	|
d| j| j| j	|j|< |jd ur| |j| |||	|
|j|< |jd ur| |j| |||	|
|j|< |jd ur| |j| |||	|
|j|< |jd ur| |j| |||	|
|j|< q#|S )	N)r   r   r   rn   rp   r   r   r   rs   r   r   r   )rq   rr   rt   ru   rv   rw   rx   ry   r   r   )r7  r8  r   r/  rf   )r6  r[   r~   r  r5  rY   r   _generate_shift_mapsr/  r0  r.  _augment_image_by_samplesr8  _augment_hm_or_sm_by_samplesr  r  r  r:  r  r  r  r1  _augment_kpsoi_by_samplesr2  _augment_bbsoi_by_samplesr3  _augment_psoi_by_samplesr4  _augment_lsoi_by_samples)r   r@  r   rA  rB  r#  rC  r   rZ   dxdyr&   r&   r,   rI    sd   
















z%ElasticTransformation._augment_batch_c                 C   s   t |j\}}}tt|j| ||}	|j}
|jjdkr#|tj	}| j
||||j| |	|j| d}|jj|
jkrAt ||
}|S )Nr   r>   rB   rS   )r[   r\   r]   ra   rb   r  r^   r   r   r   _map_coordinatesr1  r  r   )r   rL  row_idxrC  rA  rB  rh   ri   rj   rB   r   Z	image_augr&   r&   r,   r;  	  s   
z/ElasticTransformation._augment_image_by_samplesc
                 C   s>  |d ur|n|j | }|d ur|n|j| }|	d ur|	n|j| }	t||}
|
jdd |jdd krZ| j|
|||	||d}|	dkrRt|tjrRt	j
|dd|d}t||| |S |
jdd \}}||jdd }t||}
| j|
|||	||d}|	dkrt|tjrt	j
|dd|d}t||| |||f}|S )Nr   r   rC  r   r   r  r^  )r  r  r1  r;  rZ   rD  r8   r'   rb  r   rc  rd  r  )r   rZ  rE  rC  rA  rB  r\  rB   rS   r>   rf   r  Zheight_origZ
width_origr&   r&   r,   r<    s.   

z2ElasticTransformation._augment_hm_or_sm_by_samplesc              	   C   s  |j dd \}}|j| }|j| }	d|j v }
|| jkp!|	| jk}|js)|
s)|r+|S |jD ]}d|j  ko:|k n  oId|j  koG|k n  }|r|j	| j
| jdd}t|d d df tj}t|d d df tj}ttd|k||k td|k||k }|| }|| }tj|d d tjf |d d tjf gdd}t|tj}|d d df  |||f 7  < |d d df  |||f 7  < t|}|d |_|d |_q.|S )Nr   r   T)Zreturn_arrayr	   r   )rZ   r/  r0  KEYPOINT_AUG_ALPHA_THRESHKEYPOINT_AUG_SIGMA_THRESHr<  r1  r   r   Z!generate_similar_points_manhattanNB_NEIGHBOURING_KEYPOINTSNEIGHBOURING_KEYPOINTS_DISTANCEr   r   r   r   logical_andconcatenater   r   r   r'   Zcompute_geometric_median)r   rG  rE  rC  rA  rB  r   r   r7  r8  Zimage_has_zero_sized_axesZparams_below_threshr  Zwithin_image_planeZkp_neighborhoodxxyyZinside_image_maskZxxyyZxxyy_augZmedr&   r&   r,   r=  T  sJ   




4"  


z/ElasticTransformation._augment_kpsoi_by_samplesc                 C   s(   t j| j||||d}| j||| jdS )NrE  rC  rA  rB  r  )r  r  r=  r  r  )r   psoirE  rC  rA  rB  r  r&   r&   r,   r?    s   z.ElasticTransformation._augment_psoi_by_samplesc                 C   "   t j| j||||d}| ||S NrN  r  r  r=  r  )r   ZlsoirE  rC  rA  rB  r  r&   r&   r,   r@       z.ElasticTransformation._augment_lsoi_by_samplesc                 C   rP  rQ  rR  )r   bbsoirE  rC  rA  rB  r  r&   r&   r,   r>    rS  z/ElasticTransformation._augment_bbsoi_by_samplesc                 C   r,  r{  )r7  r8  r>   rB   rS   r   r&   r&   r,   r|    s   z$ElasticTransformation.get_parametersc                 C   s
  t |dksJ d|f t|}|d dkr|d n|}|}|dd \}}|d|  }	|d|  }
|d|	 |
fd d }|d|	d d f }||	d d d f }t||| }t||| }|dkr||| || f }||| || f }||fS )Nr   zExpected 2d shape, got %s.r   r	   )rY   blur_libZ_compute_gaussian_blur_ksizer   Zblur_gaussian_)r#  rZ   r7  r8  r   Zksizepaddingr  r  Zh_padZw_padZdxdy_unsmoothedZdx_unsmoothedZdy_unsmoothedrA  rB  r&   r&   r,   r:    s    
z*ElasticTransformation._generate_shift_mapsr	   c           "   	   C   s  |j dkr
t|S |dkr|jjdv rtd||jjf |j}|jjdkr.|tj}n2|dkr?|jjdv r?|tj}n!|dkrP|jjdkrP|tj	}n|dkr`|jjd	kr`|tj}d
}d}	|dkro|jjdv }
n|dkrz|jjdv }
n|jjdv }
|j
d |kp|j
d |k}|j
d |kp|j
d |k}|
s|s|rd}	|jdksJ d|jf t|}|j
dd \}}|	dkr|j
dd \}}tjt|tjt|tjdd\}}|d|  }|d|  }t|j
d D ]$}tjj|d|f | | f|||d}|||f}||d|f< qn|j
\}}}tjt|tjt|tjdd\}}|d|  }|d|  }|jjdkrUt|}nt|}| j| }| j| }|tjk}tj||tj|d\}}|dkrtjt||||||||fd}|jdkr|jdkr|dtj f }nEd}g }||k r|d||d f } tjt| |||||||fd}!|!jdkr|!dtj f }!|!|! |d7 }||k stj"|dd}|jj|jkrt#$||}|S )a@  Remap pixels in an image according to x/y shift maps.

        **Supported dtypes**:

        if (backend="scipy" and order=0):

            * ``uint8``: yes
            * ``uint16``: yes
            * ``uint32``: yes
            * ``uint64``: no (1)
            * ``int8``: yes
            * ``int16``: yes
            * ``int32``: yes
            * ``int64``: no (2)
            * ``float16``: yes
            * ``float32``: yes
            * ``float64``: yes
            * ``float128``: no (3)
            * ``bool``: yes

            - (1) produces array filled with only 0
            - (2) produces array filled with <min_value> when testing
                  with <max_value>
            - (3) causes: 'data type no supported'

        if (backend="scipy" and order>0):

            * ``uint8``: yes (1)
            * ``uint16``: yes (1)
            * ``uint32``: yes (1)
            * ``uint64``: yes (1)
            * ``int8``: yes (1)
            * ``int16``: yes (1)
            * ``int32``: yes (1)
            * ``int64``: yes (1)
            * ``float16``: yes (1)
            * ``float32``: yes (1)
            * ``float64``: yes (1)
            * ``float128``: no (2)
            * ``bool``: yes

            - (1) rather loose test, to avoid having to re-compute the
                  interpolation
            - (2) causes: 'data type no supported'

        if (backend="cv2" and order=0):

            * ``uint8``: yes
            * ``uint16``: yes
            * ``uint32``: no (1)
            * ``uint64``: no (2)
            * ``int8``: yes
            * ``int16``: yes
            * ``int32``: yes
            * ``int64``: no (2)
            * ``float16``: yes
            * ``float32``: yes
            * ``float64``: yes
            * ``float128``: no (3)
            * ``bool``: no (4)

            - (1) causes: src data type = 6 is not supported
            - (2) silently converts to int32
            - (3) causes: src data type = 13 is not supported
            - (4) causes: src data type = 0 is not supported

        if (backend="cv2" and order=1):

            * ``uint8``: yes
            * ``uint16``: yes
            * ``uint32``: no (1)
            * ``uint64``: no (2)
            * ``int8``: no (2)
            * ``int16``: no (2)
            * ``int32``: no (2)
            * ``int64``: no (2)
            * ``float16``: yes
            * ``float32``: yes
            * ``float64``: yes
            * ``float128``: no (3)
            * ``bool``: no (4)

            - (1) causes: src data type = 6 is not supported
            - (2) causes: OpenCV(3.4.5) (...)/imgwarp.cpp:1805:
                  error: (-215:Assertion failed) ifunc != 0 in function
                  'remap'
            - (3) causes: src data type = 13 is not supported
            - (4) causes: src data type = 0 is not supported

        if (backend="cv2" and order>=2):

            * ``uint8``: yes
            * ``uint16``: yes
            * ``uint32``: no (1)
            * ``uint64``: no (2)
            * ``int8``: no (2)
            * ``int16``: yes
            * ``int32``: no (2)
            * ``int64``: no (2)
            * ``float16``: yes
            * ``float32``: yes
            * ``float64``: yes
            * ``float128``: no (3)
            * ``bool``: no (4)

            - (1) causes: src data type = 6 is not supported
            - (2) causes: OpenCV(3.4.5) (...)/imgwarp.cpp:1805:
                  error: (-215:Assertion failed) ifunc != 0 in function
                  'remap'
            - (3) causes: src data type = 13 is not supported
            - (4) causes: src data type = 0 is not supported

        r   )rp   rs   zldtypes uint64 and int64 are only supported in ElasticTransformation for order=0, got order=%d with dtype=%s.r   r	   )r   r   r   r   r   r     r"   )rn   rp   rs   rw   r   )rn   rp   r   r   r   rs   rw   r   )rn   rp   r   r   rs   rw   r   scipyr   z0Expected 3-dimensional image, got %d dimensions.Zij)Zindexingr   .rC  f)Znninterpolationr   )interpolationr   r   r   )%r   r   r   r]   r^   r=   r   r   r   r   rZ   r   r  r   r   r   r   rZ  Zmap_coordinatesr   r   r  floatrX   _MAPPING_MODE_SCIPY_CV2_MAPPING_ORDER_SCIPY_CV2r"   r  ZconvertMapsZCV_16SC2Zremapr   r   rP  rK  r[   r   )"r#  rL  rA  rB  r>   rB   rS   r   Zshrt_maxr?   Zbad_dtype_cv2Zbad_dx_shape_cv2Zbad_dy_shape_cv2r   r   r   r  r  r   r   Z	x_shiftedZ	y_shiftedr   Zremapped_flatZremappedr   Zborder_moderZ  Zis_nearest_neighbourZmap1Zmap2Zcurrent_chan_idxZchannelsZresult_cr&   r&   r,   rD    s   
t




	

















z&ElasticTransformation._map_coordinates)
r5  r6  r   r   r   r!   NNr  r  )r	   r   r   )"r   r   r  r~  rH  rI  rF  rG  r"   r  r  r  r  r\  r  r  r  r]  r   r  r@   rT   r5  rI  r;  r<  r=  r?  r@  r>  r|  r:  rD  r  r&   r&   r  r,   r2    sT     3	%

56?
r2  c                       s\   e Zd ZdZ			d fdd	Zdd	 Zd
d Zedd Zdd Z	dd Z
dd Z  ZS )Rot90a  
    Rotate images clockwise by multiples of 90 degrees.

    This could also be achieved using ``Affine``, but ``Rot90`` is
    significantly more efficient.

    **Supported dtypes**:

    if (keep_size=False):

        * ``uint8``: yes; fully tested
        * ``uint16``: yes; tested
        * ``uint32``: yes; tested
        * ``uint64``: yes; tested
        * ``int8``: yes; tested
        * ``int16``: yes; tested
        * ``int32``: yes; tested
        * ``int64``: yes; tested
        * ``float16``: yes; tested
        * ``float32``: yes; tested
        * ``float64``: yes; tested
        * ``float128``: yes; tested
        * ``bool``: yes; tested

    if (keep_size=True):

        minimum of (
            ``imgaug.augmenters.geometric.Rot90(keep_size=False)``,
            :func:`~imgaug.imgaug.imresize_many_images`
        )

    Parameters
    ----------
    k : int or list of int or tuple of int or imaug.ALL or imgaug.parameters.StochasticParameter, optional
        How often to rotate clockwise by 90 degrees.

            * If a single ``int``, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the discrete interval ``[a..b]``.
            * If a list, then for each image a random value will be sampled
              from that list.
            * If ``imgaug.ALL``, then equivalant to list ``[0, 1, 2, 3]``.
            * If ``StochasticParameter``, then that parameter is queried per
              image to sample the value to use.

    keep_size : bool, optional
        After rotation by an odd-valued `k` (e.g. 1 or 3), the resulting image
        may have a different height/width than the original image.
        If this parameter is set to ``True``, then the rotated
        image will be resized to the input image's size. Note that this might
        also cause the augmented image to look distorted.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.Rot90(1)

    Rotate all images by 90 degrees.
    Resize these images afterwards to keep the size that they had before
    augmentation.
    This may cause the images to look distorted.

    >>> aug = iaa.Rot90([1, 3])

    Rotate all images by 90 or 270 degrees.
    Resize these images afterwards to keep the size that they had before
    augmentation.
    This may cause the images to look distorted.

    >>> aug = iaa.Rot90((1, 3))

    Rotate all images by 90, 180 or 270 degrees.
    Resize these images afterwards to keep the size that they had before
    augmentation.
    This may cause the images to look distorted.

    >>> aug = iaa.Rot90((1, 3), keep_size=False)

    Rotate all images by 90, 180 or 270 degrees.
    Does not resize to the original image size afterwards, i.e. each image's
    size may change.

    r	   TNr  c                    sL   t t| j||||d |tjkrg d}tj|dd dddd| _|| _d S )Nr  )r   r	   r   r   kTFr*  )	r  r^  r   r'   r4   r5   r,  r_  r	  )r   r_  r	  r   r^   r   r	  r  r&   r,   r   "  s   


zRot90.__init__c                 C   s   | j j|f|dS )Nrm  )r_  rq  )r   rS  r   r&   r&   r,   r5  1  r   zRot90._draw_samplesc           
      C   s   |  |j|}|jd ur| |j|| jtj|_|jd ur&| |jd||_|j	d ur4| |j	d||_	dD ]}t
||}|d urUtj| j|d}| ||}	t|||	 q6|S )Nr/  rf   r0  )ks)r5  r   r6  _augment_arrays_by_samplesr	  r'   r  r8  r9  r:  r;  r  r  r  r  rd  )
r   r@  r   rA  rB  r`  rD  rE  r  r  r&   r&   r,   rI  5  s0   



zRot90._augment_batch_c                 C   s   t |}|r
|jnd }g }t||D ])\}}	tj||	dd}
|o*|j|
jko*|d u}|r7||
|jdd }
||
 q|rU|rUtdd |D }|dkrUtj	||d}|S )	N)r	   r   )Zaxesr   r   c                 S   rJ  r&   rK  )r*   rf   r&   r&   r,   rM  c  rN  z3Rot90._augment_arrays_by_samples.<locals>.<setcomp>r	   rV  )
r'   rO  r]   ra  r   Zrot90rZ   rP  rY   r   )r#  rf  r`  r	  Zresize_funcrT  r   rg  rf   k_irj  Z	do_resizeZn_shapesr&   r&   r,   ra  Q  s$   

z Rot90._augment_arrays_by_samplesc                    s    fdd|D }|  ||| jd }g }t||||}|D ]B\}}	}
}|	j}t| |
 | jr9||dd }n!|d dkrY|jdd \}}t||gt|jdd   |_n	 || q|S )Nc                    rW  r&   rX  )r*   map_ir[  r&   r,   r-   k  r.   z2Rot90._augment_maps_by_samples.<locals>.<listcomp>r   r   r	   )	ra  r	  ra  rZ   rd  r  re   r9   rP  )r   re  r\  r`  rf  rg  Zmaps_augrh  ri  rf   rj  rb  r  r  r  r&   r[  r,   r9  i  s&   
zRot90._augment_maps_by_samplesc                 C   s.  g }t ||D ]\}}|j}|d dkr|| qt|d }|jdd \}}|d dkr3||fn||f\}	}
|jD ]-}|j|j}}||}}||}}t|D ]}|| |}}||}}qT||_||_q<t	|	|
gt
|jdd   }||_| jr||f|	|
fkr||}||_|| q|S )Nr   r   r   )ra  rZ   rP  rX   r1  r   r   r   r   re   r9   r	  r  )r   r  r`  r   Zkpsoi_irb  r  r  r  Zh_augZw_augr  r   r   yrZxrwrhrrk  Z	shape_augr&   r&   r,   r    s0    



z#Rot90._augment_keypoints_by_samplesc                 C   s   | j | jgS r{  )r_  r	  r   r&   r&   r,   r|       zRot90.get_parameters)r	   TNNr  r  )r   r   r  r~  r   r5  rI  r  ra  r9  r  r|  r  r&   r&   r  r,   r^    s    e
%r^  c                       s  e Zd ZdZ		dD fdd	Zdd Zedd	 Zed
d Zedd Z	edd Z
edd Zedd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zed(d) Zed*d+ Zed,d- Zed.d/ Zed0d1 Zed2d3 Zed4d5 Zed6d7 Zed8d9 Zed:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$  Z%S )EWithPolarWarpinga  Augmenter that applies other augmenters in a polar-transformed space.

    This augmenter first transforms an image into a polar representation,
    then applies its child augmenter, then transforms back to cartesian
    space. The polar representation is still in the image's input dtype
    (i.e. ``uint8`` stays ``uint8``) and can be visualized. It can be thought
    of as an "unrolled" version of the image, where previously circular lines
    appear straight. Hence, applying child augmenters in that space can lead
    to circular effects. E.g. replacing rectangular pixel areas in the polar
    representation with black pixels will lead to curved black areas in
    the cartesian result.

    This augmenter can create new pixels in the image. It will fill these
    with black pixels. For segmentation maps it will fill with class
    id ``0``. For heatmaps it will fill with ``0.0``.

    This augmenter is limited to arrays with a height and/or width of
    ``32767`` or less.

    .. warning::

        When augmenting coordinates in polar representation, it is possible
        that these are shifted outside of the polar image, but are inside the
        image plane after transforming back to cartesian representation,
        usually on newly created pixels (i.e. black backgrounds).
        These coordinates are currently not removed. It is recommended to
        not use very strong child transformations when also augmenting
        coordinate-based augmentables.

    .. warning::

        For bounding boxes, this augmenter suffers from the same problem as
        affine rotations applied to bounding boxes, i.e. the resulting
        bounding boxes can have unintuitive (seemingly wrong) appearance.
        This is due to coordinates being "rotated" that are inside the
        bounding box, but do not fall on the object and actually are
        background.
        It is recommended to use this augmenter with caution when augmenting
        bounding boxes.

    .. warning::

        For polygons, this augmenter should not be combined with
        augmenters that perform automatic polygon recovery for invalid
        polygons, as the polygons will frequently appear broken in polar
        representation and their "fixed" version will be very broken in
        cartesian representation. Augmenters that perform such polygon
        recovery are currently ``PerspectiveTransform``, ``PiecewiseAffine``
        and ``ElasticTransformation``.

    Added in 0.4.0.

    **Supported dtypes**:

        * ``uint8``: yes; fully tested
        * ``uint16``: yes; tested
        * ``uint32``: no (1)
        * ``uint64``: no (2)
        * ``int8``: yes; tested
        * ``int16``: yes; tested
        * ``int32``: yes; tested
        * ``int64``: no (2)
        * ``float16``: yes; tested (3)
        * ``float32``: yes; tested
        * ``float64``: yes; tested
        * ``float128``: no (1)
        * ``bool``: yes; tested (4)

        - (1) OpenCV produces error
          ``TypeError: Expected cv::UMat for argument 'src'``
        - (2) OpenCV produces array of nothing but zeros.
        - (3) Mapepd to ``float32``.
        - (4) Mapped to ``uint8``.

    Parameters
    ----------
    children : imgaug.augmenters.meta.Augmenter or list of imgaug.augmenters.meta.Augmenter or None, optional
        One or more augmenters to apply to images after they were transformed
        to polar representation.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.WithPolarWarping(iaa.CropAndPad(percent=(-0.1, 0.1)))

    Apply cropping and padding in polar representation, then warp back to
    cartesian representation.

    >>> aug = iaa.WithPolarWarping(
    >>>     iaa.Affine(
    >>>         translate_percent={"x": (-0.2, 0.2), "y": (-0.2, 0.2)},
    >>>         rotate=(-35, 35),
    >>>         scale=(0.8, 1.2),
    >>>         shear={"x": (-15, 15), "y": (-15, 15)}
    >>>     )
    >>> )

    Apply affine transformations in polar representation.

    >>> aug = iaa.WithPolarWarping(iaa.AveragePooling((2, 8)))

    Apply average pooling in polar representation. This leads to circular
    bins.

    Nr  c                    s.   t t| j||||d t|| jd| _d S )Nr  Zthen)r  rh  r   r
   Zhandle_children_listr^   children)r   ri  r   r^   r   r	  r  r&   r,   r   *  s
   
zWithPolarWarping.__init__c                 C   s  |j d urtj|j g dg d| d || ||e | |\}}i }|jD ]}t| d|jf }||j\}	}
t	||j
|	 |
||j< q'| jj||| g |d}|jD ]}t| d|jf }||j||j }t	||j
| qU| ||}W d    |S 1 sw   Y  |S )Nr   r   ry   z	_warp_%s_rA  rB  z_invert_warp_%s_)r6  r[   r~   Zpropagation_hooks_ctx_convert_bbs_to_polygons_columnsr;  r^   valuerd  	attr_nameri  augment_batch_ _invert_convert_bbs_to_polygons_)r   r@  r   rA  rB  Zinv_data_bbsinv_datacolumnr  Zcol_augZinv_data_colZ	col_unaugr&   r&   r,   rI  3  s8   



z WithPolarWarping._augment_batch_c                 C   s   |j d u}|jd u r|d|ffS dd |jD }dd |D }|D ]}|j D ]}|jd u r1d|_q&|jd |_q&q!|j d u rB||_ n"t|j |D ]\}}|j|jks\J d|j|jf |j |j  qHd |_|d|ffS )	NFc                 S      g | ]}|  qS r&   )Zto_polygons_on_image)r*   rT  r&   r&   r,   r-   ]  r2   z>WithPolarWarping._convert_bbs_to_polygons_.<locals>.<listcomp>c                 S   s   g | ]}| d qS )r   )Z
subdivide_)r*   rO  r&   r&   r,   r-   ^  r.   $$IMGAUG_BB_AS_POLYGON;$$IMGAUG_BB_AS_POLYGONzRExpected polygons and bounding boxes to have the same .shape value, got %s and %s.T)r3  r2  labelra  rZ   extend)r#  r@  batch_contained_polygonspsoisrO  polygonZbbs_as_psoir&   r&   r,   rk  W  s0   




z*WithPolarWarping._convert_bbs_to_polygons_c                 C   s   |\}}|s|S g }|j D ]S}g }g }|j D ]9}	d}
|	jd u r"d}
n|	jdkr-d |	_d}
n|	jdr@|	jd td  |	_d}
|
rJ||	  q||	 q||_ tj||jd}|| q||_	|sid |_ |S )NFrt  Tru  rK  )
r3  rv  endswithrY   rP  Zto_bounding_boxr'   ZBoundingBoxesOnImagerZ   r2  )r#  r@  rq  Zbatch_contained_bbsrx  bbsoisrO  r3  Zbbsrz  Zis_bbrT  r&   r&   r,   rp  y  s8   



z1WithPolarWarping._invert_convert_bbs_to_polygons_c                 C   s   |  |dS r}  )_warp_arrays)r#  r6  r&   r&   r,   _warp_images_  rg  zWithPolarWarping._warp_images_c                 C   s   |  |d|S r}  )_invert_warp_arrays)r#  Zimages_warpedrq  r&   r&   r,   _invert_warp_images_     z%WithPolarWarping._invert_warp_images_c                 C      |  |ddS Nr/  F_warp_maps_)r#  r8  r&   r&   r,   _warp_heatmaps_  r  z WithPolarWarping._warp_heatmaps_c                 C      |  |dd|S r  _invert_warp_maps_)r#  Zheatmaps_warpedrq  r&   r&   r,   _invert_warp_heatmaps_  s   
z'WithPolarWarping._invert_warp_heatmaps_c                 C   r  Nrf   Tr  )r#  r:  r&   r&   r,   _warp_segmentation_maps_  r  z)WithPolarWarping._warp_segmentation_maps_c                 C   r  r  r  )r#  Zsegmentation_maps_warpedrq  r&   r&   r,   _invert_warp_segmentation_maps_  s   
z0WithPolarWarping._invert_warp_segmentation_maps_c                 C   
   |  |S r   _warp_cbaois_)r#  r  r&   r&   r,   _warp_keypoints_     
z!WithPolarWarping._warp_keypoints_c                 C      |  ||S r   _invert_warp_cbaois_)r#  Zkpsois_warpedimage_shapes_origr&   r&   r,   _invert_warp_keypoints_  rg  z(WithPolarWarping._invert_warp_keypoints_c                 C      |d u sJ dd S Nz0Expected BBs to have been converted to polygons.r&   )r#  r|  r&   r&   r,   _warp_bounding_boxes_     z&WithPolarWarping._warp_bounding_boxes_c                 C   r  r  r&   )r#  Zbbsois_warpedZ_image_shapes_origr&   r&   r,   _invert_warp_bounding_boxes_  r  z-WithPolarWarping._invert_warp_bounding_boxes_c                 C   r  r   r  )r#  ry  r&   r&   r,   _warp_polygons_  r  z WithPolarWarping._warp_polygons_c                 C   r  r   r  )r#  Zpsois_warpedr  r&   r&   r,   _invert_warp_polygons_  rg  z'WithPolarWarping._invert_warp_polygons_c                 C   r  r   r  )r#  Zlsoisr&   r&   r,   _warp_line_strings_  r  z$WithPolarWarping._warp_line_strings_c                 C   r  r   r  )r#  Zlsois_warpedr  r&   r&   r,   _invert_warp_line_strings_  rg  z+WithPolarWarping._invert_warp_line_strings_c           	         s  |d u rdS t jt j |rt j7 g }g }|D ]ĉ d jv r,|  | j q jj}|dkr= t	j
d  n
|dkrG t	j  jdd \}}|dkrX|dks`J d jf d	|d |d ft	|d
 d
 |d
 d
   jdkr jd dkrt	j fddt	 jd D dd}n!t t }|jdkr jdkr|d d d d t	jf }|dkr|dk}n
|dkr|t	j}|| | j q||fS )NNNr   r   rA   r   r   rW  WithPolarWarping._warp_arrays() can currently only handle arrays with axis sizes below 32767, but got shape %s. This is an OpenCV limitation.r   r   r   r   r      c              	      *   g | ]}t t d |f qS .r"   	warpPolarr   r*   Zc_idxrf   	center_xy	dest_sizer   
max_radiusr&   r,   r-     s
    z1WithPolarWarping._warp_arrays.<locals>.<listcomp>r      )r"   WARP_FILL_OUTLIERSWARP_POLAR_LINEARr  rZ   rP  r]   r^   r   r   r   r   r   r   r   r   r  r   r   r   )	r#  arraysinterpolation_nearestarrays_warpedshapes_origr   r   r   r  r&   r  r,   r}    s\   





zWithPolarWarping._warp_arraysc                    s  |}|d u rd S t jt j t j |rt j7 g }t||D ]\ }d jv r.|  q jj	}|dkr? 
tjd  n
|dkrI 
tj |dd \}}	 jd dkr_ jd dksgJ d jf |	|f|	d |d ft|d	 d	 |	d	 d	   jd
kr jd dkrtj fddt jd D dd}
n!t t }
|
jdkrȈ jd
kr|
d d d d tjf }
|dkr|
dk}
n
|dkr|

tj}
||
 q|S )Nr   r   rA   r   r   rW  r	   r  r   r   r   r  c              	      r  r  r  r  r  r  r  r   r  r&   r,   r-   R  s    z8WithPolarWarping._invert_warp_arrays.<locals>.<listcomp>r   r  )r"   r  r  WARP_INVERSE_MAPr  ra  rZ   rP  r]   r^   r   r   r   r   r   r   r   r   r  r   r   r   )r#  r  r  rq  r  
arrays_invr  r   r   r   Zarr_invr&   r  r,   r  (  s`   




z$WithPolarWarping._invert_warp_arraysc                 C   s   |d u rdS dgt | }g }g }t|D ]-\}}d|jv r4d||< |tjdtjd ||j q|t|| ||j q| ||\}	}
| 	|}t|D ]\}}|| si|| |_t
|||	|  qT|||
|ffS )Nr  Fr   Tr  rV  )rY   r   rZ   rP  r   r  r   r;  r}  _warp_shape_tuplesrd  )r#  mapsr\  r  skippedr  shapes_imgs_origr   rc  r  warparr_inv_dataZshapes_imgs_warpedr&   r&   r,   r  g  s,   


zWithPolarWarping._warp_maps_c                 C   s   |d u rd S |\}}}g }t |D ]\}	}
||	 r%|tjdtjd q|t|
| q| |||}t |D ]\}	}||	 sN||	 |_t||||	  q9|S )Nr  rV  )	r   rP  r   r  r   r;  r  rZ   rd  )r#  Zmaps_warpedr\  r  Zinvert_datar  r  r  r  r   Z
map_warpedr  rc  r&   r&   r,   r    s$   

z#WithPolarWarping._invert_warp_maps_c                 C   s   |d u rdS |  |}tj}g }t|||D ]D\}}}d|v r%|| q|dd \}	}
|d |d f}|
d |	d f}t|	d d |
d d  }| |||||}|| q||fS )Nr  r   r   r	   r   )r  r"   r  ra  rP  r   r   warpPolarCoords)r#  r   rQ  image_shapes_warpedr   coords_warpedZcoords_irZ   shape_warpedr   r   r  r  r  coords_i_warpedr&   r&   r,   _warp_coords  s(   


zWithPolarWarping._warp_coordsc                 C   s   |}|d u rd S t jt j }g }tt||}|D ]I\}\}	}
d|
v r)||	 q|| }|
dd \}}|d |d f}|d |d f}t|d d |d d  }| |	||||}|| q|S )Nr   r   r	   r   )	r"   r  r  r   ra  rP  r   r   r  )r#  r  image_shapes_after_augrq  r  r   Z
coords_invrh  r   r  r  r  r   r   r  r  r  Zcoords_i_invr&   r&   r,   _invert_warp_coords  s*   
z$WithPolarWarping._invert_warp_coordsc           
      C   s   |d u rdS dd |D }dd |D }|  |}| ||\}}tt||D ]\}\}}	||	}|| |_|||< q(||fS )Nr  c                 S   rs  r&   r=  r*   rF  r&   r&   r,   r-     r2   z2WithPolarWarping._warp_cbaois_.<locals>.<listcomp>c                 S   r]  r&   rK  r  r&   r&   r,   r-     rN  )r  r  r   ra  r?  rZ   )
r#  r  r   rQ  r  r  rq  r   rF  r  r&   r&   r,   r    s   



zWithPolarWarping._warp_cbaois_c           
      C   sv   |d u rd S dd |D }dd |D }|  |||}|}tt||D ]\}\}}	||	}|| |_|||< q$|S )Nc                 S   rs  r&   r  r  r&   r&   r,   r-     r2   z9WithPolarWarping._invert_warp_cbaois_.<locals>.<listcomp>c                 S   r]  r&   rK  r  r&   r&   r,   r-     rN  )r  r   ra  r?  rZ   )
r#  Zcbaois_warpedr  r   r  r  r  r   rF  r  r&   r&   r,   r    s   


z%WithPolarWarping._invert_warp_cbaois_c              	   C   s   t j}g }|D ]D}d|v r|| q|dd \}}t |d d |d d  }tt |}tt || }|t||gt|dd    q|S )Nr   r   r   )r   pirP  r   rX   r   re   r9   )r#  r#  r  r   rZ   r   r   r  r&   r&   r,   r     s   
$z#WithPolarWarping._warp_shape_tuplesc                 C   s  |d dksJ |d dksJ |d }|d }|d }|d }	t |tjr|d d df }
|d d df }|dt j  }|| }t |tjrX|t | }t |
| }n|| }|
| }||t 	|  }|	|t 
|  }|d d t jf }|d d t jf }t j||gddS |d d df }|d d df }|dt j  }|| }|| ||	 }}t||\}}|| }|| }
t j|
|gddS )Nr   r	   r   r   )r   rJ  r"   r  r  Zbitwise_andZWARP_POLAR_LOGlogexpcossinr   rK  ZcartToPolar)r#  srcr   centerZ	maxRadiusr   Zdsize_widthZdsize_heightZcenter_xZcenter_yrhophiZKangleZangleRadZKlogZ	magnitudeZKlinr   r   ZI_xZI_yZmagnitude_IZangle_Ir&   r&   r,   r    s<   
z WithPolarWarping.warpPolarCoordsc                 C   s   g S r{  r&   r   r&   r&   r,   r|  N  s   zWithPolarWarping.get_parametersc                 C   s   | j gS )zASee :func:`~imgaug.augmenters.meta.Augmenter.get_children_lists`.)ri  r   r&   r&   r,   get_children_listsS  s   z#WithPolarWarping.get_children_listsc                 C   s*   |   }|j |_d|_| j |_|S )NT)r   ri  Zto_deterministicr	  r   Zderive_rng_)r   augr&   r&   r,   _to_deterministicX  s
   z"WithPolarWarping._to_deterministicc                 C   s   d}|| j j| j| j| jf S )Nz*%s(name=%s, children=%s, deterministic=%s))r  r   r^   ri  r	  )r   patternr&   r&   r,   __str__`  s
   zWithPolarWarping.__str__)NNr  r  )&r   r   r  r~  r   rI  r  rk  rp  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  r  r  r&   r&   r  r,   rh    s|    }	$
!
'














9
>







4rh  c                       sr   e Zd ZdZ				d fdd	Zd	d
 Zdd Zedd Zedd Z	edd Z
edd Zdd Z  ZS )JigsawaY  Move cells within images similar to jigsaw patterns.

    .. note::

        This augmenter will by default pad images until their height is a
        multiple of `nb_rows`. Analogous for `nb_cols`.

    .. note::

        This augmenter will resize heatmaps and segmentation maps to the
        image size, then apply similar padding as for the corresponding images
        and resize back to the original map size. That also means that images
        may change in shape (due to padding), but heatmaps/segmaps will not
        change. For heatmaps/segmaps, this deviates from pad augmenters that
        will change images and heatmaps/segmaps in corresponding ways and then
        keep the heatmaps/segmaps at the new size.

    .. warning::

        This augmenter currently only supports augmentation of images,
        heatmaps, segmentation maps and keypoints. Other augmentables,
        i.e. bounding boxes, polygons and line strings, will result in errors.

    Added in 0.4.0.

    **Supported dtypes**:

    See :func:`~imgaug.augmenters.geometric.apply_jigsaw`.

    Parameters
    ----------
    nb_rows : int or list of int or tuple of int or imgaug.parameters.StochasticParameter, optional
        How many rows the jigsaw pattern should have.

            * If a single ``int``, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the discrete interval ``[a..b]``.
            * If a list, then for each image a random value will be sampled
              from that list.
            * If ``StochasticParameter``, then that parameter is queried per
              image to sample the value to use.

    nb_cols : int or list of int or tuple of int or imgaug.parameters.StochasticParameter, optional
        How many cols the jigsaw pattern should have.

            * If a single ``int``, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the discrete interval ``[a..b]``.
            * If a list, then for each image a random value will be sampled
              from that list.
            * If ``StochasticParameter``, then that parameter is queried per
              image to sample the value to use.

    max_steps : int or list of int or tuple of int or imgaug.parameters.StochasticParameter, optional
        How many steps each jigsaw cell may be moved.

            * If a single ``int``, then that value will be used for all images.
            * If a tuple ``(a, b)``, then a random value will be uniformly
              sampled per image from the discrete interval ``[a..b]``.
            * If a list, then for each image a random value will be sampled
              from that list.
            * If ``StochasticParameter``, then that parameter is queried per
              image to sample the value to use.

    allow_pad : bool, optional
        Whether to allow automatically padding images until they are evenly
        divisible by ``nb_rows`` and ``nb_cols``.

    seed : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    name : None or str, optional
        See :func:`~imgaug.augmenters.meta.Augmenter.__init__`.

    random_state : None or int or imgaug.random.RNG or numpy.random.Generator or numpy.random.BitGenerator or numpy.random.SeedSequence or numpy.random.RandomState, optional
        Old name for parameter `seed`.
        Its usage will not yet cause a deprecation warning,
        but it is still recommended to use `seed` now.
        Outdated since 0.4.0.

    deterministic : bool, optional
        Deprecated since 0.4.0.
        See method ``to_deterministic()`` for an alternative and for
        details about what the "deterministic mode" actually does.

    Examples
    --------
    >>> import imgaug.augmenters as iaa
    >>> aug = iaa.Jigsaw(nb_rows=10, nb_cols=10)

    Create a jigsaw augmenter that splits images into ``10x10`` cells
    and shifts them around by ``0`` to ``2`` steps (default setting).

    >>> aug = iaa.Jigsaw(nb_rows=(1, 4), nb_cols=(1, 4))

    Create a jigsaw augmenter that splits each image into ``1`` to ``4``
    cells along each axis.

    >>> aug = iaa.Jigsaw(nb_rows=10, nb_cols=10, max_steps=(1, 5))

    Create a jigsaw augmenter that moves the cells in each image by a random
    amount between ``1`` and ``5`` times (decided per image). Some images will
    be barely changed, some will be fairly distorted.

    r   r  r	   TNr  c	           	         sj   t t| j||||d tj|dddddd| _tj|dddddd| _tj|dd	dddd| _|| _d S )
Nr  r   )r	   NTFr*  r   r   r  )	r  r  r   r5   r,  r   r   r   	allow_pad)	r   r   r   r   r  r   r^   r   r	  r  r&   r,   r     s"   

zJigsaw.__init__c                    s  |   |}|  \ }| jrAtt|jD ](}tj|j	| |j
| |d} |g}	|j|	|| g |d}	 |g|	 q jd urZt jD ]\}}
t|
|j| |
d< qK jd urst jD ]\}}t|j|j| |_qd jd urt jD ]\}}t|j|j| |_q} jd urt jD ]\}}| }t||j| |jd|d< || qt fdddD }|rtd|  |  S )	N)Zwidth_multipleZheight_multipler   rj  .)r   c                    s   g | ]	}t  |d uqS r   rX  )r*   rn  r@  r&   r,   r-     s    z*Jigsaw._augment_batch_.<locals>.<listcomp>)r2  r3  r4  zJigsaw currently only supports augmentation of images, heatmaps, segmentation maps and keypoints. Explicitly not supported are: bounding boxes, polygons and line strings.)r5  _resize_mapsr  r   r   rY   r   size_libZCenterPadToMultiplesOfr   r   Zsubselect_rows_by_indicesro  Z!invert_subselect_rows_by_indices_r6  r   r   r8  r/  r:  rf   r1  r=  r   rZ   r?  r  NotImplementedError_invert_resize_maps)r   r@  r   rA  rB  rC  Zmaps_shapes_origr   ZpadderrowrL  ZheatmapZsegmaprG  ZxyZhas_other_cbaoir&   r  r,   rI    sT   		





zJigsaw._augment_batch_c           
   	   C   s   |j }| j j|f|d}| jj|f|d}| jj|f|d}g }t|D ]}|t|| || || |d q%t||||}	|	S )Nrm  )r   )	r   rq  r   r   r   r   rP  r   _JigsawSamples)
r   r@  r   rS  r   r   r   r   r   rC  r&   r&   r,   r5  2  s(   


zJigsaw._draw_samplesc                 C   s\   |j d u r|jd u r|dfS | }| |j d|\|_ }| |jd|\|_}|||ffS )Nr  r/  rf   )r8  r:  r  _resize_maps_single_list)r#  r@  rQ  Zheatmaps_shapes_origZsm_shapes_origr&   r&   r,   r  F  s   

zJigsaw._resize_mapsc           
      C   sb   |d u rdS g }g }t ||D ]\}}t||j}||dd }	||	 || q||fS )Nr  r   r   )ra  r;  rZ   r  rP  )
r#  re  r\  rQ  r  augms_resizedrZ  r   r  Zaugm_rsr&   r&   r,   r  U  s   
zJigsaw._resize_maps_single_listc                 C   s,   |  |j|d |_|  |j|d |_|S )Nr   r	   )_invert_resize_maps_single_listr8  r:  )r#  r@  r  r&   r&   r,   r  e  s   

zJigsaw._invert_resize_mapsc                 C   s@   |d u rd S g }t ||D ]\}}|||dd  q|S )Nr   r   )ra  rP  r  )r#  re  r  r  rZ  r  r&   r&   r,   r  o  s   z&Jigsaw._invert_resize_maps_single_listc                 C   s   | j | j| j| jgS r   )r   r   r   r  r   r&   r&   r,   r|  z  s   zJigsaw.get_parameters)r  r  r	   TNNr  r  )r   r   r  r~  r   rI  r5  r  r  r  r  r  r|  r  r&   r&   r  r,   r  i  s$    kI


	

r  c                   @   r  )r  c                 C   s   || _ || _|| _|| _d S r   )r   r   r   r   )r   r   r   r   r   r&   r&   r,   r     s   
z_JigsawSamples.__init__Nr  r&   r&   r&   r,   r    s    r  )r	   r   r   Nr!   )r   )r  )Qr~  
__future__r   r   r   r  r  numpyr   rX  r   rU   r   r   r"   Z	six.movesmovesr   Zimgaugr'   Zimgaug.imgaugr   Zimgaug.augmentables.polysr    r
   r   rU  r   r  r   r5   r   r[   r   r   r_   r`   r  r  r  r   r  r  r  r  r  r   r@   rH   rT   rl   rc   rd   r   r   r   r   objectr   r  Z	Augmenterr  r  r  r  r  r  r  r  r  r  r  r  r  r-  r2  r^  rh  r  r  r&   r&   r&   r,   <module>   s    	,
;!K![B
J
l	      MMN[ZKKK     n   w
            r     A  