ropencv 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
data/ext/rbind.rb CHANGED
@@ -1,106 +1,79 @@
1
1
  require 'rbind'
2
2
  require 'pp'
3
- require File.join(File.dirname(__FILE__),'specializing.rb')
3
+ require 'yaml'
4
+
5
+ require File.join(File.dirname(__FILE__),'helper.rb')
6
+ opencv_version,opencv_headers = find_opencv
4
7
 
5
8
  rbind = Rbind::Rbind.new("OpenCV")
6
9
  rbind.pkg_config << "opencv"
10
+ rbind.includes = opencv_headers
7
11
 
8
- # find opencv header path
9
- out = IO.popen("pkg-config --cflags-only-I opencv")
10
- paths = out.read.split("-I").delete_if(&:empty?).map do |i|
11
- i.gsub("\n","").gsub(" ","").gsub("opencv","")
12
- end
13
- raise "Cannot find OpenCV" if paths.empty?
14
-
15
- #check opencv version
16
- out = IO.popen("pkg-config --modversion opencv")
17
- opencv_version = out.read.chomp
18
-
19
- ##add opencv headers
20
- if opencv_version >= "2.4.4" && opencv_version <= "2.4.6"
21
- rbind.includes = ["opencv2/core/core_c.h", "opencv2/core/types_c.h",
22
- "opencv2/core/core.hpp", "opencv2/flann/miniflann.hpp",
23
- "opencv2/imgproc/imgproc_c.h", "opencv2/imgproc/types_c.h",
24
- "opencv2/imgproc/imgproc.hpp", "opencv2/photo/photo_c.h",
25
- "opencv2/photo/photo.hpp", "opencv2/video/video.hpp",
26
- "opencv2/features2d/features2d.hpp", "opencv2/objdetect/objdetect.hpp",
27
- "opencv2/calib3d/calib3d.hpp", "opencv2/ml/ml.hpp",
28
- "opencv2/highgui/highgui_c.h", "opencv2/highgui/highgui.hpp",
29
- "opencv2/contrib/contrib.hpp", "opencv2/nonfree/nonfree.hpp",
30
- "opencv2/nonfree/features2d.hpp"]
31
- elsif opencv_version >= "2.4.9"
32
- rbind.includes = ["opencv2/core.hpp", "opencv2/core/types.hpp",
33
- "opencv2/core/utility.hpp", "opencv2/core/base.hpp",
34
- "opencv2/contrib.hpp", "opencv2/calib3d.hpp",
35
- "opencv2/features2d.hpp", "opencv2/flann.hpp",
36
- "opencv2/highgui.hpp", "opencv2/imgproc.hpp",
37
- "opencv2/ml.hpp", "opencv2/nonfree.hpp",
38
- "opencv2/nonfree/features2d.hpp", "opencv2/objdetect.hpp",
39
- "opencv2/photo.hpp", "opencv2/softcascade.hpp",
40
- "opencv2/stitching.hpp", "opencv2/superres.hpp",
41
- "opencv2/video.hpp", "opencv2/legacy.hpp",
42
- "opencv2/videostab.hpp"]
12
+ # add some templates and alias
13
+ rbind.parser.type_alias["const_c_string"] = rbind.c_string.to_const
14
+ if opencv_version >= "2.4.9"
15
+ rbind.add_std_types
43
16
  else
44
- raise "OpenCV version #{opencv_version} is not supported"
17
+ rbind.add_std_vector
45
18
  end
19
+ rbind.parser.add_type OpenCVPtr.new
46
20
 
47
- # check that all headers are available
48
- rbind.includes = rbind.includes.map do |i|
49
- path = File.join(paths[0],i)
50
- if !File.exist?(path)
51
- ::Rbind.log.info "OpenCV version does not support #{path}"
52
- nil
53
- else
54
- path
55
- end
56
- end.compact
57
- Rbind.log.info "found opencv #{opencv_version}: #{paths[0]}"
58
-
59
- # auto add vector and ptr types if missing
60
- rbind.on_type_not_found do |owner,type|
61
- if type =~ /Ptr_(.*)/
62
- t = rbind.parser.find_type(owner,$1)
63
- t2 = Rbind::RPtr.new(type,rbind,t).typedef("cv::Ptr<#{t.full_name} >")
64
- rbind.parser.add_type t2
65
- elsif type =~ /vector_(.*)/
66
- t = rbind.parser.find_type(owner,$1)
67
- t2 = Rbind::RVector.new(type,rbind,t).typedef("std::vector<#{t.full_name} >")
68
- rbind.parser.add_type t2
69
- end
21
+ # add Vec types
22
+ 2.upto(6) do |idx|
23
+ next if idx == 5
24
+ rbind.parser.add_type Vec.new("cv::Vec#{idx}d",rbind.double,idx)
25
+ rbind.parser.add_type Vec.new("cv::Vec#{idx}f",rbind.float,idx)
26
+ rbind.parser.add_type Vec.new("cv::Vec#{idx}i",rbind.int,idx)
70
27
  end
71
28
 
72
29
  # parsing
73
30
  rbind.parse File.join(File.dirname(__FILE__),"pre_opencv244.txt")
74
31
  rbind.parse File.join(File.dirname(__FILE__),"opencv.txt")
75
32
  rbind.use_namespace rbind.cv
76
- rbind.cv.types_alias["string"] = rbind.cv.String
33
+ rbind.use_namespace rbind.std
34
+ rbind.cv.type_alias["string"] = rbind.cv.String
77
35
  rbind.parse_headers
78
36
  rbind.parse File.join(File.dirname(__FILE__),"post_opencv244.txt")
79
37
 
80
38
  # post parsing + patching wrong signatures
81
39
  if opencv_version >= "2.4.9"
82
40
  rbind.parse File.join(File.dirname(__FILE__),"post_opencv249.txt")
83
- rbind.cv.randShuffle.parameter(2).add_flag(:IO)
41
+ rbind.cv.randShuffle.parameter(2).remove_const!
84
42
  end
85
43
 
86
- rbind.cv.CascadeClassifier.detectMultiScale[1].parameter(2).add_flag(:IO)
87
- rbind.cv.CascadeClassifier.detectMultiScale[1].parameter(3).add_flag(:IO)
44
+ rbind.cv.CascadeClassifier.detectMultiScale[1].parameter(2).remove_const!
45
+ rbind.cv.CascadeClassifier.detectMultiScale[1].parameter(3).remove_const!
88
46
 
89
- rbind.cv.BRISK.generateKernel.parameter(0).add_flag(:IO)
90
- rbind.cv.BRISK.generateKernel.parameter(1).add_flag(:IO)
91
- rbind.cv.BRISK.operation("BRISK")[1].parameter(0).add_flag(:IO)
92
- rbind.cv.BRISK.operation("BRISK")[1].parameter(1).add_flag(:IO)
47
+ rbind.cv.BRISK.generateKernel.parameter(0).remove_const!
48
+ rbind.cv.BRISK.generateKernel.parameter(1).remove_const!
49
+ rbind.cv.BRISK.operation("BRISK")[1].parameter(0).remove_const!
50
+ rbind.cv.BRISK.operation("BRISK")[1].parameter(1).remove_const!
93
51
 
94
- rbind.cv.putText.parameter(0).add_flag(:IO)
95
- rbind.cv.chamerMatching.parameter(0).add_flag(:IO)
96
- rbind.cv.chamerMatching.parameter(1).add_flag(:IO)
97
- rbind.cv.chamerMatching.parameter(2).add_flag(:IO)
52
+ rbind.cv.putText.parameter(0).remove_const!
53
+ rbind.cv.chamerMatching.parameter(0).remove_const!
54
+ rbind.cv.chamerMatching.parameter(1).remove_const!
55
+ rbind.cv.chamerMatching.parameter(2).remove_const!
98
56
 
99
57
  # add some more vector types
100
- rbind.parser.find_type(rbind,"vector_Point3f")
101
- rbind.parser.find_type(rbind,"vector_Point3d")
58
+ rbind.parser.type("std::vector<Point3f>")
59
+ rbind.parser.type("std::vector<Point3d>")
60
+ rbind.parser.type("std::vector<Vec4i>")
61
+
62
+ # add opencv docu
63
+ @doc = YAML.load(File.open(File.join(File.dirname(__FILE__),'opencv.yml')).read)
64
+ rbind.parser.each_type do |t|
65
+ if @doc.has_key?(t.full_name)
66
+ t.doc = @doc[t.full_name]
67
+ end
68
+ next unless t.is_a?(Rbind::RNamespace)
69
+ t.each_operation do |op|
70
+ if @doc.has_key?(op.full_name)
71
+ op.doc = @doc[op.full_name]
72
+ end
73
+ end
74
+ end
102
75
 
103
76
  # generate files
104
77
  rbind.generator_ruby.file_prefix = "ropencv"
105
- rbind.generate(File.join(File.dirname(__FILE__),"src"),File.join(File.dirname(__FILE__),"..","lib","ruby","ropencv"))
78
+ rbind.generate(File.join(File.dirname(__FILE__),"src"),File.join(File.dirname(__FILE__),"..","lib","ropencv"))
106
79
 
@@ -22,15 +22,14 @@ ADD_LIBRARY(rbind_opencv SHARED ${RBIND_SRC})
22
22
  TARGET_LINK_LIBRARIES(rbind_opencv ${OPENCV_LIBS} ${OPENCV_LDFLAGS} ${GEM_LIBRARIES})
23
23
 
24
24
  configure_file(${CMAKE_CURRENT_SOURCE_DIR}/rbind.pc.in ${CMAKE_CURRENT_BINARY_DIR}/rbind_opencv.pc @ONLY)
25
- set(ROOT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/../..)
26
25
 
27
- # global install
26
+ set(ROOT_FOLDER ${CMAKE_CURRENT_SOURCE_DIR}/../..)
28
27
  if(GEM_INSTALL)
29
28
  # local install
30
- install(TARGETS rbind_opencv LIBRARY DESTINATION ${ROOT_FOLDER}/lib/lib)
31
- install(FILES types.h operations.h conversions.hpp DESTINATION ${ROOT_FOLDER}/lib/include)
32
- install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/extern.rbind DESTINATION ${ROOT_FOLDER}/lib/include)
33
- install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/config.rbind DESTINATION ${ROOT_FOLDER}/lib/include)
29
+ install(TARGETS rbind_opencv LIBRARY DESTINATION ${ROOT_FOLDER}/lib)
30
+ install(FILES types.h operations.h conversions.hpp DESTINATION ${ROOT_FOLDER}/include)
31
+ install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/extern.rbind DESTINATION ${ROOT_FOLDER}/include)
32
+ install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/config.rbind DESTINATION ${ROOT_FOLDER}/include)
34
33
  else()
35
34
  # global install
36
35
  install(FILES ${CMAKE_CURRENT_BINARY_DIR}/rbind_opencv.pc DESTINATION lib/pkgconfig)
@@ -42,7 +41,7 @@ else()
42
41
  FIND_PACKAGE(Ruby)
43
42
  IF(NOT RUBY_INCLUDE_PATH)
44
43
  MESSAGE(STATUS "Ruby library not found. Cannot install ruby extensions")
45
- ELSEIF(NOT RUBY_EXTENSIONS_AVAILABLE)
44
+ ELSEIF(RUBY_EXTENSIONS_AVAILABLE)
46
45
  STRING(REGEX REPLACE ".*lib(32|64)?/?" "lib/" RUBY_LIBRARY_INSTALL_DIR ${RUBY_RUBY_LIB_PATH})
