
    2VhT                     T   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  G d d	e	      Z ed
      d=d       Z G d de	      Z ed      d=d       Z G d de	      Z ed      d=d       Z G d de	      Z ed      	 	 	 	 	 	 	 d>d       Z	 	 	 	 	 	 	 d>dZ G d de	      Z ed      	 	 	 	 d?d       Z G d de	      Z ed       	 	 	 	 d@d!       Z	 	 	 	 d@d"Z G d# d$e	      Z ed%      	 dAd&       Z G d' d(e	      Z ed)      	 	 	 	 	 	 	 dBd*       Z	 d=d+Z G d, d-e	      Z ed.      	 	 	 	 	 	 	 dBd/       Z 	 d=d0Z! G d1 d2e	      Z" ed3      	 	 	 dCd4       Z# G d5 d6e	      Z$ ed7      	 dDd8       Z% G d9 d:e	      Z& ed;      	 	 	 	 	 	 	 dEd<       Z'y)F    )backend)ops)keras_export)KerasTensor)any_symbolic_tensors)	Operation)compute_conv_output_shapec                   ,     e Zd Zd fd	Zd Zd Z xZS )RGBToGrayscalec                 V    t         |           t        j                  |      | _        y Nsuper__init__r   standardize_data_formatdata_formatselfr   	__class__s     C/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/ops/image.pyr   zRGBToGrayscale.__init__   !    "::;G    c                 X    t         j                  j                  || j                        S Nr   )r   imagergb_to_grayscaler   r   imagess     r   callzRGBToGrayscale.call   s)    }}-- 0 0 . 
 	
r   c                     t        |j                        }t        |      dvrt        d|       | j                  dk(  rd|d<   nd|d<   t        ||j                        S )N      hInvalid images rank: expected rank 3 (single image) or rank 4 (batch of images). Received: images.shape=channels_last   shapedtype)listr+   len
ValueErrorr   r   r,   )r   r   images_shapes      r   compute_output_specz"RGBToGrayscale.compute_output_spec   sn    FLL)|F***69 
 . L LV\\BBr   r   __name__
__module____qualname__r   r    r1   __classcell__r   s   @r   r   r   
   s    H

Cr   r   z keras.ops.image.rgb_to_grayscaleNc                     t        | f      rt        |      j                  |       S t        j                  j                  | |      S )a  Convert RGB images to grayscale.

    This function converts RGB images to grayscale images. It supports both
    3D and 4D tensors.

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Grayscale image or batch of grayscale images.

    Examples:

    >>> import numpy as np
    >>> from keras import ops
    >>> x = np.random.random((2, 4, 4, 3))
    >>> y = ops.image.rgb_to_grayscale(x)
    >>> y.shape
    (2, 4, 4, 1)

    >>> x = np.random.random((4, 4, 3)) # Single RGB image
    >>> y = ops.image.rgb_to_grayscale(x)
    >>> y.shape
    (4, 4, 1)

    >>> x = np.random.random((2, 3, 4, 4))
    >>> y = ops.image.rgb_to_grayscale(x, data_format="channels_first")
    >>> y.shape
    (2, 1, 4, 4)
    r   )r   r   symbolic_callr   r   r   r   r   s     r   r   r   #   s?    N VI&+6DDVLL==))&k)JJr   c                   ,     e Zd Zd fd	Zd Zd Z xZS )RGBToHSVc                 V    t         |           t        j                  |      | _        y r   r   r   s     r   r   zRGBToHSV.__init__P   r   r   c                 X    t         j                  j                  || j                        S r   )r   r   
rgb_to_hsvr   r   s     r   r    zRGBToHSV.callT   "    }}''D<L<L'MMr   c                     t        |j                        }|j                  }t        |      dvrt	        d|       t        j                  |      st	        d|       t        ||j                        S Nr"   r%   zCInvalid images dtype: expected float dtype. Received: images.dtype=r*   r-   r+   r,   r.   r/   r   is_float_dtyper   r   r   r0   r,   s       r   r1   zRGBToHSV.compute_output_specW       FLL)|F***69 
 %%e,**/2  V\\BBr   r   r2   r7   s   @r   r<   r<   O       HNCr   r<   zkeras.ops.image.rgb_to_hsvc                     t        | f      rt        |      j                  |       S t        j                  j                  | |      S )a2  Convert RGB images to HSV.

    `images` must be of float dtype, and the output is only well defined if the
    values in `images` are in `[0, 1]`.

    All HSV values are in `[0, 1]`. A hue of `0` corresponds to pure red, `1/3`
    is pure green, and `2/3` is pure blue.

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        HSV image or batch of HSV images.

    Examples:

    >>> import numpy as np
    >>> from keras import ops
    >>> x = np.random.random((2, 4, 4, 3))
    >>> y = ops.image.rgb_to_hsv(x)
    >>> y.shape
    (2, 4, 4, 3)

    >>> x = np.random.random((4, 4, 3)) # Single RGB image
    >>> y = ops.image.rgb_to_hsv(x)
    >>> y.shape
    (4, 4, 3)

    >>> x = np.random.random((2, 3, 4, 4))
    >>> y = ops.image.rgb_to_hsv(x, data_format="channels_first")
    >>> y.shape
    (2, 3, 4, 4)
    r   )r   r<   r9   r   r   r?   r:   s     r   r?   r?   h   s?    T VI&K0>>vFF==##F#DDr   c                   ,     e Zd Zd fd	Zd Zd Z xZS )HSVToRGBc                 V    t         |           t        j                  |      | _        y r   r   r   s     r   r   zHSVToRGB.__init__   r   r   c                 X    t         j                  j                  || j                        S r   )r   r   
hsv_to_rgbr   r   s     r   r    zHSVToRGB.call   r@   r   c                     t        |j                        }|j                  }t        |      dvrt	        d|       t        j                  |      st	        d|       t        ||j                        S rB   rC   rE   s       r   r1   zHSVToRGB.compute_output_spec   rF   r   r   r2   r7   s   @r   rJ   rJ      rG   r   rJ   zkeras.ops.image.hsv_to_rgbc                     t        | f      rt        |      j                  |       S t        j                  j                  | |      S )a  Convert HSV images to RGB.

    `images` must be of float dtype, and the output is only well defined if the
    values in `images` are in `[0, 1]`.

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        RGB image or batch of RGB images.

    Examples:

    >>> import numpy as np
    >>> from keras import ops
    >>> x = np.random.random((2, 4, 4, 3))
    >>> y = ops.image.hsv_to_rgb(x)
    >>> y.shape
    (2, 4, 4, 3)

    >>> x = np.random.random((4, 4, 3)) # Single HSV image
    >>> y = ops.image.hsv_to_rgb(x)
    >>> y.shape
    (4, 4, 3)

    >>> x = np.random.random((2, 3, 4, 4))
    >>> y = ops.image.hsv_to_rgb(x, data_format="channels_first")
    >>> y.shape
    (2, 3, 4, 4)
    r   )r   rJ   r9   r   r   rM   r:   s     r   rM   rM      s?    N VI&K0>>vFF==##F#DDr   c                   :     e Zd Z	 	 	 	 	 	 	 d fd	Zd Zd Z xZS )Resizec	                     t         	|           t        |      | _        || _        || _        || _        || _        || _        || _	        t        j                  |      | _        y r   )r   r   tuplesizeinterpolation	antialiascrop_to_aspect_ratiopad_to_aspect_ratio	fill_mode
fill_valuer   r   r   )
r   rT   rU   rV   rW   rX   rY   rZ   r   r   s
            r   r   zResize.__init__   s[     	$K	*"$8!#6 "$"::;Gr   c                     t        || j                  | j                  | j                  | j                  | j
                  | j                  | j                  | j                  	      S )NrU   rV   r   rW   rX   rY   rZ   )	_resizerT   rU   rV   r   rW   rX   rY   rZ   r   s     r   r    zResize.call   sR    II,,nn((!%!:!: $ 8 8nn

 
	
r   c                     t        |j                        }t        |      dvrt        d|j                         | j                  dk(  rd\  }}nd\  }}| j
                  d   ||<   | j
                  d   ||<   t        ||j                        S )	Nr"   yInvalid images rank: expected rank 3 (single image) or rank 4 (batch of images). Received input with shape: images.shape=r&   r)   ra   r(   r   r'   r*   )r-   r+   r.   r/   r   rT   r   r,   )r   r   r0   height_axis
width_axiss        r   r1   zResize.compute_output_spec   s    FLL)|F*  &~/ 
 .&,#K&,#K$(IIaL[!#'99Q<Z V\\BBr   bilinearFFFconstant        Nr2   r7   s   @r   rQ   rQ      s+     !"!H*
Cr   rQ   zkeras.ops.image.resizec	                 l   t        |      dk7  rt        d|       t        | j                        dk  st        | j                        dkD  rt        d| j                         |r|rt        d      t        | f      r"t	        ||||||||      j                  |       S t        | ||||||||	      S )	a	  Resize images to size using the specified interpolation method.

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        size: Size of output image in `(height, width)` format.
        interpolation: Interpolation method. Available methods are `"nearest"`,
            `"bilinear"`, and `"bicubic"`. Defaults to `"bilinear"`.
        antialias: Whether to use an antialiasing filter when downsampling an
            image. Defaults to `False`.
        crop_to_aspect_ratio: If `True`, resize the images without aspect
            ratio distortion. When the original aspect ratio differs
            from the target aspect ratio, the output image will be
            cropped so as to return the
            largest possible window in the image (of size `(height, width)`)
            that matches the target aspect ratio. By default
            (`crop_to_aspect_ratio=False`), aspect ratio may not be preserved.
        pad_to_aspect_ratio: If `True`, pad the images without aspect
            ratio distortion. When the original aspect ratio differs
            from the target aspect ratio, the output image will be
            evenly padded on the short side.
        fill_mode: When using `pad_to_aspect_ratio=True`, padded areas
            are filled according to the given mode. Only `"constant"` is
            supported at this time
            (fill with constant value, equal to `fill_value`).
        fill_value: Float. Padding value to use when `pad_to_aspect_ratio=True`.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Resized image or batch of images.

    Examples:

    >>> x = np.random.random((2, 4, 4, 3)) # batch of 2 RGB images
    >>> y = keras.ops.image.resize(x, (2, 2))
    >>> y.shape
    (2, 2, 2, 3)

    >>> x = np.random.random((4, 4, 3)) # single RGB image
    >>> y = keras.ops.image.resize(x, (2, 2))
    >>> y.shape
    (2, 2, 3)

    >>> x = np.random.random((2, 3, 4, 4)) # batch of 2 RGB images
    >>> y = keras.ops.image.resize(x, (2, 2),
    ...     data_format="channels_first")
    >>> y.shape
    (2, 3, 2, 2)
       z<Expected `size` to be a tuple of 2 integers. Received: size=r#   r$   r_   zIOnly one of `pad_to_aspect_ratio` & `crop_to_aspect_ratio` can be `True`.r\   rU   rV   rW   r   rX   rY   rZ   )r.   r/   r+   r   rQ   r9   r]   )	r   rT   rU   rV   rW   rX   rY   rZ   r   s	            r   resizerl     s    D 4yA~"V%
 	
 6<<1FLL 1A 5"LL>+
 	

 3
 	
 VI&'#!5 3!	
 -
		  #1/
 
