test_labeling.rb
  1| require 'rubygems'
  2| require_gem 'camellia'
  3| include Camellia
  4|     
  5| image=CamImage.new
  6| 
  7| # load picture alfa156.bmp
  8| image.load_bmp("resources/alfa156.bmp")
  9| yuv=image.to_yuv
 10| 
 11| # consider only the V plane (red)
 12| yuv.set_roi(CamROI.new(3,0,0,yuv.width,yuv.height)) 
 13| 
 14| # threshold and encode
 15| thr=yuv.encode_threshold(150)
 16| 
 17| # labeling
 18| blobs=thr.labeling!
 19| puts "#{blobs.nb_blobs} blobs detected"
 20| 
 21| # draw rectangle on all detected blobs 
 22| blobs.each {|b| image.draw_rectangle(b.left,b.top,b.left+b.width-1,b.top+b.height-1,cam_rgb(255,0,0))}
 23| 
 24| # save the resulting picture
 25| image.save_bmp("output/ruby_alfa156_labeling.bmp")
 26| 
 27| # find out the biggest blob
 28| sorted=blobs.sort {|a,b| b.surface<=>a.surface}
 29| puts "The bigger blob is at position (#{sorted[0].cx},#{sorted[0].cy}) and its surface is #{sorted[0].surface}"