Reputation: 6290
How do I make this into a grayscale?
After much googling it seems the best I can do is change the tint of the picture (in the code bellow, it's a green tint). How can I do it?
byte[] redGreenBlueVal = new byte[numBytes];
for (int i = 0; i < redGreenBlueVal .Length; i += 4)
{
redGreenBlueVal [i + 0] = (byte)(.114 * redGreenBlueVal [i + 0]); --> blue
redGreenBlueVal [i + 1] = (byte)(.587 * redGreenBlueVal [i + 1]); --> green
redGreenBlueVal [i + 2] = (byte)(.299 * redGreenBlueVal [i + 2]); --> red
}
Upvotes: 0
Views: 472
Reputation: 2056
In effect you're adjusting HSB so this should get you a better grayscale image: 30% RED 59% GREEN 11% BLUE
byte[] redGreenBlueVal = new byte[numBytes];
for (int i = 0; i < redGreenBlueVal .Length; i += 4)
{
gray = (byte)(.11 * redGreenBlueVal [i + 0]);
gray += (byte)(.59 * redGreenBlueVal [i + 1]);
gray += (byte)(.3 * redGreenBlueVal [i + 2]);
redGreenBlueVal [i + 0] = gray;
redGreenBlueVal [i + 1] = gray;
redGreenBlueVal [i + 2] = gray;
}
Upvotes: 2
Reputation: 3875
To get a grayscale image, the basic idea is to have all the 3 channels hold the same value. There are many ways to do this, the simplest ones are:
Upvotes: 0
Reputation: 19
You could try setting each pixels colour channels to the average value for that pixel.
i.e.
for( int i = 0; i < redGreenBlueVal.Length; += 4 )
{
int average = (redGreenBlueVal[i + 0] + redGreenBlueVal[i + 1] + redGreenBlueVal[i + 2])/3;
redGreenBlueVal[i + 0] = average;
redGreenBlueVal[i + 1] = average;
redGreenBlueVal[i + 2] = average;
}
Upvotes: 1