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

  image(img, 0, 0);
  float[] hist = histogram(img);
  for(int i = 0; i < 256; ++i) {
    println(i, hist[i]);
  }
}

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;
}