Experiments with ruby-processing (processing-2.2.1) and JRubyArt for processing-3.0

Sunday, 8 February 2015

MDArray gem is available to JRubyArt (handy for image processing)

Sketch makes use of MDArray convenience index function. Requires to be run as "k9 run sobel.rb" to get image stuff to work, but interestingly mdarray gem also just loads...(cf ruby-processing)
require 'jruby_art'
require 'mdarray'

class Strobel < Processing::App
  attr_reader :img, :edge

  def setup
    size 300, 225
    @img = load_image('engine.png')
    @edge = create_image(300, 225, ALPHA)
    edge.load_pixels
    img.load_pixels
    generate(img)
  end

  def draw
    image edge, 0, 0
    filter(GRAY)
  end

  def generate(from_image)
    sbx = MDArray.int([3, 3], [-1, 0, 1, -2, 0, 2, -1, 0, 1])
    sby = MDArray.int([3, 3], [1, 2, 1, 0, 0, 0, -1, -2, -1])
    edg = MDArray.int([225, 300])  # this seems Irish, but is correct
    pxls = from_image.pixels
    (1...from_image.width - 2).each do |x|
      (1...from_image.height - 2).each do |y|
        pixel_x = (sbx[0, 0] * pxls[x - 1 + width * (y - 1)]) + (sbx[0, 1] * pxls[x + width * (y - 1)]) + (sbx[0, 2] * pxls[x + 1+ width * (y - 1)]) +
          (sbx[1, 0] * pxls[x - 1+ width * y])   + (sbx[1, 1] * pxls[x + width * y])   + (sbx[1, 2] * pxls[x + 1+ width * y]) +
          (sbx[2, 0] * pxls[x - 1+ width * y + 1]) + (sbx[2, 1] * pxls[x + width * y + 1]) + (sbx[2, 2] * pxls[x + 1 + width * (y + 1)])
        pixel_y = (sby[0, 0] * pxls[x - 1+ width * (y - 1)]) + (sby[0, 1] * pxls[x + width * (y - 1)]) + (sby[0, 2] * pxls[x + 1+ width * (y - 1)]) +
          (sby[1, 0] * pxls[x - 1+ width * y])   + (sby[1, 1] * pxls[x + width * y])   + (sby[1, 2] * pxls[x + 1+ width * y]) +
          (sby[2, 0] * pxls[x - 1+ width * (y + 1)]) + (sby[2, 1] * pxls[x + width * (y + 1)]) + (sby[2, 2] * pxls[x + 1+ width * (y + 1)])
        val = Math.hypot(pixel_x, pixel_y).ceil
        edg[y, x] = val
      end
    end
    edge.pixels = edg.to_a # load pixels from MDArray
  end

  def mouse_pressed
    save('engine_edge.png')
  end
end

Strobel.new(title: 'Sobel Edge Detect')
before
edge detected

No comments:

Post a Comment

Followers

About Me

My photo
I have developed JRubyArt and propane new versions of ruby-processing for JRuby-9.1.5.0 and processing-3.2.2