How to convert image from gray to RGB?

0

Converting an image into RGB to grayscale is relatively easy, just do a linear (or average) combination of the 3 channels. For example:

Gray = 0.3*R +0.59*G +0.11*B

By the above expression you get a matrix in shades of gray.

My question is: how do you get the R, G, and B matrices from the Gray matrix to get a color image from a grayscale?

    
asked by anonymous 13.02.2018 / 17:39

1 answer

0

As stated in the comments, you can not retrieve the original values only from the gray value.

What you should do is store both the gray and RGB values.

For example, you will have a Pixel class (pseudo code):

class Pixel {
  byte R
  byte G
  byte B
  byte Gray

  public Pixel(byte R, byte G, byte B) {
    this.R = R
    this.G = G
    this.B = B
    this.Gray = mean([R, G, B]) // Escala de cinza a partir da média dos valores RGB
  }
}

With this class you have all the necessary information to display the color or gray image. The program will use more memory, but it is the price you pay for keeping the original color information.

    
24.10.2018 / 22:48