topical media & game development

talk show tell print

#javascript-processing-example-topic-image-histogram.htm / htm



  <!DOCTYPE html>
  <html><head>
  <script src="javascript-processing-example-processing.js"></script>
  <script src="javascript-processing-example-init.js"></script>
  <link rel="stylesheet" href="javascript-processing-example-style.css">
  </head><body><h1><a href="http://ejohn.org/blog/processingjs/">Processing.js</a></h1>
  <h2>Histogram</h2>
  
  <p>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.</p>
  
  <p><a href="http://processing.org/learning/topics/histogram.html"><b>Original Processing.org Example:</b> Histogram</a><br>
  <script type="application/processing">
  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]);
  }
  </script><canvas width="200" height="200"></canvas></p>
  <div style="overflow: hidden; height: 0px; width: 0px;"><img src="javascript-processing-example-cdi01_g.jpg" id="cdi01_g.jpg"><img src="javascript-processing-example-ystone08.jpg" id="ystone08.jpg"></div>
  
  <pre><b>// All Examples Written by <a href="http://reas.com/">Casey Reas</a> and <a href="http://benfry.com/">Ben Fry</a>
  // unless otherwise stated.</b>
  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&lt;width; i++) {
    for (int j=0; j&lt;height; j++) {
      hist[int(red(get(i, j)))]++; 
    }
  } 
  
  // Find the largest value in the histogram
  float maxval = 0;
  for (int i=0; i&lt;width; i++) {
    if(hist[i] &gt; maxval) {
      maxval = hist[i];
    }  
  }
  
  // Normalize the histogram to values between 0 and "height"
  for (int i=0; i&lt;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&lt;width; i+=2) {
    line(i, height, i, height-hist[i]);
  }</pre>
  </body></html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.