summaryrefslogtreecommitdiff
path: root/Year_2/IandM/psnr_mse/psnr_mse.pde
blob: 9aa0a8b606e2c1a10b8963473bf64b2b8eaf87a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void setup() {
  size(512, 256);
  PImage img = loadImage("lena.png");
  img.resize(256, 256);
  img.filter(GRAY);
  PImage img2 = negative(img);
  image(img, 0, 0);
  image(img2, 256, 0);

  print("PSNR: ", psnr(img, img2));
}

float mse(PImage i1, PImage i2) {
  float res = 0;
  i1.loadPixels();
  i2.loadPixels();

  for (int i = 0; i < i1.pixels.length; ++i) {
    res+= pow(red(i1.pixels[i])-red(i2.pixels[i]), 2);
  }
  res = res/i1.pixels.length;
  i1.updatePixels();
  i2.updatePixels();

  return res;
}

float psnr(PImage i1, PImage i2) {
  float mse = mse(i1, i2);
  float res = 10*log(255*255/mse)/log(10);

  return res;
}

PImage negative(PImage I) {
  PImage out = I.copy();
  out.loadPixels();
  float r, g, b;

  for (int i = 0; i < out.pixels.length; ++i) {
    r= 255-red(out.pixels[i]);
    g= 255-green(out.pixels[i]);
    b= 255-blue(out.pixels[i]);

    out.pixels[i] = color(r, g, b);
  }

  out.updatePixels();
  return out;
}