简体   繁体   English

如何将图像从灰度格式更改为 RGB 格式

[英]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 将是这样的。

图片_2

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.

相关问题 Python 将 3 通道 rgb 彩色图像更改为 1 通道灰色的速度有多快? - How fast in Python change 3 channel rgb color image to 1 channel gray? 如何使用 rgb 对 python 中的图片进行灰度化? - how to gray scale a picture in python using rgb? 无法使用 scikit 图像将 RGB 图像转换为灰度 - Not able to convert RGB image to gray scale using scikit image 我如何在 python 中使用 for 循环打印二维数组中的特定列和行,并且还想将灰色图像更改为 RGB - How I can print specific cols and row from 2d array using for loop in python and also want to change gray image to RGB 同一张图像的2个灰度图像有何不同? - How are the 2 gray scale images of the same image different? 从 RGB 中去除灰色 - Remove gray from RGB 如何在Python中转换此RGB格式 - How to Convert This RGB format in Python 在python中使用用户定义的转换功能将灰度图像转换回rgb图像时出现问题 - Problem in converting a gray scale image back to an rgb image with user defined transformation function in python 如何在python中检查我的图像是RGB格式还是BGR格式? 我如何转换它们,反之亦然? - How to check whether my image is RGB format or BGR format in python? How do i convert them and viceversa? 从原始 RGB 深度图像到灰度的错误转换 - Incorrect conversion from Raw RGB Depth image to gray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM