Home > Software engineering >  How to convert image (28,28,1) to (28,28,3) in numpy
How to convert image (28,28,1) to (28,28,3) in numpy

Time:08-05

I want to convert mnist dataset to (28, 28, 3) dimensions for fitting into the tf.keras.applications.MobileNetV2 model, but this model requires the (x, y, 3) dimensions.

enter image description here

The following code is trying to display (28,28,3) but it is NOT converted from (28,28,1):

y = np.arange(28*28*3).reshape(28,28,3)

plt.figure()
plt.imshow(y)
plt.title(y.shape)
plt.show()

enter image description here

How to convert the above (28,28,1) image to (28, 28, 3) and display in the matplotlib?

CodePudding user response:

You could use numpy's repeat function.

>>> x = np.ones((28, 28, 1))
>>> y = np.repeat(x, 3, axis=2)
>>> y.shape
(28, 28, 3)

enter image description here

You can see what is happening under the hood of tf.image.grayscale_to_rgb here.

CodePudding user response:

you can use

rgb_image = cv2.cvtColor(gray,cv2.COLOR_GRAY2RGB)

where gray is your gray image. You must import cv2

  • Related