r   c	                 0   t         j                  j                  | ||||||||	      }	|	j                  | j                  k(  r|	S t        j                  | j                        rt        j                  |	      }	t        j                  |	| j                        S )Nrk   )r   r   rl   r,   is_int_dtyper   roundsaturate_cast)
r   rT   rU   rV   rW   rX   rY   rZ   r   resizeds
             r   r]   r]   z  s     mm""#1/ # 
G }}$ FLL)))G$Wfll33r   c                   4     e Zd Z	 	 	 	 d fd	Zd Zd Z xZS )AffineTransformc                     t         |           || _        || _        || _        t        j                  |      | _        y r   )r   r   rU   rY   rZ   r   r   r   )r   rU   rY   rZ   r   r   s        r   r   zAffineTransform.__init__  s9     	*"$"::;Gr   c                     t         j                  j                  ||| j                  | j                  | j
                  | j                        S )NrU   rY   rZ   r   )r   r   affine_transformrU   rY   rZ   r   r   r   	transforms      r   r    zAffineTransform.call  sD    }}--,,nn(( . 
 	
r   c                     t        |j                        dvrt        d|j                         t        |j                        dvrt        d|j                         t        |j                  |j                        S )Nr"   r_   )r'   rj   zInvalid transform rank: expected rank 1 (single transform) or rank 2 (batch of transforms). Received input with shape: transform.shape=r,   r.   r+   r/   r   r,   rx   s      r   r1   z#AffineTransform.compute_output_spec  s~    v||F*  &~/ 
 yv-##,??"35 
 6<<v||<<r   rf   rg   r   Nr2   r7   s   @r   rs   rs     s!     !H
=r   rs   z keras.ops.image.affine_transformc                     t        | |f      rt        ||||      j                  | |      S t        j                  j                  | |||||      S )a  Applies the given transform(s) to the image(s).

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        transform: Projective transform matrix/matrices. A vector of length 8 or
            tensor of size N x 8. If one row of transform is
            `[a0, a1, a2, b0, b1, b2, c0, c1]`, then it maps the output point
            `(x, y)` to a transformed input point
            `(x', y') = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k)`,
            where `k = c0 x + c1 y + 1`. The transform is inverted compared to
            the transform mapping input points to output points. Note that
            gradients are not backpropagated into transformation parameters.
            Note that `c0` and `c1` are only effective when using TensorFlow
            backend and will be considered as `0` when using other backends.
        interpolation: Interpolation method. Available methods are `"nearest"`,
            and `"bilinear"`. Defaults to `"bilinear"`.
        fill_mode: Points outside the boundaries of the input are filled
            according to the given mode. Available methods are `"constant"`,
            `"nearest"`, `"wrap"` and `"reflect"`. Defaults to `"constant"`.
            - `"reflect"`: `(d c b a | a b c d | d c b a)`
                The input is extended by reflecting about the edge of the last
                pixel.
            - `"constant"`: `(k k k k | a b c d | k k k k)`
                The input is extended by filling all values beyond
                the edge with the same constant value k specified by
                `fill_value`.
            - `"wrap"`: `(a b c d | a b c d | a b c d)`
                The input is extended by wrapping around to the opposite edge.
            - `"nearest"`: `(a a a a | a b c d | d d d d)`
                The input is extended by the nearest pixel.
        fill_value: Value used for points outside the boundaries of the input if
            `fill_mode="constant"`. Defaults to `0`.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Applied affine transform image or batch of images.

    Examples:

    >>> x = np.random.random((2, 64, 80, 3)) # batch of 2 RGB images
    >>> transform = np.array(
    ...     [
    ...         [1.5, 0, -20, 0, 1.5, -16, 0, 0],  # zoom
    ...         [1, 0, -20, 0, 1, -16, 0, 0],  # translation
    ...     ]
    ... )
    >>> y = keras.ops.image.affine_transform(x, transform)
    >>> y.shape
    (2, 64, 80, 3)

    >>> x = np.random.random((64, 80, 3)) # single RGB image
    >>> transform = np.array([1.0, 0.5, -20, 0.5, 1.0, -16, 0, 0])  # shear
    >>> y = keras.ops.image.affine_transform(x, transform)
    >>> y.shape
    (64, 80, 3)

    >>> x = np.random.random((2, 3, 64, 80)) # batch of 2 RGB images
    >>> transform = np.array(
    ...     [
    ...         [1.5, 0, -20, 0, 1.5, -16, 0, 0],  # zoom
    ...         [1, 0, -20, 0, 1, -16, 0, 0],  # translation
    ...     ]
    ... )
    >>> y = keras.ops.image.affine_transform(x, transform,
    ...     data_format="channels_first")
    >>> y.shape
    (2, 3, 64, 80)
    rv   )r   rs   r9   r   r   rw   )r   ry   rU   rY   rZ   r   s         r   rw   rw     si    f VY/0'!#	

 -	
*	+ ==))# *  r   c                   4     e Zd Z	 	 	 	 d fd	Zd Zd Z xZS )ExtractPatchesc                     t         |           t        |t              r||f}|| _        || _        || _        || _        t        j                  |      | _
        y r   )r   r   
isinstanceintrT   stridesdilation_ratepaddingr   r   r   )r   rT   r   r   r   r   r   s         r   r   zExtractPatches.__init__&  sS     	dC $<D	*"::;Gr   c                     t        || j                  | j                  | j                  | j                  | j
                        S )Nr   rT   r   r   r   r   )_extract_patchesrT   r   r   r   r   r   s     r   r    zExtractPatches.call7  s9    LL,,LL((
 	
r   c           	         t        |j                        }t        |      }| j                  s| j                  d   | j                  d   f}| j
                  dk(  r|d   }n|d   }|dk(  rdg|z   }| j                  d   | j                  d   z  |z  }| j                  d   | j                  d   f}t        |||| j                  | j
                  | j                        }|dk(  r|dd  }t        ||j                        S )	Nr   r'   r&   r(   r)   r#   )r   r   r   r   r*   )r-   r+   r.   r   rT   r   r	   r   r   r   r,   )	r   r   r0   original_ndimr   channels_infilterskernel_size	out_shapes	            r   r1   z"ExtractPatches.compute_output_specA  s    FLL)L)||yy|TYYq\2G.&r*K&r*KA3-L))A,1-;yy|TYYq\2-LL((,,
	 A!!"I&,,??r   Nr'   validNr2   r7   s   @r   r   r   %  s"     H"
@r   r   zkeras.ops.image.extract_patchesc                 z    t        | f      rt        |||||      j                  |       S t        | |||||      S )ae  Extracts patches from the image(s).

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        size: Patch size int or tuple (patch_height, patch_width)
        strides: strides along height and width. If not specified, or
            if `None`, it defaults to the same value as `size`.
        dilation_rate: This is the input stride, specifying how far two
            consecutive patch samples are in the input. For value other than 1,
            strides must be 1. NOTE: `strides > 1` is not supported in
            conjunction with `dilation_rate > 1`
        padding: The type of padding algorithm to use: `"same"` or `"valid"`.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Extracted patches 3D (if not batched) or 4D (if batched)

    Examples:

    >>> image = np.random.random(
    ...     (2, 20, 20, 3)
    ... ).astype("float32") # batch of 2 RGB images
    >>> patches = keras.ops.image.extract_patches(image, (5, 5))
    >>> patches.shape
    (2, 4, 4, 75)
    >>> image = np.random.random((20, 20, 3)).astype("float32") # 1 RGB image
    >>> patches = keras.ops.image.extract_patches(image, (3, 3), (1, 1))
    >>> patches.shape
    (18, 18, 27)
    )rT   r   r   r   r   r   )r   r   r9   r   r   s         r   extract_patchesr   \  sS    Z VI&'#
 -
	  g}g; r   c                    t        |t              r|x}}n't        |      dk(  r|d   |d   }}nt        d|       t	        j
                  |      }|dk(  r| j                  d   }n|dk(  r| j                  d   }|s|}||z  z  }	t        j                  j                  |	| j                  	      }
t        j                  j                  |
||||	f      }
d
}t        | j                        dk(  r#d}t        j                  j                  | d      } t        j                  j                  | |
||||      }|r!t        j                  j                  |d      }|S )Nrj   r   r'   zPInvalid `size` argument. Expected an int or a tuple of length 2. Received: size=r&   r(   channels_firstr)   r{   Fr#   T)axis)inputskernelr   r   r   r   )r   r   r.   	TypeErrorr   r   r+   numpyeyer,   reshapeexpand_dimsnnconvsqueeze)r   rT   r   r   r   r   patch_hpatch_wr   out_dimr   
_unbatchedpatchess                r   r   r     sh    $  '	Ta7DG::>A
 	
 11+>Ko%ll2&	(	(ll2&+-G]]wfll;F]]""';8F J
6<<A
**6*:jjoo#  G --''a'8Nr   c                   ,     e Zd Zd fd	Zd Zd Z xZS )MapCoordinatesc                 L    t         |           || _        || _        || _        y r   )r   r   orderrY   rZ   )r   r   rY   rZ   r   s       r   r   zMapCoordinates.__init__  s#    
"$r   c                     t         j                  j                  ||| j                  | j                  | j
                        S )N)r   rY   rZ   )r   r   map_coordinatesr   rY   rZ   r   r   coordinatess      r   r    zMapCoordinates.call  s9    }},,**nn - 
 	
