How to convert existing image to black and white?

This is a discussion on How to convert existing image to black and white? within the C Programming forums, part of the General Programming Boards category; Hello, I am working on my function to convert to black and white. I was wondering how to start this? ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    32

    How to convert existing image to black and white?

    Hello, I am working on my function to convert to black and white. I was wondering how to start this?
    I know that when a image is black and white it has the same R,G,B values. So in a colored image (R,G,B) = (10,20,30), but in black in white it should be (10+20+30)/3 = 20, so Black and white image would have values of (20,20,20)

    So :

    R[x][y] = (R[i][j]+G[i][j]+B[i][j])/3
    G[x][y] = (R[i][j]+G[i][j]+B[i][j])/3
    B[x][y] = (R[i][j]+G[i][j]+B[i][j])/3

    My question is how do i start this? use a for loop like this?

    Code:
    for (y = 0; y < HEIGHT; y++)
    		for (x = 0; x < WIDTH; x++) {
    			R[x][y] = 
    			G[x][y] = 
    			B[x][y] =


    Instructions:


    Change a Color Image to Black and White
    A black and white image is the one that the intensity values are the same for all color channels, red, green, and
    blue, at each pixel. To change a color image to grey, assign a new intensity, which is given by (R+G+B)=3,
    to all the color channels at a pixel. The R; G;B are the old intensity values for the red, the green, and the
    blue channels at the pixel. You need to define and implement the following function to do the job.
    Code:
    /* change color image to black and white */
    void BlackNWhite(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT]);
    Last edited by kkmoslehpour; 10-13-2011 at 08:04 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You start it by writing out in words what the program expects of you. Then you turn those words into more words, that are the actual steps to do the job. Once you can explain it in words, and as words representing small steps, then you can think about how each one of those steps might be a line of code.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    I updated my question now does it make more sense? btw thanks quzah for all your help, i suck at this

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    color = R + G + B / 3
    R[x][y] = G[x][y] = B[x][y] = color;
    Something roughly like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    How about converting to Negative?

    1.3.4 Make a negative of an image
    A negative image is an image in which all the intensity values have been inverted. To achieve this, each
    intensity value at a pixel is subtracted from the maximum value, 255, and the result is assigned to the pixel
    as a new intensity. You need to defi ne and implement a function to do the job.
    You need to de fine and implement the following function to do this DIP.


    BTW quzah can i paypal you some money :P

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kkmoslehpour View Post
    How about converting to Negative?

    1.3.4 Make a negative of an image
    A negative image is an image in which all the intensity values have been inverted. To achieve this, each
    intensity value at a pixel is subtracted from the maximum value, 255, and the result is assigned to the pixel
    as a new intensity.
    Sounds pretty easy:
    Code:
    R[x][y] = 255 - R[x][y];
    Just like it says.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    Here is a question..

    R[x][y] = 255 - R[x][y];
    G[x][y] = 255 - G[x][y];
    B[x][y] = 255 - B[x][y];

    Don't i have to set a new set a variable on the left side? because this becomes a new picture with new inverted pixels?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That depends if you want to actually change your source, or just make a new one.
    Code:
    source[x][y] = 255 - source[x][y]; /* change self */
    newitem[x][y] = 255 - source[x][y]; /* make a new one based off of the source */
    You just have to decide what you really want.


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    But im saying does the height and width become a different variable or does RGB change
    for example, R[x][y] = R[i][j] ? and instructor said something about assigning it to a temp (tmp) I dont know why, ring any bells?

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    32
    So does this look right? Im suspecting if i use the same variable "int tmp" for both would that cause any issues? Or do i even need "int tmp" for the second one?

    /* change color image to black and white */
    Code:
    void BlackNWhite(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT],unsigned char B[WIDTH][HEIGHT])
    {
    	int tmp;//should i assign this to anything?
    	for (int y = 0; y < HEIGHT; y++)
    		for (int x = 0; x < WIDTH; x++)
    		{
    			tmp = (R[x][y] + G[x][y] + B[x][y])/ 3;
    			R[x][y] = G[x][y] = B[x][y] = tmp;
    
               /* R[x][y] = (R[i][j]+G[i][j]+B[i][j])/3;
                G[x][y] = (R[i][j]+G[i][j]+B[i][j])/3;
                B[x][y] = (R[i][j]+G[i][j]+B[i][j])/3;*/
    		}
    }
    /* reverse image color */
    Code:
    void Negative(unsigned char R[WIDTH][HEIGHT], unsigned char G[WIDTH][HEIGHT], unsigned char B[WIDTH][HEIGHT])
    {
    	int tmp;
    	for (int y = 0; y < HEIGHT; y++)
    		for (int x = 0; x < WIDTH; x++)
    		{
    			R[x][y] = 255 - R[x][y];
    			G[x][y] = 255 - G[x][y];
    			B[x][y] = 255 - B[x][y];
    		}
    }
    Last edited by kkmoslehpour; 10-13-2011 at 10:25 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a white image
    By mr_empty in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2009, 12:44 AM
  2. RGB to Black&White
    By Rustik_ in forum C++ Programming
    Replies: 30
    Last Post: 08-25-2009, 11:19 AM
  3. OpenGL example cube is black, white, grey only
    By edwardtisdale in forum Windows Programming
    Replies: 7
    Last Post: 09-22-2007, 03:37 PM
  4. Need to set EDIT background BLACK and EDIT text WHITE
    By Garfield in forum Windows Programming
    Replies: 3
    Last Post: 08-29-2003, 02:36 PM
  5. Replies: 3
    Last Post: 04-04-2002, 05:27 PM

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