1. Member
Join Date
Jan 2011
Posts
11
Rep Power
0

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

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

3. Member
Join Date
Jan 2011
Posts
11
Rep Power
0
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?

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

5. Member
Join Date
Jan 2011
Posts
11
Rep Power
0
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?

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

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

Jos

7. Member
Join Date
Jan 2011
Posts
11
Rep Power
0
Thank You Jos.
You've been a great help!

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

9. Member
Join Date
Jan 2011
Posts
11
Rep Power
0
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
Last edited by unexpert; 01-28-2011 at 07:15 PM.

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

11. Member
Join Date
Jan 2011
Posts
11
Rep Power
0
is this the correct way to create the ARGB image?

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

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

Java 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

13. Member
Join Date
Jan 2011
Posts
11
Rep Power
0
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
Last edited by unexpert; 01-29-2011 at 09:25 AM.

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

15. Member
Join Date
Aug 2011
Posts
1
Rep Power
0
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..

Java 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

16. dont know if thats what u looking for

Closing.

db

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•