简体   繁体   中英

is this correct to alter contrast of an image using matrices?

so I have been trying to alter an image's contrast with matrices and this is what I have come up with:

final defaultColorMatrix = const <double>[
  1, 0, 0, 0, 0,
  0, 1, 0, 0, 0,
  0, 0, 1, 0, 0,
  0, 0, 0, 1, 0,
];

List<double> calculateContrastMatrix(double contrast) {
final m = List<double>.from(defaultColorMatrix);
m[0] = contrast;
m[6] = contrast;
m[12] = contrast;
m[5] = (1 - contrast) / 2;
m[10] = (1 - contrast) / 2;
m[15] = (1 - contrast) / 2;
return m;

being contrast and adjustable value between 0 and 1

it is giving me sub optimal results

I just realized that the last column is not m[5] , m[10] , m[15] but m[4] , m[9] , m[14] . I forgot that dart's array index starts at 0.

I also forgot that the last column in flutter matrices is not normalized to it expects values between 0-255 so the correct solution is:

List<double> calculateContrastMatrix(double contrast) {
  final m = List<double>.from(defaultColorMatrix);

  m[0] = contrast;
  m[6] = contrast;
  m[12] = contrast;

  m[4] = ((1 - contrast) / 2) * 255;
  m[9] = ((1 - contrast) / 2) * 255;
  m[14] = ((1 - contrast) / 2) * 255;

  return m;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM