ruby-opencv 0.0.10-i386-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (231) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +26 -0
  3. data/DEVELOPERS_NOTE.md +137 -0
  4. data/Gemfile +8 -0
  5. data/History.txt +5 -0
  6. data/License.txt +30 -0
  7. data/Manifest.txt +227 -0
  8. data/README.md +98 -0
  9. data/Rakefile +90 -0
  10. data/config.yml +7 -0
  11. data/examples/alpha_blend.rb +21 -0
  12. data/examples/box.png +0 -0
  13. data/examples/box_in_scene.png +0 -0
  14. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  15. data/examples/contours/bitmap-contours.png +0 -0
  16. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  17. data/examples/contours/contour_retrieval_modes.rb +139 -0
  18. data/examples/contours/rotated-boxes.jpg +0 -0
  19. data/examples/convexhull.rb +47 -0
  20. data/examples/face_detect.rb +20 -0
  21. data/examples/find_obj.rb +169 -0
  22. data/examples/houghcircle.rb +22 -0
  23. data/examples/inpaint.png +0 -0
  24. data/examples/inpaint.rb +57 -0
  25. data/examples/lenna-rotated.jpg +0 -0
  26. data/examples/lenna.jpg +0 -0
  27. data/examples/match_kdtree.rb +88 -0
  28. data/examples/matching_to_many_images.rb +16 -0
  29. data/examples/matching_to_many_images/query.png +0 -0
  30. data/examples/matching_to_many_images/train/1.png +0 -0
  31. data/examples/matching_to_many_images/train/2.png +0 -0
  32. data/examples/matching_to_many_images/train/3.png +0 -0
  33. data/examples/matching_to_many_images/train/trainImages.txt +3 -0
  34. data/examples/paint.rb +70 -0
  35. data/examples/snake.rb +43 -0
  36. data/examples/stuff.jpg +0 -0
  37. data/examples/tiffany.jpg +0 -0
  38. data/ext/opencv/curve.cpp +112 -0
  39. data/ext/opencv/curve.h +34 -0
  40. data/ext/opencv/cvavgcomp.cpp +67 -0
  41. data/ext/opencv/cvavgcomp.h +39 -0
  42. data/ext/opencv/cvbox2d.cpp +197 -0
  43. data/ext/opencv/cvbox2d.h +61 -0
  44. data/ext/opencv/cvcapture.cpp +506 -0
  45. data/ext/opencv/cvcapture.h +72 -0
  46. data/ext/opencv/cvchain.cpp +233 -0
  47. data/ext/opencv/cvchain.h +46 -0
  48. data/ext/opencv/cvcircle32f.cpp +116 -0
  49. data/ext/opencv/cvcircle32f.h +52 -0
  50. data/ext/opencv/cvcondensation.cpp +282 -0
  51. data/ext/opencv/cvcondensation.h +49 -0
  52. data/ext/opencv/cvconnectedcomp.cpp +143 -0
  53. data/ext/opencv/cvconnectedcomp.h +49 -0
  54. data/ext/opencv/cvcontour.cpp +296 -0
  55. data/ext/opencv/cvcontour.h +48 -0
  56. data/ext/opencv/cvcontourtree.cpp +91 -0
  57. data/ext/opencv/cvcontourtree.h +41 -0
  58. data/ext/opencv/cvconvexitydefect.cpp +103 -0
  59. data/ext/opencv/cvconvexitydefect.h +42 -0
  60. data/ext/opencv/cverror.cpp +159 -0
  61. data/ext/opencv/cverror.h +28 -0
  62. data/ext/opencv/cvfeaturetree.cpp +125 -0
  63. data/ext/opencv/cvfeaturetree.h +55 -0
  64. data/ext/opencv/cvfont.cpp +208 -0
  65. data/ext/opencv/cvfont.h +64 -0
  66. data/ext/opencv/cvhaarclassifiercascade.cpp +168 -0
  67. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  68. data/ext/opencv/cvhistogram.cpp +546 -0
  69. data/ext/opencv/cvhistogram.h +73 -0
  70. data/ext/opencv/cvhumoments.cpp +139 -0
  71. data/ext/opencv/cvhumoments.h +51 -0
  72. data/ext/opencv/cvline.cpp +154 -0
  73. data/ext/opencv/cvline.h +54 -0
  74. data/ext/opencv/cvmat.cpp +5848 -0
  75. data/ext/opencv/cvmat.h +284 -0
  76. data/ext/opencv/cvmatnd.cpp +44 -0
  77. data/ext/opencv/cvmatnd.h +28 -0
  78. data/ext/opencv/cvmemstorage.cpp +68 -0
  79. data/ext/opencv/cvmemstorage.h +53 -0
  80. data/ext/opencv/cvmoments.cpp +287 -0
  81. data/ext/opencv/cvmoments.h +75 -0
  82. data/ext/opencv/cvpoint.cpp +228 -0
  83. data/ext/opencv/cvpoint.h +64 -0
  84. data/ext/opencv/cvpoint2d32f.cpp +211 -0
  85. data/ext/opencv/cvpoint2d32f.h +63 -0
  86. data/ext/opencv/cvpoint3d32f.cpp +245 -0
  87. data/ext/opencv/cvpoint3d32f.h +66 -0
  88. data/ext/opencv/cvrect.cpp +333 -0
  89. data/ext/opencv/cvrect.h +79 -0
  90. data/ext/opencv/cvscalar.cpp +236 -0
  91. data/ext/opencv/cvscalar.h +71 -0
  92. data/ext/opencv/cvseq.cpp +599 -0
  93. data/ext/opencv/cvseq.h +74 -0
  94. data/ext/opencv/cvsize.cpp +221 -0
  95. data/ext/opencv/cvsize.h +65 -0
  96. data/ext/opencv/cvsize2d32f.cpp +209 -0
  97. data/ext/opencv/cvsize2d32f.h +64 -0
  98. data/ext/opencv/cvslice.cpp +120 -0
  99. data/ext/opencv/cvslice.h +61 -0
  100. data/ext/opencv/cvsparsemat.cpp +44 -0
  101. data/ext/opencv/cvsparsemat.h +28 -0
  102. data/ext/opencv/cvsurfparams.cpp +199 -0
  103. data/ext/opencv/cvsurfparams.h +58 -0
  104. data/ext/opencv/cvsurfpoint.cpp +223 -0
  105. data/ext/opencv/cvsurfpoint.h +52 -0
  106. data/ext/opencv/cvtermcriteria.cpp +192 -0
  107. data/ext/opencv/cvtermcriteria.h +71 -0
  108. data/ext/opencv/cvtwopoints.cpp +116 -0
  109. data/ext/opencv/cvtwopoints.h +51 -0
  110. data/ext/opencv/cvutils.cpp +192 -0
  111. data/ext/opencv/cvutils.h +30 -0
  112. data/ext/opencv/cvvideowriter.cpp +137 -0
  113. data/ext/opencv/cvvideowriter.h +43 -0
  114. data/ext/opencv/extconf.rb +83 -0
  115. data/ext/opencv/gui.cpp +68 -0
  116. data/ext/opencv/gui.h +30 -0
  117. data/ext/opencv/iplconvkernel.cpp +192 -0
  118. data/ext/opencv/iplconvkernel.h +71 -0
  119. data/ext/opencv/iplimage.cpp +644 -0
  120. data/ext/opencv/iplimage.h +73 -0
  121. data/ext/opencv/mouseevent.cpp +181 -0
  122. data/ext/opencv/mouseevent.h +56 -0
  123. data/ext/opencv/opencv.cpp +727 -0
  124. data/ext/opencv/opencv.h +400 -0
  125. data/ext/opencv/pointset.cpp +274 -0
  126. data/ext/opencv/pointset.h +68 -0
  127. data/ext/opencv/trackbar.cpp +121 -0
  128. data/ext/opencv/trackbar.h +69 -0
  129. data/ext/opencv/window.cpp +357 -0
  130. data/ext/opencv/window.h +66 -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/1.9/opencv.so +0 -0
  136. data/lib/2.0/opencv.so +0 -0
  137. data/lib/opencv.rb +12 -0
  138. data/lib/opencv/psyched_yaml.rb +22 -0
  139. data/lib/opencv/version.rb +3 -0
  140. data/ruby-opencv.gemspec +44 -0
  141. data/test/helper.rb +166 -0
  142. data/test/runner.rb +30 -0
  143. data/test/samples/airplane.jpg +0 -0
  144. data/test/samples/baboon.jpg +0 -0
  145. data/test/samples/baboon200.jpg +0 -0
  146. data/test/samples/baboon200_rotated.jpg +0 -0
  147. data/test/samples/blank0.jpg +0 -0
  148. data/test/samples/blank1.jpg +0 -0
  149. data/test/samples/blank2.jpg +0 -0
  150. data/test/samples/blank3.jpg +0 -0
  151. data/test/samples/blank4.jpg +0 -0
  152. data/test/samples/blank5.jpg +0 -0
  153. data/test/samples/blank6.jpg +0 -0
  154. data/test/samples/blank7.jpg +0 -0
  155. data/test/samples/blank8.jpg +0 -0
  156. data/test/samples/blank9.jpg +0 -0
  157. data/test/samples/cat.jpg +0 -0
  158. data/test/samples/chessboard.jpg +0 -0
  159. data/test/samples/contours.jpg +0 -0
  160. data/test/samples/fruits.jpg +0 -0
  161. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  162. data/test/samples/inpaint-mask.bmp +0 -0
  163. data/test/samples/lena-256x256.jpg +0 -0
  164. data/test/samples/lena-32x32.jpg +0 -0
  165. data/test/samples/lena-eyes.jpg +0 -0
  166. data/test/samples/lena-inpaint.jpg +0 -0
  167. data/test/samples/lena.jpg +0 -0
  168. data/test/samples/lines.jpg +0 -0
  169. data/test/samples/messy0.jpg +0 -0
  170. data/test/samples/messy1.jpg +0 -0
  171. data/test/samples/movie_sample.avi +0 -0
  172. data/test/samples/one_way_train_0000.jpg +0 -0
  173. data/test/samples/one_way_train_0001.jpg +0 -0
  174. data/test/samples/partially_blank0.jpg +0 -0
  175. data/test/samples/partially_blank1.jpg +0 -0
  176. data/test/samples/smooth0.jpg +0 -0
  177. data/test/samples/smooth1.jpg +0 -0
  178. data/test/samples/smooth2.jpg +0 -0
  179. data/test/samples/smooth3.jpg +0 -0
  180. data/test/samples/smooth4.jpg +0 -0
  181. data/test/samples/smooth5.jpg +0 -0
  182. data/test/samples/smooth6.jpg +0 -0
  183. data/test/samples/str-cv-rotated.jpg +0 -0
  184. data/test/samples/str-cv.jpg +0 -0
  185. data/test/samples/str-ov.jpg +0 -0
  186. data/test/samples/stuff.jpg +0 -0
  187. data/test/test_curve.rb +43 -0
  188. data/test/test_cvavgcomp.rb +24 -0
  189. data/test/test_cvbox2d.rb +76 -0
  190. data/test/test_cvcapture.rb +183 -0
  191. data/test/test_cvchain.rb +108 -0
  192. data/test/test_cvcircle32f.rb +41 -0
  193. data/test/test_cvconnectedcomp.rb +61 -0
  194. data/test/test_cvcontour.rb +150 -0
  195. data/test/test_cvcontourtree.rb +43 -0
  196. data/test/test_cverror.rb +50 -0
  197. data/test/test_cvfeaturetree.rb +65 -0
  198. data/test/test_cvfont.rb +58 -0
  199. data/test/test_cvhaarclassifiercascade.rb +63 -0
  200. data/test/test_cvhistogram.rb +271 -0
  201. data/test/test_cvhumoments.rb +83 -0
  202. data/test/test_cvline.rb +50 -0
  203. data/test/test_cvmat.rb +2947 -0
  204. data/test/test_cvmat_drawing.rb +349 -0
  205. data/test/test_cvmat_dxt.rb +150 -0
  206. data/test/test_cvmat_imageprocessing.rb +2015 -0
  207. data/test/test_cvmat_matching.rb +57 -0
  208. data/test/test_cvmoments.rb +180 -0
  209. data/test/test_cvpoint.rb +75 -0
  210. data/test/test_cvpoint2d32f.rb +75 -0
  211. data/test/test_cvpoint3d32f.rb +93 -0
  212. data/test/test_cvrect.rb +144 -0
  213. data/test/test_cvscalar.rb +113 -0
  214. data/test/test_cvseq.rb +295 -0
  215. data/test/test_cvsize.rb +75 -0
  216. data/test/test_cvsize2d32f.rb +75 -0
  217. data/test/test_cvslice.rb +31 -0
  218. data/test/test_cvsurfparams.rb +57 -0
  219. data/test/test_cvsurfpoint.rb +66 -0
  220. data/test/test_cvtermcriteria.rb +56 -0
  221. data/test/test_cvtwopoints.rb +40 -0
  222. data/test/test_cvvideowriter.rb +58 -0
  223. data/test/test_iplconvkernel.rb +54 -0
  224. data/test/test_iplimage.rb +236 -0
  225. data/test/test_mouseevent.rb +17 -0
  226. data/test/test_opencv.rb +329 -0
  227. data/test/test_pointset.rb +126 -0
  228. data/test/test_preliminary.rb +130 -0
  229. data/test/test_trackbar.rb +47 -0
  230. data/test/test_window.rb +115 -0
  231. metadata +386 -0
