adamb-ruby-opencv 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. data/CHANGELOG +2 -0
  2. data/License.txt +30 -0
  3. data/README.rdoc +47 -0
  4. data/Rakefile +15 -0
  5. data/examples/convexhull.rb +41 -0
  6. data/examples/face_detect.rb +25 -0
  7. data/examples/houghcircle.rb +23 -0
  8. data/examples/inpaint.png +0 -0
  9. data/examples/inpaint.rb +43 -0
  10. data/examples/paint.rb +72 -0
  11. data/examples/snake.rb +43 -0
  12. data/examples/stuff.jpg +0 -0
  13. data/ext/curve.cpp +103 -0
  14. data/ext/curve.h +34 -0
  15. data/ext/cvavgcomp.cpp +67 -0
  16. data/ext/cvavgcomp.h +39 -0
  17. data/ext/cvbox2d.cpp +114 -0
  18. data/ext/cvbox2d.h +53 -0
  19. data/ext/cvcapture.cpp +276 -0
  20. data/ext/cvcapture.h +54 -0
  21. data/ext/cvchain.cpp +184 -0
  22. data/ext/cvchain.h +43 -0
  23. data/ext/cvchaincode.cpp +49 -0
  24. data/ext/cvchaincode.h +43 -0
  25. data/ext/cvcircle32f.cpp +90 -0
  26. data/ext/cvcircle32f.h +53 -0
  27. data/ext/cvcondensation.cpp +230 -0
  28. data/ext/cvcondensation.h +49 -0
  29. data/ext/cvconnectedcomp.cpp +115 -0
  30. data/ext/cvconnectedcomp.h +46 -0
  31. data/ext/cvcontour.cpp +219 -0
  32. data/ext/cvcontour.h +47 -0
  33. data/ext/cvcontourtree.cpp +86 -0
  34. data/ext/cvcontourtree.h +41 -0
  35. data/ext/cvconvexitydefect.cpp +103 -0
  36. data/ext/cvconvexitydefect.h +42 -0
  37. data/ext/cverror.cpp +140 -0
  38. data/ext/cverror.h +79 -0
  39. data/ext/cvfont.cpp +173 -0
  40. data/ext/cvfont.h +56 -0
  41. data/ext/cvhaarclassifiercascade.cpp +159 -0
  42. data/ext/cvhaarclassifiercascade.h +41 -0
  43. data/ext/cvhistogram.cpp +200 -0
  44. data/ext/cvhistogram.h +51 -0
  45. data/ext/cvindex.cpp +73 -0
  46. data/ext/cvindex.h +40 -0
  47. data/ext/cvline.cpp +106 -0
  48. data/ext/cvline.h +52 -0
  49. data/ext/cvmat.cpp +4809 -0
  50. data/ext/cvmat.h +286 -0
  51. data/ext/cvmatnd.cpp +44 -0
  52. data/ext/cvmatnd.h +28 -0
  53. data/ext/cvmemstorage.cpp +64 -0
  54. data/ext/cvmemstorage.h +53 -0
  55. data/ext/cvmoments.cpp +204 -0
  56. data/ext/cvmoments.h +48 -0
  57. data/ext/cvpoint.cpp +229 -0
  58. data/ext/cvpoint.h +59 -0
  59. data/ext/cvpoint2d32f.cpp +213 -0
  60. data/ext/cvpoint2d32f.h +61 -0
  61. data/ext/cvpoint3d32f.cpp +245 -0
  62. data/ext/cvpoint3d32f.h +64 -0
  63. data/ext/cvrect.cpp +340 -0
  64. data/ext/cvrect.h +79 -0
  65. data/ext/cvscalar.cpp +227 -0
  66. data/ext/cvscalar.h +63 -0
  67. data/ext/cvseq.cpp +583 -0
  68. data/ext/cvseq.h +71 -0
  69. data/ext/cvset.cpp +63 -0
  70. data/ext/cvset.h +39 -0
  71. data/ext/cvsize.cpp +223 -0
  72. data/ext/cvsize.h +63 -0
  73. data/ext/cvsize2d32f.cpp +180 -0
  74. data/ext/cvsize2d32f.h +59 -0
  75. data/ext/cvslice.cpp +82 -0
  76. data/ext/cvslice.h +53 -0
  77. data/ext/cvsparsemat.cpp +44 -0
  78. data/ext/cvsparsemat.h +28 -0
  79. data/ext/cvtermcriteria.cpp +183 -0
  80. data/ext/cvtermcriteria.h +71 -0
  81. data/ext/cvtwopoints.cpp +98 -0
  82. data/ext/cvtwopoints.h +50 -0
  83. data/ext/cvvector.cpp +206 -0
  84. data/ext/cvvector.h +54 -0
  85. data/ext/cvvideowriter.cpp +116 -0
  86. data/ext/cvvideowriter.h +41 -0
  87. data/ext/extconf.rb +66 -0
  88. data/ext/gui.cpp +65 -0
  89. data/ext/gui.h +33 -0
  90. data/ext/iplconvkernel.cpp +177 -0
  91. data/ext/iplconvkernel.h +52 -0
  92. data/ext/iplimage.cpp +238 -0
  93. data/ext/iplimage.h +54 -0
  94. data/ext/mouseevent.cpp +184 -0
  95. data/ext/mouseevent.h +59 -0
  96. data/ext/opencv.cpp +481 -0
  97. data/ext/opencv.h +356 -0
  98. data/ext/point3dset.cpp +41 -0
  99. data/ext/point3dset.h +31 -0
  100. data/ext/pointset.cpp +238 -0
  101. data/ext/pointset.h +69 -0
  102. data/ext/trackbar.cpp +122 -0
  103. data/ext/trackbar.h +65 -0
  104. data/ext/window.cpp +368 -0
  105. data/ext/window.h +56 -0
  106. data/images/CvMat_sobel.png +0 -0
  107. data/images/CvMat_sub_rect.png +0 -0
  108. data/images/CvSeq_relationmap.png +0 -0
  109. data/images/face_detect_from_lena.jpg +0 -0
  110. data/lib/opencv.rb +3 -0
  111. data/lib/version.rb +3 -0
  112. data/metadata +191 -0
  113. data/ruby-opencv.gemspec +33 -0
  114. data/setup/setup.cygwin.rb +120 -0
  115. data/setup/setup.mingw.rb +99 -0
  116. data/setup/setup.mswin32.rb +103 -0
  117. data/test/test_opencv.rb +6 -0
  118. metadata +272 -0
