Sha256: 0903b54a6e82d890ef9f96259bcbce6092c10e07c572cc9942a10e8e72030c09

Contents?: true

Size: 1.41 KB

Versions: 5

Compression:

Stored size: 1.41 KB

Contents

#
# Edge Detection. 
# 
# A high-pass filter sharpens an image. This program analyzes every
# pixel in an image in relation to the neighboring pixels to sharpen 
# the image. 
#

KERNEL = [ [ -1, -1, -1 ], [ -1,  9, -1 ], [ -1, -1, -1 ] ]
                    
attr_reader :img

def setup 
  size(640, 360)
  @img = load_image('moon.jpg') # Load the original image
  noLoop
end

def draw
  image(img, 0, 0) # Displays the image from point (0,0) 
  img.load_pixels
  # Create an opaque image of the same size as the original
  edge_img = create_image(img.width, img.height, RGB)
  # Loop through every pixel in the image
  (1 ... img.height - 1).each do |y| 
    (1 ... img.width - 1).each do |x| 
      sum = 0 # Kernel sum for this pixel
      (-1 .. 1).each do |ky| 
        (-1 .. 1).each do |kx|
          # Calculate the adjacent pixel for this kernel point
          pos = (y + ky)*img.width + (x + kx)
          # Image is grayscale, red/green/blue are identical
          val = red(img.pixels[pos])
          # Multiply adjacent pixels based on the kernel values
          sum += KERNEL[ky+1][kx+1] * val
        end
      end
      # For this pixel in the new image, set the gray value
      # based on the sum from the kernel
      edge_img.pixels[y*img.width + x] = color(sum, sum, sum)
    end
  end
  # State that there are changes to edge_img.pixels[]
  edge_img.update_pixels
  image(edge_img, width/2, 0) # Draw the new image
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ruby-processing-2.6.2 samples/processing_app/topics/image_processing/edge_detection.rb
ruby-processing-2.6.1 samples/processing_app/topics/image_processing/edge_detection.rb
ruby-processing-2.6.0 samples/processing_app/topics/image_processing/edge_detection.rb
ruby-processing-2.5.1 samples/processing_app/topics/image_processing/edge_detection.rb
ruby-processing-2.5.0 samples/processing_app/topics/image_processing/edge_detection.rb