An OpenCV wrapper for Ruby.
- Web site: https://github.com/ruby-opencv/ruby-opencv
- Ruby 1.9.3, 2.0.0 and OpenCV 2.4.5 are supported.
- Install OpenCV
- Install ruby-opencv
$ gem install ruby-opencv -- --with-opencv-dir=/path/to/opencvdir
Note: /path/to/opencvdir is the directory where you installed OpenCV.
You can use pre-build binary for Windows (mswin32, mingw32).
- Install OpenCV
- Set path to OpenCV libraries. When you installed OpenCV to C:\opencv, add C:\opencv\build\x86\vc10\bin (for mswin32) or C:\opencv\build\x86\mingw\bin (for mingw32) to the systems path.
- Install ruby-opencv
$ gem install ruby-opencv
A sample to load and display an image. An equivalent code of this tutorial.
require'opencv'includeOpenCVifARGV.size == 0puts"Usage: ruby #{__FILE__} ImageToLoadAndDisplay"exitendimage=nilbeginimage=CvMat.load(ARGV[0],CV_LOAD_IMAGE_COLOR)# Read the file.rescueputs'Could not open or find the image.'exitendwindow=GUI::Window.new('Display window')# Create a window for display.window.show(image)# Show our image inside it.GUI::wait_key# Wait for a keystroke in the window.A sample to detect faces from an image.
require'opencv'includeOpenCVifARGV.length < 2puts"Usage: ruby #{__FILE__} source dest"exitenddata='./data/haarcascades/haarcascade_frontalface_alt.xml'detector=CvHaarClassifierCascade::load(data)image=CvMat.load(ARGV[0])detector.detect_objects(image).eachdo |region|
color=CvColor::Blueimage.rectangle!region.top_left,region.bottom_right,:color=>colorendimage.save_image(ARGV[1])window=GUI::Window.new('Face detection')window.show(image)GUI::wait_keyFor more samples, see examples/*.rb
The BSD Liscense
see LICENSE.txt