简体   繁体   中英

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. 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. 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.

The 3 commands which I have tried are given below:

> 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:

GrayScale Image

RGB Image

Going from Grayscale to an RGB approximation is hard mathematically. Consider (one of) the formula(s) for going from RGB to grayvalue Y:

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). 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.

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.

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.

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