简体   繁体   English

有没有办法在不更改图像的情况下将灰度图像转换为RGB图像?

[英]Is there a way to convert a grayscale image to an RGB image without altering the image?

I am trying to train a resnet50 model with EMNIST data which is a dataset containing 300k images of letters and numbers. 我正在尝试使用EMNIST数据训练一个resnet50模型,该模型是一个包含300k字母和数字图像的数据集。 Resnet50 requires 3 dimensional images as its input and not grayscale so i tried to convert all the grayscale images to RGB but it isnt working like I want it too. Resnet50需要3维图像作为其输入,而不是灰度图像,因此我尝试将所有灰度图像都转换为RGB,但它却不能像我想要的那样工作。 When i view them using pyplot.imshow, the RGB image is really different from the grayscale one which is queer because these commands are actually just copy pasting the same grayscale matrix in 3 dimensions. 当我使用pyplot.imshow查看它们时,RGB图像确实与灰度图像不同,这是奇怪的,因为这些命令实际上只是在3维上复制粘贴相同的灰度矩阵。

The 3 commands which I have tried are given below: 下面是我尝试过的3个命令:

> resizedImageRGB = cv2.cvtColor(resizedImage,cv2.COLOR_GRAY2RGB)
> resizedImageRGB = np.repeat(resizedImage[:,:,np.newaxis],3,-1) arr =
> np.expand_dims(resizedImage, axis=2) resizedImageRGB =
> np.concatenate((arr,arr,arr), axis=2)

The grayscale and RGB image of one of the letters are given respectively: 字母之一的灰度和RGB图像分别给出:

GrayScale Image 灰度图像

RGB Image RGB图像

Going from Grayscale to an RGB approximation is hard mathematically. 从灰度到RGB近似在数学上很难。 Consider (one of) the formula(s) for going from RGB to grayvalue Y: 考虑从RGB到灰度值Y的公式(之一):

Y = 0.299R + 0.587G + 0.114B Y = 0.299R + 0.587G + 0.114B

Now you can imagine that going in the other direction, and attempting to derive R, G and B values from Y, well.. requires too much information (1 eq. 3 unknown). 现在您可以想象,朝另一个方向尝试从Y导出R,G和B值需要太多的信息(1个等式3个未知数)。 People actually use Neural Networks for this stuff.. 人们实际上将神经网络用于此类工作。

Rather, the right approach for you is to do it the other way around. 相反,适合您的方法是相反的方法。 That is, if you have access of only grayscale data (or 1-channeled data for that matter) you should modify your network such that it accepts the right input. 也就是说,如果您只能访问灰度数据(或与此相关的1通道数据),则应修改网络以使其接受正确的输入。

I'm not sure what libraries or exact code you are using from your question, but in general this shouldn't be too hard. 我不确定您要从问题中使用什么库或确切的代码,但是总的来说这应该不太难。

Usually the code that you find online has function that create these nets for you with the right input arguments supplied. 通常,您在网上找到的代码具有使用提供的正确输入参数为您创建这些网络的功能。

def ResNET(shape=(256,256,3), ...):
    some_code()

then you can usually just pass your own input: 那么您通常可以只传递自己的输入:

net = ResNET(shape=(256,256,1))

Hope this helps. 希望这可以帮助。

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

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