Sha256: 452f40731216e8e41d34e25f3f0551026ccff5f5d20fcbf493c2849b08b327e4

Contents?: true

Size: 1.05 KB

Versions: 3

Compression:

Stored size: 1.05 KB

Contents

require 'narray'

module Mork
  # Handles low-level computations on a Mimage
  # Typically used on smaller patches
  class NPatch
    def initialize(mim)
      @mim = mim
      @width  = mim.width
      @height = mim.height
    end
    
    def average
      narr.mean
    end
    
    def dark_centroid
      return nil, nil unless sufficient_contrast?
      xp = patch.sum(1).to_a
      yp = patch.sum(0).to_a
      # find the intensity trough
      ctr_x = xp.find_index(xp.min)
      ctr_y = yp.find_index(yp.min)
      return nil, nil if edgy?(ctr_x, ctr_y)
      return ctr_x, ctr_y
    end
    
  private
    def patch
      @the_npatch ||= blurry_narr.reshape!(@width, @height)
    end
    
    def narr
      @narr ||= NArray[@mim.pixels]
    end
    
    def blurry_narr
      @blurry_narr ||= NArray[@mim.blur!(10,5).pixels]
    end

    def sufficient_contrast?
      # just a wild guess for now
      blurry_narr.stddev > 5000
    end
    
    def edgy?(x, y)
      tol = 5
      (x < tol) or (y < tol) or (y > @height - tol) or (x > @width - tol)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mork-0.0.5 lib/mork/npatch.rb
mork-0.0.3 lib/mork/npatch.rb
mork-0.0.2 lib/mork/npatch.rb