ruby-opencv 0.0.8-x86-mingw32 → 0.0.9.pre2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/DEVELOPERS_NOTE.md +29 -12
  3. data/Gemfile +1 -2
  4. data/License.txt +30 -30
  5. data/Manifest.txt +5 -4
  6. data/README.md +1 -1
  7. data/Rakefile +62 -4
  8. data/config.yml +7 -0
  9. data/examples/alpha_blend.rb +21 -21
  10. data/examples/find_obj.rb +169 -169
  11. data/examples/match_kdtree.rb +88 -88
  12. data/ext/opencv/cvcapture.cpp +19 -12
  13. data/ext/opencv/cvutils.cpp +192 -194
  14. data/ext/opencv/cvutils.h +30 -29
  15. data/{extconf.rb → ext/opencv/extconf.rb} +12 -4
  16. data/lib/opencv.rb +12 -3
  17. data/lib/opencv/psyched_yaml.rb +22 -22
  18. data/lib/opencv/version.rb +1 -1
  19. data/ruby-opencv.gemspec +17 -16
  20. data/test/helper.rb +1 -1
  21. data/test/runner.rb +30 -30
  22. data/test/test_curve.rb +1 -1
  23. data/test/test_cvavgcomp.rb +24 -24
  24. data/test/test_cvbox2d.rb +76 -76
  25. data/test/test_cvcapture.rb +183 -183
  26. data/test/test_cvchain.rb +108 -108
  27. data/test/test_cvcircle32f.rb +41 -41
  28. data/test/test_cvconnectedcomp.rb +61 -61
  29. data/test/test_cvcontour.rb +150 -150
  30. data/test/test_cvcontourtree.rb +43 -43
  31. data/test/test_cverror.rb +1 -1
  32. data/test/test_cvfeaturetree.rb +65 -65
  33. data/test/test_cvfont.rb +58 -58
  34. data/test/test_cvhaarclassifiercascade.rb +63 -63
  35. data/test/test_cvhistogram.rb +1 -1
  36. data/test/test_cvhumoments.rb +83 -83
  37. data/test/test_cvline.rb +50 -50
  38. data/test/test_cvmat.rb +1 -1
  39. data/test/test_cvmat_drawing.rb +1 -1
  40. data/test/test_cvmat_dxt.rb +1 -1
  41. data/test/test_cvmat_imageprocessing.rb +1 -1
  42. data/test/test_cvmat_matching.rb +1 -1
  43. data/test/test_cvmoments.rb +180 -180
  44. data/test/test_cvpoint.rb +75 -75
  45. data/test/test_cvpoint2d32f.rb +75 -75
  46. data/test/test_cvpoint3d32f.rb +93 -93
  47. data/test/test_cvrect.rb +144 -144
  48. data/test/test_cvscalar.rb +113 -113
  49. data/test/test_cvseq.rb +295 -295
  50. data/test/test_cvsize.rb +75 -75
  51. data/test/test_cvsize2d32f.rb +75 -75
  52. data/test/test_cvslice.rb +31 -31
  53. data/test/test_cvsurfparams.rb +57 -57
  54. data/test/test_cvsurfpoint.rb +66 -66
  55. data/test/test_cvtermcriteria.rb +56 -56
  56. data/test/test_cvtwopoints.rb +40 -40
  57. data/test/test_cvvideowriter.rb +58 -58
  58. data/test/test_iplconvkernel.rb +54 -54
  59. data/test/test_iplimage.rb +1 -1
  60. data/test/test_mouseevent.rb +17 -17
  61. data/test/test_opencv.rb +1 -1
  62. data/test/test_pointset.rb +1 -1
  63. data/test/test_preliminary.rb +130 -130
  64. data/test/test_trackbar.rb +47 -47
  65. data/test/test_window.rb +115 -115
  66. metadata +26 -54
  67. data/ext/opencv/lib/opencv.rb +0 -3
  68. data/ext/opencv/lib/opencv/psyched_yaml.rb +0 -22
  69. data/ext/opencv/lib/opencv/version.rb +0 -3
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0a4168af4998ed69e69f9cc4f65cc4823f0bc5b9
4
+ data.tar.gz: 9cc4ad8a1fe60f3ae95c70837151381f4a6f9d13
5
+ SHA512:
6
+ metadata.gz: 1f78f622de1d449ce2d9882887f04ed35181a9394d8e856a6f939fa4a28892c6311a2996d5992cd59353e81ea07e661d81086d2ae5c8d5b77d83ad1c5f522931
7
+ data.tar.gz: 94ecb941ed131422d4ba69c0d067907c569915ca70817b62852ed6d5d34b16c6bbc645e35ddd553575781d23496c253adebd27df63f863ee840ba8f01a2f1c2c
@@ -14,12 +14,11 @@
14
14
  * [hoe](https://github.com/seattlerb/hoe)
15
15
  * [hoe-gemspec](https://github.com/flavorjones/hoe-gemspec)
16
16
  * [rake-compiler](https://github.com/luislavena/rake-compiler)
17
- * [gem-compile](https://github.com/frsyuki/gem-compile)
18
17
 
19
18
 
20
19
  ## Create ruby-opencv gem
21
20
  Run the following commands.
22
- When you use mingw32, use **MSYS console**, or when you use mswin32,
21
+ When you use mingw32, use **MSYS console**, or when you use mswin32,
23
22
  use [**Visual Studio Command Prompt**](http://msdn.microsoft.com/en-us/library/ms229859.aspx)
24
23
  instead of cmd.exe.
25
24
 
@@ -32,16 +31,34 @@ $ git ls-files > Manifest.txt
32
31
  $ rake gem:spec
33
32
  $ rake gem
34
33
  ```
35
- **ruby-opencv-x.y.z.gem** will be created in pkg/ directory.
34
+ **ruby-opencv-x.y.z.gem** will be created in **pkg** directory.
36
35
 
37
- To create pre-build binaries, run the following commands in Windows.
36
+ To create pre-build binaries, create a config file firstly:
38
37
 
38
+ ```yml
39
+ # config.yml
40
+ platform: mingw32
41
+ rubies:
42
+ - C:/ruby-1.9.3-p392-mingw32/bin/ruby.exe
43
+ - C:/ruby-2.0.0-p0-mingw32/bin/ruby.exe
44
+ extopts:
45
+ - --with-opencv-include=C:/opencv/build/include
46
+ - --with-opencv-lib=C:/opencv/build/x86/mingw/lib
39
47
  ```
40
- $ cd pkg
41
- $ gem compile ruby-opencv-*.gem
48
+
49
+ Entries are below:
50
+
51
+ - **platform**: Target platform (e.g. mingw32, mswin32)
52
+ - **rubies**: Array of target versions of ruby's paths (You can create fat gems if you specify multiple versions of ruby)
53
+ - **extopts**: Array of options to be passed to **extconf.rb**
54
+
55
+ Then, run the following command:
56
+
57
+ ```
58
+ $ rake gem:precompile CONFIG=config.yml
42
59
  ```
43
60
 
44
- **ruby-opencv-x.y.z-x86-mingw32.gem** will be created when you use mingw32, or
61
+ **ruby-opencv-x.y.z-mingw32.gem** will be created when you use mingw32, or
45
62
  **ruby-opencv-x.y.z-x86-mswin32.gem** when you use mswin32.
46
63
 
47
64
 
@@ -52,7 +69,7 @@ $ gem compile ruby-opencv-*.gem
52
69
  $ git clone git://github.com/ruby-opencv/ruby-opencv.git
53
70
  $ cd ruby-opencv
54
71
  $ git checkout master
55
- $ ruby extconf.rb --with-opencv-dir=/path/to/opencvdir
72
+ $ ruby ext/opencv/extconf.rb --with-opencv-dir=/path/to/opencvdir
56
73
  $ make
57
74
  $ make install
58
75
  ```
@@ -68,7 +85,7 @@ Run the following commands on [**Visual Studio Command Prompt**](http://msdn.mic
68
85
  $ git clone git://github.com/ruby-opencv/ruby-opencv.git
69
86
  $ cd ruby-opencv
70
87
  $ git checkout master
71
- $ ruby extconf.rb --with-opencv-dir=C:\path\to\opencvdir\install # for your own built OpenCV library
88
+ $ ruby ext/opencv/extconf.rb --with-opencv-dir=C:\path\to\opencvdir\install # for your own built OpenCV library
72
89
  $ nmake
73
90
  $ nmake install
74
91
  ```
@@ -76,7 +93,7 @@ $ nmake install
76
93
  To use pre-built OpenCV libraries, set the following option to extconf.rb.
77
94
 
78
95
  ```
79
- $ ruby extconf.rb --with-opencv-include=C:\path\to\opencvdir\build\include --with-opencv-lib=C:\path\to\opencvdir\build\x86\vc10\lib
96
+ $ ruby ext/opencv/extconf.rb --with-opencv-include=C:\path\to\opencvdir\build\include --with-opencv-lib=C:\path\to\opencvdir\build\x86\vc10\lib
80
97
  ```
81
98
 
82
99
 
@@ -88,7 +105,7 @@ Run the following commands on **MSYS console**.
88
105
  $ git clone git://github.com/ruby-opencv/ruby-opencv.git
89
106
  $ cd ruby-opencv
90
107
  $ git checkout master
91
- $ ruby extconf.rb --with-opencv-dir=/C/path/to/opencvdir/install # for your own built OpenCV library
108
+ $ ruby ext/opencv/extconf.rb --with-opencv-dir=/C/path/to/opencvdir/install # for your own built OpenCV library
92
109
  $ make
93
110
  $ make install
94
111
  ```
@@ -96,7 +113,7 @@ $ make install
96
113
  To use pre-built OpenCV libraries, set the following option to extconf.rb.
97
114
 
98
115
  ```
99
- $ ruby extconf.rb --with-opencv-include=/c/path/to/opencvdir/build/include --with-opencv-lib=/c/path/to/opencvdir/build/x86/mingw/lib
116
+ $ ruby ext/opencv/extconf.rb --with-opencv-include=/c/path/to/opencvdir/build/include --with-opencv-lib=/c/path/to/opencvdir/build/x86/mingw/lib
100
117
  ```
101
118
 
102
119
 
data/Gemfile CHANGED
@@ -1,9 +1,8 @@
1
- source :gemcutter
1
+ source 'https://rubygems.org'
2
2
 
3
3
  group :development do
4
4
  gem "hoe"
5
5
  gem "hoe-gemspec"
6
6
  gem "rake-compiler"
7
- gem "gem-compile"
8
7
  end
9
8
 
@@ -1,30 +1,30 @@
1
- The BSD Liscense
2
-
3
- Copyright (c) 2008, Masakazu Yonekura
4
- All rights reserved.
5
-
6
- Redistribution and use of this software in source and binary forms, with or without modification, are
7
- permitted provided that the following conditions are met:
8
-
9
- * Redistributions of source code must retain the above
10
- copyright notice, this list of conditions and the
11
- following disclaimer.
12
-
13
- * Redistributions in binary form must reproduce the above
14
- copyright notice, this list of conditions and the
15
- following disclaimer in the documentation and/or other
16
- materials provided with the distribution.
17
-
18
- * Neither the name of Masakazu Yonekura. nor the names of its
19
- contributors may be used to endorse or promote products
20
- derived from this software without specific prior
21
- written permission of Masakazu Yonekura.
22
-
23
- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24
- WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25
- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26
- ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
- LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
- INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
- TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30
- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1
+ The BSD License
2
+
3
+ Copyright (c) 2008, Masakazu Yonekura
4
+ All rights reserved.
5
+
6
+ Redistribution and use of this software in source and binary forms, with or without modification, are
7
+ permitted provided that the following conditions are met:
8
+
9
+ * Redistributions of source code must retain the above
10
+ copyright notice, this list of conditions and the
11
+ following disclaimer.
12
+
13
+ * Redistributions in binary form must reproduce the above
14
+ copyright notice, this list of conditions and the
15
+ following disclaimer in the documentation and/or other
16
+ materials provided with the distribution.
17
+
18
+ * Neither the name of Masakazu Yonekura. nor the names of its
19
+ contributors may be used to endorse or promote products
20
+ derived from this software without specific prior
21
+ written permission of Masakazu Yonekura.
22
+
23
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
24
+ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25
+ PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
26
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
29
+ TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30
+ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -6,6 +6,7 @@ License.txt
6
6
  Manifest.txt
7
7
  README.md
8
8
  Rakefile
9
+ config.yml
9
10
  examples/alpha_blend.rb
10
11
  examples/box.png
11
12
  examples/box_in_scene.png
@@ -109,15 +110,13 @@ ext/opencv/cvutils.cpp
109
110
  ext/opencv/cvutils.h
110
111
  ext/opencv/cvvideowriter.cpp
111
112
  ext/opencv/cvvideowriter.h
113
+ ext/opencv/extconf.rb
112
114
  ext/opencv/gui.cpp
113
115
  ext/opencv/gui.h
114
116
  ext/opencv/iplconvkernel.cpp
115
117
  ext/opencv/iplconvkernel.h
116
118
  ext/opencv/iplimage.cpp
117
119
  ext/opencv/iplimage.h
118
- ext/opencv/lib/opencv.rb
119
- ext/opencv/lib/opencv/psyched_yaml.rb
120
- ext/opencv/lib/opencv/version.rb
121
120
  ext/opencv/mouseevent.cpp
122
121
  ext/opencv/mouseevent.h
123
122
  ext/opencv/opencv.cpp
@@ -128,11 +127,13 @@ ext/opencv/trackbar.cpp
128
127
  ext/opencv/trackbar.h
129
128
  ext/opencv/window.cpp
130
129
  ext/opencv/window.h
131
- extconf.rb
132
130
  images/CvMat_sobel.png
133
131
  images/CvMat_sub_rect.png
134
132
  images/CvSeq_relationmap.png
135
133
  images/face_detect_from_lena.jpg
134
+ lib/opencv.rb
135
+ lib/opencv/psyched_yaml.rb
136
+ lib/opencv/version.rb
136
137
  ruby-opencv.gemspec
137
138
  test/helper.rb
138
139
  test/runner.rb
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  An OpenCV wrapper for Ruby.
4
4
 
5
5
  * Web site: <https://github.com/ruby-opencv/ruby-opencv>
6
- * Ruby 1.8.7, 1.9.3 and OpenCV 2.4.3 are supported.
6
+ * Ruby 1.9.3, 2.0.0 and OpenCV 2.4.5 are supported.
7
7
 
8
8
  ## Requirement
9
9
 
data/Rakefile CHANGED
@@ -1,27 +1,36 @@
1
- # -*- mode: ruby; coding: utf-8-unix -*-
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
2
  require 'rubygems'
3
- require './ext/opencv/lib/opencv/psyched_yaml'
3
+ require "rubygems/ext"
4
+ require "rubygems/installer"
4
5
  require 'hoe'
5
6
  require 'rake/extensiontask'
7
+ require 'fileutils'
8
+ require './lib/opencv/psyched_yaml'
9
+
10
+ SO_FILE = 'opencv.so'
6
11
 
7
12
  Hoe.plugin :gemspec
8
13
 
9
14
  hoespec = Hoe.spec 'ruby-opencv' do |s|
10
15
  s.summary = 'OpenCV wrapper for Ruby'
16
+ s.description = 'ruby-opencv is a wrapper of OpenCV for Ruby. It helps you to write computer vision programs (e.g. detecting faces from pictures) with Ruby.'
17
+ s.licenses = ['The BSD License']
11
18
  s.developer('lsxi', 'masakazu.yonekura@gmail.com')
12
19
  s.developer('ser1zw', 'azariahsawtikes@gmail.com')
13
20
  s.developer('pcting', 'pcting@gmail.com')
14
21
 
15
22
  s.readme_file = 'README.md'
16
23
  s.history_file = 'History.txt'
17
- s.spec_extras = { :extensions => ['extconf.rb'] }
24
+
25
+ s.spec_extras = { :extensions => ['ext/opencv/extconf.rb'] }
26
+
18
27
  s.test_globs = ['test/test_*.rb']
19
28
  s.urls = ['https://github.com/ruby-opencv/ruby-opencv/']
20
29
 
21
30
  s.extra_dev_deps << ['rake-compiler', '>= 0'] << ['hoe-gemspec']
22
31
 
23
32
  Rake::ExtensionTask.new('opencv', spec) do |ext|
24
- ext.lib_dir = File.join('lib', 'opencv')
33
+ ext.lib_dir = 'lib'
25
34
  end
26
35
  end
27
36
 
@@ -29,4 +38,53 @@ hoespec.spec.files.delete('.gemtest')
29
38
 
30
39
  Rake::Task[:test].prerequisites << :compile
31
40
 
41
+ desc 'Create a pre-compiled gem'
42
+ task 'gem:precompile' => ['gem'] do
43
+ tmp_dir = Dir.mktmpdir('tmp', '.')
44
+ gemfile = Dir.glob("pkg/*.gem")[0]
45
+ target_dir = File.join(tmp_dir, File.basename(gemfile, '.gem'))
46
+
47
+ installer = Gem::Installer.new(gemfile)
48
+ installer.unpack(target_dir)
49
+
50
+ gemspec = installer.spec
51
+ extension = gemspec.extensions[0]
52
+ gemspec.extensions.clear
53
+
54
+ config = ENV['CONFIG'] ? YAML.load_file(ENV['CONFIG']) : {}
55
+ rubies = config['rubies'] || [Gem.ruby]
56
+ args = config['extopts'] || []
57
+ gemspec.platform = config['platform'] || Gem::Platform::CURRENT
58
+
59
+ multi = rubies.size > 1
60
+ rubies.each { |ruby|
61
+ lib_dir = 'lib'
62
+ if multi
63
+ major, minor, _ = `#{ruby} -e "print RUBY_VERSION"`.chomp.split('.')
64
+ lib_dir = File.join(lib_dir, [major, minor].join('.'))
65
+ end
66
+
67
+ make_cmd = (`#{ruby} -e "print RUBY_PLATFORM"` =~ /mswin/) ? 'nmake' : 'make'
68
+ Dir.chdir(target_dir) {
69
+ cmd = [ruby, extension, *args].join(' ')
70
+ results = []
71
+ Gem::Ext::ExtConfBuilder.run(cmd, results)
72
+ Gem::Ext::ExtConfBuilder.make('', results)
73
+
74
+ FileUtils.mkdir_p lib_dir
75
+ FileUtils.mv SO_FILE, lib_dir
76
+ sh "#{make_cmd} clean"
77
+ }
78
+
79
+ gemspec.files << File.join(lib_dir, SO_FILE)
80
+ }
81
+
82
+ Dir.chdir(target_dir) {
83
+ gemfile = Gem::Package.build(gemspec)
84
+ FileUtils.mv gemfile, File.dirname(__FILE__)
85
+ }
86
+
87
+ FileUtils.rm_rf tmp_dir
88
+ end
89
+
32
90
  # vim: syntax=ruby
@@ -0,0 +1,7 @@
1
+ platform: mingw32
2
+ rubies:
3
+ - C:/ruby-1.9.3-p392-mingw32/bin/ruby.exe
4
+ - C:/ruby-2.0.0-p0-mingw32/bin/ruby.exe
5
+ extopts:
6
+ - --with-opencv-include=C:/opencv/build/include
7
+ - --with-opencv-lib=C:/opencv/build/x86/mingw/lib
@@ -1,21 +1,21 @@
1
- #!/usr/bin/env ruby
2
- # -*- mode: ruby; coding: utf-8-unix -*-
3
-
4
- # Alpha blending sample with GUI
5
-
6
- require 'opencv'
7
- include OpenCV
8
-
9
- img1 = IplImage.load('lenna.jpg', CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
10
- img2 = IplImage.load('tiffany.jpg', CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
11
-
12
- window = GUI::Window.new('Alpha blend')
13
- max = 100.0
14
- val = max / 2.0
15
- window.set_trackbar("Alpha", max, val) { |v|
16
- a = v.to_f / max
17
- window.show CvMat.add_weighted(img1, a, img2, 1.0 - a, 0)
18
- }
19
- window.show CvMat.add_weighted(img1, val / max, img2, 1.0 - val / max, 0)
20
- GUI::wait_key
21
-
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8 -*-
3
+
4
+ # Alpha blending sample with GUI
5
+
6
+ require 'opencv'
7
+ include OpenCV
8
+
9
+ img1 = IplImage.load('lenna.jpg', CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
10
+ img2 = IplImage.load('tiffany.jpg', CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH)
11
+
12
+ window = GUI::Window.new('Alpha blend')
13
+ max = 100.0
14
+ val = max / 2.0
15
+ window.set_trackbar("Alpha", max, val) { |v|
16
+ a = v.to_f / max
17
+ window.show CvMat.add_weighted(img1, a, img2, 1.0 - a, 0)
18
+ }
19
+ window.show CvMat.add_weighted(img1, val / max, img2, 1.0 - val / max, 0)
20
+ GUI::wait_key
21
+
@@ -1,169 +1,169 @@
1
- #!/usr/bin/env ruby
2
- # -*- mode: ruby; coding: utf-8-unix -*-
3
-
4
- # A Demo Ruby/OpenCV Implementation of SURF
5
- # See https://code.ros.org/trac/opencv/browser/tags/2.3.1/opencv/samples/c/find_obj.cpp
6
- require 'opencv'
7
- require 'benchmark'
8
- include OpenCV
9
-
10
- def compare_surf_descriptors(d1, d2, best, length)
11
- raise ArgumentError unless (length % 4) == 0
12
- total_cost = 0
13
- 0.step(length - 1, 4) { |i|
14
- t0 = d1[i] - d2[i]
15
- t1 = d1[i + 1] - d2[i + 1]
16
- t2 = d1[i + 2] - d2[i + 2]
17
- t3 = d1[i + 3] - d2[i + 3]
18
- total_cost += t0 * t0 + t1 * t1 + t2 * t2 + t3 * t3
19
- break if total_cost > best
20
- }
21
- total_cost
22
- end
23
-
24
- def naive_nearest_neighbor(vec, laplacian, model_keypoints, model_descriptors)
25
- length = model_descriptors[0].size
26
- neighbor = nil
27
- dist1 = 1e6
28
- dist2 = 1e6
29
-
30
- model_descriptors.size.times { |i|
31
- kp = model_keypoints[i]
32
- mvec = model_descriptors[i]
33
- next if laplacian != kp.laplacian
34
-
35
- d = compare_surf_descriptors(vec, mvec, dist2, length)
36
- if d < dist1
37
- dist2 = dist1
38
- dist1 = d
39
- neighbor = i
40
- elsif d < dist2
41
- dist2 = d
42
- end
43
- }
44
-
45
- return (dist1 < 0.6 * dist2) ? neighbor : nil
46
- end
47
-
48
- def find_pairs(object_keypoints, object_descriptors,
49
- image_keypoints, image_descriptors)
50
- ptpairs = []
51
- object_descriptors.size.times { |i|
52
- kp = object_keypoints[i]
53
- descriptor = object_descriptors[i]
54
- nearest_neighbor = naive_nearest_neighbor(descriptor, kp.laplacian, image_keypoints, image_descriptors)
55
- unless nearest_neighbor.nil?
56
- ptpairs << i
57
- ptpairs << nearest_neighbor
58
- end
59
- }
60
- ptpairs
61
- end
62
-
63
- def locate_planar_object(object_keypoints, object_descriptors,
64
- image_keypoints, image_descriptors, src_corners)
65
- ptpairs = find_pairs(object_keypoints, object_descriptors, image_keypoints, image_descriptors)
66
- n = ptpairs.size / 2
67
- return nil if n < 4
68
-
69
- pt1 = []
70
- pt2 = []
71
- n.times { |i|
72
- pt1 << object_keypoints[ptpairs[i * 2]].pt
73
- pt2 << image_keypoints[ptpairs[i * 2 + 1]].pt
74
- }
75
-
76
- _pt1 = CvMat.new(1, n, CV_32F, 2)
77
- _pt2 = CvMat.new(1, n, CV_32F, 2)
78
- _pt1.set_data(pt1)
79
- _pt2.set_data(pt2)
80
- h = CvMat.find_homography(_pt1, _pt2, :ransac, 5)
81
-
82
- dst_corners = []
83
- 4.times { |i|
84
- x = src_corners[i].x
85
- y = src_corners[i].y
86
- z = 1.0 / (h[6][0] * x + h[7][0] * y + h[8][0])
87
- x = (h[0][0] * x + h[1][0] * y + h[2][0]) * z
88
- y = (h[3][0] * x + h[4][0] * y + h[5][0]) * z
89
- dst_corners << CvPoint.new(x.to_i, y.to_i)
90
- }
91
-
92
- dst_corners
93
- end
94
-
95
-
96
- ##### Main #####
97
- puts 'This program demonstrated the use of the SURF Detector and Descriptor using'
98
- puts 'brute force matching on planar objects.'
99
- puts 'Usage:'
100
- puts "ruby #{__FILE__} <object_filename> <scene_filename>, default is box.png and box_in_scene.png"
101
- puts
102
-
103
- object_filename = (ARGV.size == 2) ? ARGV[0] : 'box.png'
104
- scene_filename = (ARGV.size == 2) ? ARGV[1] : 'box_in_scene.png'
105
-
106
- object, image = nil, nil
107
- begin
108
- object = IplImage.load(object_filename, CV_LOAD_IMAGE_GRAYSCALE)
109
- image = IplImage.load(scene_filename, CV_LOAD_IMAGE_GRAYSCALE)
110
- rescue
111
- puts "Can not load #{object_filename} and/or #{scene_filename}"
112
- puts "Usage: ruby #{__FILE__} [<object_filename> <scene_filename>]"
113
- exit
114
- end
115
- object_color = object.GRAY2BGR
116
-
117
- param = CvSURFParams.new(1500)
118
-
119
- object_keypoints, object_descriptors = nil, nil
120
- image_keypoints, image_descriptors = nil, nil
121
- tms = Benchmark.measure {
122
- object_keypoints, object_descriptors = object.extract_surf(param)
123
- puts "Object Descriptors: #{object_descriptors.size}"
124
-
125
- image_keypoints, image_descriptors = image.extract_surf(param)
126
- puts "Image Descriptors: #{image_descriptors.size}"
127
- }
128
- puts "Extraction time = #{tms.real * 1000} ms"
129
-
130
- correspond = IplImage.new(image.width, object.height + image.height, CV_8U, 1);
131
- correspond.set_roi(CvRect.new(0, 0, object.width, object.height))
132
- object.copy(correspond)
133
- correspond.set_roi(CvRect.new(0, object.height, image.width, image.height))
134
- image.copy(correspond)
135
- correspond.reset_roi
136
-
137
- src_corners = [CvPoint.new(0, 0), CvPoint.new(object.width, 0),
138
- CvPoint.new(object.width, object.height), CvPoint.new(0, object.height)]
139
- dst_corners = locate_planar_object(object_keypoints, object_descriptors,
140
- image_keypoints, image_descriptors, src_corners)
141
-
142
- correspond = correspond.GRAY2BGR
143
- if dst_corners
144
- 4.times { |i|
145
- r1 = dst_corners[i % 4]
146
- r2 = dst_corners[(i + 1) % 4]
147
- correspond.line!(CvPoint.new(r1.x, r1.y + object.height), CvPoint.new(r2.x, r2.y + object.height),
148
- :color => CvColor::Red, :thickness => 2, :line_type => :aa)
149
- }
150
- end
151
-
152
- ptpairs = find_pairs(object_keypoints, object_descriptors, image_keypoints, image_descriptors)
153
-
154
- 0.step(ptpairs.size - 1, 2) { |i|
155
- r1 = object_keypoints[ptpairs[i]]
156
- r2 = image_keypoints[ptpairs[i + 1]]
157
- correspond.line!(r1.pt, CvPoint.new(r2.pt.x, r2.pt.y + object.height),
158
- :color => CvColor::Red, :line_type => :aa)
159
- }
160
-
161
- object_keypoints.each { |r|
162
- radius = (r.size * 1.2 / 9.0 * 2).to_i
163
- object_color.circle!(r.pt, radius, :color => CvColor::Red, :line_type => :aa)
164
- }
165
-
166
- GUI::Window.new('Object Correspond').show correspond
167
- GUI::Window.new('Object').show object_color
168
- GUI::wait_key
169
-
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8 -*-
3
+
4
+ # A Demo Ruby/OpenCV Implementation of SURF
5
+ # See https://code.ros.org/trac/opencv/browser/tags/2.3.1/opencv/samples/c/find_obj.cpp
6
+ require 'opencv'
7
+ require 'benchmark'
8
+ include OpenCV
9
+
10
+ def compare_surf_descriptors(d1, d2, best, length)
11
+ raise ArgumentError unless (length % 4) == 0
12
+ total_cost = 0
13
+ 0.step(length - 1, 4) { |i|
14
+ t0 = d1[i] - d2[i]
15
+ t1 = d1[i + 1] - d2[i + 1]
16
+ t2 = d1[i + 2] - d2[i + 2]
17
+ t3 = d1[i + 3] - d2[i + 3]
18
+ total_cost += t0 * t0 + t1 * t1 + t2 * t2 + t3 * t3
19
+ break if total_cost > best
20
+ }
21
+ total_cost
22
+ end
23
+
24
+ def naive_nearest_neighbor(vec, laplacian, model_keypoints, model_descriptors)
25
+ length = model_descriptors[0].size
26
+ neighbor = nil
27
+ dist1 = 1e6
28
+ dist2 = 1e6
29
+
30
+ model_descriptors.size.times { |i|
31
+ kp = model_keypoints[i]
32
+ mvec = model_descriptors[i]
33
+ next if laplacian != kp.laplacian
34
+
35
+ d = compare_surf_descriptors(vec, mvec, dist2, length)
36
+ if d < dist1
37
+ dist2 = dist1
38
+ dist1 = d
39
+ neighbor = i
40
+ elsif d < dist2
41
+ dist2 = d
42
+ end
43
+ }
44
+
45
+ return (dist1 < 0.6 * dist2) ? neighbor : nil
46
+ end
47
+
48
+ def find_pairs(object_keypoints, object_descriptors,
49
+ image_keypoints, image_descriptors)
50
+ ptpairs = []
51
+ object_descriptors.size.times { |i|
52
+ kp = object_keypoints[i]
53
+ descriptor = object_descriptors[i]
54
+ nearest_neighbor = naive_nearest_neighbor(descriptor, kp.laplacian, image_keypoints, image_descriptors)
55
+ unless nearest_neighbor.nil?
56
+ ptpairs << i
57
+ ptpairs << nearest_neighbor
58
+ end
59
+ }
60
+ ptpairs
61
+ end
62
+
63
+ def locate_planar_object(object_keypoints, object_descriptors,
64
+ image_keypoints, image_descriptors, src_corners)
65
+ ptpairs = find_pairs(object_keypoints, object_descriptors, image_keypoints, image_descriptors)
66
+ n = ptpairs.size / 2
67
+ return nil if n < 4
68
+
69
+ pt1 = []
70
+ pt2 = []
71
+ n.times { |i|
72
+ pt1 << object_keypoints[ptpairs[i * 2]].pt
73
+ pt2 << image_keypoints[ptpairs[i * 2 + 1]].pt
74
+ }
75
+
76
+ _pt1 = CvMat.new(1, n, CV_32F, 2)
77
+ _pt2 = CvMat.new(1, n, CV_32F, 2)
78
+ _pt1.set_data(pt1)
79
+ _pt2.set_data(pt2)
80
+ h = CvMat.find_homography(_pt1, _pt2, :ransac, 5)
81
+
82
+ dst_corners = []
83
+ 4.times { |i|
84
+ x = src_corners[i].x
85
+ y = src_corners[i].y
86
+ z = 1.0 / (h[6][0] * x + h[7][0] * y + h[8][0])
87
+ x = (h[0][0] * x + h[1][0] * y + h[2][0]) * z
88
+ y = (h[3][0] * x + h[4][0] * y + h[5][0]) * z
89
+ dst_corners << CvPoint.new(x.to_i, y.to_i)
90
+ }
91
+
92
+ dst_corners
93
+ end
94
+
95
+
96
+ ##### Main #####
97
+ puts 'This program demonstrated the use of the SURF Detector and Descriptor using'
98
+ puts 'brute force matching on planar objects.'
99
+ puts 'Usage:'
100
+ puts "ruby #{__FILE__} <object_filename> <scene_filename>, default is box.png and box_in_scene.png"
101
+ puts
102
+
103
+ object_filename = (ARGV.size == 2) ? ARGV[0] : 'box.png'
104
+ scene_filename = (ARGV.size == 2) ? ARGV[1] : 'box_in_scene.png'
105
+
106
+ object, image = nil, nil
107
+ begin
108
+ object = IplImage.load(object_filename, CV_LOAD_IMAGE_GRAYSCALE)
109
+ image = IplImage.load(scene_filename, CV_LOAD_IMAGE_GRAYSCALE)
110
+ rescue
111
+ puts "Can not load #{object_filename} and/or #{scene_filename}"
112
+ puts "Usage: ruby #{__FILE__} [<object_filename> <scene_filename>]"
113
+ exit
114
+ end
115
+ object_color = object.GRAY2BGR
116
+
117
+ param = CvSURFParams.new(1500)
118
+
119
+ object_keypoints, object_descriptors = nil, nil
120
+ image_keypoints, image_descriptors = nil, nil
121
+ tms = Benchmark.measure {
122
+ object_keypoints, object_descriptors = object.extract_surf(param)
123
+ puts "Object Descriptors: #{object_descriptors.size}"
124
+
125
+ image_keypoints, image_descriptors = image.extract_surf(param)
126
+ puts "Image Descriptors: #{image_descriptors.size}"
127
+ }
128
+ puts "Extraction time = #{tms.real * 1000} ms"
129
+
130
+ correspond = IplImage.new(image.width, object.height + image.height, CV_8U, 1);
131
+ correspond.set_roi(CvRect.new(0, 0, object.width, object.height))
132
+ object.copy(correspond)
133
+ correspond.set_roi(CvRect.new(0, object.height, image.width, image.height))
134
+ image.copy(correspond)
135
+ correspond.reset_roi
136
+
137
+ src_corners = [CvPoint.new(0, 0), CvPoint.new(object.width, 0),
138
+ CvPoint.new(object.width, object.height), CvPoint.new(0, object.height)]
139
+ dst_corners = locate_planar_object(object_keypoints, object_descriptors,
140
+ image_keypoints, image_descriptors, src_corners)
141
+
142
+ correspond = correspond.GRAY2BGR
143
+ if dst_corners
144
+ 4.times { |i|
145
+ r1 = dst_corners[i % 4]
146
+ r2 = dst_corners[(i + 1) % 4]
147
+ correspond.line!(CvPoint.new(r1.x, r1.y + object.height), CvPoint.new(r2.x, r2.y + object.height),
148
+ :color => CvColor::Red, :thickness => 2, :line_type => :aa)
149
+ }
150
+ end
151
+
152
+ ptpairs = find_pairs(object_keypoints, object_descriptors, image_keypoints, image_descriptors)
153
+
154
+ 0.step(ptpairs.size - 1, 2) { |i|
155
+ r1 = object_keypoints[ptpairs[i]]
156
+ r2 = image_keypoints[ptpairs[i + 1]]
157
+ correspond.line!(r1.pt, CvPoint.new(r2.pt.x, r2.pt.y + object.height),
158
+ :color => CvColor::Red, :line_type => :aa)
159
+ }
160
+
161
+ object_keypoints.each { |r|
162
+ radius = (r.size * 1.2 / 9.0 * 2).to_i
163
+ object_color.circle!(r.pt, radius, :color => CvColor::Red, :line_type => :aa)
164
+ }
165
+
166
+ GUI::Window.new('Object Correspond').show correspond
167
+ GUI::Window.new('Object').show object_color
168
+ GUI::wait_key
169
+