47
46
  INSTALL(DIRECTORY ${ROOT_FOLDER}/lib/ruby/ DESTINATION ${RUBY_LIBRARY_INSTALL_DIR}
48
47
  FILES_MATCHING PATTERN "*.rb")
@@ -111,10 +111,10 @@ foreach(Gem_NAME ${Gem_FIND_COMPONENTS})
111
111
  endforeach()
112
112
 
113
113
  # Compact the lists
114
- if(${GEM_LIBRARIES})
114
+ if(DEFINED GEM_LIBRARIES)
115
115
  LIST(REMOVE_DUPLICATES GEM_LIBRARIES)
116
116
  endif()
117
- if(${GEM_INCLUDE_DIRS})
117
+ if(DEFINED GEM_INCLUDE_DIRS)
118
118
  LIST(REMOVE_DUPLICATES GEM_INCLUDE_DIRS)
119
119
  endif()
120
120
  endif()
File without changes
@@ -7,7 +7,39 @@ module OpenCV
7
7
  end
8
8
  end
9
9
 
10
+ class Std::Vector::Cv_Mat
11
+ def self.to_native(obj,context)
12
+ if obj.is_a?(Vector::Std_Vector_Cv_Point2f)
13
+ t = Vector::Cv_Mat.new
14
+ obj.each do |e|
15
+ t << cv::Mat.new(e.size,2,cv::CV_32FC1,e.data,cv::Mat::AUTO_STEP)
16
+ end
17
+ t.__obj_ptr__
18
+ elsif obj.is_a?(Vector::Std_Vector_Cv_Point)
19
+ t = Vector::Cv_Mat.new
20
+ obj.each do |e|
21
+ t << cv::Mat.new(e.size,2,cv::CV_32SC1,e.data,cv::Mat::AUTO_STEP)
22
+ end
23
+ t.__obj_ptr__
24
+ else
25
+ rbind_to_native(obj,context)
26
+ end
27
+ end
28
+ end
29
+ include Std
30
+
10
31
  module Cv
32
+ class Size
33
+ def *(val)
34
+ Size.new(width*val,height*val)
35
+ end
36
+ def +(val)
37
+ Size.new(width+val,height+val)
38
+ end
39
+ def -(val)
40
+ Size.new(width-val,height-val)
41
+ end
42
+ end
11
43
  class String
12
44
  def self.to_native(obj,context)
13
45
  if obj.is_a? ::String
@@ -22,69 +54,76 @@ module OpenCV
22
54
  end
23
55
  end
24
56
 
25
- class Scalar
26
- def []=(i,value=nil)
27
- raise "out of bound #{value}" if value < 0 || value > 3
28
- val.put_float64(i*8,value)
29
- value
30
- end
31
- end
32
-
33
- class Vec2d
34
- def [](i)
35
- raise "Out of bound #{i}" if i < 0 || i > 1
36
- val.get_float64(i*8)
37
- end
38
- def []=(i,val0)
39
- raise "Out of bound #{i}" if i < 0 || i > 1
40
- val.put_float64(i*8,val0)
57
+ class Point
58
+ def self.to_native(obj,context)
59
+ if obj.is_a? ::OpenCV::Cv::Point2f
60
+ OpenCV::Cv::Point.new(obj.x,obj.y).__obj_ptr__
61
+ else
62
+ rbind_to_native(obj,context)
63
+ end
41
64
  end
42
65
  end
43
66
 
44
- class Vec3d
67
+ module Vecxd
45
68
  def [](i)
46
- raise "Out of bound #{i}" if i < 0 || i > 2
69
+ raise "Out of bound #{i}" if i < 0 || i >= self.class::SIZE
47
70
  val.get_float64(i*8)
48
71
  end
49
72
  def []=(i,val0)
