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
。與將輸入點映射到輸出點的變換相比,此變換是反向的。請注意,梯度不會反向傳播到變換參數中。請注意,僅當使用 TensorFlow 後端時,c0
和 c1
才有效,並且當使用其他後端時,將被視為 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
裁剪為指定的高度和寬度。
參數
"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
用零填充到指定的高度和寬度。
參數
"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)