RGB to Black&White

This is a discussion on RGB to Black&White within the C++ Programming forums, part of the General Programming Boards category; Hi. I have a little question: are there people who used CImg library to work with images? Namely, how to ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    11

    Question RGB to Black&White

    Hi.

    I have a little question: are there people who used CImg library to work with images? Namely, how to convert RGB-file (jpeg in my case) to black&white one?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,656
    1. Read the manual - The CImg Library - C++ Template Image Processing Toolkit
    2. scroll down to "Color bases"
    3. read/write the pixels to remove the hue (I guess) from each pixel value.

    The colorspace FAQ for what RGB, HSL etc all mean
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    A brute force approach is to average the RGB's and use the average as the final RGB components. However the correct approach is discussed in the links that have been posted already.

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Theres actually a scaling issue for the output to 'look right'. I forget the exact values but its like 60% red 30% green and 10% of the blue.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Guys, thanx a lot.

    2 abachler: Listen, can u remember exact values , plz ?
    Last edited by Rustik_; 08-18-2009 at 12:27 AM.

  6. #6
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Rustik_ View Post
    Guys, thanx a lot.

    2 abachler: Listen, can u remember exact values , plz ?
    Yeah i actually confused the red and green ratios

    Y = 0.299R + 0.587G + 0.114B

    29.9% red
    58.7% Green
    11.4% Blue
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by abachler View Post
    Yeah i actually confused the red and green ratios

    Y = 0.299R + 0.587G + 0.114B

    29.9% red
    58.7% Green
    11.4% Blue
    Interesting. I've always used the averaging technique. What colorspace is that related to?

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    Quote Originally Posted by abachler View Post
    Yeah i actually confused the red and green ratios

    Y = 0.299R + 0.587G + 0.114B

    29.9% red
    58.7% Green
    11.4% Blue
    thank u.

  9. #9
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Quote Originally Posted by Sebastiani View Post
    Interesting. I've always used the averaging technique. What colorspace is that related to?
    YUV, specifically its the luminosity channel. It's the colorspace used by B&W CCTV cameras
    Last edited by abachler; 08-18-2009 at 06:30 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by abachler View Post
    YUV, specifically its the luminosity channel. It's the colorspace used by B&W CCTV cameras
    Ah, I see. Thanks.

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    I would have posted the coefficients but I forgot them and was too lazy to look them up. Averaging works for most applications but if it's a paint program or something very color dependent then you probably would use the accepted formulas.

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by abachler View Post
    Yeah i actually confused the red and green ratios

    Y = 0.299R + 0.587G + 0.114B

    29.9% red
    58.7% Green
    11.4% Blue
    A few comments.

    1. The OP asked about black and white, and everybody is talking about grayscale. To get black and white, dithering will be required. There are multiple methods.

    2. The coefficients of the Y=F(R,G,B) luminosity equation are calibrated for RGB values which are gamma-corrected to a linear intensity profile. However, most RGB images are not so corrected, and actually represent device-dependent intensities which need gamma correction before the RGB perceptual coefficients can be applied. I would NOT get too hung up on the particular coefficients unless I knew that the image was already perceptually corrected. In fact, I would probably just take the arithmetic average and only use something more complicated if the situation requires.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  13. #13
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by abachler View Post
    YUV, specifically its the luminosity channel. It's the colorspace used by B&W CCTV cameras
    Why would a black and white video signal require a U or V channel?

    Having said that, Y is perceptual luminosity with a huge serving of caveats. The YUV and YCbCr color spaces share the same definition of Y, given that the underlying signal represents true radiant intensities. As I said in a previous comment, this is almost never the case since most people do not bother to link their device-dependent images to an appropriate ICC Profile, and even if they do, chances are that along the chain of image processing the profile information is lost or corrupted anyway.

    Just average the R,G,B components and call it good, unless you have a specific justification to do it otherwise.
    Last edited by brewbuck; 08-19-2009 at 10:16 PM.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    11
    2 brewbuck:
    So, u mean averaging the R,G,B components is a better way for obtaining b/w image, than working with coefficients?? *confused*

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Actually brew, those coefficients are specifically for conversion of a color NTSC signal to a B&W signal. YUV is the colorspace used to general the color channels in a way that is compatible with existing B&W sets. Its a legacy issue now, since most B&W sets are no longer in service.

    No, its not better to just average unless speed is a critical issue. Using the coefficients will produce the best results on 99% of the images you process.
    Last edited by abachler; 08-20-2009 at 12:57 AM.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting RGB to Hex.
    By mintsmike in forum C Programming
    Replies: 2
    Last Post: 03-23-2009, 12:22 PM
  2. OpenGL: Pixel addition
    By Hunter2 in forum Game Programming
    Replies: 4
    Last Post: 12-15-2008, 01:36 PM
  3. How to read out the RGB values of an image?
    By pjeremy in forum C Programming
    Replies: 5
    Last Post: 05-28-2006, 08:49 AM
  4. Stopwatch program (need help!)
    By modnar in forum C Programming
    Replies: 9
    Last Post: 03-21-2004, 11:42 PM
  5. Background color in RGB
    By bennyandthejets in forum Windows Programming
    Replies: 16
    Last Post: 06-20-2003, 05:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21