affine_transform
functionkeras.ops.image.affine_transform(
image,
transform,
interpolation="bilinear",
fill_mode="constant",
fill_value=0,
data_format="channels_last",
)
Applies the given transform(s) to the image(s).
Arguments
[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."nearest"
,
and "bilinear"
. Defaults to "bilinear"
."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_mode="constant"
. Defaults to 0
."channels_last"
or "channels_first"
.
The ordering of the dimensions in the inputs. "channels_last"
corresponds to inputs with shape (batch, height, width, channels)
while "channels_first"
corresponds to inputs with shape
(batch, channels, height, weight)
. It defaults to the
image_data_format
value found in your Keras config file at
~/.keras/keras.json
. If you never set it, then it will be
"channels_last"
.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)
extract_patches
functionkeras.ops.image.extract_patches(
image,
size,
strides=None,
dilation_rate=1,
padding="valid",
data_format="channels_last",
)
Extracts patches from the image(s).
Arguments
None
, it defaults to the same value as size
.strides > 1
is not supported in
conjunction with dilation_rate > 1
"same"
or "valid"
."channels_last"
or "channels_first"
.
The ordering of the dimensions in the inputs. "channels_last"
corresponds to inputs with shape (batch, height, width, channels)
while "channels_first"
corresponds to inputs with shape
(batch, channels, height, weight)
. It defaults to the
image_data_format
value found in your Keras config file at
~/.keras/keras.json
. If you never set it, then it will be
"channels_last"
.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)
map_coordinates
functionkeras.ops.image.map_coordinates(
input, coordinates, order, fill_mode="constant", fill_value=0
)
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.
Arguments
0
or
1
. 0
indicates the nearest neighbor and 1
indicates the linear
interpolation."constant"
,
"nearest"
, "wrap"
and "mirror"
and "reflect"
. Defaults to
"constant"
."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
."nearest"
: (a a a a | a b c d | d d d d)
The input is extended by the nearest pixel."wrap"
: (a b c d | a b c d | a b c d)
The input is extended by wrapping around to the opposite edge."mirror"
: (c d c b | a b c d | c b a b)
The input is extended by mirroring about the edge."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.fill_mode="constant"
. Defaults to 0
.Returns
Output image or batch of images.
pad_images
functionkeras.ops.image.pad_images(
images,
top_padding=None,
left_padding=None,
target_height=None,
target_width=None,
bottom_padding=None,
right_padding=None,
)
Pad images
with zeros to the specified height
and width
.
Arguments
(batch, height, width, channels)
or 3D
Tensor of shape (height, width, channels)
.Returns
If images
were 4D, a 4D float Tensor of shape
(batch, target_height, target_width, channels)
If images
were 3D, a 3D float Tensor of shape
(target_height, target_width, channels)
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)
resize
functionkeras.ops.image.resize(
image,
size,
interpolation="bilinear",
antialias=False,
crop_to_aspect_ratio=False,
pad_to_aspect_ratio=False,
fill_mode="constant",
fill_value=0.0,
data_format="channels_last",
)
Resize images to size using the specified interpolation method.
Arguments
(height, width)
format."nearest"
,
"bilinear"
, and "bicubic"
. Defaults to "bilinear"
.False
.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.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.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
).pad_to_aspect_ratio=True
."channels_last"
or "channels_first"
.
The ordering of the dimensions in the inputs. "channels_last"
corresponds to inputs with shape (batch, height, width, channels)
while "channels_first"
corresponds to inputs with shape
(batch, channels, height, weight)
. It defaults to the
image_data_format
value found in your Keras config file at
~/.keras/keras.json
. If you never set it, then it will be
"channels_last"
.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)