Class: Webcam

Inherits:
Object
  • Object
show all
Defined in:
lib/rb_webcam.rb

Overview

Class to controll your webcam. Initialize camera, grab image, then close.

Defined Under Namespace

Classes: Image

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(camera_id = 0) ⇒ Webcam

Open camera with camera_id, and size. camera_id: ‘0’ to autodetect.



90
91
92
# File 'lib/rb_webcam.rb', line 90

def initialize(camera_id=0)
  @capture_handler = Highgui.create_camera_capture(camera_id)
end

Instance Attribute Details

#capture_handlerObject (readonly)

Getter for debug use. Internally used to call OpenCV C functions for specified camera.



125
126
127
# File 'lib/rb_webcam.rb', line 125

def capture_handler
  @capture_handler
end

Class Method Details

.open(camera_id = 0) {|webcam| ... } ⇒ Object

Open camera with ‘method with block’ sentence. This will open then close at start and end of block. ex. Webcam.open { |camera| @image = camera.grab }

Yields:

  • (webcam)


82
83
84
85
86
# File 'lib/rb_webcam.rb', line 82

def self.open(camera_id=0)
  webcam = Webcam.new(camera_id)
  yield webcam
  webcam.close
end

Instance Method Details

#closeObject

Close camera. You need close opened camera for cleaner behavior.



103
104
105
106
# File 'lib/rb_webcam.rb', line 103

def close
  Highgui.release_capture(FFI::MemoryPointer.new(:pointer).write_pointer(@capture_handler))
  @capture_handler = nil
end

#grabObject

Grab a frame from camera and returns IplImage struct. This needs camera still opened.



96
97
98
99
100
# File 'lib/rb_webcam.rb', line 96

def grab
  raise "Camera has'nt be initialized" if @capture_handler.nil?
  image = Highgui.query(@capture_handler)
  return Image.new(image)
end

#resolution_modeObject

Get resolution mode of camera. return format is written in resolution_mode=(resolution)



118
119
120
121
# File 'lib/rb_webcam.rb', line 118

def resolution_mode
  {width: Highgui.get_property(@capture_handler, :width),
  height: Highgui.get_property(@capture_handler, :height)}
end

#resolution_mode=(resolution) ⇒ Object

Set resolution of camera output.

usage

@webcam.resolution_mode = 160, height: 120

Available resolution_mode is depends on your camera.



111
112
113
114
# File 'lib/rb_webcam.rb', line 111

def resolution_mode=(resolution)
  Highgui.set_property(@capture_handler, :width, resolution[:width])
  Highgui.set_property(@capture_handler, :height, resolution[:height])
end