# Grayscale to RGB

• 01-27-2011, 08:31 AM
unexpert
Grayscale to RGB
Is there a way to convert an 8bit grayscale image to RGB?
I've read many examples that converts RGB to grayscale. But I still didn't manage to do it the other way round.

Hope I could get some help here.
• 01-27-2011, 08:33 AM
JosAH
Quote:

Originally Posted by unexpert
Is there a way to convert an 8bit grayscale image to RGB?
I've read many examples that converts RGB to grayscale. But I still didn't manage to do it the other way round.

Hope I could get some help here.

It can't be done; mapping from the three values r, g and b to a gray level, i.e. gray == f(r, g, b) is a surjection, i.e. many different values map to a same gray level so going the other way is not possible.

kind regards,

Jos
• 01-27-2011, 05:08 PM
unexpert
I thought it's possible cause my teacher said it can be done :confused:
Anyway, actually I just want to convert my grayscale level of 255 to red value. Still can't be done?
• 01-27-2011, 05:16 PM
JosAH
Quote:

Originally Posted by unexpert
I thought it's possible cause my teacher said it can be done :confused:
Anyway, actually I just want to convert my grayscale level of 255 to red value. Still can't be done?

Sure, you're talking "pseudo colours" here. You can convert a gray level of 255 (pure white) to a triple (r,g,b) == (255, 0, 0) (pure red). But my previous remark still stands: a mapping f(r, g, b) to a one dimensional number in the range [0, 255] is a surjection and there is no way to go the other way.

kind regards,

Jos
• 01-27-2011, 06:48 PM
unexpert
Oh, now I get ya! :)
But, how do I convert them? I think I need some examples :confused:
Also, do I need to include anything at the header?
• 01-27-2011, 06:54 PM
JosAH
Quote:

Originally Posted by unexpert
Oh, now I get ya! :)
But, how do I convert them? I think I need some examples :confused:
Also, do I need to include anything at the header?

If you have a gray scale value you can convert it to a colour as follows:

Code:

```int gray= ...; Color c; if (gray == 255)   c= new Color(255, 0, 0); // red, completely opague else   // you decide here```
kind regards,

Jos
• 01-27-2011, 07:12 PM
unexpert
Thank You Jos.
You've been a great help!
• 01-27-2011, 08:25 PM
JosAH
Quote:

Originally Posted by unexpert
Thank You Jos.
You've been a great help!

You're welcome of course; another small tip: use a gray level larger than a value M and transform that to a colour (r, g, b) == (gray, 0, 0). Otherwise you only see a few red spots in your processed image. Experiment with the value of M for best results.

kind regards,

Jos
• 01-28-2011, 07:03 PM
unexpert
And I thought I got this prob solved but actually, no. The color didn't change at all :(
So, I'm stuck now :eek:

edit:
hmm, correct me if im wrong but this line:
"c= new Color(255, 0, 0); // red, completely opague"
will not display anything right?
so, ill have to do this?
ip.putPixel(_ ,_ , _);
oh wait, but how do I make use of the line above because previously, my values are in this form (a,b). Now since its (a,b,c). How do I go about it? =x
• 01-28-2011, 08:03 PM
JosAH
Quote:

Originally Posted by unexpert
And I thought I got this prob solved but actually, no. The color didn't change at all :(
So, I'm stuck now :eek:

edit:
hmm, correct me if im wrong but this line:
"c= new Color(255, 0, 0); // red, completely opague"
will not display anything right?
so, ill have to do this?
ip.putPixel(_ ,_ , _);
oh wait, but how do I make use of the line above because previously, my values are in this form (a,b). Now since its (a,b,c). How do I go about it? =x

Yep, creating a new Color object doesn't change any image (and therefor any display) at all. Read the API documentation for the BufferedImage class. You create two of them, a gray scale image and an ARGB image (alpha, red, green, blue). You have to read the pixel values from your gray scale image, use my suggestion of a previous reply of mine and write a pixel in the ARGB image. When that's done you can draw the image or save it to disk (that's what the ImageIO class is for) or do anything you want with it.

kind regards,

Jos
• 01-29-2011, 07:02 AM
unexpert
is this the correct way to create the ARGB image?

Code:

```BufferedImage bimage = new BufferedImage(X, Y, BufferedImage.TYPE_INT_RGB); bimage = new BufferedImage(X, Y, BufferedImage.TYPE_INT_ARGB);```
From here on, how do I make use of the ARGB :confused:
• 01-29-2011, 08:41 AM
JosAH
Quote:

Originally Posted by unexpert
is this the correct way to create the ARGB image?

Code:

```BufferedImage bimage = new BufferedImage(X, Y, BufferedImage.TYPE_INT_RGB); bimage = new BufferedImage(X, Y, BufferedImage.TYPE_INT_ARGB);```
From here on, how do I make use of the ARGB :confused:

Why are you creating two images? One of type TYPE_INT_RGB and another one of type TYPE_INT_ARGB? You only need one colour image. This is just elementary Java. Have you read some tutorials covering this?

kind regards,

Jos
• 01-29-2011, 09:22 AM
unexpert
Oh, cause I thought I might use RGB instead og ARGB. I've read some but I'm not sure if what I'm reading is the correct one that might help me in this
• 01-29-2011, 11:15 AM
JosAH
Quote:

Originally Posted by unexpert
Oh, cause I thought I might use RGB instead og ARGB. I've read some but I'm not sure if what I'm reading is the correct one that might help me in this

The Sun Java Tutorials are always fine. Also, the Sun Java API Documentation is a must read. Bookmark those two links and read them before you attempt to program anything at all.

kind regards,

Jos
• 08-09-2011, 10:46 PM
sholzan
hello,

i was just searching for some ideas regarding grayscale to rgb images and manage to create this function, that creates an grayscale image (100px height) from an int (0-255) array :

dont know if thats what u looking for, but anyway..

Code:

```public static BufferedImage grayLineToBufferedImage(int[] grayLine) {                 BufferedImage image = new BufferedImage(grayLine.length, 100,                                 BufferedImage.TYPE_INT_RGB);                 for (int x = 0; x < grayLine.length; x++) {                         for (int y = 0; y < image.getHeight(); y++) {                                 Color color = new Color(grayLine[x], grayLine[x], grayLine[x]);                                 image.setRGB(x, y, color.getRGB());                         }                 }                 return image;         }```
regards

Sholzan
• 08-10-2011, 07:07 AM
DarrylBurke
Quote:

dont know if thats what u looking for