summaryrefslogtreecommitdiff
path: root/Year_2/IandM/equalization/equalization.pde
blob: fa7b1e2d421ab1051cea5b9df3f483e9fbc47ed6 (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
void setup() {
  size(512, 256);
  PImage img = loadImage("lena.png");
  img.resize(256, 256);
  img.filter(GRAY);

  image(img, 0, 0);
  image(equalization(img), 256, 0);
}

PImage equalization(PImage I) {
  PImage out = I.copy();
  float[] h = histogram(out);

  for (int i = 1; i < 256; ++i) {
    h[i] = h[i]+h[i-1];
  }

  out.loadPixels();

  for (int i = 0; i < out.pixels.length; ++i) {
    out.pixels[i] = color(255*h[int(red(out.pixels[i]))]);
  }

  out.updatePixels();

  return out;
}

float[] histogram(PImage I) {
  float[] h = new float[256];
  for (int i = 0; i< 256; ++i) { 
    h[i] = 0;
  }

  I.loadPixels();

  for (int i = 0; i < I.pixels.length; ++i) {
    h[int(red(I.pixels[i]))]++;
  }

  for (int i = 0; i < 256; ++i) {
    h[i] = h[i] / I.pixels.length;
  }

  I.updatePixels();

  return h;
}