Sha256: 74f5ea6641746e65bdcecdb6bd12a2bffb920d86161cf73c272666300d6888aa

Contents?: true

Size: 1.55 KB

Versions: 4

Compression:

Stored size: 1.55 KB

Contents

require 'narray'

module Mork
  # @private
  # NPatch handles low-level computations on pixels by leveraging NArray
  class NPatch
    # NPatch.new(source, width, height) constructs an NPatch object
    # from the `source` linear array of bytes, to be reshaped as a
    # `width` by `height` matrix
    def initialize(source, width, height)
      @patch = NArray.float(width, height)
      @patch[true] = source
      # @width  = width
      # @height = height
    end

    def average(coord)
      @patch[coord.x_rng, coord.y_rng].mean
    end

    def stddev(coord)
      @patch[coord.x_rng, coord.y_rng].stddev
    end

    # def length
    #   # is this only going to be used for testing purposes?
    #   @patch.length
    # end

    def centroid
      xp = @patch.sum(1).to_a
      yp = @patch.sum(0).to_a
      return xp.find_index(xp.min), yp.find_index(yp.min), @patch.stddev
    end
  end
end

# def dark_centroid(c = nil)
#   p = crop c
#   sufficient_contrast?(p) or return
#   xp = p.sum(1).to_a
#   yp = p.sum(0).to_a
#   # find the intensity trough
#   ctr_x = xp.find_index(xp.min)
#   ctr_y = yp.find_index(yp.min)
#   # puts "Centroid: #{ctr_x}, #{ctr_y} - MinX #{xp.min/xp.length}, MaxX #{xp.max/xp.length}, MinY #{yp.min/yp.length}, MaxY #{yp.max/yp.length}"
#   return ctr_x, ctr_y
# end

# def crop(c)
#   raise "crop HELL" if c.nil?
#   p = NArray.float c.w, c.h
#   p[true,true] = @patch[c.x_rng, c.y_rng]
#   p
# end

# def sufficient_contrast?(p)
#   # puts "Contrast: #{p.stddev}"
#   # tested with the few examples: spec/samples/rm0x.jpeg
#   p.stddev > 20
# end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mork-0.9.3 lib/mork/npatch.rb
mork-0.9.2 lib/mork/npatch.rb
mork-0.9.1 lib/mork/npatch.rb
mork-0.9.0 lib/mork/npatch.rb