Class: Webcam

Inherits:
Object
  • Object
show all
Defined in:
lib/magickcam.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.



97
98
99
# File 'lib/magickcam.rb', line 97

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.



132
133
134
# File 'lib/magickcam.rb', line 132

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)


89
90
91
92
93
# File 'lib/magickcam.rb', line 89

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.



110
111
112
113
# File 'lib/magickcam.rb', line 110

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.



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

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)



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

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.



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

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