Keras 3 API文档 / Ops API / 图像 ops

图像 ops

[源代码]

affine_transform 函数

keras.ops.image.affine_transform(
    images,
    transform,
    interpolation="bilinear",
    fill_mode="constant",
    fill_value=0,
    data_format=None,
)

对图像应用给定的变换。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • transform: 射影变换矩阵/矩阵。长度为8的向量或大小为N x 8的张量。如果变换的一行是[a0, a1, a2, b0, b1, b2, c0, c1],则它将输出点(x, y)映射到变换后的输入点(x', y') = ((a0 x + a1 y + a2) / k, (b0 x + b1 y + b2) / k),其中k = c0 x + c1 y + 1。该变换与映射输入点到输出点的变换相反。请注意,梯度不会反向传播到变换参数中。请注意,c0c1仅在使用TensorFlow后端时有效,而在使用其他后端时将被视为0
  • interpolation: 插值方法。可用方法有"nearest""bilinear"。默认为"bilinear"
  • fill_mode: 输入边界之外的点根据给定的模式填充。可用方法有"constant""nearest""wrap""reflect"。默认为"constant"
    • "reflect": `(d c b a | a b c d | d c b a)` 输入通过围绕最后一个像素的边缘进行反射来扩展。
    • "constant": `(k k k k | a b c d | k k k k)` 输入通过用 `fill_value` 指定的相同常量值 k 填充边缘以外的所有值来扩展。
    • "wrap": `(a b c d | a b c d | a b c d)` 输入通过环绕到相对边缘进行扩展。
    • "nearest": (a a a a | a b c d | d d d d) 输入通过最近的像素进行扩展。
  • fill_value: 当fill_mode="constant"时,用于输入边界之外的点的数值。默认为0
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

应用仿射变换后的图像或图像批次。

示例

>>> 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)

[源代码]

crop_images 函数

keras.ops.image.crop_images(
    images,
    top_cropping=None,
    left_cropping=None,
    bottom_cropping=None,
    right_cropping=None,
    target_height=None,
    target_width=None,
    data_format=None,
)

images裁剪到指定的heightwidth

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • top_cropping: 从顶部裁剪的列数。
  • left_cropping: 从左侧裁剪的列数。
  • bottom_cropping: 从底部裁剪的列数。
  • right_cropping: 从右侧裁剪的列数。
  • target_height: 输出图像的高度。
  • target_width: 输出图像的宽度。
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

裁剪后的图像或图像批次。

示例

>>> 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)

[源代码]

extract_patches 函数

keras.ops.image.extract_patches(
    images, size, strides=None, dilation_rate=1, padding="valid", data_format=None
)

从图像中提取图像块。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • size: 图像块大小,可以是整数或元组 (patch_height, patch_width)
  • strides: 高度和宽度上的步幅。如果未指定或为None,则默认为与size相同的值。
  • dilation_rate: 这是输入步幅,指定两个连续图像块样本在输入中的间隔。对于大于1的值,步幅必须为1。注意:strides > 1不支持与dilation_rate > 1结合使用
  • padding: 要使用的填充算法类型:"same""valid"
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

提取的图像块,3D(如果未批量处理)或4D(如果批量处理)。

示例

>>> 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)

[源代码]

gaussian_blur 函数

keras.ops.image.gaussian_blur(
    images, kernel_size=(3, 3), sigma=(1.0, 1.0), data_format=None
)

对图像应用高斯模糊。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • kernel_size: 一个包含两个整数的元组,指定高斯核的高度和宽度。
  • sigma: 一个包含两个浮点数的元组,指定高斯核在高度和宽度上的标准差。
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

模糊后的图像或图像批次。

示例

>>> 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)

[源代码]

hsv_to_rgb 函数

keras.ops.image.hsv_to_rgb(images, data_format=None)

将HSV图像转换为RGB。

images 必须是浮点类型,并且只有当images中的值在[0, 1]范围内时,输出才有明确定义。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

RGB图像或RGB图像批次。

示例

>>> 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)

[源代码]

map_coordinates 函数

keras.ops.image.map_coordinates(
    inputs, coordinates, order, fill_mode="constant", fill_value=0
)

通过插值将输入数组映射到新坐标。

请注意,边界附近的插值与scipy函数不同,因为我们修复了一个已知的bug scipy/issues/2640

