def to_grayscale(image):
"""Converts a colored image to a grayscaled one.
Parameters
----------
image: ndarray()
An image with the shape of [height, width, 3].
Returns
---------
image: ndarray(uint8)
Returns a grayscaled image with shape [height, width, 1].
"""
img_channels = np.shape(image)[2]
if img_channels == 3:
image = cast(image)
image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
image = np.expand_dims(image, axis=2)
# introduce a 1-channel dimension to handle the indexing
# of color and gray images the same way
return image
评论列表
文章目录