Results 1 to 16 of 16
  1. #1
    unexpert is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default 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. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unexpert View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    unexpert is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Oh, thanks for replying.
    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. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unexpert View Post
    Oh, thanks for replying.
    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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    unexpert is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    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. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unexpert View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    unexpert is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    Thank You Jos.
    You've been a great help!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unexpert View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    unexpert is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    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 06:15 PM.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unexpert View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    unexpert is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    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. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unexpert View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    unexpert is offline Member
    Join Date
    Jan 2011
    Posts
    11
    Rep Power
    0

    Default

    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 08:25 AM.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unexpert View Post
    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
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    sholzan is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default

    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. #16
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default

    dont know if thats what u looking for
    Rather obviously, it wasn't. Please don't post to old dead threads.

    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
  •