Sha256: 891333631ea5389b2d63e6e14197950c8b796698b258b69d299cf5569e1c1016

Contents?: true

Size: 1.59 KB

Versions: 2

Compression:

Stored size: 1.59 KB

Contents

# encoding: utf-8
# frozen_string_literal: true
require 'propane'
# 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.
class EdgeDetection < Propane::App
  KERNEL = [[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]].freeze
  attr_reader :img

  def setup
    size(640, 360)
    @img = load_image('moon.jpg') # Load the original image
    no_loop
  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
end

EdgeDetection.new title: 'Edge Detection'

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
propane-0.7.0-java examples/complete/edge_detection.rb
propane-0.6.0-java examples/complete/edge_detection.rb