How to convert gray image to RGB?

Asked

Viewed 947 times

4

To convert a RGB image 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 expression above you get a grayscale matrix.

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

  • 3

    It does not. The transformation is one-way

  • 4

    Basically opening in an image editing program and coloring "as per the taste"

  • 6

    To make it easier to understand the problem: 16 + 2 + 22 = 40, right? Starting from number 40, how could you know which three numbers were summed up? There is no way, because it has infinite combinations of three numbers that result in 40 when added up. The conversion to gray is almost the same thing (changes the formula a little, has a more defined numerical range, but that in nothing helps without an analysis of other factors)

  • Coloring once in a while is good for the mind. :)

2 answers

1

As stated in the comments, there is no way to recover the original values only from the gray value.

What you can do is store both the gray value and the RGB.

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 information necessary to display the color or gray image. The program will use more memory, but it is the price that is paid for keeping the original color information.

-1

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

Just make Gray=R=G=B that the image in point RGB(i,j,k) will have the 'same colors' of the gray image.

No Math/Octave:

pkg load image;
imagemGray = imread('gray.png'); %ler arquivo gray

for i = 1 : 3
 imagemRGB(:,:,i) = imagemGray(:,:);
endfor

Browser other questions tagged

You are not signed in. Login or sign up in order to post.