data/README.md ADDED
@@ -0,0 +1,98 @@
1
+ # ruby-opencv
2
+
3
+ An OpenCV wrapper for Ruby.
4
+
5
+ * Web site: <https://github.com/ruby-opencv/ruby-opencv>
6
+ * Ruby 1.9.3, 2.0.0 and OpenCV 2.4.6.1 are supported.
7
+
8
+ ## Requirement
9
+
10
+ * OpenCV <http://opencv.org/>
11
+ * [Download](http://sourceforge.net/projects/opencvlibrary/)
12
+ * [Install guide](http://docs.opencv.org/doc/tutorials/introduction/table_of_content_introduction/table_of_content_introduction.html#table-of-content-introduction)
13
+
14
+ ## Install
15
+ ### Linux/Mac
16
+ 1. Install [OpenCV](http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/)
17
+ 2. Install ruby-opencv
18
+
19
+ ```
20
+ $ gem install ruby-opencv -- --with-opencv-dir=/path/to/opencvdir
21
+ ```
22
+
23
+ Note: **/path/to/opencvdir** is the directory where you installed OpenCV.
24
+
25
+
26
+ ### Windows
27
+ You can use pre-build binary for Windows (mswin32, mingw32).
28
+
29
+ 1. Install [OpenCV](http://sourceforge.net/projects/opencvlibrary/files/opencv-win/)
30
+ 2. Set path to OpenCV libraries. When you installed OpenCV to **C:\opencv**, add **C:\opencv\build\x86\vc10\bin (for mswin32)** or **C:\opencv\build\x86\mingw\bin (for mingw32)** to the systems path.
31
+ 3. Install ruby-opencv
32
+
33
+ ```
34
+ $ gem install ruby-opencv
35
+ ```
36
+
37
+ ## Sample code
38
+ ### Load and Display an Image
39
+
40
+ A sample to load and display an image. An equivalent code of [this tutorial](http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html#display-image).
41
+
42
+ ```ruby
43
+ require 'opencv'
44
+ include OpenCV
45
+
46
+ if ARGV.size == 0
47
+ puts "Usage: ruby #{__FILE__} ImageToLoadAndDisplay"
48
+ exit
49
+ end
50
+
51
+ image = nil
52
+ begin
53
+ image = CvMat.load(ARGV[0], CV_LOAD_IMAGE_COLOR) # Read the file.
54
+ rescue
55
+ puts 'Could not open or find the image.'
56
+ exit
57
+ end
58
+
59
+ window = GUI::Window.new('Display window') # Create a window for display.
60
+ window.show(image) # Show our image inside it.
61
+ GUI::wait_key # Wait for a keystroke in the window.
62
+ ```
63
+
64
+ ### Face Detection
65
+
66
+ A sample to detect faces from an image.
67
+
68
+ ```ruby
69
+ require 'opencv'
70
+ include OpenCV
71
+
72
+ if ARGV.length < 2
73
+ puts "Usage: ruby #{__FILE__} source dest"
74
+ exit
75
+ end
76
+
77
+ data = './data/haarcascades/haarcascade_frontalface_alt.xml'
78
+ detector = CvHaarClassifierCascade::load(data)
79
+ image = CvMat.load(ARGV[0])
80
+ detector.detect_objects(image).each do |region|
81
+ color = CvColor::Blue
82
+ image.rectangle! region.top_left, region.bottom_right, :color => color
83
+ end
84
+
85
+ image.save_image(ARGV[1])
86
+ window = GUI::Window.new('Face detection')
87
+ window.show(image)
88
+ GUI::wait_key
89
+ ```
90
+
91
+ For more samples, see examples/*.rb
92
+
93
+ ## LICENSE:
94
+
95
+ The BSD Liscense
96
+
97
+ see LICENSE.txt
98
+
data/Rakefile ADDED
@@ -0,0 +1,90 @@
1
+ # -*- mode: ruby; coding: utf-8 -*-
2
+ require 'rubygems'
3
+ require "rubygems/ext"
4
+ require "rubygems/installer"
5
+ require 'hoe'
6
+ require 'rake/extensiontask'
7
+ require 'fileutils'
8
+ require './lib/opencv/psyched_yaml'
9
+
10
+ SO_FILE = 'opencv.so'
11
+
12
+ Hoe.plugin :gemspec
13
+
14
+ hoespec = Hoe.spec 'ruby-opencv' do |s|
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']
18
+ s.developer('lsxi', 'masakazu.yonekura@gmail.com')
19
+ s.developer('ser1zw', 'azariahsawtikes@gmail.com')
20
+ s.developer('pcting', 'pcting@gmail.com')
21
+
22
+ s.readme_file = 'README.md'
23
+ s.history_file = 'History.txt'
24
+
25
+ s.spec_extras = { :extensions => ['ext/opencv/extconf.rb'] }
26
+
27
+ s.test_globs = ['test/test_*.rb']
28
+ s.urls = ['https://github.com/ruby-opencv/ruby-opencv/']
29
+
30
+ s.extra_dev_deps << ['rake-compiler', '>= 0'] << ['hoe-gemspec']
31
+
32
+ Rake::ExtensionTask.new('opencv', spec) do |ext|
33
+ ext.lib_dir = 'lib'
34
+ end
35
+ end
36
+
37
+ hoespec.spec.files.delete('.gemtest')
38
+
39
+ Rake::Task[:test].prerequisites << :compile
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
+
90
+ # vim: syntax=ruby
data/config.yml ADDED
@@ -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
@@ -0,0 +1,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
+
data/examples/box.png ADDED
Binary file
Binary file
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Detects contours in an image and
4
+ # their boundingboxes
5
+ #
6
+ require "opencv"
7
+
8
+ # Load image
9
+ cvmat = OpenCV::CvMat.load("rotated-boxes.jpg")
10
+
11
+ # The "canny" edge-detector does only work with grayscale images
12
+ # so to be sure, convert the image
13
+ # otherwise you will get an OpenCV::CvStsAssert exception.
14
+ cvmat = cvmat.BGR2GRAY
15
+
16
+ # Use the "canny" edge detection algorithm (http://en.wikipedia.org/wiki/Canny_edge_detector)
17
+ # Parameters are two colors that are then used to determine possible corners
18
+ canny = cvmat.canny(50, 150)
19
+
20
+ # Look for contours
21
+ # We want them to be returned as a flat list (CV_RETR_LIST) and simplified (CV_CHAIN_APPROX_SIMPLE)
22
+ # Both are the defaults but included here for clarity
23
+ contour = canny.find_contours(:mode => OpenCV::CV_RETR_LIST, :method => OpenCV::CV_CHAIN_APPROX_SIMPLE)
24
+
25
+ # The Canny Algorithm returns two matches for every contour (see O'Reilly: Learning OpenCV Page 235)
26
+ # We need only the external edges so we ignore holes.
27
+ # When there are no more contours, contours.h_next will return nil
28
+ while contour
29
+ # No "holes" please (aka. internal contours)
30
+ unless contour.hole?
31
+
32
+ puts '-' * 80
33
+ puts "BOUNDING RECT FOUND"
34
+ puts '-' * 80
35
+
36
+ # You can detect the "bounding rectangle" which is always oriented horizontally and vertically
37
+ box = contour.bounding_rect
38
+ puts "found external contour with bounding rectangle from #{box.top_left.x},#{box.top_left.y} to #{box.bottom_right.x},#{box.bottom_right.y}"
39
+
40
+ # The contour area can be computed:
41
+ puts "that contour encloses an area of #{contour.contour_area} square pixels"
42
+
43
+ # .. as can be the length of the contour
44
+ puts "that contour is #{contour.arc_length} pixels long "
45
+
46
+ # Draw that bounding rectangle
47
+ cvmat.rectangle! box.top_left, box.bottom_right, :color => OpenCV::CvColor::Black
48
+
49
+ # You can also detect the "minimal rectangle" which has an angle, width, height and center coordinates
50
+ # and is not neccessarily horizonally or vertically aligned.
51
+ # The corner of the rectangle with the lowest y and x position is the anchor (see image here: http://bit.ly/lT1XvB)
52
+ # The zero angle position is always straight up.
53
+ # Positive angle values are clockwise and negative values counter clockwise (so -60 means 60 degree counter clockwise)
54
+ box = contour.min_area_rect2
55
+ puts "found minimal rectangle with its center at (#{box.center.x.round},#{box.center.y.round}), width of #{box.size.width.round}px, height of #{box.size.height.round} and an angle of #{box.angle.round} degree"
56
+ end
57
+ contour = contour.h_next
58
+ end
59
+
60
+ # And save the image
61
+ puts "\nSaving image with bounding rectangles"
62
+ cvmat.save_image("rotated-boxes-with-detected-bounding-rectangles.jpg")
@@ -0,0 +1,139 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file shows the different retrieval modes for contour detection
4
+ #
5
+ require "opencv"
6
+
7
+ # Load image
8
+ # The structure of the image is "explained" in bitmap-contours-with-labels.png
9
+ cvmat = OpenCV::CvMat.load("bitmap-contours.png")
10
+
11
+ # "find_contours" does only operate on bitmap images (black/white)
12
+ mat = OpenCV::CvMat.new(cvmat.rows, cvmat.cols, :cv8u, 1)
13
+ (cvmat.rows * cvmat.cols).times do |i|
14
+ mat[i] = (cvmat[i][0] <= 128) ? OpenCV::CvScalar.new(0) : OpenCV::CvScalar.new(255)
15
+ end
16
+
17
+ # find_contours takes two parameters:
18
+ # 1. Retrieval mode (:mode, defines the structure of the contour sequence returned)
19
+ # - CV_RETR_LIST (default)
20
+ # - CV_RETR_EXTERNAL
21
+ # - CV_RETR_CCOMP
22
+ # - CV_RETR_TREE
23
+ # 2. Retrieval Method (:method, how the contours are approximated)
24
+ # - CV_CHAIN_CODE
25
+ # - CV_CHAIN_APPROX_NONE
26
+ # - CV_CHAIN_APPROX_SIMPLE (default)
27
+ # - CV_CHAIN_APPROX_TC89_L1
28
+ # - CV_CHAIN_APPROX_T89_KCOS
29
+ # - CV_LINK_RUNS
30
+
31
+ #
32
+ # The default: CV_RETR_LIST and CV_CHAIN_APPROX_SIMPLE
33
+ # This produces a flat list of contours that can be traversed with .h_next and .h_prev
34
+ #
35
+ puts "Detecting using CV_RETR_LIST and CV_CHAIN_APPROX_SIMPLE"
36
+ contour = mat.find_contours(:mode => OpenCV::CV_RETR_LIST, :method => OpenCV::CV_CHAIN_APPROX_SIMPLE)
37
+ cindex=1
38
+
39
+ while contour
40
+ puts "Contour ##{cindex} is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
41
+ contour = contour.h_next
42
+ cindex+=1
43
+ end
44
+
45
+ #
46
+ # CV_RETR_EXTERNAL retrieves only the outer most non "hole" contour
47
+ #
48
+ puts '-'*80
49
+ puts "Detecting using CV_RETR_EXTERNAL and CV_CHAIN_APPROX_SIMPLE"
50
+ contour = mat.find_contours(:mode => OpenCV::CV_RETR_EXTERNAL, :method => OpenCV::CV_CHAIN_APPROX_SIMPLE)
51
+ cindex=1
52
+
53
+ while contour
54
+ puts "Contour ##{cindex} is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
55
+ contour = contour.h_next
56
+ cindex+=1
57
+ end
58
+
59
+ #
60
+ # CV_RETR_CCOMP organizes the contours in a two level deep stack
61
+ # The first level holds the contours
62
+ # The second level contains the holes of the contours in level 1
63
+ #
64
+ # C00001 <-> C00000 <-> C000 <-> C0
65
+ # | |
66
+ # V V
67
+ # H0000 H00
68
+ #
69
+ puts '-'*80
70
+ puts "Detecting using CV_RETR_CCOMP and CV_CHAIN_APPROX_SIMPLE"
71
+ contour = mat.find_contours(:mode => OpenCV::CV_RETR_CCOMP, :method => OpenCV::CV_CHAIN_APPROX_SIMPLE)
72
+
73
+ # C00001
74
+ puts "Contour #1 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
75
+ contour = contour.h_next
76
+
77
+ # C00000
78
+ puts "Contour #2 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
79
+ contour = contour.h_next
80
+
81
+ # C000
82
+ puts "Contour #3 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
83
+ contour_down = contour.v_next
84
+
85
+ # H0000
86
+ puts "Contour #4 is #{contour_down.contour_area} px^2 (width: #{contour_down.bounding_rect.width}, height: #{contour_down.bounding_rect.height}, type: #{(contour_down.hole?)?"hole":"contour"})"
87
+ contour = contour.h_next
88
+
89
+ # C0
90
+ puts "Contour #5 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
91
+ contour_down = contour.v_next
92
+
93
+ # H00
94
+ puts "Contour #6 is #{contour_down.contour_area} px^2 (width: #{contour_down.bounding_rect.width}, height: #{contour_down.bounding_rect.height}, type: #{(contour_down.hole?)?"hole":"contour"})"
95
+
96
+ #
97
+ # CV_RETR_TREE manages the contours in a tree structure
98
+ # This reconstructs the complete hierarchy of contours and holes that the image displayed
99
+ #
100
+ # C0
101
+ # |
102
+ # V
103
+ # H00
104
+ # |
105
+ # V
106
+ # C000
107
+ # |
108
+ # V
109
+ # H0000-------+
110
+ # | |
111
+ # V V
112
+ # C00000 C00001
113
+ #
114
+ puts '-'*80
115
+ puts "Detecting using CV_RETR_TREE and CV_CHAIN_APPROX_SIMPLE"
116
+ contour = mat.find_contours(:mode => OpenCV::CV_RETR_TREE, :method => OpenCV::CV_CHAIN_APPROX_SIMPLE)
117
+
118
+ # C0
119
+ puts "Contour #1 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
120
+ contour = contour.v_next
121
+
122
+ # H00
123
+ puts "Contour #2 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
124
+ contour = contour.v_next
125
+
126
+ # C000
127
+ puts "Contour #3 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
128
+ contour = contour.v_next
129
+
130
+ # H0000
131
+ puts "Contour #4 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
132
+ contour = contour.v_next
133
+
134
+ # C00000
135
+ puts "Contour #5 is #{contour.contour_area} px^2 (width: #{contour.bounding_rect.width}, height: #{contour.bounding_rect.height}, type: #{(contour.hole?)?"hole":"contour"})"
136
+ contour_right = contour.h_next
137
+
138
+ # C00001
139
+ puts "Contour #6 is #{contour_right.contour_area} px^2 (width: #{contour_right.bounding_rect.width}, height: #{contour_right.bounding_rect.height}, type: #{(contour_right.hole?)?"hole":"contour"})"
Binary file
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+ # convexhull.rb
3
+ # Draw contours and convexity defect points to captured image
4
+ require "rubygems"
5
+ require "opencv"
6
+ include OpenCV
7
+
8
+ window = GUI::Window.new("convexhull")
9
+ capture = CvCapture::open
10
+
11
+ accuracy = 1
12
+ t = window.set_trackbar("accuracy", 10, 1) { |v|
13
+ accuracy = v
14
+ }
15
+
16
+ circle_options = { :color => CvColor::Blue, :line_type => :aa, :thickness => -1 }
17
+
18
+ loop do
19
+ image = capture.query
20
+
21
+ # Calculate contours from a binary image
22
+ gray = image.BGR2GRAY
23
+ bin = gray.threshold(0x44, 0xFF, :binary)
24
+ contours = bin.find_contours
25
+
26
+ while contours
27
+ # Draw contours
28
+ poly = contours.approx(:accuracy => accuracy)
29
+ begin
30
+ image.draw_contours!(poly, CvColor::Red, CvColor::Black, 2,
31
+ :thickness => 2, :line_type => :aa)
32
+ end while (poly = poly.h_next)
33
+
34
+ # Draw convexity defects
35
+ hull = contours.convex_hull2(true, false)
36
+ contours.convexity_defects(hull).each { |cd|
37
+ image.circle!(cd.start, 3, circle_options)
38
+ image.circle!(cd.depth_point, 3, circle_options)
39
+ image.circle!(cd.end, 3, circle_options)
40
+ }
41
+ contours = contours.h_next
42
+ end
43
+
44
+ window.show image
45
+ exit if GUI::wait_key(1)
46
+ end
47
+