affine_transform
函数keras.ops.image.affine_transform(
images,
transform,
interpolation="bilinear",
fill_mode="constant",
fill_value=0,
data_format=None,
)
将给定的变换应用于图像。
参数
[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
。与将输入点映射到输出点的变换相比,该变换是反转的。请注意,梯度不会反向传播到变换参数中。请注意,c0
和 c1
仅在使用 TensorFlow 后端时有效,并且在使用其他后端时将被视为 0
。"nearest"
和 "bilinear"
。默认为 "bilinear"
。"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_mode="constant"
,则用于输入边界之外的点的值。默认为 0
。"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
裁剪为指定的 height
和 width
。
参数
"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
)
从图像中提取补丁。
参数
None
,则默认为与 size
相同的值。strides > 1
与 dilation_rate > 1
一起使用"same"
或 "valid"
。"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)
hsv_to_rgb
函数keras.ops.image.hsv_to_rgb(images, data_format=None)
将 HSV 图像转换为 RGB。
images
必须为浮点 dtype,并且仅当 images
中的值在 [0, 1]
中时,输出才明确。
参数
"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 函数不同,因为我们修复了一个未解决的错误 scipy/issues/2640。
参数
0
或 1
。 0
表示最近邻,1
表示线性插值。"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_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
用零填充到指定的 height
和 width
。
参数
"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)
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,
)
使用指定的插值方法将图像调整为大小。
参数
(height, width)
。"nearest"
、"bilinear"
和 "bicubic"
。默认为 "bilinear"
。False
。True
,则在不失真纵横比的情况下调整图像大小。当原始纵横比与目标纵横比不同时,输出图像将被裁剪,以便返回与目标纵横比匹配的图像中最大可能的窗口(大小为 (height, width)
)。默认情况下(crop_to_aspect_ratio=False
),纵横比可能不会保留。True
,则在不失真纵横比的情况下填充图像。当原始纵横比与目标纵横比不同时,输出图像将在短边上均匀填充。pad_to_aspect_ratio=True
时,填充区域将根据给定的模式填充。此时仅支持 "constant"
(用等于 fill_value
的常数值填充)。pad_to_aspect_ratio=True
时使用的填充值。"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
必须为浮点 dtype,并且仅当 images
中的值在 [0, 1]
中时,输出才明确。
所有 HSV 值都在 [0, 1]
中。色调 0
对应于纯红色,1/3
是纯绿色,2/3
是纯蓝色。
参数
"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 张量。
参数
"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)