2 回答

TA贡献1801条经验 获得超8个赞
由于您想以与之前的白色/灰色相同的强度更改为红色,为什么不将两个空白图像与其一起堆叠。
OpenCV 使用 BGR,所以我将使用 BGR 而不是 RGB,但如果需要 RGB,您可以更改它。
import numpy as np
#assuming img contains a grayscale image of size 28x28
b = np.zeros((28, 28), dtype=np.uint8)
g = np.zeros((28, 28), dtype=np.uint8)
res = cv2.merge((b, g, img))
cv2.imshow('Result', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
您可以使用此代码并查看。它应该工作。

TA贡献1829条经验 获得超7个赞
您可以将当前的灰度输入用作红色通道,并将所有绿色和蓝色通道设置为零。您可以切换它们以使数字具有蓝色或绿色。
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
(x, _), (_, _) = tf.keras.datasets.mnist.load_data()
x = x[0]
f = np.concatenate([x[..., None],
np.zeros((28, 28, 1)).astype(int),
np.zeros((28, 28, 1)).astype(int)], axis=-1)
plt.imshow(f)
添加回答
举报