从灰度图像转换为 RGB 图像时,每个像素值都会重复。所以它看起来仍然像灰度图像。
我将简要地向您展示一个示例。
import cv2
import numpy as np
from matplotlib import pyplot as plt
image = np.random.rand(28, 28)
image = (image - image.min()) / (image.max() - image.min()) #to normalize
image = (image * 255).astype(np.uint8)
print(image[0][0]) #100
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
image[0][0] #[100, 100, 100]
归一化灰度图像的第一个像素值为100。
转换为 RGB 后的第一个值为 [100, 100, 100]。
添加使用 HSV 的示例
import cv2
import numpy as np
image = np.random.rand(28, 28)
image = (image - image.min()) / (image.max() - image.min()) #to normalize
image = image * 120 #you can modify range of H for HSV
image = np.expand_dims(image, 2)
sv = np.zeros([28, 28, 2])
sv[:, :, 0] = 200 #you can modify value of S for HSV
sv[:, :, 1] = 100 #you can modify value of V for HSV
hsv_image = np.concatenate([image, sv], 2).astype(np.uint8)
rgb_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2RGB)
然后你可以得到颜色从红色到蓝色的RGB图像。