SlideShare a Scribd company logo
1 of 7
Download to read offline
http://vision.eng.shu.ac.uk/jan/shef11.pdf
                      Computer vision with special reference to
                                      Ruby




                      Computer vision with special reference to Ruby

                                         Jan Wedekind

                                 Wed Apr 06 14:00:00 BST 2011

c 2011 Jan Wedekind                            1/7
http://vision.eng.shu.ac.uk/jan/shef11.pdf
                         Ruby: Open Classes and Closures
                 # open classes
                 class Fixnum
                   def times three
                                     File.open( ’test.txt’ , ’r’ ) do |f|
                     3 * self
                                       3.times { puts f.readline }
                   end
                                     end
                 end
                 4.times three
                 # 12

                                 http://www.ruby-lang.org/




c 2011 Jan Wedekind                           2/7
http://vision.eng.shu.ac.uk/jan/shef11.pdf
                              Ruby: Ruby Extensions
   // gcc -shared -fPIC -I/usr/lib/ruby/1.8/x86 64-linux
    //   -o myextension.so myextension.c
   #include <ruby.h>
   #include <math.h>

   VALUE wrap logx( VALUE self, VALUE x )
   {
     return rb float new( log( NUM2DBL( self ) ) / log( NUM2DBL( x ) ) );
   }

   void Init myextension(void) {
     VALUE numeric = rb const get( rb cObject, rb intern( "Numeric" ) );
     rb define method( numeric, "logx" , RUBY METHOD FUNC( wrap logx ), 1 );
   }
                              #!/usr/bin/env ruby
                              require ’myextension’
                              e = 1024.logx( 2 )
                              puts "2 ** #{e} = 1024"


c 2011 Jan Wedekind                          3/7
http://vision.eng.shu.ac.uk/jan/shef11.pdf
                      Example: Histogram-Based Classification




c 2011 Jan Wedekind                            4/7
http://vision.eng.shu.ac.uk/jan/shef11.pdf

                                   Hue-Saturation


                               α = 1 (2 R − G − B)
                                   2




                                                                   histogram
                                β = sqrt3 (G − B)
                                      2
                                H ≈ atan2(β, α)
                                 C ≈ α2 + β2
                                                               «
                                      «

                       http://en.wikipedia.org/wiki/HSL_and_HSV




c 2011 Jan Wedekind                            5/7
http://vision.eng.shu.ac.uk/jan/shef11.pdf
                                 Similarity Measure
                                                                ai, j − bi, j
                                similarity(a, b)
                                                         i, j
                                                                ai, j + bi, j




                      ⇓               ⇓                            ⇓            ⇓




c 2011 Jan Wedekind                                6/7
http://vision.eng.shu.ac.uk/jan/shef11.pdf

                                                          Source Code
                      MAXC = 117.0
                      HBINS, CBINS = 8, 8
                      THRESHOLD, N = 32, 5
                      BOX = [220 ... 420, 140 ... 340]
                      class Node
                        def hsv hist(hbins = HBINS, cbins = CBINS)
                          alpha = 2 * r.to sint - g - b
                          beta = Math.sqrt(3) / 2 * ( g.to sint - b )
                          h = ( (Math.atan2(beta, alpha) + PI) * (HBINS / PI2) ).to int.clip 0 .. HBINS - 1
                          c = ( Math.sqrt(alpha ** 2 + beta ** 2) * (CBINS / MAXC) ).to int.clip 0 .. CBINS - 1
                          [h, c].histogram(hbins, cbins).to int
                        end
                        def cmp(other)
                          (self - other) / (self + other).major(1.0)
                        end
                      end
                      input = V4L2Input.new
                      mask = MultiArray.bool(640, 480).fill!
                      mask[*BOX] = true
                      labels = [ ’reference’ ,   ’dragon’ ,   ’knight’ ,   ’camel’ ]

                      hists = labels.collect { |object| MultiArray.load ubytergb( "#{object}.jpg" ).hsv hist }

                      history = [ ’reference’ ] * N
                      X11Display.show do
                        img = input.read ubytergb
                        img hist = img[*BOX].hsv hist
                        similarities = hists.collect { |hist| img hist.cmp(hist).abs.sum }
                        label = labels[similarities.index(similarities.min)]
                        history = [label] + history[0 ... N - 1]
                        if history == [label] * N
                          system "echo ’#{label}’ | festival --tts" if label != ’reference’
                        end
                        history != [ ’reference’ ] * N ? mask.conditional(img, img >> 1) : img
                      end



c 2011 Jan Wedekind                                                        7/7

More Related Content

More from Jan Wedekind

Computer vision for microscopes
Computer vision for microscopesComputer vision for microscopes
Computer vision for microscopes
Jan Wedekind
 
Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...
Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...
Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...
Jan Wedekind
 
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Jan Wedekind
 

More from Jan Wedekind (11)

The SOLID Principles for Software Design
The SOLID Principles for Software DesignThe SOLID Principles for Software Design
The SOLID Principles for Software Design
 
