Keras 3 API 文件 / 運算 API / 圖像運算

圖像運算

[原始碼]

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

參數

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

[原始碼]

hsv_to_rgb 函數

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

將 HSV 圖像轉換為 RGB。

images 必須為浮點數 dtype,並且只有當 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 函數不同,因為我們修復了一個突出的錯誤 scipy/issues/2640

參數

  • inputs:輸入陣列。
  • coordinates:輸入被評估的座標。
  • order:樣條插值的階數。階數必須為 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 用零填充到指定的高度和寬度。

參數

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

[原始碼]

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 必須為浮點數 dtype,並且只有當 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)