r   c                 D   |j                   d   t        |j                         k7  r(t        d|j                    d|j                   d          t        |j                         dk  rt        d|j                          t        |j                   dd  |j                        S )Nr   zaFirst dim of `coordinates` must be the same as the rank of `inputs`. Received inputs with shape: z and coordinate leading dim of rj   zOInvalid coordinates rank: expected at least rank 2. Received input with shape: r'   r{   )r+   r.   r/   r   r,   r   s      r   r1   z"MapCoordinates.compute_output_spec  s    Q3v||#44//5||n =""-"3"3A"6!79  {  !A%//:/@/@.AC  ;,,QR0EEr   rg   r   r2   r7   s   @r   r   r     s    %
Fr   r   zkeras.ops.image.map_coordinatesc                     t        | |f      rt        |||      j                  | |      S t        j                  j                  | ||||      S )a  Map the input array to new coordinates by interpolation.

    Note that interpolation near boundaries differs from the scipy function,
    because we fixed an outstanding bug
    [scipy/issues/2640](https://github.com/scipy/scipy/issues/2640).

    Args:
        inputs: The input array.
        coordinates: The coordinates at which inputs is evaluated.
        order: The order of the spline interpolation. The order must be `0` or
            `1`. `0` indicates the nearest neighbor and `1` indicates the linear
            interpolation.
        fill_mode: Points outside the boundaries of the inputs are filled
            according to the given mode. Available methods are `"constant"`,
            `"nearest"`, `"wrap"` and `"mirror"` and `"reflect"`. Defaults to
            `"constant"`.
            - `"constant"`: `(k k k k | a b c d | k k k k)`
                The inputs is extended by filling all values beyond
                the edge with the same constant value k specified by
                `fill_value`.
            - `"nearest"`: `(a a a a | a b c d | d d d d)`
                The inputs is extended by the nearest pixel.
            - `"wrap"`: `(a b c d | a b c d | a b c d)`
                The inputs is extended by wrapping around to the opposite edge.
            - `"mirror"`: `(c d c b | a b c d | c b a b)`
                The inputs is extended by mirroring about the edge.
            - `"reflect"`: `(d c b a | a b c d | d c b a)`
                The inputs is extended by reflecting about the edge of the last
                pixel.
        fill_value: Value used for points outside the boundaries of the inputs
            if `fill_mode="constant"`. Defaults to `0`.

    Returns:
        Output input or batch of inputs.

    )r   r   r9   r   r   r   )r   r   r   rY   rZ   s        r   r   r     s^    P V[12
 -
,		-
 ==(( r   c                   :     e Zd Z	 	 	 	 	 	 	 d fd	Zd Zd Z xZS )	PadImagesc                     t         |           || _        || _        || _        || _        || _        || _        t        j                  |      | _
        y r   )r   r   top_paddingleft_paddingbottom_paddingright_paddingtarget_heighttarget_widthr   r   r   )	r   r   r   r   r   r   r   r   r   s	           r   r   zPadImages.__init__  sS     	&(,**("::;Gr   c           
          t        || j                  | j                  | j                  | j                  | j
                  | j                  | j                        S r   )_pad_imagesr   r   r   r   r   r   r   r   s     r   r    zPadImages.call0  sQ    	
 		
r   c                 z   t        |j                        }| j                  dk(  rd\  }}||   ||   }}nd\  }}||   ||   }}| j                  }||| j                  |z   | j
                  z   }| j                  }||| j                  |z   | j                  z   }|||<   |||<   t        ||j                        S )Nr&   r`   rb   r*   )r-   r+   r   r   r   r   r   r   r   r   r,   	r   r   r0   rc   rd   heightwidthr   r   s	            r   r1   zPadImages.compute_output_spec<  s    FLL).&,#K(5|J7OEF&,#K(5|J7OEF** V%7 ,,v58K8KKM((E$5,,u4t7I7IIL$1[!#/Z V\\BBr   NNNNNNNr2   r7   s   @r   r   r     s+     H&

Cr   r   zkeras.ops.image.pad_imagesc           
      ~    t        | f      r t        |||||||      j                  |       S t        | |||||||      S )a  Pad `images` with zeros to the specified `height` and `width`.

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        top_padding: Number of rows of zeros to add on top.
        left_padding: Number of columns of zeros to add on the left.
        bottom_padding: Number of rows of zeros to add at the bottom.
        right_padding: Number of columns of zeros to add on the right.
        target_height: Height of output images.
        target_width: Width of output images.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Padded image or batch of images.

    Example:

    >>> images = np.random.random((15, 25, 3))
    >>> padded_images = keras.ops.image.pad_images(
    ...     images, 2, 3, target_height=20, target_width=30
    ... )
    >>> padded_images.shape
    (20, 30, 3)

    >>> batch_images = np.random.random((2, 15, 25, 3))
    >>> padded_batch = keras.ops.image.pad_images(
    ...     batch_images, 2, 3, target_height=20, target_width=30
    ... )
    >>> padded_batch.shape
    (2, 20, 30, 3))r   r   r9   r   )r   r   r   r   r   r   r   r   s           r   
pad_imagesr   R  sd    ` VI&
 -
	  	 	r   c                 6   t        j                  |      }t        j                  |       } t        j                  |       }t        |      dvrt        d|       |||gj                  d       dk7  rt        d| d| d|       |||gj                  d       dk7  rt        d| d| d	|       t        |      d
k(  rdnd}	|dk(  r|d   |d   }}
n
|d   |d   }}
|||z
  |
z
  }|||z
  |
z
  }|||z
  |z
  }|||z
  |z
  }|dk  rt        d|       |dk  rt        d|       |dk  rt        d|       |dk  rt        d|       ||g||gg}|dk(  r	|ddggz   }nddgg|z   }|	rddgg|z   }t         j                  j                  | |      }|S )Nr"   VInvalid shape for argument `images`: it must have rank 3 or 4. Received: images.shape=r'   z^Must specify exactly two of top_padding, bottom_padding, target_height. Received: top_padding=z, bottom_padding=, target_height=z^Must specify exactly two of left_padding, right_padding, target_width. Received: left_padding=z, right_padding=, target_width=r#   FTr&   r)   ra   r(   r   z0top_padding must be >= 0. Received: top_padding=z2left_padding must be >= 0. Received: left_padding=z4right_padding must be >= 0. Received: right_padding=z6bottom_padding must be >= 0. Received: bottom_padding=)
