Get color from grayscale

Post your questions regarding programming in C# in here.
10 posts Page 1 of 1
Contributors
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Get color from grayscale
MrAksel
I'm making this image editing library, and I thought of making an "anti grayscale" filter. The problem is, I just can't find the right formula to get the red, green and blue values from the gray value. Lets say I have the color R=128, G=191, B = 191. To get the gray value, I use the following function:
Code: Select all
byte grayScale = (byte)((b * 0.114) +
                        (g * 0.587) +
                        (r * 0.299));
Since this is basic math, there should be a somewhat easy way to reverse it. Though my math grades are good :D, I have failed in every attempt to calculate the red, green and blue components from the value that is returned by the grayscale function, in this case 172.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

Re: Get color from grayscale
Cheatmasterbw
You can't do it easily...
Basically grayscale is averaging the RGB values of each pixel.

say the original RGB values of the pixel were 126, 127, 128. This in grayscale is 127, 127, 127. There are also many other pixel colors that can average to 127, for example, 27, 127, 227. These two colors are completely different, even though they average to the same grayscale color.

The method you are using is similar to averaging the values together, so this applies o what you are doing.

So basically, this can't be (easily) done.
http://www.megaapps.tk/
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Get color from grayscale
MrAksel
My method isn't averaging. Average would be (R + G + B) / 3. I have (R * 0.299 + G * 0.587 + B * 0.114) which makes the result the way the human eye would see it, so I thought I could use those constants to convert the grayscale value back to its original colors.
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

Re: Get color from grayscale
Cheatmasterbw
MrAksel wrote:
My method isn't averaging. Average would be (R + G + B) / 3. I have (R * 0.299 + G * 0.587 + B * 0.114) which makes the result the way the human eye would see it, so I thought I could use those constants to convert the grayscale value back to its original colors.
I know you are using a different grayscaling method, but the concept is the same.
http://www.megaapps.tk/
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Get color from grayscale
comathi
Well, seeing as you are multiplying it by those values to get the grayscale, wouldn't it be logical that you divide your grayscale by those same values? It would cancel the math operation, and theoretically restore the color to it's original tone.
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

Re: Get color from grayscale
Cheatmasterbw
comathi wrote:
Well, seeing as you are multiplying it by those values to get the grayscale, wouldn't it be logical that you divide your grayscale by those same values? It would cancel the math operation, and theoretically restore the color to it's original tone.
The problem with that is that you don't know what to divide by.
http://www.megaapps.tk/
User avatar
Codex
Coding God
Coding God
Posts: 2028
Joined: Wed Mar 31, 2010 5:50 pm

Re: Get color from grayscale
Codex
We shall let the revolution begin.. the revolution for freedom, freedom against censorship. We shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Get color from grayscale
MrAksel
Im trying to restore the original colors Codex, not making a Color from a grayscale value (Color.FromArgb(gray, gray, gray)).
The problem is where I add the three values together to make the gray. That is ruining my possibilities to restore the colors. You are right Cheatmasterbw :(
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
User avatar
Cheatmasterbw
Coding God
Coding God
Posts: 1506
Joined: Fri Jan 01, 2010 2:30 pm

Re: Get color from grayscale
Cheatmasterbw
If you have 2 of the 3 original RGB values, you can find out the third. You can probably make it so the user chooses 2 of the original values of each pixel, and then paint over part of the image. The user could adjust the 2 values until the part of the image looks right.

Edit:
like this
http://planetphotoshop.com/colorizing-a ... image.html
Last edited by Cheatmasterbw on Mon Apr 23, 2012 9:11 pm, edited 1 time in total.
http://www.megaapps.tk/
User avatar
MrAksel
C# Coder
C# Coder
Posts: 1758
Joined: Fri Mar 26, 2010 12:27 pm

Re: Get color from grayscale
MrAksel
That looks pretty good, thanks. Ill probably implement a "key color" that will fill the grayscale image as in the PS tutorial :)
LMAOSHMSFOAIDMT
Laughing my a** of so hard my sombrero fell off and I dropped my taco lmao;


Over 30 projects with source code!
Please give reputation to helpful members!

Image
Image
10 posts Page 1 of 1
Return to “General coding help”