# Thread: How to convert existing image to black and white?

1. ## 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]);```

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

3. I updated my question now does it make more sense? btw thanks quzah for all your help, i suck at this

4. Code:
```color = R + G + B / 3
R[x][y] = G[x][y] = B[x][y] = color;```
Something roughly like that.

Quzah.

5. 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. Originally Posted by kkmoslehpour
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.

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

9. 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. 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];
}
}```

Popular pages Recent additions