r   r   convert_to_tensorr   r+   r.   r/   countr   pad)r   r   r   r   r   r   r   r   r0   is_batchr   r   	pad_widthpadded_imagess                 r   r   r     s    11+>K&&v.F99V$L <&&&2^5
 	

 	^]399$?1D%%0M 2,- .*O	-
 	
 	m\288>!C&&2^ 4*O ,(>	+
 	
 L)Q.uDHo%$R(,r*:$R(,r*: #n4v=&4v=#m3e;$|3e;Q>{mL
 	
 a@O
 	
 q''4o7
 	
 ((6'79
 	
 ~.}0MNIo%!Q(	VHy(	VHy(	MM%%fi8Mr   c                   .     e Zd Z	 d fd	Zd Zd Z xZS )
CropImagesc                     t         |           || _        || _        || _        || _        || _        || _        t        j                  |      | _
        y r   )r   r   top_croppingbottom_croppingleft_croppingright_croppingr   r   r   r   r   )	r   r   r   r   r   r   r   r   r   s	           r   r   zCropImages.__init__  sS     	(.*,*("::;Gr   c           
          t        || j                  | j                  | j                  | j                  | j
                  | j                  | j                        S r   )_crop_imagesr   r   r   r   r   r   r   r   s     r   r    zCropImages.call  sQ      	
 		
r   c                    t        |j                        }| j                  dk(  rd\  }}nd\  }}||   ||   }}|'| j                  t	        d| d| j                         |'| j
                  t	        d| d| j
                         | j                  }||| j                  z
  | j                  z
  }| j
                  }||| j                  z
  | j                  z
  }|||<   |||<   t        ||j                        S )	Nr&   r`   rb   zbWhen the height of the images is unknown, `target_height` must be specified.Received images.shape=z and target_height=z`When the width of the images is unknown, `target_width` must be specified.Received images.shape=z and target_width=r*   )r-   r+   r   r   r/   r   r   r   r   r   r   r,   r   s	            r   r1   zCropImages.compute_output_spec  s@   FLL).&,#K&,#K$[1<
3K>d008))5 7!!%!3!3 46  =T..6))5 7  $ 1 124  ** "T%6%669M9MMM(( 4#5#558K8KKL$1[!#/Z V\\BBr   r   r2   r7   s   @r   r   r     s     H&

!Cr   r   zkeras.ops.image.crop_imagesc           
      ~    t        | f      r t        |||||||      j                  |       S t        | |||||||      S )a  Crop `images` to a specified `height` and `width`.

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        top_cropping: Number of columns to crop from the top.
        left_cropping: Number of columns to crop from the left.
        bottom_cropping: Number of columns to crop from the bottom.
        right_cropping: Number of columns to crop from the right.
        target_height: Height of the output images.
        target_width: Width of the output images.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Cropped image or batch of images.

    Example:

    >>> images = np.reshape(np.arange(1, 28, dtype="float32"), [3, 3, 3])
    >>> images[:,:,0] # print the first channel of the images
    array([[ 1.,  4.,  7.],
           [10., 13., 16.],
           [19., 22., 25.]], dtype=float32)
    >>> cropped_images = keras.image.crop_images(images, 0, 0, 2, 2)
    >>> cropped_images[:,:,0] # print the first channel of the cropped images
    array([[ 1.,  4.],
           [10., 13.]], dtype=float32))r   r   r9   r   )r   r   r   r   r   r   r   r   s           r   crop_imagesr   3  sd    X VI&
 -
	  	 	r   c                 Z   t        j                  |      }t        j                  |       } t        j                  |       }t        |      dvrt        d|       |||gj                  d       dk7  rt        d| d| d|       |||gj                  d       dk7  rt        d| d| d	|       t        |      d
k(  rdnd}	|dk(  r|d   |d   }}
|d   }n|d   |d   }}
|d   }||
|z
  |z
  }||
|z
  |z
  }|||z
  |z
  }|||z
  |z
  }|dk  rt        d|       |dk  rt        d|       |dk  rt        d|       |dk  rt        d|       ||g}||g}|dk(  r|dgz   }||gz   }ndg|z   }|g|z   }|	r|d   }dg|z   }|g|z   }t        j                  | ||      }|S )Nr"   r   r'   zaMust specify exactly two of top_cropping, bottom_cropping, target_height. Received: top_cropping=z, bottom_cropping=r   zaMust specify exactly two of left_cropping, right_cropping, target_width. Received: left_cropping=z, right_cropping=r   r#   FTr&   r)   ra   r(   r   z2top_cropping must be >= 0. Received: top_cropping=z4target_height must be >= 0. Received: target_height=z4left_cropping must be >= 0. Received: left_cropping=z2target_width must be >= 0. Received: target_width=)	r   r   r   r   r+   r.   r/   r   slice)r   r   r   r   r   r   r   r   r0   r   r   r   channelsstart_indicesr+   
batch_sizecropped_imagess                    r   r   r   v  s    11+>K&&v.F99V$L <&&&2^5
 	

 	o}5;;DAQF&&2^ 4./ 0*O	-
 	
 	~|4::4@AE''4o 6,- .(>	+
 	
 L)Q.uDHo%$R(,r*:#$R(,r*:# -?0<?,~=~-=a@O
 	
 q''4o7
 	
 q''4o7
 	
 a@O
 	

 "=1ML)Eo%%+
"m+
U"!!_
m+u$YYv}e<Nr   c                   2     e Zd Z	 	 	 d fd	Zd Zd Z xZS )PerspectiveTransformc                 r    t         |           || _        || _        t	        j
                  |      | _        y r   )r   r   rU   rZ   r   r   r   )r   rU   rZ   r   r   s       r   r   zPerspectiveTransform.__init__  s2     	*$"::;Gr   c                     t         j                  j                  |||| j                  | j                  | j
                        S )NrU   rZ   r   )r   r   perspective_transformrU   rZ   r   r   r   start_points
