knorbika
knorbika

Reputation: 53

Get pixel values from an image in C#? Why are they different from values in Matlab?

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:

Upvotes: 0

Views: 76

Answers (1)

Dmitrii Bychenko
Dmitrii Bychenko

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

Related Questions