参数

  • inputs: 输入数组。
  • coordinates: 评估inputs时的坐标。
  • order: Spline插值的阶数。阶数必须是010表示最近邻插值,1表示线性插值。
  • fill_mode: 输入边界之外的点根据给定的模式填充。可用方法有"constant""nearest""wrap""mirror""reflect"。默认为"constant"
    • "constant": (k k k k | a b c d | k k k k) 输入通过用fill_value指定的相同常量值k填充所有边界外的区域来扩展。
    • "nearest": (a a a a | a b c d | d d d d) 输入通过最近的像素进行扩展。
    • "wrap": (a b c d | a b c d | a b c d) 输入通过在相反边缘进行环绕来扩展。
    • "mirror": (c d c b | a b c d | c b a b) 输入通过围绕边缘进行镜像来扩展。
    • "reflect": (d c b a | a b c d | d c b a) 输入通过围绕最后一个像素的边缘进行反射来扩展。
  • fill_value: 当fill_mode="constant"时,用于输入边界之外的点的数值。默认为0

返回

输出输入或输入的批次。


[源代码]

pad_images 函数

keras.ops.image.pad_images(
    images,
    top_padding=None,
    left_padding=None,
    bottom_padding=None,
    right_padding=None,
    target_height=None,
    target_width=None,
    data_format=None,
)

用零填充images,使其达到指定的heightwidth

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • top_padding: 在顶部添加的零行数。
  • left_padding: 在左侧添加的零列数。
  • bottom_padding: 在底部添加的零行数。
  • right_padding: 在右侧添加的零列数。
  • target_height: 输出图像的高度。
  • target_width: 输出图像的宽度。
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

填充后的图像或图像批次。

示例

>>> 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)

[源代码]

perspective_transform 函数

keras.ops.image.perspective_transform(
    images,
    start_points,
    end_points,
    interpolation="bilinear",
    fill_value=0,
    data_format=None,
)

对图像应用透视变换。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • start_points: 一个形状为(N, 4, 2)(4, 2)的张量,表示原始图像中定义变换的源点。
  • end_points: 一个形状为(N, 4, 2)(4, 2)的张量,表示变换后输出图像中的目标点。
  • interpolation: 插值方法。可用方法有"nearest""bilinear"。默认为"bilinear"
  • fill_value: 当需要外插时,用于输入边界之外的点的数值。默认为0
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

应用透视变换后的图像或图像批次。

示例

>>> 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)

[源代码]

resize 函数

keras.ops.image.resize(
    images,
    size,
    interpolation="bilinear",
    antialias=False,
    crop_to_aspect_ratio=False,
    pad_to_aspect_ratio=False,
    fill_mode="constant",
    fill_value=0.0,
    data_format=None,
)

使用指定的插值方法将图像缩放到指定大小。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • size: 输出图像的大小,格式为(height, width)
  • interpolation: 插值方法。可用方法有"nearest""bilinear""bicubic"。默认为"bilinear"
  • antialias:下采样图像时是否使用抗锯齿滤波器。默认为 `False`。
  • crop_to_aspect_ratio: 如果为 True,则在不扭曲纵横比的情况下重塑图像。当原始纵横比与目标纵横比不同时,将裁剪输出图像,以便返回图像中(大小为 (height, width))与目标纵横比匹配的最大可能窗口。默认情况下(crop_to_aspect_ratio=False),可能不保留纵横比。
  • pad_to_aspect_ratio: 如果为True,则在不失真的情况下填充图像。当原始宽高比与目标宽高比不同时,输出图像将在较短的边上均匀填充。
  • fill_mode: 当使用pad_to_aspect_ratio=True时,填充区域根据给定的模式填充。目前只支持"constant"(用等于fill_value的常量值填充)。
  • fill_value: 浮点数。当pad_to_aspect_ratio=True时使用的填充值。
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

缩放后的图像或图像批次。

示例

>>> 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)

[源代码]

rgb_to_hsv 函数

keras.ops.image.rgb_to_hsv(images, data_format=None)

将RGB图像转换为HSV。

images 必须是浮点类型,并且只有当images中的值在[0, 1]范围内时,输出才有明确定义。

所有HSV值都在[0, 1]范围内。色调为0对应纯红色,1/3对应纯绿色,2/3对应纯蓝色。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

HSV图像或HSV图像批次。

示例

>>> 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)

[源代码]

rgb_to_grayscale 函数

keras.ops.image.rgb_to_grayscale(images, data_format=None)

将RGB图像转换为灰度图。

此函数将RGB图像转换为灰度图像。它支持3D和4D张量。

参数

  • images: 输入图像或图像批次。必须是3D或4D。
  • data_format: 指定输入张量数据格式的字符串。它可以是"channels_last""channels_first""channels_last"对应于形状为(batch, height, width, channels)的输入,而"channels_first"对应于形状为(batch, channels, height, width)的输入。如果未指定,则值将默认为keras.config.image_data_format

返回

灰度图像或灰度图像批次。

示例

>>> 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)