Reputation: 53
I write a c# code to get the pixel values from a grayscale image.
Color x= c.GetPixel(i,j);
byte y=(byte)(((int)x.R+x.G+x.B)/3);
I compared them with the values getting in Matlab with imread command. Why are they completely different? The values are between 0 and 255 in C# and in Matlab. For example:
i=0, j=0
- C#: 153
, Matlab: 149
i=0, j=1
- C#: 153
, Matlab: 152
i=0, j=1
- C#: 170
, Matlab: 156
Upvotes: 0
Views: 76
Reputation: 186668
Grayscale is not just an average
Y != (R + G + B) / 3
For instance, mix of R + B
- purple - should be darker than R + G
- yellow. Correct formula is (see https://en.wikipedia.org/wiki/Grayscale for details)
Y = (299 * R + 587 * G + 114 * B) / 1000
C# Code:
byte y = (byte)((499 + 299 * x.R + 587 * x.G + 114 * x.B) / 1000);
Upvotes: 2