@@ -0,0 +1,2 @@
1
+ v0.0.7. Moving to Echoe and github
2
+ v0.0.6. Some OpenCV function wrapped.
@@ -0,0 +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.
@@ -0,0 +1,47 @@
1
+ = opencv
2
+
3
+ OpenCV Sourceforge Project
4
+ http://sourceforge.net/projects/opencvlibrary/
5
+
6
+ Ruby/OpenCV Author's Web Page
7
+ http://blueruby.mydns.jp/opencv
8
+
9
+ == DESCRIPTION:
10
+
11
+ OpenCV Ruby Wrapper
12
+
13
+ == FEATURES/PROBLEMS:
14
+
15
+ * First release rubygems, Some OpenCV function wrapped.
16
+
17
+ == SYNOPSIS:
18
+
19
+ # Show image via GUI Window.
20
+
21
+ require "rubygems"
22
+ gem "opencv"
23
+ require "opencv"
24
+
25
+ image = OpenCV::IplImage.load("sample.jpg")
26
+ window = OpenCV::GUI::Window.new("preview")
27
+ window.show(image)
28
+ OpenCV::GUI::wait_key
29
+
30
+ # other sample code, see examples/*.rb
31
+
32
+ == REQUIREMENTS:
33
+
34
+ * OpenCV 1.0 or later.
35
+ http://sourceforge.net/projects/opencvlibrary/
36
+ * ffcall (optional)
37
+ http://www.haible.de/bruno/packages-ffcall.html
38
+
39
+ == INSTALL:
40
+
41
+ gem install opencv
42
+
43
+ == LICENSE:
44
+
45
+ The BSD Liscense
46
+
47
+ see LICENSE.txt
@@ -0,0 +1,15 @@
1
+ # Rakefile
2
+ require 'rubygems'
3
+ require 'rake'
4
+ require 'echoe'
5
+
6
+ Echoe.new('ruby-opencv', '0.0.7') do |p|
7
+ p.description = "OpenCV wrapper for Ruby."
8
+ p.url = "http://github.com/adamb/ruby-opencv"
9
+ p.author = "Masakazu Yonekura"
10
+ p.email = "masakazu.yonekura@gmail.com"
11
+ p.ignore_pattern = ["tmp/*", "script/*", "ext/*.o"]
12
+ p.development_dependencies = []
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby
2
+ # convexhull.rb
3
+ gem "opencv"
4
+ require "opencv"
5
+ require "pp"
6
+ include OpenCV
7
+
8
+ window = GUI::Window.new("convexhull")
9
+ pp CvCapture::INTERFACE
10
+ capture = CvCapture::open
11
+
12
+ accuracy = 0.1
13
+ t = window.set_trackbar("accuracy", 100, 1){|v|
14
+ accuracy = 0.1 * v
15
+ }
16
+
17
+ while true
18
+ key = GUI::wait_key(1)
19
+ image = capture.query
20
+ gray = image.BGR2GRAY
21
+ bin = gray.threshold_binary(0x44, 0xFF)
22
+ contours = bin.find_contours
23
+ while contours
24
+ image.poly_line! contours.approx(:accuracy => accuracy), :color => CvScalar::Red
25
+ contours.convexity_defects.each{|cd|
26
+ image.circle! cd.start, 1, :color => CvScalar::Blue
27
+ image.circle! cd.end, 1, :color => CvScalar::Blue
28
+ image.circle! cd.depth_point, 1, :color => CvScalar::Blue
29
+ }
30
+
31
+ contours = contours.h_next
32
+ end
33
+ #pts = gray.good_features_to_track(0.01, 10)
34
+ #puts pts.length
35
+ window.show image
36
+ next unless key
37
+ case key.chr
38
+ when "\e"
39
+ exit
40
+ end
41
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ # face_detect.rb
3
+ require "rubygems"
4
+ gem "opencv"
5
+ require "opencv"
6
+
7
+ include OpenCV
8
+
9
+ window = GUI::Window.new("face detect")
10
+ capture = CvCapture.open
11
+ detector = CvHaarClassifierCascade::load("C:/Program Files/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml")
12
+
13
+ while true
14
+ key = GUI::wait_key(1)
15
+ image = capture.query
16
+ detector.detect_objects(image){|i|
17
+ image.rectangle! i.top_left, i.bottom_right, :color => CvColor::Red
18
+ }
19
+ window.show image
20
+ next unless key
21
+ case key.chr
22
+ when "\e"
23
+ exit
24
+ end
25
+ end
@@ -0,0 +1,23 @@
1
+ #!/usr/bin/env ruby
2
+ # houghcircle.rb
3
+ require "rubygems"
4
+ gem "opencv"
5
+ require "opencv"
6
+ include OpenCV
7
+
8
+ original_window = GUI::Window.new "original"
9
+ hough_window = GUI::Window.new "hough circles"
10
+
11
+ image = IplImage::load "stuff.jpg"
12
+ gray = image.BGR2GRAY
13
+
14
+ result = image.clone
15
+ original_window.show image
16
+ detect = gray.hough_circles_gradient(2.0, 10, 200, 50)
17
+ puts detect.size
18
+ detect.each{|circle|
19
+ puts "#{circle.center.x},#{circle.center.y} - #{circle.radius}"
20
+ result.circle! circle.center, circle.radius, :color => CvColor::Red, :thickness => 3
21
+ }
22
+ hough_window.show result
23
+ GUI::wait_key
Binary file
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # inpaint.rb
3
+ require "rubygems"
4
+ gem "opencv"
5
+ require "opencv"
6
+
7
+ include OpenCV
8
+
9
+ owindow = GUI::Window.new "original"
10
+ mwindow = GUI::Window.new "mask"
11
+ iwindow = GUI::Window.new "inpaint"
12
+
13
+ image = IplImage::load "inpaint.png"
14
+ b, g, r = image.split
15
+ original_mask = r.threshold_binary_inverse(0x00, 0xFF) & b.threshold_binary_inverse(0x00, 0xFF)
16
+ mask = original_mask.copy
17
+
18
+ num_dilate = 3
19
+ radius = 5
20
+ dilate_bar = mwindow.set_trackbar("dilate", 10, num_dilate){|v|
21
+ num_dilate = v
22
+ mask = original_mask.dilate(nil, num_dilate)
23
+ mwindow.show mask
24
+ }
25
+
26
+ radius_bar = mwindow.set_trackbar("radius", 30, radius){|v|
27
+ radius = v
28
+ }
29
+
30
+ owindow.show image
31
+ mwindow.show mask
32
+
33
+ while key = GUI::wait_key
34
+ case key.chr
35
+ when "\e" # esc
36
+ exit
37
+ when "n"
38
+ iwindow.show image.inpaint_ns(mask, radius)
39
+ when "t"
40
+ iwindow.show image.inpaint_telea(mask, radius)
41
+ end
42
+ end
43
+
@@ -0,0 +1,72 @@
1
+ #!/usr/bin/env ruby
2
+ # paint.rb
3
+ require "rubygems"
4
+ gem "opencv"
5
+ require "opencv"
6
+
7
+ include OpenCV
8
+
9
+ window = GUI::Window.new("free canvas")
10
+ canvas = CvMat.new(500, 500, 0, 3).fill!(0xFF) # create white canvas
11
+ window.show canvas
12
+
13
+ colors = CvColor::constants.collect{|i| i.to_s }
14
+
15
+ usage =<<USAGE
16
+ [mouse]
17
+ drag - draw
18
+ right button - fill by color
19
+ [keyborad]
20
+ 1 to 9 - change thickness of line
21
+ type color name - change color
22
+ esc - exit
23
+ USAGE
24
+ puts usage
25
+
26
+ point = nil
27
+
28
+ # drawing option
29
+ opt = {
30
+ :color => CvColor::Black,
31
+ :tickness => 1
32
+ }
33
+
34
+ window.on_mouse{|m|
35
+ case m.event
36
+ when :move
37
+ if m.left_button?
38
+ canvas.line!(point, m, opt) if point
39
+ point = m
40
+ end
41
+ when :left_button_down
42
+ canvas.line!(m, m, opt)
43
+ point = m
44
+ when :left_button_up
45
+ point = nil
46
+ when :right_button_down
47
+ mask = canvas.flood_fill!(m, opt[:color])
48
+ end
49
+ window.show canvas
50
+ }
51
+
52
+ color_name = ''
53
+ while key = GUI.wait_key
54
+ next if key < 0
55
+ case key.chr
56
+ when "\e" # [esc] - exit
57
+ exit
58
+ when '1'..'9'
59
+ puts "change thickness to #{key.chr.to_i}."
60
+ opt[:thickness] = key.chr.to_i
61
+ else
62
+ color_name << key.chr
63
+ choice = colors.find_all{|i| i =~ /\A#{color_name}/i}
64
+ if choice.size == 1
65
+ color,= choice
66
+ puts "change color to #{color}."
67
+ opt[:color] = CvColor::const_get(color)
68
+ end
69
+ color_name = '' if choice.length < 2
70
+ end
71
+ end
72
+
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # snake.rb
3
+ require "rubygems"
4
+ gem "opencv"
5
+ require "opencv"
6
+ include OpenCV
7
+
8
+ puts <<USAGE
9
+ usage:
10
+ left-click: set point
11
+ right-click: do snake
12
+ USAGE
13
+
14
+ window = GUI::Window.new "snake demo"
15
+ image = CvMat.new(256, 256, :cv8u, 1).clear!
16
+ image.circle!(CvPoint.new(128,128), 40, :color => CvColor::White, :thickness => -1)
17
+ display = image.GRAY2BGR
18
+
19
+ window.show display
20
+
21
+ points = []
22
+
23
+ window.on_mouse{|mouse|
24
+ case mouse.event
25
+ when :left_button_down
26
+ display[mouse.x, mouse.y] = CvColor::Red
27
+ puts "set point (#{mouse.x},#{mouse.y})"
28
+ points << CvPoint.new(mouse.x, mouse.y)
29
+ window.show display
30
+ when :right_button_down
31
+ if points.length < 3
32
+ puts "please set more point!"
33
+ next
34
+ end
35
+ points = image.snake_image(points, 1.0, 0.5, 1.5, CvSize.new(3, 3), 100)
36
+ display = image.GRAY2BGR
37
+ display.poly_line! points, :color => CvColor::Red, :is_closed => true
38
+ window.show display
39
+ end
40
+ }
41
+
42
+ GUI::wait_key
43
+
Binary file
@@ -0,0 +1,103 @@
1
+ /************************************************************
2
+
3
+ curve.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include"curve.h"
11
+ /*
12
+ * Document-class: OpenCV::Curve
13
+ *
14
+ * Curve sequence.
15
+ */
16
+ __NAMESPACE_BEGIN_OPENCV
17
+ __NAMESPACE_BEGIN_CURVE
18
+
19
+ VALUE module;
20
+
21
+ VALUE
22
+ rb_module()
23
+ {
24
+ return module;
25
+ }
26
+
27
+ void
28
+ define_ruby_module()
29
+ {
30
+ if(module)
31
+ return;
32
+ /*
33
+ * opencv = rb_define_module("OpenCV");
34
+ *
35
+ * note: this comment is used by rdoc.
36
+ */
37
+ VALUE opencv = rb_module_opencv();
38
+ module = rb_define_module_under(opencv, "Curve");
39
+ rb_define_method(module, "close?", RUBY_METHOD_FUNC(rb_closed_q), 0);
40
+ rb_define_method(module, "convex?", RUBY_METHOD_FUNC(rb_convex_q), 0);
41
+ rb_define_method(module, "hole?", RUBY_METHOD_FUNC(rb_hole_q), 0);
42
+ rb_define_method(module, "simple?", RUBY_METHOD_FUNC(rb_simple_q), 0);
43
+ rb_define_method(module, "arc_length", RUBY_METHOD_FUNC(rb_arc_length), -1);
44
+ }
45
+
46
+ /*
47
+ * If curve is closed, return true. Otherwise return false.
48
+ */
49
+ VALUE
50
+ rb_closed_q(VALUE self)
51
+ {
52
+ return CV_IS_SEQ_CLOSED(CVSEQ(self)) ? Qtrue : Qfalse;
53
+ }
54
+
55
+ /*
56
+ * If curve is convex, return true. Otherwise return false.
57
+ */
58
+ VALUE
59
+ rb_convex_q(VALUE self)
60
+ {
61
+ return CV_IS_SEQ_CONVEX(CVSEQ(self)) ? Qtrue : Qfalse;
62
+ }
63
+
64
+ /*
65
+ * If curve is hole(inner contour), return true. Otherwise return false.
66
+ */
67
+ VALUE
68
+ rb_hole_q(VALUE self)
69
+ {
70
+ return CV_IS_SEQ_HOLE(CVSEQ(self)) ? Qtrue : Qfalse;
71
+ }
72
+
73
+ /*
74
+ * no idia.
75
+ */
76
+ VALUE
77
+ rb_simple_q(VALUE self)
78
+ {
79
+ return CV_IS_SEQ_SIMPLE(CVSEQ(self)) ? Qtrue : Qfalse;
80
+ }
81
+
82
+ /*
83
+ * call-seq:
84
+ * arc_length(<i>[slice = nil][,is_closed = nil]</i>) -> float
85
+ *
86
+ * Calculates contour perimeter or curve length.
87
+ * <i>slice</i> is starting and ending points of the curve.
88
+ * <i>is_closed</i> is indicates whether the curve is closed or not. There are 3 cases:
89
+ * * is_closed = true - the curve is assumed to be unclosed.
90
+ * * is_closed = false - the curve is assumed to be closed.
91
+ * * is_closed = nil (default) use self#close?
92
+ */
93
+ VALUE
94
+ rb_arc_length(int argc, VALUE *argv, VALUE self)
95
+ {
96
+ VALUE slice, is_closed;
97
+ rb_scan_args(argc, argv, "02", &slice, &is_closed);
98
+ return rb_float_new(cvArcLength(CVARR(self), NIL_P(slice) ? CV_WHOLE_SEQ : VALUE_TO_CVSLICE(slice), TRUE_OR_FALSE(is_closed, -1)));
99
+ }
100
+
101
+ __NAMESPACE_END_CURVE
102
+ __NAMESPACE_END_OPENCV
103
+