end_pointss       r   r    zPerspectiveTransform.call  s@    }}22,,(( 3 
 	
r   c                    t        |j                        dvrt        d|j                         |j                  dd  dk7  s|j                  dvrt        d|j                         |j                  dd  dk7  s|j                  dvrt        d|j                         |j                  |j                  k7  r%t        d|j                   d	|j                         t	        |j                  |j
                  
      S )Nr"   r_   ra   )r$   rj   )rj   r#   zfInvalid start_points shape: expected (4,2) for a single image or (N,4,2) for a batch. Received shape: zdInvalid end_points shape: expected (4,2) for a single image or (N,4,2) for a batch. Received shape: zRstart_points and end_points must have the same shape. Received start_points.shape=z, end_points.shape=r{   )r.   r+   r/   ndimr   r,   r   s       r   r1   z(PerspectiveTransform.compute_output_spec  s#   v||F*  &~/ 
 bc"f,0A0A0O<<H<N<N;OQ  BC F*jooV.K<<F<L<L;MO  !1!1100<0B0B/C D$$.$4$4#57 
 6<<v||<<r   rf   r   Nr2   r7   s   @r   r   r     s     !		H
=r   r   z%keras.ops.image.perspective_transformc                     t        | ||f      rt        |||      j                  | ||      S t        j                  j                  | |||||      S )a
  Applies a perspective transformation to the image(s).

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        start_points: A tensor of shape `(N, 4, 2)` or `(4, 2)`,
            representing the source points in the original image
            that define the transformation.
        end_points: A tensor of shape `(N, 4, 2)` or `(4, 2)`,
            representing the target points in the output image
            after transformation.
        interpolation: Interpolation method. Available methods are `"nearest"`,
            and `"bilinear"`. Defaults to `"bilinear"`.
        fill_value: Value used for points outside the boundaries of the input if
            extrapolation is needed. Defaults to `0`.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Applied perspective transform image or batch of images.

    Examples:

    >>> x = np.random.random((2, 64, 80, 3))  # batch of 2 RGB images
    >>> start_points = np.array(
    ...     [
    ...         [[0, 0], [0, 64], [80, 0], [80, 64]],
    ...         [[0, 0], [0, 64], [80, 0], [80, 64]],
    ...     ]
    ... )
    >>> end_points = np.array(
    ...     [
    ...         [[3, 5], [7, 64], [76, -10], [84, 61]],
    ...         [[8, 10], [10, 61], [65, 3], [88, 43]],
    ...     ]
    ... )
    >>> y = keras.ops.image.perspective_transform(x, start_points, end_points)
    >>> y.shape
    (2, 64, 80, 3)

    >>> x = np.random.random((64, 80, 3))  # single RGB image
    >>> start_points = np.array([[0, 0], [0, 64], [80, 0], [80, 64]])
    >>> end_points = np.array([[3, 5], [7, 64], [76, -10], [84, 61]])
    >>> y = keras.ops.image.perspective_transform(x, start_points, end_points)
    >>> y.shape
    (64, 80, 3)

    >>> x = np.random.random((2, 3, 64, 80))  # batch of 2 RGB images
    >>> start_points = np.array(
    ...     [
    ...         [[0, 0], [0, 64], [80, 0], [80, 64]],
    ...         [[0, 0], [0, 64], [80, 0], [80, 64]],
    ...     ]
    ... )
    >>> end_points = np.array(
    ...     [
    ...         [[3, 5], [7, 64], [76, -10], [84, 61]],
    ...         [[8, 10], [10, 61], [65, 3], [88, 43]],
    ...     ]
    ... )
    >>> y = keras.ops.image.perspective_transform(
    ...     x, start_points, end_points, data_format="channels_first"
    ... )
    >>> y.shape
    (2, 3, 64, 80)
    r   )r   r   r9   r   r   r   )r   r   r   rU   rZ   r   s         r   r   r     sj    ^ V\:>?#'!#
 -j
9		:
 ==..# /  r   c                   2     e Zd Z	 	 	 d fd	Zd Zd Z xZS )GaussianBlurc                 r    t         |           || _        || _        t	        j
                  |      | _        y r   )r   r   r   sigmar   r   r   )r   r   r   r   r   s       r   r   zGaussianBlur.__init__c  s2     	&
