FAQ
 
 
Cover  \ Exhibition  \ Learning  \ Reference  \ Download  \ Discourse   
    Examples \ Tutorials \ Courses & Workshops
 
   
 
 
Examples for Processing (BETA) version 91+. If you have a previous version, use the examples included with your software. If you see any errors or have comments, please let us know.



Histogram
By REAS <http://reas.com>

Calculates the histogram of an image. A histogram is the frequency distribution of the gray levels with the number of pure black values displayed on the left and number of pure white values on the right.

Created 10 August 2002

   
// Histogram 
// By REAS <http://reas.com> 

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("cdi01_g.jpg"); 
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]); 
} 
 
 







   
  Processing is an open project initiated by Ben Fry and Casey Reas  
  © Info