50
- raise "Out of bound #{i}" if i < 0 || i > 2
73
+ raise "Out of bound #{i}" if i < 0 || i >= self.class::SIZE
51
74
  val.put_float64(i*8,val0)
52
75
  end
53
- end
54
-
55
- class Vec4d
56
- def [](i)
57
- raise "Out of bound #{i}" if i < 0 || i > 3
58
- val.get_float64(i*8)
59
- end
60
- def []=(i,val0)
61
- raise "Out of bound #{i}" if i < 0 || i > 3
62
- val.put_float64(i*8,val0)
76
+ def to_a
77
+ val.get_array_of_float64(0,self.class::SIZE)
63
78
  end
64
79
  end
65
80
 
66
- class Vec4f
81
+ module Vecxf
67
82
  def [](i)
68
- raise "Out of bound #{i}" if i < 0 || i > 3
83
+ raise "Out of bound #{i}" if i < 0 || i >= self.class::SIZE
69
84
  val.get_float32(i*4)
70
85
  end
71
86
  def []=(i,val0)
72
- raise "Out of bound #{i}" if i < 0 || i > 3
87
+ raise "Out of bound #{i}" if i < 0 || i >= self.class::SIZE
73
88
  val.put_float32(i*4,val0)
74
89
  end
90
+ def to_a
91
+ val.get_array_of_float32(0,self.class::SIZE)
92
+ end
75
93
  end
76
-
77
- class Vec6f
94
+
95
+ module Vecxi
78
96
  def [](i)
79
- raise "Out of bound #{i}" if i < 0 || i > 5
80
- val.get_float32(i*4)
97
+ raise "Out of bound #{i}" if i < 0 || i >= self.class::SIZE
98
+ val.get_int(i*FFI.type_size(FFI::Type::INT))
81
99
  end
82
100
  def []=(i,val0)
83
- raise "Out of bound #{i}" if i < 0 || i > 5
84
- val.put_float32(i*4,val0)
101
+ raise "Out of bound #{i}" if i < 0 || i >= self.class::SIZE
102
+ val.put_int(i*FFI.type_size(FFI::Type::INT),val0)
103
+ end
104
+ def to_a
105
+ val.get_array_of_int(0,self.class::SIZE)
85
106
  end
86
107
  end
87
108
 
109
+ class Vec2d;include Vecxd; SIZE=2;end
110
+ class Vec2f;include Vecxf; SIZE=2;end
111
+ class Vec2i;include Vecxi; SIZE=2;end
112
+
113
+ class Vec3d;include Vecxd; SIZE=3;end
114
+ class Vec3f;include Vecxf; SIZE=3;end
115
+ class Vec3i;include Vecxi; SIZE=3;end
116
+
117
+ class Vec4d;include Vecxd; SIZE=4;end
118
+ class Vec4f;include Vecxf; SIZE=4;end
119
+ class Vec4i;include Vecxi; SIZE=4;end
120
+
121
+ class Vec6d;include Vecxd; SIZE=6;end
122
+ class Vec6f;include Vecxf; SIZE=6;end
123
+ class Vec6i;include Vecxi; SIZE=6;end
124
+
125
+ class Scalar; include Vecxd; SIZE=4;end
126
+
88
127
  class Mat
89
128
  class << self
90
129
  alias :rbind_new :new
@@ -100,9 +139,11 @@ module OpenCV
100
139
  end
101
140
 
102
141
  def self.to_native(obj,context)
103
- if obj.is_a?(VectorPoint2f)
142
+ if obj.is_a?(Std::Vector::Cv_Point)
143
+ cv::Mat.new(obj.size,2,cv::CV_32SC1,obj.data,cv::Mat::AUTO_STEP).__obj_ptr__
144
+ elsif obj.is_a?(Std::Vector::Cv_Point2f)
104
145
  cv::Mat.new(obj.size,2,cv::CV_32FC1,obj.data,cv::Mat::AUTO_STEP).__obj_ptr__
105
- elsif obj.is_a?(VectorPoint3f)
146
+ elsif obj.is_a?(Std::Vector::Cv_Point3f)
106
147
  cv::Mat.new(obj.size,3,cv::CV_32FC1,obj.data,cv::Mat::AUTO_STEP).__obj_ptr__
107
148
  elsif obj.is_a?(Array)
108
149
  h,w,e= if obj.first.is_a? Array
@@ -151,17 +192,25 @@ module OpenCV
151
192
  [i,j]
152
193
  end
153
194
  if i >= rows || i < 0 || j >= cols || j <0
154
- raise ArgumentError,"out of bound #{i}/#{j}"
195
+ raise ArgumentError,"out of bound #{i}/#{j} #{rows}/#{cols}"
155
196
  end
156
197
  case type & 7
157
198
  when CV_8U
158
199
  data.get_uint8(i*step+j)
200
+ when CV_16U
201
+ data.get_uint16(i*step+j*2)
202
+ when CV_16S
203
+ data.get_int16(i*step+j*2)
204
+ when CV_32S
205
+ data.get_int32(i*step+j*4)
206
+ when CV_32F
207
+ data.get_float32(i*step+j*4)
159
208
  when CV_32F
160
209
  data.get_float32(i*step+j*4)
161
210
  when CV_64F
162
211
  data.get_float64(i*step+j*8)
163
212
  else
164
- raise "cannot connvert #{self.class} to ruby"
213
+ raise "cannot connvert #{self.class} of type #{type} to ruby"
165
214
  end
166
215
  end
167
216
 
@@ -210,12 +259,18 @@ module OpenCV
210
259
  case type & 7
211
260
  when CV_8U
212
261
  data.put_uint8(i*step+j,val)
262
+ when CV_16U
263
+ data.put_uint16(i*step+j,val*2)
264
+ when CV_16S
265
+ data.put_int16(i*step+j,val*2)
266
+ when CV_32S
267
+ data.put_int32(i*step+j,val*4)
213
268
  when CV_32F
214
269
  data.put_float32(i*step+j*4,val)
215
270
  when CV_64F
216
271
  data.put_float64(i*step+j*8,val)
217
272
  else
218
- raise "cannot connvert #{self.class} to ruby"
273
+ raise "cannot connvert #{self.class} of type #{type} to ruby"
219
274
  end
220
275
  end
221
276
 
@@ -274,12 +329,7 @@ module OpenCV
274
329
  else
275
330
  '%6.3f'
276
331
  end
277
- a = if cols == 1 || rows == 1
278
- [to_a]
279
- else
280
- to_a
281
- end
282
- str = a.map do |r|
332
+ str = to_a.map do |r|
283
333
  str = r.map do |e|
284
334
  sprintf(format,e)
285
335
  end.join(" ")
@@ -293,6 +343,12 @@ module OpenCV
293
343
  getter = case type & 7
294
344
  when CV_8U
295
345
  ptr.method(:get_array_of_uint8)
346
+ when CV_16U
347
+ ptr.method(:get_array_of_uint16)
348
+ when CV_16S
349
+ ptr.method(:get_array_of_int16)
350
+ when CV_32S
351
+ ptr.method(:get_array_of_int32)
296
352
  when CV_32F
297
353
  ptr.method(:get_array_of_float32)
298
354
  when CV_64F
@@ -300,15 +356,11 @@ module OpenCV
300
356
  else
301
357
  raise "cannot connvert #{self.class} to array"
302
358
  end
303
- if w == 1 || h == 1
304
- getter.call(0,total)
305
- else
306
- result = []
307
- 0.upto(h-1) do |i|
308
- result << getter.call(s*i,w*c)
309
- end
310
- result
359
+ result = []
360
+ 0.upto(h-1) do |i|
361
+ result << getter.call(s*i,w*c)
311
362
  end
363
+ result
312
364
  end
313
365
  end
314
366
  end