"::;Gr   c                     t         j                  j                  || j                  | j                  | j
                        S )Nr   r   r   )r   r   gaussian_blurr   r   r   r   s     r   r    zGaussianBlur.calln  s:    }}**((**((	 + 
 	
r   c                     t        |j                        dvrt        d|j                         t        |j                  |j                        S Nr"   r_   r{   r|   r   s     r   r1   z GaussianBlur.compute_output_specv  J    v||F*  &~/ 
 6<<v||<<r   )r#   r#   )      ?r   Nr2   r7   s   @r   r   r   b  s     		H
=r   r   zkeras.ops.image.gaussian_blurc                     t        | f      rt        |||      j                  |       S t        j                  j                  | |||      S )an  Applies a Gaussian blur to the image(s).

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        kernel_size: A tuple of two integers, specifying the height and width
            of the Gaussian kernel.
        sigma: A tuple of two floats, specifying the standard deviation of
            the Gaussian kernel along height and width.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Blurred image or batch of images.

    Examples:

    >>> x = np.random.random((2, 64, 80, 3))  # batch of 2 RGB images
    >>> y = keras.ops.image.gaussian_blur(x)
    >>> y.shape
    (2, 64, 80, 3)

    >>> x = np.random.random((64, 80, 3))  # single RGB image
    >>> y = keras.ops.image.gaussian_blur(x)
    >>> y.shape
    (64, 80, 3)

    >>> x = np.random.random((2, 3, 64, 80))  # batch of 2 RGB images
    >>> y = keras.ops.image.gaussian_blur(
    ...     x, data_format="channels_first")
    >>> y.shape
    (2, 3, 64, 80)
    r   )r   r   r9   r   r   r   )r   r   r   r   s       r   r   r     s[    R VI&##
 -
		 
 ==&&	 '  r   c                   :     e Zd Z	 	 	 	 	 	 	 d fd	Zd Zd Z xZS )ElasticTransformc                     t         |           || _        || _        || _        || _        || _        || _        t        j                  |      | _
        y r   )r   r   alphar   rU   rY   rZ   seedr   r   r   )	r   r   r   rU   rY   rZ   r   r   r   s	           r   r   zElasticTransform.__init__  sN     	

*"$	"::;Gr   c           
          t         j                  j                  || j                  | j                  | j
                  | j                  | j                  | j                  | j                        S )Nr   r   rU   rY   rZ   r   r   )
r   r   elastic_transformr   r   rU   rY   rZ   r   r   r   s     r   r    zElasticTransform.call  sV    }}..****,,nn(( / 	
 		
r   c                     t        |j                        dvrt        d|j                         t        |j                  |j                        S r   r|   r   s     r   r1   z$ElasticTransform.compute_output_spec  r   r   g      4@g      @rf   reflectrh   NNr2   r7   s   @r   r   r     s*      H&

=r   r   z!keras.ops.image.elastic_transformc           
          t        | f      r!t        |||||||      j                  |       S t        j                  j                  | |||||||      S )a	  Applies elastic deformation to the image(s).

    Args:
        images: Input image or batch of images. Must be 3D or 4D.
        alpha: Scaling factor that controls the intensity of the deformation.
        sigma: Standard deviation of the Gaussian filter used for
            smoothing the displacement fields.
        interpolation: Interpolation method. Available methods are `"nearest"`,
            and `"bilinear"`. Defaults to `"bilinear"`.
        fill_mode: Points outside the boundaries of the input are filled
            according to the given mode. Available methods are `"constant"`,
            `"nearest"`, `"wrap"` and `"reflect"`. Defaults to `"constant"`.
            - `"reflect"`: `(d c b a | a b c d | d c b a)`
                The input is extended by reflecting about the edge of the last
                pixel.
            - `"constant"`: `(k k k k | a b c d | k k k k)`
                The input is extended by filling all values beyond
                the edge with the same constant value k specified by
                `fill_value`.
            - `"wrap"`: `(a b c d | a b c d | a b c d)`
                The input is extended by wrapping around to the opposite edge.
            - `"nearest"`: `(a a a a | a b c d | d d d d)`
                The input is extended by the nearest pixel.
        fill_value: Value used for points outside the boundaries of the input if
            `fill_mode="constant"`. Defaults to `0`.
        data_format: A string specifying the data format of the input tensor.
            It can be either `"channels_last"` or `"channels_first"`.
            `"channels_last"` corresponds to inputs with shape
            `(batch, height, width, channels)`, while `"channels_first"`
            corresponds to inputs with shape `(batch, channels, height, width)`.
            If not specified, the value will default to
            `keras.config.image_data_format`.

    Returns:
        Transformed image or batch of images with elastic deformation.

    Examples:

    >>> x = np.random.random((2, 64, 80, 3))  # batch of 2 RGB images
    >>> y = keras.ops.image.elastic_transform(x)
    >>> y.shape
    (2, 64, 80, 3)

    >>> x = np.random.random((64, 80, 3))  # single RGB image
    >>> y = keras.ops.image.elastic_transform(x)
    >>> y.shape
    (64, 80, 3)

    >>> x = np.random.random((2, 3, 64, 80))  # batch of 2 RGB images
    >>> y = keras.ops.image.elastic_transform(
    ...     x, data_format="channels_first")
    >>> y.shape
    (2, 3, 64, 80)
    r   )r   r   r9   r   r   r   )r   r   r   rU   rY   rZ   r   r   s           r   r   r     ss    B VI&'!#
 -
	  ==**# + 	 	r   r   re   r}   r   r   r   r   r   r  )(	keras.srcr   r   keras.src.api_exportr   keras.src.backendr   r   keras.src.ops.operationr   keras.src.ops.operation_utilsr	   r   r   r<   r?   rJ   rM   rQ   rl   r]   rs   rw   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r   r   <module>r     s     - ) 2 - CCY C2 01(K 2(KVCy C2 *++E ,+E\Cy C2 *+(E ,(EV1CY 1Ch &' f (fX 4>%=i %=P 01 ` 2`F4@Y 4@n /0 7 17z +\FY F@ /0AB3 13l3C	 3Cl *+ C ,C\ SlAC ACH +, ? -?T Zz-=9 -=` 56
 [ 7[|=9 =< -.>B3 /3l'=y '=T 12 
	S 3Sr   