gpt4 book ai didi

ruby - Highgui 和 ruby

转载 作者:数据小太阳 更新时间:2023-10-29 07:05:48 26 4
gpt4 key购买 nike

我需要编写一个简单的项目,我正在使用 opencv、ruby 和 mac。我已经通过 brew 安装了 opencv,通过 gem 安装了 rb_webcam。

# -*- coding: utf-8 -*-

require "opencv"
require "rb_webcam"

capture = Webcam.new

这段代码抛出

$ ruby tracking.rb
/Users/evilgeniuz/.rvm/gems/ruby-1.9.3-p125/gems/nice-ffi-0.4/lib/nice-ffi/library.rb:98:in `load_library': Could not load highgui. (LoadError)
from /Users/evilgeniuz/.rvm/gems/ruby-1.9.3-p125/gems/rb_webcam-0.3.0/lib/rb_webcam.rb:7:in `<module:Highgui>'
from /Users/evilgeniuz/.rvm/gems/ruby-1.9.3-p125/gems/rb_webcam-0.3.0/lib/rb_webcam.rb:4:in `<top (required)>'
from /Users/evilgeniuz/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `require'
from /Users/evilgeniuz/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
from /Users/evilgeniuz/.rvm/rubies/ruby-1.9.3-p125/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from tracking.rb:4:in `<main>'

我不知道如何指出 highgui 的位置。

更新:通过从这里下载 gem 解决它 https://github.com/TyounanMOTI/rb_webcam并手动构建和安装它。

最佳答案

不确定您使用的是哪个包装器,但您应该看看这个:Ruby/OpenCV - An OpenCV Ruby wrapper .

人脸检测示例:

#!/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) do |rect|
img.rectangle!(rect.top_left, rect.bottom_right, color: CvColor::Red)
end

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

分类器可以下载here .

编辑:

The following code uses OpenCV, the rb_webcam gem, and RMagick to capture an image from a webcam and save it as a jpg file:

require 'rb_webcam'
require 'RMagick'

capture = Webcam.new

image = capture.grab
width = image.size[:width]
rows = image.data.unpack("C*").each_slice(3).to_a.each_slice(width).to_a
capture.close

height = rows.length
img = Magick::Image.new width, height

rows.each_with_index do |r, i|
q = r.map {|b, g, r| Magick::Pixel.new r * 256, g * 256, b * 256, 0}
img.store_pixels(0, i, width, 1, q)
end

img.format = 'jpg'
img.write 'webcam.jpg'

关于ruby - Highgui 和 ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10215736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com