HornetsEye

(Difference between revisions)
Jump to: navigation, search
m (See Also)
m (Examples)
Line 15: Line 15:
  
 
=Examples=
 
=Examples=
See [http://www.wedesoft.demon.co.uk/hornetseye-api/files/inputgrey-txt.html Hornetseye homepage] for more examples.
 
==Simple Webcam Application==
 
[[Image:Rubywebcam.jpg|thumb|240px|right|Screenshot of Linux webcam application written in Ruby]]
 
The Linux webcam application uses [[Hornetseye|HornetsEye-1.5]], [http://rubyforge.org/projects/rmagick/ RMagick], and [http://rubyforge.org/projects/korundum/ qt4-ruby] (there also is a [http://www.wedesoft.demon.co.uk/hornetseye-api/files/vfwcamapp-txt.html Windows webcam] already).
 
 
You need to install qt4-qtruby-1.4.8 or later because there was a memory leak in Qt::ByteArray of qt4-qtruby-1.4.7. Many thanks to [http://rubyforge.org/users/rdale/ Richard Dale] for fixing the problem!
 
 
<!-- To install [http://rubyforge.org/frs/shownotes.php?release_id=7630 qt4-qtruby-1.4.7] I had to [http://rubyforge.org/forum/message.php?msg_id=19132 change the script ./smoke/qt/qtguess.pl.in]. Otherwise on gets an error message like this:
 
problem with QListWidget missing parent at ../../kalyptus/kalyptusCxxToSmoke.pm line 2207.
 
 
Also [http://rubyforge.org/frs/shownotes.php?release_id=7630 qt4-qtruby-1.4.7] seems to have a [http://rubyforge.org/forum/message.php?msg_id=19077 memory leak in Qt::ByteArray]. Thanks to [http://rubyforge.org/users/rdale/ Richard Dale] the problem was solved and the bugfix will be included in the next release of qt4-qtruby. The modified code already is [http://websvn.kde.org/trunk/KDE/kdebindings/qtruby/ available via the KDE source repository]. -->
 
 
Once the required software is installed, one can develop sophisticated applications within a very short time.
 
As an example here is the source code of the webcam application. Note that you need the user-interface description file as well (which was created with [http://trolltech.com/products/qt/features/designer Qt4 designer]). You need to compile the user-interface file with '''rbuic4''' to get ''ui_webcamwindow.rb''.
 
<pre>
 
#!/usr/bin/ruby
 
# Qt webcam (requires Qt4-Ruby)
 
require 'Qt'
 
require 'hornetseye'
 
require 'ui_webcamwindow'
 
app=Qt::Application.new(ARGV)
 
class WebcamWindow < Qt::Dialog
 
  slots 'open_camera()'
 
  slots 'set_value(int)'
 
  def initialize( parent = nil )
 
    super
 
    @ui = Ui::WebcamWindow.new
 
    @ui.setupUi( self )
 
    Qt::Object.connect( @ui.reconnectButton, SIGNAL('clicked()'),
 
                        self, SLOT('open_camera()'))
 
    Qt::Object.connect( @ui.brightnessSlider, SIGNAL('valueChanged(int)'),
 
                        self, SLOT('set_value(int)'))
 
    Qt::Object.connect( @ui.hueSlider, SIGNAL('valueChanged(int)'),
 
                        self, SLOT('set_value(int)'))
 
    Qt::Object.connect( @ui.colourSlider, SIGNAL('valueChanged(int)'),
 
                        self, SLOT('set_value(int)'))
 
    Qt::Object.connect( @ui.contrastSlider, SIGNAL('valueChanged(int)'),
 
                        self, SLOT('set_value(int)'))
 
    @timer = 0
 
    open_camera
 
  end
 
  def open_camera
 
    @ui.errorLabel.text = ""
 
    begin
 
      @input.close if @input != nil
 
      @input = nil
 
      @input = Hornetseye::V4LInput.new( @ui.deviceEdit.text )
 
      @timer = startTimer( 0 ) if @timer == 0
 
    rescue RuntimeError => e
 
      @ui.errorLabel.text = e.to_s
 
      @input = nil
 
    end
 
    @input
 
  end
 
  def set_value( value )
 
    puts "hi"
 
    @input.set_sensivity( @ui.brightnessSlider.value,
 
                          @ui.hueSlider.value,
 
                          @ui.colourSlider.value,
 
                          @ui.contrastSlider.value ) if @input != nil
 
  end
 
  def timerEvent( e )
 
    begin
 
      raise "No input available" if @input == nil
 
      str = @input.read.to_magick.to_blob {
 
        self.format = "PPM"
 
        self.depth = 8
 
      }
 
      pix = Qt::Pixmap.new
 
      pix.loadFromData( Qt::ByteArray.fromRawData( str, str.size ) )
 
      @ui.displayLabel.setPixmap( pix )
 
      @ui.displayLabel.update
 
    rescue RuntimeError => e
 
      killTimer( @timer )
 
      @timer = 0
 
    end
 
  end
 
end
 
win = WebcamWindow.new
 
win.show
 
app.exec
 
</pre>
 
 
Also see [http://www.wedesoft.demon.co.uk/hornetseye-api/files/webcamapp-txt.html project documentation].
 
 
==Phase Correlation==
 
 
{|align="right"
 
{|align="right"
 
|-
 
|-
|[[Image:Apollo left.jpg|thumb|76px|Left part of image]]||[[Image:Apollo right.jpg|thumb|114px|Right part of image]]||[[Image:Apollo result.jpg|thumb|163px|Stitched image]]
+
|colspan="2" align="center" | [[Image:Rubywebcam.jpg|thumb|240px|Screenshot of Linux webcam application written in Ruby]]
 +
|-
 +
|[[Image:Apollo left.jpg|thumb|76px|Left part of image]]||[[Image:Apollo right.jpg|thumb|114px|Right part of image]]
 +
|-
 +
|colspan="2" align="center" | [[Image:Apollo result.jpg|thumb|163px|Stitched image]]
 
|-
 
|-
 
|}
 
|}
This is an implementation of the [http://en.wikipedia.org/wiki/Phase_correlation phase correlation] for aligning images.
+
==Webcam==
The code depends on [[Hornetseye|HornetsEye-1.5]] and [http://rubyforge.org/projects/narray/ NArray-fftw3].
+
The [http://www.wedesoft.demon.co.uk/hornetseye-api/files/webcamapp-txt.html Linux webcam application] uses [[Hornetseye|HornetsEye-1.5]], [http://rubyforge.org/projects/rmagick/ RMagick], and [http://rubyforge.org/projects/korundum/ qt4-ruby] (there also is a [http://www.wedesoft.demon.co.uk/hornetseye-api/files/vfwcamapp-txt.html Windows webcam] already).
<pre>
+
 
#!/usr/bin/ruby
+
See [http://www.wedesoft.demon.co.uk/hornetseye-api/files/inputgrey-txt.html Hornetseye homepage] for more examples.
require 'hornetseye'
+
 
require 'fftw3'
+
==Phase Correlation==
include Hornetseye
+
The [http://www.wedesoft.demon.co.uk/hornetseye-api/files/phasecorrelation-txt.html phase correlation example] is an implementation of the [http://en.wikipedia.org/wiki/Phase_correlation phase correlation] for aligning images.
syntax = <<END_OF_STRING
+
The code depends on [[Hornetseye|HornetsEye-1.5]], [http://rubyforge.org/projects/rmagick/ RMagick] and [http://rubyforge.org/projects/narray/ NArray-fftw3].
Shift estimation
+
Syntax: registration.rb <image1> <image2>
+
Example: registration.rb astronaut.jpg apollo-16.jpg
+
Example: registration.rb apollo_left.jpg apollo_right.jpg
+
END_OF_STRING
+
if ARGV.size != 2
+
  puts syntax
+
  raise "Wrong number of command-line arguments."
+
end
+
image = (0...2).collect { |i| NArray.load_grey8( ARGV[i] ) }
+
# TODO: Apply windowing function?
+
# Force images to have same size. Make image twice as big to avoid cyclical
+
# correlation.
+
maxwidth  = [ image[0].shape[0], image[1].shape[0] ].max * 2
+
maxheight = [ image[0].shape[1], image[1].shape[1] ].max * 2
+
limage = image.collect { |img|
+
  nimg = NArray.new( NArray::BYTE, maxwidth, maxheight )
+
  nimg[ 0...img.shape[0], 0...img.shape[1] ] = img
+
  nimg
+
}
+
fimage = limage.collect { |img|
+
  FFTW3.dft( img.to_type( NArray::DCOMPLEX ), +1 )
+
}
+
limage = nil
+
fshift = ( fimage[0] * fimage[1].conj ) / ( fimage[0].abs * fimage[1].abs )
+
fimage = nil
+
# TODO: Replace with native implementation for higher performance.
+
fshift = fshift.collect { |value|
+
  if value.real.nan? or value.imag.nan?
+
    0
+
  else
+
    value
+
  end
+
}
+
shift = FFTW3.dft( fshift, -1 )
+
width  = shift.shape[0]
+
height = shift.shape[1]
+
shiftx = nil
+
shifty = nil
+
maxvalue = 0
+
for i in 0...width
+
  for j in 0...height
+
    if shift[i,j] > maxvalue
+
      shiftx = i
+
      shifty = j
+
      maxvalue = shift[i,j]
+
    end
+
  end
+
end
+
shiftx = shiftx - width  if shiftx > width  / 2
+
shifty = shifty - height if shifty > height / 2
+
shift = nil
+
puts "shift-x = #{shiftx}"
+
puts "shift-y = #{shifty}"
+
minx = [ 0, shiftx ].min
+
miny = [ 0, shifty ].min
+
maxx = [ image[0].shape[0], image[1].shape[0] + shiftx ].max - 1
+
maxy = [ image[0].shape[1], image[1].shape[1] + shifty ].max - 1
+
offsetx = -minx
+
offsety = -miny
+
resultwidth  = maxx + 1 - minx
+
resultheight = maxy + 1 - miny
+
result1 = NArray.new( NArray::BYTE, resultwidth, resultheight )
+
result1[ offsetx...( offsetx + image[0].shape[0] ),
+
        offsety...( offsety + image[0].shape[1] ) ] = image[0] / 2
+
result2 = NArray.new( NArray::BYTE, resultwidth, resultheight )
+
result2[ ( shiftx + offsetx )...( shiftx + offsetx + image[1].shape[0] ),
+
        ( shifty + offsety )...( shifty + offsety + image[1].shape[1] ) ] = image[1] / 2
+
result = result1 + result2
+
result.display
+
</pre>
+
  
 
=Downloads=
 
=Downloads=

Revision as of 12:17, 4 October 2007

Nanoworkshop.jpg 420px

Contents

Introduction

Hornetseye.png

HornetsEye is a Ruby-extension for real-time computer vision under GNU/Linux offering interfaces to do image- and video-I/O with RMagick, Xine, IIDC/DCAM-compatible firewire digital camera (DC1394) and video for Linux (V4L).

HornetsEye also is an attempt to use the Mimas library and create a minimalistic and consistent real-time computer vision library.

  • minimalistic: The library is focused on real-time computer vision. Existing libraries are being made used of.
  • consistent:: A non-redundant set of data-types is used. Also the library tries to stay consistent with existing libraries.

The logo was created using GIMP and it shows a honeycomb structure as you would find on an insect's compound eye. A hornet is capable of navigating and detecting objects with the limited resolution of its compound eyes.

Examples

File:Rubywebcam.jpg
Screenshot of Linux webcam application written in Ruby
File:Apollo left.jpg
Left part of image
File:Apollo right.jpg
Right part of image
File:Apollo result.jpg
Stitched image

Webcam

The Linux webcam application uses HornetsEye-1.5, RMagick, and qt4-ruby (there also is a Windows webcam already).

See Hornetseye homepage for more examples.

Phase Correlation

The phase correlation example is an implementation of the phase correlation for aligning images. The code depends on HornetsEye-1.5, RMagick and NArray-fftw3.

Downloads

Hornetseye.png Before downloading you may want to check the installation instructions for information on what other software you need to install and run Hornetseye. See download instructions on how to obtain Hornetseye.

Software Engineering

HornetsEye brings the functionality of existing powerful free software packages into Ruby. HornetsEye also tries to make existing Ruby extension operate with each other to enable the development of novel solutions:

  1. Qt logo.png QtRuby, Kde.png Korundum: QtRuby and Korundum can be used to develop graphical user interfaces and desktop applications.
  2. Xine logo.png Xine: Using Xine one can read virtually any video file and it is even possible to read streaming videos.
  3. Tanaka.png NArray: Masahiro Tanaka's NArray is an implementation of n-dimensional arrays for Ruby.
  4. Fftw logo.gif FFTW. The fftw-library can is maybe the fastest library for performing discrete Fourier transforms. It can be invoked by using Masahiro Tanaka's fftw3 extension.
  5. RMagick.png RMagick: The RMagick Ruby-extension allows to use the powerful Magick++ library in Ruby for loading and saving images.
  6. Coriander.png libdc1394: Using libdc1394 one can make use of a large choice of firewire digital cameras.
  7. OpenEXR.jpg OpenEXR: The OpenEXR library is used for saving and loading high dynamic range images.
  8. C--boost logo.gif Boost: The Boost Library offers smart pointers to do exception safe programming, multi-dimensional arrays, template meta-programming, abstract data types for linear algebra and many other programming concepts. The Boost library is going to be part of a future C++ standard.
  9. Stl logo.gif STL: The software makes use of the Standard Template Library
  10. Ruby.png Ruby programming language
  11. Gnu-arch logo.png gnu-arch: gnu-arch is being used for version control.
  12. Gcc logo.png Gcc: gcc is the C++ compiler of the GNU project.
  13. Gnu-head.jpg autoconf, automake and make: make, autoconf and automake are used to configure and perform the build of the software on various distributions of the Linux operating system.
  14. Naturaldocs.png Natural Docs: Natural Docs is used to create the HTML documentation.

See Also

External Links

Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox