ruby-opencv 0.0.8.pre-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (229) hide show
  1. data/.gitignore +25 -0
  2. data/Gemfile +8 -0
  3. data/History.txt +5 -0
  4. data/License.txt +30 -0
  5. data/Manifest.txt +225 -0
  6. data/README.rdoc +149 -0
  7. data/Rakefile +32 -0
  8. data/examples/alpha_blend.rb +21 -0
  9. data/examples/box.png +0 -0
  10. data/examples/box_in_scene.png +0 -0
  11. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  12. data/examples/contours/bitmap-contours.png +0 -0
  13. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  14. data/examples/contours/contour_retrieval_modes.rb +139 -0
  15. data/examples/contours/rotated-boxes.jpg +0 -0
  16. data/examples/convexhull.rb +47 -0
  17. data/examples/face_detect.rb +20 -0
  18. data/examples/find_obj.rb +169 -0
  19. data/examples/houghcircle.rb +22 -0
  20. data/examples/inpaint.png +0 -0
  21. data/examples/inpaint.rb +57 -0
  22. data/examples/lenna-rotated.jpg +0 -0
  23. data/examples/lenna.jpg +0 -0
  24. data/examples/match_kdtree.rb +88 -0
  25. data/examples/matching_to_many_images.rb +16 -0
  26. data/examples/matching_to_many_images/query.png +0 -0
  27. data/examples/matching_to_many_images/train/1.png +0 -0
  28. data/examples/matching_to_many_images/train/2.png +0 -0
  29. data/examples/matching_to_many_images/train/3.png +0 -0
  30. data/examples/matching_to_many_images/train/trainImages.txt +3 -0
  31. data/examples/paint.rb +70 -0
  32. data/examples/snake.rb +43 -0
  33. data/examples/stuff.jpg +0 -0
  34. data/examples/tiffany.jpg +0 -0
  35. data/ext/opencv/curve.cpp +112 -0
  36. data/ext/opencv/curve.h +34 -0
  37. data/ext/opencv/cvavgcomp.cpp +67 -0
  38. data/ext/opencv/cvavgcomp.h +39 -0
  39. data/ext/opencv/cvbox2d.cpp +197 -0
  40. data/ext/opencv/cvbox2d.h +61 -0
  41. data/ext/opencv/cvcapture.cpp +499 -0
  42. data/ext/opencv/cvcapture.h +72 -0
  43. data/ext/opencv/cvchain.cpp +233 -0
  44. data/ext/opencv/cvchain.h +46 -0
  45. data/ext/opencv/cvcircle32f.cpp +116 -0
  46. data/ext/opencv/cvcircle32f.h +52 -0
  47. data/ext/opencv/cvcondensation.cpp +282 -0
  48. data/ext/opencv/cvcondensation.h +49 -0
  49. data/ext/opencv/cvconnectedcomp.cpp +143 -0
  50. data/ext/opencv/cvconnectedcomp.h +49 -0
  51. data/ext/opencv/cvcontour.cpp +296 -0
  52. data/ext/opencv/cvcontour.h +48 -0
  53. data/ext/opencv/cvcontourtree.cpp +91 -0
  54. data/ext/opencv/cvcontourtree.h +41 -0
  55. data/ext/opencv/cvconvexitydefect.cpp +103 -0
  56. data/ext/opencv/cvconvexitydefect.h +42 -0
  57. data/ext/opencv/cverror.cpp +159 -0
  58. data/ext/opencv/cverror.h +28 -0
  59. data/ext/opencv/cvfeaturetree.cpp +125 -0
  60. data/ext/opencv/cvfeaturetree.h +55 -0
  61. data/ext/opencv/cvfont.cpp +208 -0
  62. data/ext/opencv/cvfont.h +64 -0
  63. data/ext/opencv/cvhaarclassifiercascade.cpp +168 -0
  64. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  65. data/ext/opencv/cvhistogram.cpp +546 -0
  66. data/ext/opencv/cvhistogram.h +73 -0
  67. data/ext/opencv/cvhumoments.cpp +139 -0
  68. data/ext/opencv/cvhumoments.h +51 -0
  69. data/ext/opencv/cvline.cpp +154 -0
  70. data/ext/opencv/cvline.h +54 -0
  71. data/ext/opencv/cvmat.cpp +5848 -0
  72. data/ext/opencv/cvmat.h +284 -0
  73. data/ext/opencv/cvmatnd.cpp +44 -0
  74. data/ext/opencv/cvmatnd.h +28 -0
  75. data/ext/opencv/cvmemstorage.cpp +68 -0
  76. data/ext/opencv/cvmemstorage.h +53 -0
  77. data/ext/opencv/cvmoments.cpp +287 -0
  78. data/ext/opencv/cvmoments.h +75 -0
  79. data/ext/opencv/cvpoint.cpp +228 -0
  80. data/ext/opencv/cvpoint.h +64 -0
  81. data/ext/opencv/cvpoint2d32f.cpp +211 -0
  82. data/ext/opencv/cvpoint2d32f.h +63 -0
  83. data/ext/opencv/cvpoint3d32f.cpp +245 -0
  84. data/ext/opencv/cvpoint3d32f.h +66 -0
  85. data/ext/opencv/cvrect.cpp +333 -0
  86. data/ext/opencv/cvrect.h +79 -0
  87. data/ext/opencv/cvscalar.cpp +236 -0
  88. data/ext/opencv/cvscalar.h +71 -0
  89. data/ext/opencv/cvseq.cpp +599 -0
  90. data/ext/opencv/cvseq.h +74 -0
  91. data/ext/opencv/cvsize.cpp +221 -0
  92. data/ext/opencv/cvsize.h +65 -0
  93. data/ext/opencv/cvsize2d32f.cpp +209 -0
  94. data/ext/opencv/cvsize2d32f.h +64 -0
  95. data/ext/opencv/cvslice.cpp +120 -0
  96. data/ext/opencv/cvslice.h +61 -0
  97. data/ext/opencv/cvsparsemat.cpp +44 -0
  98. data/ext/opencv/cvsparsemat.h +28 -0
  99. data/ext/opencv/cvsurfparams.cpp +199 -0
  100. data/ext/opencv/cvsurfparams.h +58 -0
  101. data/ext/opencv/cvsurfpoint.cpp +223 -0
  102. data/ext/opencv/cvsurfpoint.h +52 -0
  103. data/ext/opencv/cvtermcriteria.cpp +192 -0
  104. data/ext/opencv/cvtermcriteria.h +71 -0
  105. data/ext/opencv/cvtwopoints.cpp +116 -0
  106. data/ext/opencv/cvtwopoints.h +51 -0
  107. data/ext/opencv/cvutils.cpp +194 -0
  108. data/ext/opencv/cvutils.h +29 -0
  109. data/ext/opencv/cvvideowriter.cpp +137 -0
  110. data/ext/opencv/cvvideowriter.h +43 -0
  111. data/ext/opencv/gui.cpp +68 -0
  112. data/ext/opencv/gui.h +30 -0
  113. data/ext/opencv/iplconvkernel.cpp +192 -0
  114. data/ext/opencv/iplconvkernel.h +71 -0
  115. data/ext/opencv/iplimage.cpp +644 -0
  116. data/ext/opencv/iplimage.h +73 -0
  117. data/ext/opencv/lib/opencv.rb +3 -0
  118. data/ext/opencv/lib/opencv/psyched_yaml.rb +22 -0
  119. data/ext/opencv/lib/opencv/version.rb +3 -0
  120. data/ext/opencv/mouseevent.cpp +181 -0
  121. data/ext/opencv/mouseevent.h +56 -0
  122. data/ext/opencv/opencv.cpp +722 -0
  123. data/ext/opencv/opencv.h +400 -0
  124. data/ext/opencv/pointset.cpp +274 -0
  125. data/ext/opencv/pointset.h +68 -0
  126. data/ext/opencv/trackbar.cpp +121 -0
  127. data/ext/opencv/trackbar.h +69 -0
  128. data/ext/opencv/window.cpp +357 -0
  129. data/ext/opencv/window.h +66 -0
  130. data/extconf.rb +75 -0
  131. data/images/CvMat_sobel.png +0 -0
  132. data/images/CvMat_sub_rect.png +0 -0
  133. data/images/CvSeq_relationmap.png +0 -0
  134. data/images/face_detect_from_lena.jpg +0 -0
  135. data/lib/opencv.rb +3 -0
  136. data/lib/opencv/psyched_yaml.rb +22 -0
  137. data/lib/opencv/version.rb +3 -0
  138. data/ruby-opencv.gemspec +43 -0
  139. data/test/helper.rb +166 -0
  140. data/test/runner.rb +30 -0
  141. data/test/samples/airplane.jpg +0 -0
  142. data/test/samples/baboon.jpg +0 -0
  143. data/test/samples/baboon200.jpg +0 -0
  144. data/test/samples/baboon200_rotated.jpg +0 -0
  145. data/test/samples/blank0.jpg +0 -0
  146. data/test/samples/blank1.jpg +0 -0
  147. data/test/samples/blank2.jpg +0 -0
  148. data/test/samples/blank3.jpg +0 -0
  149. data/test/samples/blank4.jpg +0 -0
  150. data/test/samples/blank5.jpg +0 -0
  151. data/test/samples/blank6.jpg +0 -0
  152. data/test/samples/blank7.jpg +0 -0
  153. data/test/samples/blank8.jpg +0 -0
  154. data/test/samples/blank9.jpg +0 -0
  155. data/test/samples/cat.jpg +0 -0
  156. data/test/samples/chessboard.jpg +0 -0
  157. data/test/samples/contours.jpg +0 -0
  158. data/test/samples/fruits.jpg +0 -0
  159. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  160. data/test/samples/inpaint-mask.bmp +0 -0
  161. data/test/samples/lena-256x256.jpg +0 -0
  162. data/test/samples/lena-32x32.jpg +0 -0
  163. data/test/samples/lena-eyes.jpg +0 -0
  164. data/test/samples/lena-inpaint.jpg +0 -0
  165. data/test/samples/lena.jpg +0 -0
  166. data/test/samples/lines.jpg +0 -0
  167. data/test/samples/messy0.jpg +0 -0
  168. data/test/samples/messy1.jpg +0 -0
  169. data/test/samples/movie_sample.avi +0 -0
  170. data/test/samples/one_way_train_0000.jpg +0 -0
  171. data/test/samples/one_way_train_0001.jpg +0 -0
  172. data/test/samples/partially_blank0.jpg +0 -0
  173. data/test/samples/partially_blank1.jpg +0 -0
  174. data/test/samples/smooth0.jpg +0 -0
  175. data/test/samples/smooth1.jpg +0 -0
  176. data/test/samples/smooth2.jpg +0 -0
  177. data/test/samples/smooth3.jpg +0 -0
  178. data/test/samples/smooth4.jpg +0 -0
  179. data/test/samples/smooth5.jpg +0 -0
  180. data/test/samples/smooth6.jpg +0 -0
  181. data/test/samples/str-cv-rotated.jpg +0 -0
  182. data/test/samples/str-cv.jpg +0 -0
  183. data/test/samples/str-ov.jpg +0 -0
  184. data/test/samples/stuff.jpg +0 -0
  185. data/test/test_curve.rb +43 -0
  186. data/test/test_cvavgcomp.rb +24 -0
  187. data/test/test_cvbox2d.rb +76 -0
  188. data/test/test_cvcapture.rb +183 -0
  189. data/test/test_cvchain.rb +108 -0
  190. data/test/test_cvcircle32f.rb +41 -0
  191. data/test/test_cvconnectedcomp.rb +61 -0
  192. data/test/test_cvcontour.rb +150 -0
  193. data/test/test_cvcontourtree.rb +43 -0
  194. data/test/test_cverror.rb +50 -0
  195. data/test/test_cvfeaturetree.rb +65 -0
  196. data/test/test_cvfont.rb +58 -0
  197. data/test/test_cvhaarclassifiercascade.rb +63 -0
  198. data/test/test_cvhistogram.rb +271 -0
  199. data/test/test_cvhumoments.rb +83 -0
  200. data/test/test_cvline.rb +50 -0
  201. data/test/test_cvmat.rb +2947 -0
  202. data/test/test_cvmat_drawing.rb +349 -0
  203. data/test/test_cvmat_dxt.rb +150 -0
  204. data/test/test_cvmat_imageprocessing.rb +2025 -0
  205. data/test/test_cvmat_matching.rb +57 -0
  206. data/test/test_cvmoments.rb +180 -0
  207. data/test/test_cvpoint.rb +75 -0
  208. data/test/test_cvpoint2d32f.rb +75 -0
  209. data/test/test_cvpoint3d32f.rb +93 -0
  210. data/test/test_cvrect.rb +144 -0
  211. data/test/test_cvscalar.rb +113 -0
  212. data/test/test_cvseq.rb +295 -0
  213. data/test/test_cvsize.rb +75 -0
  214. data/test/test_cvsize2d32f.rb +75 -0
  215. data/test/test_cvslice.rb +31 -0
  216. data/test/test_cvsurfparams.rb +57 -0
  217. data/test/test_cvsurfpoint.rb +66 -0
  218. data/test/test_cvtermcriteria.rb +56 -0
  219. data/test/test_cvtwopoints.rb +40 -0
  220. data/test/test_cvvideowriter.rb +58 -0
  221. data/test/test_iplconvkernel.rb +54 -0
  222. data/test/test_iplimage.rb +236 -0
  223. data/test/test_mouseevent.rb +17 -0
  224. data/test/test_opencv.rb +324 -0
  225. data/test/test_pointset.rb +126 -0
  226. data/test/test_preliminary.rb +130 -0
  227. data/test/test_trackbar.rb +47 -0
  228. data/test/test_window.rb +115 -0
  229. metadata +414 -0
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ # houghcircle.rb
3
+ require "rubygems"
4
+ require "opencv"
5
+ include OpenCV
6
+
7
+ original_window = GUI::Window.new "original"
8
+ hough_window = GUI::Window.new "hough circles"
9
+
10
+ image = IplImage::load "stuff.jpg"
11
+ gray = image.BGR2GRAY
12
+
13
+ result = image.clone
14
+ original_window.show image
15
+ detect = gray.hough_circles(CV_HOUGH_GRADIENT, 2.0, 10, 200, 50)
16
+ puts detect.size
17
+ detect.each{|circle|
18
+ puts "#{circle.center.x},#{circle.center.y} - #{circle.radius}"
19
+ result.circle! circle.center, circle.radius, :color => CvColor::Red, :thickness => 3
20
+ }
21
+ hough_window.show result
22
+ GUI::wait_key
Binary file
@@ -0,0 +1,57 @@
1
+ #!/usr/bin/env ruby
2
+ # inpaint.rb
3
+ require "rubygems"
4
+ require "opencv"
5
+ include OpenCV
6
+
7
+ puts <<EOS
8
+ Inpainting sample
9
+
10
+ Usage:
11
+ 'dilate' bar - Adjust mask to inpaint (Non-black areas indicate the area that needs to be inpainted).
12
+ 'radius' bar - Adjust radius of a circular neighborhood of each point inpainted.
13
+ 'n' key - Inpaint using Navier-Stokes based method
14
+ 't' key - Inpaint using Alexandru Telea's method
15
+ 'c' key - Clear the inpaint window
16
+ 'ESC' key - exit"
17
+ EOS
18
+
19
+ owindow = GUI::Window.new('original')
20
+ mwindow = GUI::Window.new('mask')
21
+ iwindow = GUI::Window.new('inpaint')
22
+
23
+ image = IplImage::load('inpaint.png')
24
+ noimage = image.zero
25
+ b, g, r = image.split
26
+ original_mask = r.threshold(0x00, 0xFF, CV_THRESH_BINARY_INV) & b.threshold(0x00, 0xFF, CV_THRESH_BINARY_INV)
27
+ mask = original_mask.copy
28
+
29
+ num_dilate = 3
30
+ mwindow.set_trackbar("dilate", 10, num_dilate) { |v|
31
+ num_dilate = v
32
+ mask = original_mask.dilate(nil, num_dilate)
33
+ mwindow.show mask
34
+ }
35
+
36
+ radius = 5
37
+ mwindow.set_trackbar("radius", 10, radius) { |v|
38
+ radius = v
39
+ }
40
+
41
+ owindow.show image
42
+ mwindow.show mask
43
+ iwindow.show noimage
44
+
45
+ while key = GUI::wait_key
46
+ case key.chr
47
+ when "\e" # esc
48
+ exit
49
+ when "n"
50
+ iwindow.show image.inpaint(:ns, mask, radius)
51
+ when "t"
52
+ iwindow.show image.inpaint(:telea, mask, radius)
53
+ when "c"
54
+ iwindow.show noimage
55
+ end
56
+ end
57
+
Binary file
Binary file
@@ -0,0 +1,88 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- mode: ruby; coding: utf-8-unix -*-
3
+
4
+ # A sample of matching SURF feature points using kd-tree
5
+ # See http://tech.groups.yahoo.com/group/OpenCV/message/62318
6
+
7
+ require 'opencv'
8
+ include OpenCV
9
+
10
+ USE_EXTENDED_DESCRIPTOR = true
11
+ THRESHOLD = 1500
12
+ DESCRIPTOR_SIZE = USE_EXTENDED_DESCRIPTOR ? 128 : 64
13
+
14
+ img1 = CvMat.load('lenna.jpg', CV_LOAD_IMAGE_GRAYSCALE)
15
+ img2 = CvMat.load('lenna-rotated.jpg', CV_LOAD_IMAGE_GRAYSCALE)
16
+
17
+ puts 'Extracting features from img1 using SURF...'
18
+ param = CvSURFParams.new(THRESHOLD, USE_EXTENDED_DESCRIPTOR)
19
+ kp1, desc1 = img1.extract_surf(param)
20
+ puts "found #{kp1.size} keypoints from img1"
21
+
22
+ puts 'Extracting features from img2 using SURF...'
23
+ kp2, desc2 = img2.extract_surf(param)
24
+ puts "found #{kp2.size} keypoints from img2"
25
+
26
+ puts 'Matching keypoints...'
27
+ desc1mat = CvMat.new(kp1.size, DESCRIPTOR_SIZE, :cv32f, 1)
28
+ desc2mat = CvMat.new(kp2.size, DESCRIPTOR_SIZE, :cv32f, 1)
29
+ desc1.each_with_index { |desc, i|
30
+ desc.each_with_index { |d, j|
31
+ desc1mat[i, j] = CvScalar.new(d)
32
+ }
33
+ }
34
+ desc2.each_with_index { |desc, i|
35
+ desc.each_with_index { |d, j|
36
+ desc2mat[i, j] = CvScalar.new(d)
37
+ }
38
+ }
39
+
40
+ feature_tree = CvFeatureTree.new(desc1mat)
41
+ results, distances = feature_tree.find_features(desc2mat, 1, 250)
42
+
43
+ reverse_lookup = []
44
+ reverse_lookup_dist = []
45
+ kp1.size.times { |i|
46
+ reverse_lookup << -1
47
+ reverse_lookup_dist << Float::MAX
48
+ }
49
+
50
+ match_count = 0
51
+ kp2.size.times { |j|
52
+ i = results[j][0].to_i
53
+ d = distances[j][0]
54
+ if (d < reverse_lookup_dist[i])
55
+ match_count += 1 if reverse_lookup_dist[i] == Float::MAX
56
+ reverse_lookup[i] = j
57
+ reverse_lookup_dist[i] = d
58
+ end
59
+ }
60
+ puts "found #{match_count} putative correspondences"
61
+
62
+ points1 = []
63
+ points2 = []
64
+ kp2.size.times { |j|
65
+ i = results[j][0].to_i
66
+ if (j == reverse_lookup[i])
67
+ points1 << kp1[i].pt
68
+ points2 << kp2[j].pt
69
+ end
70
+ }
71
+
72
+ width = img1.cols + img2.cols
73
+ height = (img1.rows > img2.rows) ? img1.rows : img2.rows
74
+ correspond = IplImage.new(width, height, :cv8u, 1);
75
+ correspond.set_roi(CvRect.new(0, 0, img1.cols, img1.rows))
76
+ img1.copy(correspond)
77
+ correspond.set_roi(CvRect.new(img1.cols, 0, img1.cols + img2.cols, img2.rows))
78
+ img2.copy(correspond)
79
+ correspond.reset_roi
80
+
81
+ points1.zip(points2) { |pt1, pt2|
82
+ pt2.x += img1.cols
83
+ correspond.line!(pt1, pt2, :color => CvColor::White)
84
+ }
85
+
86
+ GUI::Window.new('Object Correspond').show correspond
87
+ GUI::wait_key
88
+
@@ -0,0 +1,16 @@
1
+ require 'opencv'
2
+ require 'benchmark'
3
+ include OpenCV
4
+
5
+ data = File.join(File.dirname(__FILE__), 'matching_to_many_images')
6
+
7
+ query = IplImage.load File.join(data, 'query.png'), CV_LOAD_IMAGE_GRAYSCALE
8
+ image_files = ['1.png', '2.png', '3.png'].map{|f| File.join(data, 'train', f)}
9
+ images = image_files.map{|f| IplImage.load f, CV_LOAD_IMAGE_GRAYSCALE}
10
+
11
+
12
+ matchs = query.match_descriptors(images)
13
+
14
+ match_index, count = matchs.max_by {|image_index, count| count}
15
+
16
+ puts "max match: #{image_files[match_index]}"
@@ -0,0 +1,3 @@
1
+ 1.png
2
+ 2.png
3
+ 3.png
data/examples/paint.rb ADDED
@@ -0,0 +1,70 @@
1
+ #!/usr/bin/env ruby
2
+ # paint.rb
3
+ require "rubygems"
4
+ require "opencv"
5
+
6
+ include OpenCV
7
+
8
+ window = GUI::Window.new("free canvas")
9
+ canvas = CvMat.new(500, 500, CV_8U, 3).fill!(CvColor::White) # create white canvas
10
+ window.show canvas
11
+
12
+ colors = CvColor::constants.collect{ |i| i.to_s }
13
+
14
+ usage =<<USAGE
15
+ [mouse]
16
+ drag - draw
17
+ right button - fill by color
18
+ [keyborad]
19
+ 1 to 9 - change thickness of line
20
+ type color name - change color
21
+ esc - exit
22
+ USAGE
23
+ puts usage
24
+
25
+ # drawing option
26
+ opt = {
27
+ :color => CvColor::Black,
28
+ :tickness => 1
29
+ }
30
+
31
+ point = nil
32
+ window.on_mouse{ |m|
33
+ case m.event
34
+ when :move
35
+ if m.left_button?
36
+ canvas.line!(point, m, opt) if point
37
+ point = m
38
+ end
39
+ when :left_button_down
40
+ canvas.line!(m, m, opt)
41
+ point = m
42
+ when :left_button_up
43
+ point = nil
44
+ when :right_button_down
45
+ mask = canvas.flood_fill!(m, opt[:color])
46
+ end
47
+ window.show canvas
48
+ }
49
+
50
+ color_name = ''
51
+ while key = GUI.wait_key
52
+ next if key < 0 or key > 255
53
+ case key.chr
54
+ when "\e" # [esc] - exit
55
+ exit
56
+ when '1'..'9'
57
+ puts "change thickness to #{key.chr.to_i}."
58
+ opt[:thickness] = key.chr.to_i
59
+ when /[A-Za-z]/
60
+ color_name << key.chr
61
+ choice = colors.find_all{ |i| i =~ /\A#{color_name}/i }
62
+ if choice.size == 1
63
+ color,= choice
64
+ puts "change color to #{color}."
65
+ opt[:color] = CvColor::const_get(color)
66
+ end
67
+ color_name = '' if choice.size < 2
68
+ end
69
+ end
70
+
data/examples/snake.rb ADDED
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # snake.rb
3
+ require "rubygems"
4
+ require "opencv"
5
+ include OpenCV
6
+
7
+ puts <<USAGE
8
+ usage:
9
+ left-click: set a point
10
+ right-click: do snake
11
+ USAGE
12
+
13
+ window = GUI::Window.new "snake demo"
14
+ image = CvMat.new(256, 256, :cv8u, 1).clear!
15
+ image.circle!(CvPoint.new(128,128), 40, :color => CvColor::White, :thickness => -1)
16
+ display = image.GRAY2BGR
17
+
18
+ window.show display
19
+
20
+ points = []
21
+
22
+ window.on_mouse{|mouse|
23
+ case mouse.event
24
+ when :left_button_down
25
+ display.circle!(mouse, 1, :color => CvColor::Red, :thickness => 2)
26
+ puts "set point (#{mouse.x},#{mouse.y})"
27
+ points << CvPoint.new(mouse.x, mouse.y)
28
+ window.show display
29
+ when :right_button_down
30
+ if points.length < 3
31
+ puts "please set more point!"
32
+ next
33
+ end
34
+ snake_points = image.snake_image(points, 1.0, 0.5, 1.5, CvSize.new(3, 3), 100)
35
+ display = image.GRAY2BGR
36
+ display.poly_line!([snake_points], :color => CvColor::Red, :is_closed => true, :thickness => 2)
37
+ window.show display
38
+ points.clear
39
+ end
40
+ }
41
+
42
+ GUI::wait_key
43
+
Binary file
Binary file
@@ -0,0 +1,112 @@
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, "closed?", 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
+ double length = 0;
99
+ try {
100
+ length = cvArcLength(CVARR(self),
101
+ NIL_P(slice) ? CV_WHOLE_SEQ : VALUE_TO_CVSLICE(slice),
102
+ TRUE_OR_FALSE(is_closed, -1));
103
+ }
104
+ catch (cv::Exception& e) {
105
+ raise_cverror(e);
106
+ }
107
+ return rb_float_new(length);
108
+ }
109
+
110
+ __NAMESPACE_END_CURVE
111
+ __NAMESPACE_END_OPENCV
112
+
@@ -0,0 +1,34 @@
1
+ /************************************************************
2
+
3
+ curve.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_CVSEQ_CURVE_H
11
+ #define RUBY_OPENCV_CVSEQ_CURVE_H
12
+
13
+ #include "opencv.h"
14
+
15
+ #define __NAMESPACE_BEGIN_CURVE namespace mCurve {
16
+ #define __NAMESPACE_END_CURVE }
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_CURVE
20
+
21
+ VALUE rb_module();
22
+
23
+ void define_ruby_module();
24
+
25
+ VALUE rb_closed_q(VALUE self);
26
+ VALUE rb_convex_q(VALUE self);
27
+ VALUE rb_hole_q(VALUE self);
28
+ VALUE rb_simple_q(VALUE self);
29
+ VALUE rb_arc_length(int argc, VALUE *argv, VALUE self);
30
+
31
+ __NAMESPACE_END_CURVE
32
+ __NAMESPACE_END_OPENCV
33
+
34
+ #endif // RUBY_OPENCV_CVSEQ_CURVE_H