magickcam 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/magickcam.rb +174 -1
- metadata +3 -5
data/lib/magickcam.rb
CHANGED
@@ -2,8 +2,180 @@
|
|
2
2
|
|
3
3
|
# file: magickcam.rb
|
4
4
|
|
5
|
-
require 'rb_webcam'
|
5
|
+
#require 'rb_webcam'
|
6
6
|
require 'RMagick'
|
7
|
+
require 'nice-ffi'
|
8
|
+
|
9
|
+
# wrapper module of OpenCV::Highgui
|
10
|
+
module Highgui
|
11
|
+
extend NiceFFI::Library
|
12
|
+
|
13
|
+
load_library("opencv_highgui")
|
14
|
+
|
15
|
+
class CvCapture < NiceFFI::OpaqueStruct
|
16
|
+
end
|
17
|
+
|
18
|
+
SIGNED_FLAG = 0x80000000
|
19
|
+
enum :color_depth, [ :unsinged_1bit, 1,
|
20
|
+
:unsigned_8bit, 8,
|
21
|
+
:unsigned_16bit, 16,
|
22
|
+
:unsinged_32bit, 32,
|
23
|
+
:signed_8bit, SIGNED_FLAG | 8,
|
24
|
+
:signed_16bit, SIGNED_FLAG | 16,
|
25
|
+
:signed_32bit, SIGNED_FLAG | 32 ]
|
26
|
+
|
27
|
+
# Grabbed webcam image will be stored in this structure.
|
28
|
+
# http://opencv.willowgarage.com/documentation/basic_structures.html#iplimage
|
29
|
+
class IplImage < NiceFFI::Struct
|
30
|
+
layout :n_size, :int,
|
31
|
+
:id, :int,
|
32
|
+
:n_channels, :int,
|
33
|
+
:alpha_channel, :int,
|
34
|
+
:depth, :color_depth,
|
35
|
+
:color_mode, [:char, 4], # pointer to char[4]
|
36
|
+
:channel_seq, [:char, 4], # pointer to char[4]
|
37
|
+
:data_order, :int,
|
38
|
+
:origin, :int,
|
39
|
+
:align, :int,
|
40
|
+
:width, :int,
|
41
|
+
:height, :int,
|
42
|
+
:roi, :pointer, # pointer to struct _IplROI
|
43
|
+
:mask_roi, :pointer, # pointer to struct _IplImage
|
44
|
+
:image_id, :pointer, # pointer to void
|
45
|
+
:tile_info, :pointer, # pointer to struct _IplTileInfo
|
46
|
+
:image_size, :int,
|
47
|
+
:image_data, :pointer, # pointer to char array
|
48
|
+
:width_step, :int,
|
49
|
+
:border_mode, [:int, 4], # pointer to int[4]
|
50
|
+
:border_const, [:int, 4], # pointer to int[4]
|
51
|
+
:image_data_origin, :pointer # pointer to char array
|
52
|
+
end
|
53
|
+
|
54
|
+
enum :property, [ :position_msec, 0,
|
55
|
+
:position_frames,
|
56
|
+
:position_avi_ratio,
|
57
|
+
:width,
|
58
|
+
:height,
|
59
|
+
:fps,
|
60
|
+
:fourcc,
|
61
|
+
:frame_count,
|
62
|
+
:format,
|
63
|
+
:mode,
|
64
|
+
:brightness,
|
65
|
+
:contrast,
|
66
|
+
:saturation,
|
67
|
+
:hue,
|
68
|
+
:gain,
|
69
|
+
:exposure,
|
70
|
+
:convert_rgb,
|
71
|
+
:white_balance,
|
72
|
+
:rectification ]
|
73
|
+
|
74
|
+
attach_function :create_camera_capture, :cvCreateCameraCapture, [:int], :pointer
|
75
|
+
attach_function :release_capture, :cvReleaseCapture, [:pointer], :void
|
76
|
+
attach_function :query, :cvQueryFrame, [:pointer], IplImage.typed_pointer
|
77
|
+
attach_function :get_property, :cvGetCaptureProperty, [:pointer, :property], :double
|
78
|
+
attach_function :set_property, :cvSetCaptureProperty, [:pointer, :property, :double], :int
|
79
|
+
attach_function :save_image, :cvSaveImage, [:string, IplImage], :int
|
80
|
+
end
|
81
|
+
|
82
|
+
# Class to controll your webcam.
|
83
|
+
# Initialize camera, grab image, then close.
|
84
|
+
class Webcam
|
85
|
+
# Open camera with 'method with block' sentence.
|
86
|
+
# This will open then close at start and end of block.
|
87
|
+
# ex.
|
88
|
+
# Webcam.open { |camera| @image = camera.grab }
|
89
|
+
def self.open(camera_id=0)
|
90
|
+
webcam = Webcam.new(camera_id)
|
91
|
+
yield webcam
|
92
|
+
webcam.close
|
93
|
+
end
|
94
|
+
|
95
|
+
# Open camera with camera_id, and size.
|
96
|
+
# camera_id: '0' to autodetect.
|
97
|
+
def initialize(camera_id=0)
|
98
|
+
@capture_handler = Highgui.create_camera_capture(camera_id)
|
99
|
+
end
|
100
|
+
|
101
|
+
# Grab a frame from camera and returns IplImage struct.
|
102
|
+
# This needs camera still opened.
|
103
|
+
def grab
|
104
|
+
raise "Camera has'nt be initialized" if @capture_handler.nil?
|
105
|
+
image = Highgui.query(@capture_handler)
|
106
|
+
return Image.new(image)
|
107
|
+
end
|
108
|
+
|
109
|
+
# Close camera. You need close opened camera for cleaner behavior.
|
110
|
+
def close
|
111
|
+
Highgui.release_capture(FFI::MemoryPointer.new(:pointer).write_pointer(@capture_handler))
|
112
|
+
@capture_handler = nil
|
113
|
+
end
|
114
|
+
|
115
|
+
# Set resolution of camera output.
|
116
|
+
# [usage] @webcam.resolution_mode = {width: 160, height: 120}
|
117
|
+
# Available resolution_mode is depends on your camera.
|
118
|
+
def resolution_mode=(resolution)
|
119
|
+
Highgui.set_property(@capture_handler, :width, resolution[:width])
|
120
|
+
Highgui.set_property(@capture_handler, :height, resolution[:height])
|
121
|
+
end
|
122
|
+
|
123
|
+
# Get resolution mode of camera.
|
124
|
+
# return format is written in resolution_mode=(resolution)
|
125
|
+
def resolution_mode
|
126
|
+
{width: Highgui.get_property(@capture_handler, :width),
|
127
|
+
height: Highgui.get_property(@capture_handler, :height)}
|
128
|
+
end
|
129
|
+
|
130
|
+
# Getter for debug use.
|
131
|
+
# Internally used to call OpenCV C functions for specified camera.
|
132
|
+
attr_reader :capture_handler
|
133
|
+
|
134
|
+
# Property Container of image from webcam.
|
135
|
+
# Get size, color_depth, mode, etc.. of image.
|
136
|
+
# Each instance have IplImage struct data.
|
137
|
+
# TODO: Having pointer to IplImage struct will be good for performance?
|
138
|
+
# (Does whole structure copying occur at Image.new(iplimage) ?)
|
139
|
+
class Image
|
140
|
+
def initialize(iplimage_struct)
|
141
|
+
@iplimage_struct = iplimage_struct
|
142
|
+
end
|
143
|
+
|
144
|
+
# get size of image. Hash with :width, :height elements.
|
145
|
+
def size
|
146
|
+
{width: @iplimage_struct.width, height: @iplimage_struct.height}
|
147
|
+
end
|
148
|
+
|
149
|
+
# get color depth of image.
|
150
|
+
def color_depth
|
151
|
+
@iplimage_struct.depth
|
152
|
+
end
|
153
|
+
|
154
|
+
# String of image data
|
155
|
+
def data
|
156
|
+
self.data_pointer.read_string(self.data_size)
|
157
|
+
end
|
158
|
+
|
159
|
+
# FFI::Pointer to image data
|
160
|
+
def data_pointer
|
161
|
+
@iplimage_struct.image_data
|
162
|
+
end
|
163
|
+
|
164
|
+
# data size of image
|
165
|
+
def data_size
|
166
|
+
@iplimage_struct.image_size
|
167
|
+
end
|
168
|
+
|
169
|
+
# save the image to "name" incl. extension
|
170
|
+
def save(name)
|
171
|
+
Highgui.save_image name, @iplimage_struct
|
172
|
+
end
|
173
|
+
|
174
|
+
# IplImage structure stored grabbed image from camera.
|
175
|
+
attr_reader :iplimage_struct
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
7
179
|
|
8
180
|
class MagickCam
|
9
181
|
|
@@ -21,6 +193,7 @@ class MagickCam
|
|
21
193
|
img.store_pixels(0, i, width, 1, q)
|
22
194
|
end
|
23
195
|
img.format = 'jpg'
|
196
|
+
puts 'saving ' + filename
|
24
197
|
img.write filename
|
25
198
|
end
|
26
199
|
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: magickcam
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- James Robertson
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date:
|
14
|
-
default_executable:
|
13
|
+
date: 2012-12-01 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: rmagick
|
@@ -34,7 +33,6 @@ extra_rdoc_files: []
|
|
34
33
|
|
35
34
|
files:
|
36
35
|
- lib/magickcam.rb
|
37
|
-
has_rdoc: true
|
38
36
|
homepage:
|
39
37
|
licenses: []
|
40
38
|
|
@@ -58,7 +56,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
56
|
requirements: []
|
59
57
|
|
60
58
|
rubyforge_project:
|
61
|
-
rubygems_version: 1.
|
59
|
+
rubygems_version: 1.8.23
|
62
60
|
signing_key:
|
63
61
|
specification_version: 3
|
64
62
|
summary: magickcam = rb_webcam + rmagick
|