I have a greyscale image represented as 2D numpy array and want to make it a 3D numpy array representing a color rgb image (which is obviously still grey).
img.shape // (100, 100) img[10, 10] // eg 42 // do something img.shape // (100, 100, 3) img[10, 10] // eg [42, 42, 42]
I found this question which asked the opposite: numpy 3D-image array to 2D
You can use Numpy's dstack()
to stack three grey images depthwise:
RGB = np.dstack((grey, grey, grey))
You could use an opencv function for this: image_color = cv2.cvtColor(image_gray, colorcode)
.
See the available colorcodes in documentation https://docs.opencv.org/3.4/d8/d01/group__imgproc__color__conversions.html
The right one for you could be cv2.COLOR_GRAY2RGB
.
import cv2
image_color = cv2.cvtColor(image_gray, cv2.COLOR_GRAY2RGB)
That would give you the grayscale image as 3 channel rgb array.
Using numpy you could look at https://stackoverflow.com/a/40119878/14997346
Does the following code help?
import numpy as np
aa = np.zeros((4,4),dtype='int64')
aa[1,1] = 42
aa = aa.reshape((4,4,1))
bb = np.broadcast_to(aa,(4,4,3))
print(bb[1,1,:])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.