Files
FreeJ/scripts/processing/topics/histogram.pde
Jaromil ae7b1ad056 progresses on processing script
processing-js 0.4 has been merged in some relevant parts
basic and topic scripts added for test
color handling fixed, more scripts show up now
2010-02-12 18:36:54 +01:00

40 lines
850 B
Plaintext

size(200, 200);
colorMode(RGB, width);
int[] hist = new int[width];
// Load an image from the data directory
// Load a different image by modifying the comments
PImage a;
a = loadImage("data/cdi01_g.jpg", null, function(){
image(a, 0, 0);
// Calculate the histogram
for (int i=0; i<width; i++) {
for (int j=0; j<height; j++) {
hist[int(red(get(i, j)))]++;
}
}
// Find the largest value in the histogram
float maxval = 0;
for (int i=0; i<width; i++) {
if(hist[i] > maxval) {
maxval = hist[i];
}
}
// Normalize the histogram to values between 0 and "height"
for (int i=0; i<width; i++) {
hist[i] = int(hist[i]/maxval * height);
}
// Draw half of the histogram (skip every second value)
stroke(width);
for (int i=0; i<width; i+=2) {
line(i, height, i, height-hist[i]);
}
});