An OpenCV wrapper for Ruby.



### Linux/Mac 1. Install OpenCV 2. Install ruby-opencv

$ gem install ruby-opencv -- --with-opencv-dir=/path/to/opencvdir

Note: /path/to/opencvdir is the directory where you installed OpenCV.

Windows (RubyInstaller)


Sample code

### Load and Display an Image

A sample to load and display an image. An equivalent code of this tutorial.

```ruby require ‘opencv’ include OpenCV

if ARGV.size == 0 puts “Usage: ruby #<strong>FILE</strong> ImageToLoadAndDisplay” exit end

image = nil begin image = CvMat.load(ARGV[0], CV_LOAD_IMAGE_COLOR) # Read the file. rescue puts ‘Could not open or find the image.’ exit end

window =‘Display window’) # Create a window for display. # Show our image inside it. GUI::wait_key # Wait for a keystroke in the window. ```

Face Detection

A sample to detect faces from an image.

```ruby require ‘opencv’ include OpenCV

if ARGV.length < 2 puts “Usage: ruby #<strong>FILE</strong> source dest” exit end

data = ‘./data/haarcascades/haarcascade_frontalface_alt.xml’ detector = CvHaarClassifierCascade::load(data) image = CvMat.load(ARGV[0]) detector.detect_objects(image).each do |region| color = CvColor::Blue image.rectangle! region.top_left, region.bottom_right, :color => color end

image.save_image(ARGV[1]) window =‘Face detection’) GUI::wait_key ```

For more samples, see examples/*.rb


The BSD Liscense

see LICENSE.txt