Sha256: 21d670d4ad0a1c3d062cd70865da45bc1f34d16e6357193d26ba045204d618a2

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

#
# Histogram. 
# 
# 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. 
#

attr_reader :hist

def setup
  size(640, 360)
  
  # Load an image from the data directory
  # Load a different image by modifying the comments
  img = loadImage("frontier.jpg")
  image(img, 0, 0)
  @hist = Array.new(256, 0)
  
  # Calculate the histogram
  (0 ... img.width).each do |i|
    (0 ... img.height).each do |j|
      bright = (brightness(get(i, j))).to_i
      hist[bright] += 1  
    end
  end
  
  # Find the largest value in the histogram using processings max function
  # that's why we use to java cinversion
  histMax = max(hist.to_java Java::int)
  
  stroke(255)
  # Draw half of the histogram (skip every second value)
  (0 ... img.width).step(2) do |i|
    # Map i (from 0..img.width) to a location in the histogram (0..255)
    which = (map(i, 0, img.width, 0, 255)).to_i
    # Convert the histogram value to a location between 
    # the bottom and the top of the picture
    y = (map(hist[which], 0, histMax, img.height, 0)).to_i
    line(i, img.height, i, y)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ruby-processing-2.4.4 samples/processing_app/topics/image_processing/histogram.rb
ruby-processing-2.4.3 samples/processing_app/topics/image_processing/histogram.rb
ruby-processing-2.4.2 samples/processing_app/topics/image_processing/histogram.rb
ruby-processing-2.4.1 samples/processing_app/topics/image_processing/histogram.rb