View source on GitHub |
Convert RGB images to grayscale.
tf.keras.ops.image.rgb_to_grayscale(
image, data_format='channels_last'
)
This function converts RGB images to grayscale images. It supports both 3D and 4D tensors, where the last dimension represents channels.
Returns | |
---|---|
Grayscale image or batch of grayscale images. |
Examples:
import numpy as np
from keras.src 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)