[英]How to change image from gray scale format into RGB format
I was working on research about computer vision, but I got stuck when following a paper that said he could convert Grayscale image into RGB image using Python.我当时正在研究计算机视觉,但在遵循一篇说他可以使用 Python 将灰度图像转换为 RGB 图像的论文时,我陷入了困境。
The first image would be like this.第一张图片应该是这样的。
The output will be like this. output 将是这样的。
Is anyone could help me finish this problem?有谁能帮我解决这个问题吗?
I don't think this is possible, a grayscale image has just one channel (shades of gray), while, BGR (default color space in opencv, or BGRA if on mac)just like RGB has three channels (RED GREEN BLUE), you can convert it into grayscale using this formula:我不认为这是可能的,灰度图像只有一个通道(灰色阴影),而 BGR(opencv 中的默认颜色空间,如果在 Mac 上则为 BGRA)就像 RGB 有三个通道(红绿蓝),您可以使用以下公式将其转换为灰度:
Y = 0.299 R + 0.587 G + 0.114 B
Y = 0.299 R + 0.587 G + 0.114 B
where Y
is the one channel of grayscale image.其中
Y
是灰度图像的一个通道。 But you can't do the vice versa.但你不能反过来。 Grayscale is basically just different shades of gray.
灰度基本上只是不同的灰色阴影。 You can't do the reverse.
你不能反过来。 In RGB/BGR a pixel looks like this
[R,G,B]
which is somewhat like an array, whereas in grayscale it is just a single value.在 RGB/BGR 中,一个像素看起来像这样
[R,G,B]
,这有点像一个数组,而在灰度中它只是一个单一的值。 We often consider value < 100
as black in grayscale.我们通常将
value < 100
视为灰度中的黑色。
Image is often reprsented in form of an array ( np.array
in case of python), and the thing is, in most of the arrays, you can't modify the dimensions.图像通常以数组的形式表示(在 python 的情况下为
np.array
),问题是,在大多数 arrays 中,您无法修改尺寸。 Like np.array
has an attribute called shape
which tells us about the dimensions of the array, or in an image think of it like resolution.就像
np.array
有一个叫做shape
的属性,它告诉我们数组的维度,或者在图像中把它想象成分辨率。 Shape of a grayscale image looks like this (RESOLUTION-Y, RESOLUTION-X)
, which means it's a 2d matrix/array and one pixel is represented by a single value, whereas in RGB/BGR, the shape looks like this, (RESOLUTION-Y, RESOLUTION-X, 3)
, so it's a 3d array, where 3 is the number of channels.灰度图像的形状看起来像这样
(RESOLUTION-Y, RESOLUTION-X)
,这意味着它是一个二维矩阵/数组,一个像素由单个值表示,而在 RGB/BGR 中,形状看起来像这样, (RESOLUTION-Y, RESOLUTION-X, 3)
,所以它是一个 3d 数组,其中 3 是通道数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.