test_color_labeling.rb
  1| require 'rubygems'
  2| require_gem 'camellia'
  3| include Camellia
  4| 
  5| # load picture alfa156.bmp
  6| image=CamImage.new
  7| image.load_bmp("resources/alfa156.bmp")
  8| yuv=image.to_yuv
  9| 
 10| # set color clusters
 11| clusters=CamTable.new
 12| clusters.set([
 13|   # Ymin Ymax Umin Umax Vmin Vmax
 14|     0,   60,  0,   255, 0,   255, # Black
 15|     230, 255, 0,   255, 0,   255, # White
 16|     0,   255, 0,   255, 140, 255  # Red
 17| ])
 18| cluster_colors=[cam_rgb(0,0,0),cam_rgb(255,255,255),cam_rgb(255,0,0)]
 19| 
 20| # threshold and encode
 21| encoded=yuv.encode_color(clusters)
 22| puts "Number of runs : #{encoded.nb_runs}"
 23|  
 24| # labeling
 25| blobs=encoded.labeling!    
 26| puts "#{blobs.nb_blobs} blobs detected"
 27|  
 28| # print and deaw the results 
 29| i=0
 30| blobs.each {|b|
 31|   puts "Blob #{i} : Val=#{b.value} (#{b.top},#{b.left},#{b.width},#{b.height}) Surface=#{b.surface}"
 32|   image.draw_rectangle(b.left,b.top,b.left+b.width-1,b.top+b.height-1,cluster_colors[b.value-1])
 33|   i=i+1
 34| }
 35|  
 36| # save the result
 37| image.save_bmp("output/ruby_alfa156_labeling.bmp")