Computer vision for microscopes
Computer vision for microscopesComputer vision for microscopes
Computer vision for microscopes
 
Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...
Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...
Reconstruction (of micro-objects) based on focus-sets using blind deconvoluti...
 
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
Fokus-serien basierte Rekonstruktion von Mikroobjekten (2002)
 
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
Digital Imaging with Free Software - Talk at Sheffield Astronomical Society J...
 
Machine Vision made easy with Ruby - ShRUG June 2010
Machine Vision made easy with Ruby - ShRUG June 2010Machine Vision made easy with Ruby - ShRUG June 2010
Machine Vision made easy with Ruby - ShRUG June 2010
 
Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009Computer Vision using Ruby and libJIT - RubyConf 2009
Computer Vision using Ruby and libJIT - RubyConf 2009
 
Real-time Computer Vision With Ruby - OSCON 2008
Real-time Computer Vision With Ruby - OSCON 2008Real-time Computer Vision With Ruby - OSCON 2008
Real-time Computer Vision With Ruby - OSCON 2008
 
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
Object Recognition and Real-Time Tracking in Microscope Imaging - IMVIP 2006
 
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
Steerable Filters generated with the Hypercomplex Dual-Tree Wavelet Transform...
 
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
Ruby & Machine Vision - Talk at Sheffield Hallam University Feb 2009
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 

Recently uploaded (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Computer vision with special reference to Ruby (2011)

  • 1. http://vision.eng.shu.ac.uk/jan/shef11.pdf Computer vision with special reference to Ruby Computer vision with special reference to Ruby Jan Wedekind Wed Apr 06 14:00:00 BST 2011 c 2011 Jan Wedekind 1/7
  • 2. http://vision.eng.shu.ac.uk/jan/shef11.pdf Ruby: Open Classes and Closures # open classes class Fixnum def times three File.open( ’test.txt’ , ’r’ ) do |f| 3 * self 3.times { puts f.readline } end end end 4.times three # 12 http://www.ruby-lang.org/ c 2011 Jan Wedekind 2/7
  • 3. http://vision.eng.shu.ac.uk/jan/shef11.pdf Ruby: Ruby Extensions // gcc -shared -fPIC -I/usr/lib/ruby/1.8/x86 64-linux // -o myextension.so myextension.c #include <ruby.h> #include <math.h> VALUE wrap logx( VALUE self, VALUE x ) { return rb float new( log( NUM2DBL( self ) ) / log( NUM2DBL( x ) ) ); } void Init myextension(void) { VALUE numeric = rb const get( rb cObject, rb intern( "Numeric" ) ); rb define method( numeric, "logx" , RUBY METHOD FUNC( wrap logx ), 1 ); } #!/usr/bin/env ruby require ’myextension’ e = 1024.logx( 2 ) puts "2 ** #{e} = 1024" c 2011 Jan Wedekind 3/7
  • 4. http://vision.eng.shu.ac.uk/jan/shef11.pdf Example: Histogram-Based Classification c 2011 Jan Wedekind 4/7
  • 5. http://vision.eng.shu.ac.uk/jan/shef11.pdf Hue-Saturation α = 1 (2 R − G − B) 2 histogram β = sqrt3 (G − B) 2 H ≈ atan2(β, α) C ≈ α2 + β2 « « http://en.wikipedia.org/wiki/HSL_and_HSV c 2011 Jan Wedekind 5/7
  • 6. http://vision.eng.shu.ac.uk/jan/shef11.pdf Similarity Measure ai, j − bi, j similarity(a, b) i, j ai, j + bi, j ⇓ ⇓ ⇓ ⇓ c 2011 Jan Wedekind 6/7
  • 7. http://vision.eng.shu.ac.uk/jan/shef11.pdf Source Code MAXC = 117.0 HBINS, CBINS = 8, 8 THRESHOLD, N = 32, 5 BOX = [220 ... 420, 140 ... 340] class Node def hsv hist(hbins = HBINS, cbins = CBINS) alpha = 2 * r.to sint - g - b beta = Math.sqrt(3) / 2 * ( g.to sint - b ) h = ( (Math.atan2(beta, alpha) + PI) * (HBINS / PI2) ).to int.clip 0 .. HBINS - 1 c = ( Math.sqrt(alpha ** 2 + beta ** 2) * (CBINS / MAXC) ).to int.clip 0 .. CBINS - 1 [h, c].histogram(hbins, cbins).to int end def cmp(other) (self - other) / (self + other).major(1.0) end end input = V4L2Input.new mask = MultiArray.bool(640, 480).fill! mask[*BOX] = true labels = [ ’reference’ , ’dragon’ , ’knight’ , ’camel’ ] hists = labels.collect { |object| MultiArray.load ubytergb( "#{object}.jpg" ).hsv hist } history = [ ’reference’ ] * N X11Display.show do img = input.read ubytergb img hist = img[*BOX].hsv hist similarities = hists.collect { |hist| img hist.cmp(hist).abs.sum } label = labels[similarities.index(similarities.min)] history = [label] + history[0 ... N - 1] if history == [label] * N system "echo ’#{label}’ | festival --tts" if label != ’reference’ end history != [ ’reference’ ] * N ? mask.conditional(img, img >> 1) : img end c 2011 Jan Wedekind 7/7