Ruby/OpenCV - An OpenCV Ruby wrapper

(Japanese version is here.)
Introduction

Ruby/OpenCV is a wrapper of OpenCV (a computer vision programming library) for Ruby. It helps you to write computer vision programs (e.g. detecting faces from an image) with Ruby.
There, I'll show you how to install it and use it.


Environment


Install

1. Install OpenCV

$ sudo apt-get install libcv-dev libcv2.1 libcvaux-dev libcvaux2.1 libhighgui-dev libhighgui2.1 opencv-doc


2. Install Ruby/OpenCV

$ git clone git://github.com/jeffrafter/ruby-opencv.git
$ cd ruby-opencv
$ ruby ext/extconf.rb
$ make
$ make install

NOTE: If you installed OpenCV using apt-get on Ubuntu, specify the include path of OpenCV as below:

$ ruby ext/extconf.rb --with-opencv-include=/usr/include/opencv/


How to use

Here is a face detection sample.

#!/usr/bin/env ruby
require 'opencv'
include OpenCV

# Load an image
img = IplImage.load('sample.jpg')

# Load the cascade for detecting faces
detector = CvHaarClassifierCascade::load('haarcascade_frontalface_alt.xml.gz')

# Detect faces and draw rectangles around them
detector.detect_objects(img) { |rect|
  img.rectangle!(rect.top_left, rect.bottom_right, :color => CvColor::Red)
}

# Create a window and show the image
window = GUI::Window.new('Face Detection')
window.show(img)
GUI::wait_key

NOTE: If you use Ubuntu and installed OpenCV as above, the cascade "haarcascade_frontalface_alt.xml.gz" is here:

/usr/share/doc/opencv-doc/examples/haarcascades/haarcascades/haarcascade_frontalface_alt.xml.gz

The result is here.
f:id:ser1zw:20101208224902p:image


For more samples, see examples/*.rb.


Ruby Advent Calendar jp-en: 2010
This entry is for Ruby Advent Calendar jp-en: 2010. The previous post was from tagomoris and the next post will be from takano32.