Line fit

This program fits a line assuming that the input image is showing a white line without outliers.  The problem of ambiguity with the orientation of the line is overcome by estimating “2*a” instead of the angle “a”.

See also

#!/usr/bin/env ruby
require 'hornetseye'
include Hornetseye
class MultiArray
  class << self
    def cramp( w, h )
      idx = int( w, h ).indgen!
      retval = MultiArray( COMPLEX( INT ), w, h ).new
      retval.real = idx % w
      retval.imag = idx / w
      retval
    end
  end
end
raise "Syntax: ./line.rb <input image> <output image>" unless ARGV.size == 2
img = MultiArray.load_ubyte ARGV[0]
x = MultiArray.cramp( *img.shape ).mask img >= 128
c = x.sum / x.size.to_f
a = Math.sqrt( ( ( x - c ) ** 2 ).sum / x.size )
gc = Magick::Draw.new
gc.stroke 'red'
gc.stroke_width 2
gc.line( ( c - 2 * a ).real, ( c - 2 * a ).imag,
         ( c + 2 * a ).real, ( c + 2 * a ).imag )
result = img.to_ubytergb.to_magick
gc.draw result
result.to_multiarray.save_ubytergb ARGV[1]
Close