简体   繁体   English

Numpy 2D 图像到 3D

[英]Numpy 2D image to 3D

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).我有一个灰度图像,表示为 2D numpy 数组,并希望将其设为 3D numpy 数组,表示彩色 rgb 图像(显然仍然是灰色的)。

 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我发现这个问题相反: numpy 3D-image array to 2D

You can use Numpy's dstack() to stack three grey images depthwise:您可以使用 Numpy 的dstack()深度堆叠三个灰色图像:

RGB = np.dstack((grey, grey, grey))

You could use an opencv function for this: image_color = cv2.cvtColor(image_gray, colorcode) .您可以为此使用 opencv function : 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请参阅文档https://docs.opencv.org/3.4/d8/d01/group__imgproc__color__conversions.ZFC35FDC70D5FC69D269883A822C7A中可用的颜色代码

The right one for you could be cv2.COLOR_GRAY2RGB .适合您的可能是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.这会给你灰度图像作为 3 通道 rgb 数组。

Using numpy you could look at https://stackoverflow.com/a/40119878/14997346使用 numpy 您可以查看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,:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM