Ruby Weekly is a weekly newsletter covering the latest Ruby and Rails news.

Camellia: Image processing from Ruby

By Peter Cooper / June 1, 2006

Camellia
In this photo, a Ruby script has found the license plate and marked it with a red box (source below).

Camellia is a powerful 'computer vision' library you can use from Ruby. It's written in C, is cross platform, and offers lots of features like color conversion, warping, filtering, drawing, and labeling. Here's the source code to find the license plate in the photo above:

require 'rubygems'
require_gem 'camellia'
include Camellia

image=CamImage.new

# load picture alfa156.bmp
image.load_bmp("resources/alfa156.bmp")
yuv=image.to_yuv

# consider only the V plane (red)
yuv.set_roi(CamROI.new(3,0,0,yuv.width,yuv.height)) 

# threshold and encode
thr=yuv.encode_threshold(150)

# labeling
blobs=thr.labeling!
puts "#{blobs.nb_blobs} blobs detected"

# draw rectangle on all detected blobs 
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))}

# save the resulting picture
image.save_bmp("output/ruby_alfa156_labeling.bmp")

# find out the biggest blob
sorted=blobs.sort {|a,b| b.surface<=>a.surface}
puts "The bigger blob is at position (#{sorted[0].cx},#{sorted[0].cy}) and its surface is #{sorted[0].surface}"

Update - May 2007: Paul Battley has worked out how to get this all running on Mac OS X.

Technorati Tags: ,

Other Posts to Enjoy

Twitter Mentions