Skip to content
Browse files

Initial commit, with some minor changes from orig (date, exec bit, li…

…ne endings, /usr/bin/env)
  • Loading branch information...
0 parents commit 9196cb13aa8e3ab4b58139c75ce7017e44a2d969 @ryanfb ryanfb committed
Showing with 14,658 additions and 0 deletions.
  1. +5 −0 History.txt
  2. +30 −0 License.txt
  3. +115 −0 Manifest.txt
  4. +47 −0 README.txt
  5. +34 −0 Rakefile
  6. +41 −0 examples/convexhull.rb
  7. +25 −0 examples/face_detect.rb
  8. +23 −0 examples/houghcircle.rb
  9. +2 −0 examples/inpaint.png
  10. +43 −0 examples/inpaint.rb
  11. +72 −0 examples/paint.rb
  12. +43 −0 examples/snake.rb
  13. +1 −0 examples/stuff.jpg
  14. +103 −0 ext/curve.cpp
  15. +34 −0 ext/curve.h
  16. +67 −0 ext/cvavgcomp.cpp
  17. +39 −0 ext/cvavgcomp.h
  18. +114 −0 ext/cvbox2d.cpp
  19. +53 −0 ext/cvbox2d.h
  20. +276 −0 ext/cvcapture.cpp
  21. +54 −0 ext/cvcapture.h
  22. +184 −0 ext/cvchain.cpp
  23. +43 −0 ext/cvchain.h
  24. +49 −0 ext/cvchaincode.cpp
  25. +43 −0 ext/cvchaincode.h
  26. +90 −0 ext/cvcircle32f.cpp
  27. +53 −0 ext/cvcircle32f.h
  28. +230 −0 ext/cvcondensation.cpp
  29. +49 −0 ext/cvcondensation.h
  30. +115 −0 ext/cvconnectedcomp.cpp
  31. +46 −0 ext/cvconnectedcomp.h
  32. +219 −0 ext/cvcontour.cpp
  33. +47 −0 ext/cvcontour.h
  34. +86 −0 ext/cvcontourtree.cpp
  35. +41 −0 ext/cvcontourtree.h
  36. +103 −0 ext/cvconvexitydefect.cpp
  37. +42 −0 ext/cvconvexitydefect.h
  38. +140 −0 ext/cverror.cpp
  39. +79 −0 ext/cverror.h
  40. +173 −0 ext/cvfont.cpp
  41. +56 −0 ext/cvfont.h
  42. +159 −0 ext/cvhaarclassifiercascade.cpp
  43. +41 −0 ext/cvhaarclassifiercascade.h
  44. +200 −0 ext/cvhistogram.cpp
  45. +51 −0 ext/cvhistogram.h
  46. +73 −0 ext/cvindex.cpp
  47. +40 −0 ext/cvindex.h
  48. +106 −0 ext/cvline.cpp
  49. +52 −0 ext/cvline.h
  50. +4,809 −0 ext/cvmat.cpp
  51. +286 −0 ext/cvmat.h
  52. +44 −0 ext/cvmatnd.cpp
  53. +28 −0 ext/cvmatnd.h
  54. +64 −0 ext/cvmemstorage.cpp
  55. +53 −0 ext/cvmemstorage.h
  56. +204 −0 ext/cvmoments.cpp
  57. +48 −0 ext/cvmoments.h
  58. +229 −0 ext/cvpoint.cpp
  59. +59 −0 ext/cvpoint.h
  60. +213 −0 ext/cvpoint2d32f.cpp
  61. +61 −0 ext/cvpoint2d32f.h
  62. +245 −0 ext/cvpoint3d32f.cpp
  63. +64 −0 ext/cvpoint3d32f.h
  64. +340 −0 ext/cvrect.cpp
  65. +79 −0 ext/cvrect.h
  66. +227 −0 ext/cvscalar.cpp
  67. +63 −0 ext/cvscalar.h
  68. +583 −0 ext/cvseq.cpp
  69. +71 −0 ext/cvseq.h
  70. +63 −0 ext/cvset.cpp
  71. +39 −0 ext/cvset.h
  72. +223 −0 ext/cvsize.cpp
  73. +63 −0 ext/cvsize.h
  74. +180 −0 ext/cvsize2d32f.cpp
  75. +59 −0 ext/cvsize2d32f.h
  76. +82 −0 ext/cvslice.cpp
  77. +53 −0 ext/cvslice.h
  78. +44 −0 ext/cvsparsemat.cpp
  79. +28 −0 ext/cvsparsemat.h
  80. +183 −0 ext/cvtermcriteria.cpp
  81. +71 −0 ext/cvtermcriteria.h
  82. +98 −0 ext/cvtwopoints.cpp
  83. +50 −0 ext/cvtwopoints.h
  84. +206 −0 ext/cvvector.cpp
  85. +54 −0 ext/cvvector.h
  86. +116 −0 ext/cvvideowriter.cpp
  87. +41 −0 ext/cvvideowriter.h
  88. +61 −0 ext/extconf.rb
  89. +65 −0 ext/gui.cpp
  90. +33 −0 ext/gui.h
  91. +177 −0 ext/iplconvkernel.cpp
  92. +52 −0 ext/iplconvkernel.h
  93. +238 −0 ext/iplimage.cpp
  94. +54 −0 ext/iplimage.h
  95. +184 −0 ext/mouseevent.cpp
  96. +59 −0 ext/mouseevent.h
  97. +481 −0 ext/opencv.cpp
Sorry, we could not display the entire diff because it was too big.
5 History.txt
@@ -0,0 +1,5 @@
+=== 0.0.6 / 2008-06-27
+
+* First gem release.
+
+ * Some OpenCV function wrapped.
30 License.txt
@@ -0,0 +1,30 @@
+The BSD Liscense
+
+Copyright (c) 2008, Masakazu Yonekura
+All rights reserved.
+
+Redistribution and use of this software in source and binary forms, with or without modification, are
+permitted provided that the following conditions are met:
+
+* Redistributions of source code must retain the above
+ copyright notice, this list of conditions and the
+ following disclaimer.
+
+* Redistributions in binary form must reproduce the above
+ copyright notice, this list of conditions and the
+ following disclaimer in the documentation and/or other
+ materials provided with the distribution.
+
+* Neither the name of Masakazu Yonekura. nor the names of its
+ contributors may be used to endorse or promote products
+ derived from this software without specific prior
+ written permission of Masakazu Yonekura.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
+TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
115 Manifest.txt
@@ -0,0 +1,115 @@
+History.txt
+Manifest.txt
+README.txt
+License.txt
+Rakefile
+examples/convexhull.rb
+examples/face_detect.rb
+examples/houghcircle.rb
+examples/inpaint.png
+examples/inpaint.rb
+examples/paint.rb
+examples/snake.rb
+examples/stuff.jpg
+ext/curve.cpp
+ext/curve.h
+ext/cvavgcomp.cpp
+ext/cvavgcomp.h
+ext/cvbox2d.cpp
+ext/cvbox2d.h
+ext/cvcapture.cpp
+ext/cvcapture.h
+ext/cvchain.cpp
+ext/cvchain.h
+ext/cvchaincode.cpp
+ext/cvchaincode.h
+ext/cvcircle32f.cpp
+ext/cvcircle32f.h
+ext/cvcondensation.cpp
+ext/cvcondensation.h
+ext/cvconnectedcomp.cpp
+ext/cvconnectedcomp.h
+ext/cvcontour.cpp
+ext/cvcontour.h
+ext/cvcontourtree.cpp
+ext/cvcontourtree.h
+ext/cvconvexitydefect.cpp
+ext/cvconvexitydefect.h
+ext/cverror.cpp
+ext/cverror.h
+ext/cvfont.cpp
+ext/cvfont.h
+ext/cvhaarclassifiercascade.cpp
+ext/cvhaarclassifiercascade.h
+ext/cvhistogram.cpp
+ext/cvhistogram.h
+ext/cvindex.cpp
+ext/cvindex.h
+ext/cvline.cpp
+ext/cvline.h
+ext/cvmat.cpp
+ext/cvmat.h
+ext/cvmatnd.cpp
+ext/cvmatnd.h
+ext/cvmemstorage.cpp
+ext/cvmemstorage.h
+ext/cvmoments.cpp
+ext/cvmoments.h
+ext/cvpoint.cpp
+ext/cvpoint.h
+ext/cvpoint2d32f.cpp
+ext/cvpoint2d32f.h
+ext/cvpoint3d32f.cpp
+ext/cvpoint3d32f.h
+ext/cvrect.cpp
+ext/cvrect.h
+ext/cvscalar.cpp
+ext/cvscalar.h
+ext/cvseq.cpp
+ext/cvseq.h
+ext/cvset.cpp
+ext/cvset.h
+ext/cvsize.cpp
+ext/cvsize.h
+ext/cvsize2d32f.cpp
+ext/cvsize2d32f.h
+ext/cvslice.cpp
+ext/cvslice.h
+ext/cvsparsemat.cpp
+ext/cvsparsemat.h
+ext/cvtermcriteria.cpp
+ext/cvtermcriteria.h
+ext/cvtwopoints.cpp
+ext/cvtwopoints.h
+ext/cvvector.cpp
+ext/cvvector.h
+ext/cvvideowriter.cpp
+ext/cvvideowriter.h
+ext/extconf.rb
+ext/gui.cpp
+ext/gui.h
+ext/iplconvkernel.cpp
+ext/iplconvkernel.h
+ext/iplimage.cpp
+ext/iplimage.h
+ext/mouseevent.cpp
+ext/mouseevent.h
+ext/opencv.cpp
+ext/opencv.h
+ext/point3dset.cpp
+ext/point3dset.h
+ext/pointset.cpp
+ext/pointset.h
+ext/trackbar.cpp
+ext/trackbar.h
+ext/window.cpp
+ext/window.h
+images/CvMat_sobel.png
+images/CvMat_sub_rect.png
+images/CvSeq_relationmap.png
+images/face_detect_from_lena.jpg
+lib/opencv.rb
+lib/version.rb
+setup/setup.cygwin.rb
+setup/setup.mingw.rb
+setup/setup.mswin32.rb
47 README.txt
@@ -0,0 +1,47 @@
+= opencv
+
+OpenCV Sourceforge Project
+http://sourceforge.net/projects/opencvlibrary/
+
+Ruby/OpenCV Author's Web Page
+http://blueruby.mydns.jp/opencv
+
+== DESCRIPTION:
+
+OpenCV Ruby Wrapper
+
+== FEATURES/PROBLEMS:
+
+* First release rubygems, Some OpenCV function wrapped.
+
+== SYNOPSIS:
+
+# Show image via GUI Window.
+
+require "rubygems"
+gem "opencv"
+require "opencv"
+
+image = OpenCV::IplImage.load("sample.jpg")
+window = OpenCV::GUI::Window.new("preview")
+window.show(image)
+OpenCV::GUI::wait_key
+
+# other sample code, see examples/*.rb
+
+== REQUIREMENTS:
+
+* OpenCV 1.0 or later.
+ http://sourceforge.net/projects/opencvlibrary/
+* ffcall (optional)
+ http://www.haible.de/bruno/packages-ffcall.html
+
+== INSTALL:
+
+gem install opencv
+
+== LICENSE:
+
+The BSD Liscense
+
+see LICENSE.txt
34 Rakefile
@@ -0,0 +1,34 @@
+# -*- ruby -*-
+
+require 'rubygems'
+require 'hoe'
+require './lib/version'
+
+Hoe.new('opencv', OpenCV::VERSION) do |p|
+ p.author = ['Masakazu Yonekura']
+ p.changes = p.paragraphs_of('History.txt', 0..1).join("\n\n")
+ p.description = <<EOF
+OpenCV wrapper for Ruby
+EOF
+ p.rubyforge_name = 'opencv'
+ p.developer('lsxi', 'masakazu.yonekura@gmail.com')
+ p.email = ['masakazu.yonekura@gmail.com']
+
+ p.need_tar = false
+ p.need_zip = false
+ # p.rdoc_pattern =
+ # p.remote_rdoc_dir =
+ # p.rsync =
+ p.spec_extras = {
+ :extensions => %w{ext/extconf.rb}
+ }
+ p.summary = 'OpenCV wrapper for Ruby.'
+ # p.test_globs = 'spec/**/*_spec.rb'
+ p.clean_globs |= ['*.o']
+
+ p.url = 'http://blueruby.mydns.jp/opencv'
+
+ p.extra_deps << ['hoe']
+end
+
+# vim: syntax=Ruby
41 examples/convexhull.rb
@@ -0,0 +1,41 @@
+#!/usr/bin/env ruby
+# convexhull.rb
+gem "opencv"
+require "opencv"
+require "pp"
+include OpenCV
+
+window = GUI::Window.new("convexhull")
+pp CvCapture::INTERFACE
+capture = CvCapture::open
+
+accuracy = 0.1
+t = window.set_trackbar("accuracy", 100, 1){|v|
+ accuracy = 0.1 * v
+}
+
+while true
+ key = GUI::wait_key(1)
+ image = capture.query
+ gray = image.BGR2GRAY
+ bin = gray.threshold_binary(0x44, 0xFF)
+ contours = bin.find_contours
+ while contours
+ image.poly_line! contours.approx(:accuracy => accuracy), :color => CvScalar::Red
+ contours.convexity_defects.each{|cd|
+ image.circle! cd.start, 1, :color => CvScalar::Blue
+ image.circle! cd.end, 1, :color => CvScalar::Blue
+ image.circle! cd.depth_point, 1, :color => CvScalar::Blue
+ }
+
+ contours = contours.h_next
+ end
+ #pts = gray.good_features_to_track(0.01, 10)
+ #puts pts.length
+ window.show image
+ next unless key
+ case key.chr
+ when "\e"
+ exit
+ end
+end
25 examples/face_detect.rb
@@ -0,0 +1,25 @@
+#!/usr/bin/env ruby
+# face_detect.rb
+require "rubygems"
+gem "opencv"
+require "opencv"
+
+include OpenCV
+
+window = GUI::Window.new("face detect")
+capture = CvCapture.open
+detector = CvHaarClassifierCascade::load("C:/Program Files/OpenCV/data/haarcascades/haarcascade_frontalface_alt.xml")
+
+while true
+ key = GUI::wait_key(1)
+ image = capture.query
+ detector.detect_objects(image){|i|
+ image.rectangle! i.top_left, i.bottom_right, :color => CvColor::Red
+ }
+ window.show image
+ next unless key
+ case key.chr
+ when "\e"
+ exit
+ end
+end
23 examples/houghcircle.rb
@@ -0,0 +1,23 @@
+#!/usr/bin/env ruby
+# houghcircle.rb
+require "rubygems"
+gem "opencv"
+require "opencv"
+include OpenCV
+
+original_window = GUI::Window.new "original"
+hough_window = GUI::Window.new "hough circles"
+
+image = IplImage::load "stuff.jpg"
+gray = image.BGR2GRAY
+
+result = image.clone
+original_window.show image
+detect = gray.hough_circles_gradient(2.0, 10, 200, 50)
+puts detect.size
+detect.each{|circle|
+ puts "#{circle.center.x},#{circle.center.y} - #{circle.radius}"
+ result.circle! circle.center, circle.radius, :color => CvColor::Red, :thickness => 3
+}
+hough_window.show result
+GUI::wait_key
2 examples/inpaint.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 examples/inpaint.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+# inpaint.rb
+require "rubygems"
+gem "opencv"
+require "opencv"
+
+include OpenCV
+
+owindow = GUI::Window.new "original"
+mwindow = GUI::Window.new "mask"
+iwindow = GUI::Window.new "inpaint"
+
+image = IplImage::load "inpaint.png"
+b, g, r = image.split
+original_mask = r.threshold_binary_inverse(0x00, 0xFF) & b.threshold_binary_inverse(0x00, 0xFF)
+mask = original_mask.copy
+
+num_dilate = 3
+radius = 5
+dilate_bar = mwindow.set_trackbar("dilate", 10, num_dilate){|v|
+ num_dilate = v
+ mask = original_mask.dilate(nil, num_dilate)
+ mwindow.show mask
+}
+
+radius_bar = mwindow.set_trackbar("radius", 30, radius){|v|
+ radius = v
+}
+
+owindow.show image
+mwindow.show mask
+
+while key = GUI::wait_key
+ case key.chr
+ when "\e" # esc
+ exit
+ when "n"
+ iwindow.show image.inpaint_ns(mask, radius)
+ when "t"
+ iwindow.show image.inpaint_telea(mask, radius)
+ end
+end
+
72 examples/paint.rb
@@ -0,0 +1,72 @@
+#!/usr/bin/env ruby
+# paint.rb
+require "rubygems"
+gem "opencv"
+require "opencv"
+
+include OpenCV
+
+window = GUI::Window.new("free canvas")
+canvas = CvMat.new(500, 500, 0, 3).fill!(0xFF) # create white canvas
+window.show canvas
+
+colors = CvColor::constants.collect{|i| i.to_s }
+
+usage =<<USAGE
+[mouse]
+drag - draw
+right button - fill by color
+[keyborad]
+1 to 9 - change thickness of line
+type color name - change color
+esc - exit
+USAGE
+puts usage
+
+point = nil
+
+# drawing option
+opt = {
+ :color => CvColor::Black,
+ :tickness => 1
+}
+
+window.on_mouse{|m|
+ case m.event
+ when :move
+ if m.left_button?
+ canvas.line!(point, m, opt) if point
+ point = m
+ end
+ when :left_button_down
+ canvas.line!(m, m, opt)
+ point = m
+ when :left_button_up
+ point = nil
+ when :right_button_down
+ mask = canvas.flood_fill!(m, opt[:color])
+ end
+ window.show canvas
+}
+
+color_name = ''
+while key = GUI.wait_key
+ next if key < 0
+ case key.chr
+ when "\e" # [esc] - exit
+ exit
+ when '1'..'9'
+ puts "change thickness to #{key.chr.to_i}."
+ opt[:thickness] = key.chr.to_i
+ else
+ color_name << key.chr
+ choice = colors.find_all{|i| i =~ /\A#{color_name}/i}
+ if choice.size == 1
+ color,= choice
+ puts "change color to #{color}."
+ opt[:color] = CvColor::const_get(color)
+ end
+ color_name = '' if choice.length < 2
+ end
+end
+
43 examples/snake.rb
@@ -0,0 +1,43 @@
+#!/usr/bin/env ruby
+# snake.rb
+require "rubygems"
+gem "opencv"
+require "opencv"
+include OpenCV
+
+puts <<USAGE
+usage:
+ left-click: set point
+ right-click: do snake
+USAGE
+
+window = GUI::Window.new "snake demo"
+image = CvMat.new(256, 256, :cv8u, 1).clear!
+image.circle!(CvPoint.new(128,128), 40, :color => CvColor::White, :thickness => -1)
+display = image.GRAY2BGR
+
+window.show display
+
+points = []
+
+window.on_mouse{|mouse|
+ case mouse.event
+ when :left_button_down
+ display[mouse.x, mouse.y] = CvColor::Red
+ puts "set point (#{mouse.x},#{mouse.y})"
+ points << CvPoint.new(mouse.x, mouse.y)
+ window.show display
+ when :right_button_down
+ if points.length < 3
+ puts "please set more point!"
+ next
+ end
+ points = image.snake_image(points, 1.0, 0.5, 1.5, CvSize.new(3, 3), 100)
+ display = image.GRAY2BGR
+ display.poly_line! points, :color => CvColor::Red, :is_closed => true
+ window.show display
+ end
+}
+
+GUI::wait_key
+
1 examples/stuff.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 ext/curve.cpp
@@ -0,0 +1,103 @@
+/************************************************************
+
+ curve.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005 Masakazu Yonekura
+
+************************************************************/
+#include"curve.h"
+/*
+ * Document-class: OpenCV::Curve
+ *
+ * Curve sequence.
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CURVE
+
+VALUE module;
+
+VALUE
+rb_module()
+{
+ return module;
+}
+
+void
+define_ruby_module()
+{
+ if(module)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+ module = rb_define_module_under(opencv, "Curve");
+ rb_define_method(module, "close?", RUBY_METHOD_FUNC(rb_closed_q), 0);
+ rb_define_method(module, "convex?", RUBY_METHOD_FUNC(rb_convex_q), 0);
+ rb_define_method(module, "hole?", RUBY_METHOD_FUNC(rb_hole_q), 0);
+ rb_define_method(module, "simple?", RUBY_METHOD_FUNC(rb_simple_q), 0);
+ rb_define_method(module, "arc_length", RUBY_METHOD_FUNC(rb_arc_length), -1);
+}
+
+/*
+ * If curve is closed, return true. Otherwise return false.
+ */
+VALUE
+rb_closed_q(VALUE self)
+{
+ return CV_IS_SEQ_CLOSED(CVSEQ(self)) ? Qtrue : Qfalse;
+}
+
+/*
+ * If curve is convex, return true. Otherwise return false.
+ */
+VALUE
+rb_convex_q(VALUE self)
+{
+ return CV_IS_SEQ_CONVEX(CVSEQ(self)) ? Qtrue : Qfalse;
+}
+
+/*
+ * If curve is hole(inner contour), return true. Otherwise return false.
+ */
+VALUE
+rb_hole_q(VALUE self)
+{
+ return CV_IS_SEQ_HOLE(CVSEQ(self)) ? Qtrue : Qfalse;
+}
+
+/*
+ * no idia.
+ */
+VALUE
+rb_simple_q(VALUE self)
+{
+ return CV_IS_SEQ_SIMPLE(CVSEQ(self)) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ * arc_length(<i>[slice = nil][,is_closed = nil]</i>) -> float
+ *
+ * Calculates contour perimeter or curve length.
+ * <i>slice</i> is starting and ending points of the curve.
+ * <i>is_closed</i> is indicates whether the curve is closed or not. There are 3 cases:
+ * * is_closed = true - the curve is assumed to be unclosed.
+ * * is_closed = false - the curve is assumed to be closed.
+ * * is_closed = nil (default) use self#close?
+ */
+VALUE
+rb_arc_length(int argc, VALUE *argv, VALUE self)
+{
+ VALUE slice, is_closed;
+ rb_scan_args(argc, argv, "02", &slice, &is_closed);
+ return rb_float_new(cvArcLength(CVARR(self), NIL_P(slice) ? CV_WHOLE_SEQ : VALUE_TO_CVSLICE(slice), TRUE_OR_FALSE(is_closed, -1)));
+}
+
+__NAMESPACE_END_CURVE
+__NAMESPACE_END_OPENCV
+
34 ext/curve.h
@@ -0,0 +1,34 @@
+/************************************************************
+
+ curve.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#ifndef RUBY_OPENCV_CVSEQ_CURVE_H
+#define RUBY_OPENCV_CVSEQ_CURVE_H
+
+#include"opencv.h"
+
+#define __NAMESPACE_BEGIN_CURVE namespace mCurve{
+#define __NAMESPACE_END_CURVE }
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CURVE
+
+VALUE rb_module();
+
+void define_ruby_module();
+
+VALUE rb_closed_q(VALUE self);
+VALUE rb_convex_q(VALUE self);
+VALUE rb_hole_q(VALUE self);
+VALUE rb_simple_q(VALUE self);
+VALUE rb_arc_length(int argc, VALUE *argv, VALUE self);
+
+__NAMESPACE_END_CURVE
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVSEQ_CURVE_H
67 ext/cvavgcomp.cpp
@@ -0,0 +1,67 @@
+/************************************************************
+
+ cvavgcomp.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#include "cvavgcomp.h"
+/*
+ * Document-class: OpenCV::CvAvgComp
+ *
+ * CvRect with parameter "neighbors".
+ * CvHaarClassifierCascade#detect_object.
+ *
+ * typedef struct CvAvgComp{
+ * CvRect rect;
+ * int neighbors;
+ * }
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_AVGCOMP
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if (rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ * cvrect = rb_define_class_under(opencv, "CvRect", rb_cObject);
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv(), cvrect = cCvRect::rb_class();
+ rb_klass = rb_define_class_under(opencv, "CvAvgComp", cvrect);
+ rb_define_alloc_func(rb_klass, rb_allocate);
+ rb_define_method(rb_klass, "neighbors", RUBY_METHOD_FUNC(rb_neighbors), 0);
+}
+
+VALUE
+rb_allocate(VALUE klass)
+{
+ CvAvgComp *ptr;
+ return Data_Make_Struct(klass, CvAvgComp, 0, -1, ptr);
+}
+
+/*
+ * Return neighbors.
+ */
+VALUE
+rb_neighbors(VALUE self)
+{
+ return INT2FIX(CVAVGCOMP(self)->neighbors);
+}
+
+__NAMESPACE_END_AVGCOMP
+__NAMESPACE_END_OPENCV
39 ext/cvavgcomp.h
@@ -0,0 +1,39 @@
+/**********************************************************************
+
+ cvavgcomp.h
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+**********************************************************************/
+#ifndef RUBY_OPENCV_AVGCOMP_H
+#define RUBY_OPENCV_AVGCOMP_H
+
+#define __NAMESPACE_BEGIN_AVGCOMP namespace cCvAvgComp{
+#define __NAMESPACE_END_AVGCOMP }
+
+#include <opencv.h>
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_AVGCOMP
+
+VALUE rb_class();
+
+void define_ruby_class();
+
+VALUE rb_allocate(VALUE klass);
+VALUE rb_neighbors(VALUE self);
+
+__NAMESPACE_END_AVGCOMP
+
+inline CvAvgComp *CVAVGCOMP(VALUE object){
+ CvAvgComp *ptr;
+ Data_Get_Struct(object, CvAvgComp, ptr);
+ return ptr;
+}
+
+__NAMESPACE_END_OPENCV
+
+
+#endif // RUBY_OPENCV_AVGCOMP_H
114 ext/cvbox2d.cpp
@@ -0,0 +1,114 @@
+/************************************************************
+
+ cvbox2d.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#include "cvbox2d.h"
+/*
+ * Document-class: OpenCV::CvBox2D
+ *
+ * C structure is here.
+ * typdef struct CvBox2D{
+ * CvPoint2D32f center; // center of the box.
+ * CvSize2D32f size; // box width and length
+ * float angle; // angle between the horizonal axis and the first side (i.e length) in radians
+ * }CvBox2D;
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVBOX2D
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if(rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+ rb_klass = rb_define_class_under(opencv, "CvBox2D", rb_cObject);
+ rb_define_method(rb_klass, "center", RUBY_METHOD_FUNC(rb_center), 0);
+ rb_define_method(rb_klass, "size", RUBY_METHOD_FUNC(rb_size), 0);
+ rb_define_method(rb_klass, "angle", RUBY_METHOD_FUNC(rb_angle), 0);
+ rb_define_method(rb_klass, "points", RUBY_METHOD_FUNC(rb_points), 0);
+}
+
+VALUE
+rb_allocate(VALUE klass)
+{
+ CvBox2D *ptr;
+ return Data_Make_Struct(klass, CvBox2D, 0, -1, ptr);
+}
+
+/*
+ * Return center point of box as CvPoint2D32f.
+ */
+VALUE
+rb_center(VALUE self)
+{
+ return REFER_OBJECT(cCvPoint2D32f::rb_class(), &CVBOX2D(self)->center, self);
+}
+
+/*
+ * Return size of box as CvSize2D32f.
+ */
+VALUE
+rb_size(VALUE self)
+{
+ return REFER_OBJECT(cCvSize2D32f::rb_class(), &CVBOX2D(self)->size, self);
+}
+
+/*
+ * Return angle of box as Float.
+ */
+VALUE
+rb_angle(VALUE self)
+{
+ return rb_float_new(CVBOX2D(self)->angle);
+}
+
+/*
+ * Find box vertices. Return Array contain 4 CvPoint2D32f.
+ */
+VALUE
+rb_points(VALUE self)
+{
+ const int n = 4;
+ CvPoint2D32f p[n];
+ cvBoxPoints(*CVBOX2D(self), p);
+ VALUE points = rb_ary_new2(n);
+ for(int i = 0; i < n; i++)
+ rb_ary_store(points, i, cCvPoint2D32f::new_object(p[i]));
+ return points;
+}
+
+VALUE
+new_object()
+{
+ return rb_allocate(cCvBox2D::rb_class());
+}
+
+VALUE
+new_object(CvBox2D box)
+{
+ VALUE object = rb_allocate(rb_klass);
+ *CVBOX2D(object) = box;
+ return object;
+}
+
+__NAMESPACE_END_CVBOX2D
+__NAMESPACE_END_OPENCV
53 ext/cvbox2d.h
@@ -0,0 +1,53 @@
+/************************************************************
+
+ cvbox2d.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#ifndef RUBY_OPENCV_CVBOX2D_H
+#define RUBY_OPENCV_CVBOX2D_H
+
+#include"opencv.h"
+
+#define __NAMESPACE_BEGIN_CVBOX2D namespace cCvBox2D{
+#define __NAMESPACE_END_CVBOX2D }
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVBOX2D
+
+VALUE rb_class();
+
+void define_ruby_class();
+
+VALUE rb_allocate(VALUE klass);
+
+VALUE rb_center(VALUE self);
+VALUE rb_size(VALUE self);
+VALUE rb_angle(VALUE self);
+VALUE rb_points(VALUE self);
+
+VALUE new_object();
+VALUE new_object(CvBox2D box);
+
+__NAMESPACE_END_CVBOX2D
+
+inline CvBox2D *CVBOX2D(VALUE object){
+ CvBox2D *ptr;
+ Data_Get_Struct(object, CvBox2D, ptr);
+ return ptr;
+}
+
+inline CvBox2D VALUE_TO_CVBOX2D(VALUE object){
+ if(rb_obj_is_kind_of(object, cCvBox2D::rb_class())) {
+ return *CVBOX2D(object);
+ }else{
+ rb_raise(rb_eTypeError, "require %s or compatible object.", rb_class2name(cCvBox2D::rb_class()));
+ }
+}
+
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVBOX2D_H
276 ext/cvcapture.cpp
@@ -0,0 +1,276 @@
+/************************************************************
+
+ cvcapture.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#include"cvcapture.h"
+/*
+ * Document-class: OpenCV::CvCapture
+ *
+ * Capture image from video stream.
+ *
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCAPTURE
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if (rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+
+ rb_klass = rb_define_class_under(opencv, "CvCapture", rb_cData);
+
+ VALUE video_interface = rb_hash_new();
+ /* {:any, :mil, :vfw, :v4l, :v4l2, :fireware, :ieee1394, :dc1394, :cmu1394, :stereo, :tyzx, :tyzx_left, :tyzx_right, :tyzx_color, :tyzx_z, :qt, :qtuicktime}: video source */
+ rb_define_const(rb_klass, "INTERFACE", video_interface);
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("any")), INT2FIX(CV_CAP_ANY));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("mil")), INT2FIX(CV_CAP_MIL));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("vfw")), INT2FIX(CV_CAP_VFW));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("v4l")), INT2FIX(CV_CAP_V4L));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("v4l2")), INT2FIX(CV_CAP_V4L2));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("fireware")), INT2FIX(CV_CAP_FIREWARE));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("ieee1394")), INT2FIX(CV_CAP_IEEE1394));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("dc1394")), INT2FIX(CV_CAP_DC1394));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("cmu1394")), INT2FIX(CV_CAP_CMU1394));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("stereo")), INT2FIX(CV_CAP_STEREO));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("tyzx")), INT2FIX(CV_CAP_TYZX));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("tyzx_left")), INT2FIX(CV_TYZX_LEFT));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("tyzx_right")), INT2FIX(CV_TYZX_RIGHT));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("tyzx_color")), INT2FIX(CV_TYZX_COLOR));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("tyzx_z")), INT2FIX(CV_TYZX_Z));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("qt")), INT2FIX(CV_CAP_QT));
+ rb_hash_aset(video_interface, ID2SYM(rb_intern("quicktime")), INT2FIX(CV_CAP_QT));
+
+ rb_define_singleton_method(rb_klass, "open", RUBY_METHOD_FUNC(rb_open), -1);
+
+ rb_define_method(rb_klass, "grab", RUBY_METHOD_FUNC(rb_grab), 0);
+ rb_define_method(rb_klass, "retrieve", RUBY_METHOD_FUNC(rb_retrieve), 0);
+ rb_define_method(rb_klass, "query", RUBY_METHOD_FUNC(rb_query), 0);
+ rb_define_method(rb_klass, "millisecond", RUBY_METHOD_FUNC(rb_millisecond), 0);
+ rb_define_method(rb_klass, "frames", RUBY_METHOD_FUNC(rb_frames), 0);
+ rb_define_method(rb_klass, "size", RUBY_METHOD_FUNC(rb_size), 0);
+ rb_define_method(rb_klass, "width", RUBY_METHOD_FUNC(rb_width), 0);
+ rb_define_method(rb_klass, "height", RUBY_METHOD_FUNC(rb_height), 0);
+ rb_define_method(rb_klass, "fps", RUBY_METHOD_FUNC(rb_fps), 0);
+ rb_define_method(rb_klass, "fourcc", RUBY_METHOD_FUNC(rb_fourcc), 0);
+ rb_define_method(rb_klass, "frame_count", RUBY_METHOD_FUNC(rb_frame_count), 0);
+}
+
+void
+free(void *ptr)
+{
+ if(ptr)
+ cvReleaseCapture((CvCapture**)&ptr);
+}
+
+/*
+ * call-seq:
+ * CvCapture.open(<i>[dev = -1]</i>)
+ *
+ * Reading video stream from the specified file or camera device.
+ * If <i>dev</i> is string (i.e "stream.avi"), reading video stream from file.
+ * If <i>dev</i> is number or symbol(include CvCapture::INTERFACE),
+ * reading video stream from camera.
+ * Currently two camera interfaces can be used on Windows:
+ * * Video for Windows(VFW)
+ * * Matrox Imaging Library(MIL)
+ * and two on Linux
+ * * V4L
+ * * FireWire(IEEE1394).
+ * If there is only one camera or it does not matter what camera to use <i>nil</i> may be passed.
+ */
+VALUE
+rb_open(int argc, VALUE *argv, VALUE self)
+{
+ VALUE device, i;
+ rb_scan_args(argc, argv, "01", &device);
+ CvCapture *capture = 0;
+ switch (TYPE(device)) {
+ case T_STRING:
+ capture = cvCaptureFromFile(StringValueCStr(device));
+ break;
+ case T_FIXNUM:
+ capture = cvCaptureFromCAM(FIX2INT(device));
+ break;
+ case T_SYMBOL:
+ i = rb_hash_aref(rb_const_get(rb_class(), rb_intern("INTERFACE")), device);
+ if (NIL_P(i))
+ rb_raise(rb_eArgError, "undefined interface.");
+ capture = cvCaptureFromCAM(NUM2INT(i));
+ break;
+ case T_NIL:
+ capture = cvCaptureFromCAM(CV_CAP_ANY);
+ break;
+ }
+ if (!capture)
+ rb_raise(rb_eStandardError, "Invalid capture format.");
+ return Data_Wrap_Struct(rb_klass, 0, free, capture);
+}
+
+
+/*
+ * call-seq:
+ * grab -> true or false
+ *
+ * Grabbed frame is stored internally. To grab frame
+ * <i>fast</i> that is important for syncronization in case of reading from
+ * several cameras simultaneously. The grabbed frames are not exposed because
+ * they may be stored in compressed format (as defined by camera/driver).
+ * To retrieve the grabbed frame, retrieve should be used.
+ *
+ * If grabbed frame was success, return true. Otherwise return false.
+ */
+VALUE
+rb_grab(VALUE self)
+{
+ return cvGrabFrame(CVCAPTURE(self)) ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ * retrieve -> CvMat or nil
+ *
+ * Gets the image grabbed with grab.
+ */
+VALUE
+rb_retrieve(VALUE self)
+{
+ IplImage *frame = cvRetrieveFrame(CVCAPTURE(self));
+ if(!frame)
+ return Qnil;
+ VALUE image = cIplImage::new_object(cvSize(frame->width, frame->height), CV_MAKETYPE(CV_8U, frame->nChannels));
+ if (frame->origin == IPL_ORIGIN_TL) {
+ cvCopy(frame, CVARR(image));
+ } else {
+ cvFlip(frame, CVARR(image));
+ }
+ return image;
+}
+
+/*
+ * call-seq:
+ * query -> CvMat or nil
+ *
+ * Grabs and returns a frame camera or file. Just a combination of grab and retrieve in one call.
+ */
+VALUE
+rb_query(VALUE self)
+{
+ IplImage *frame = cvQueryFrame(CVCAPTURE(self));
+ if(!frame)
+ return Qnil;
+ VALUE image = cIplImage::new_object(cvSize(frame->width, frame->height), CV_MAKETYPE(CV_8U, frame->nChannels));
+ if (frame->origin == IPL_ORIGIN_TL) {
+ cvCopy(frame, CVARR(image));
+ } else {
+ cvFlip(frame, CVARR(image));
+ }
+ return image;
+}
+
+/*
+ * Film current position in milliseconds or video capture timestamp.
+ */
+VALUE
+rb_millisecond(VALUE self)
+{
+ return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_POS_MSEC));
+}
+
+/*
+ * 0-based index of the frame to be decoded/captured next
+ */
+VALUE
+rb_frames(VALUE self)
+{
+ return rb_float_new(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_POS_FRAMES));
+}
+
+/*
+ * Relative position of video file (0 - start of the film, 1 - end of the film)
+ */
+VALUE
+rb_avi_ratio(VALUE self)
+{
+ return rb_float_new(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_POS_AVI_RATIO));
+}
+
+/*
+ * Size of frames in the video stream.
+ */
+VALUE
+rb_size(VALUE self)
+{
+ return cCvSize::new_object(cvSize((int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_WIDTH), (int)cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_HEIGHT)));
+}
+
+/*
+ * Width of frames in the video stream.
+ */
+VALUE
+rb_width(VALUE self)
+{
+ return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_WIDTH));
+}
+
+/*
+ * Height of frames in the video stream.
+ */
+VALUE
+rb_height(VALUE self)
+{
+ return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_HEIGHT));
+}
+
+/*
+ * Frame rate
+ */
+VALUE
+rb_fps(VALUE self)
+{
+ return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FPS));
+}
+
+/*
+ * 4character code of codec. see http://www.fourcc.org/
+ */
+VALUE
+rb_fourcc(VALUE self)
+{
+ char str[4];
+ double fourcc = cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FOURCC);
+ sprintf(str, "%s", (char*)&fourcc);
+ return rb_str_new2(str);
+}
+
+/*
+ * Number of frames in video file.
+ */
+VALUE
+rb_frame_count(VALUE self)
+{
+ return rb_dbl2big(cvGetCaptureProperty(CVCAPTURE(self), CV_CAP_PROP_FRAME_COUNT));
+}
+
+
+__NAMESPACE_END_CVCAPTURE
+__NAMESPACE_END_OPENCV
54 ext/cvcapture.h
@@ -0,0 +1,54 @@
+/************************************************************
+
+ cvcapture.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#ifndef RUBY_OPENCV_CVCAPTURE_H
+#define RUBY_OPENCV_CVCAPTURE_H
+
+#include "opencv.h"
+
+#define __NAMESPACE_BEGIN_CVCAPTURE namespace cCvCapture{
+#define __NAMESPACE_END_CVCAPTURE }
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCAPTURE
+
+
+VALUE rb_class();
+
+void define_ruby_class();
+
+void free(void *ptr);
+VALUE rb_open(int argc, VALUE *argv, VALUE klass);
+
+VALUE rb_grab(VALUE self);
+VALUE rb_retrieve(VALUE self);
+VALUE rb_query(VALUE self);
+
+VALUE rb_millisecond(VALUE self);
+VALUE rb_frames(VALUE self);
+VALUE rb_avi_ratio(VALUE self);
+VALUE rb_size(VALUE self);
+VALUE rb_width(VALUE self);
+VALUE rb_height(VALUE self);
+VALUE rb_fps(VALUE self);
+VALUE rb_fourcc(VALUE self);
+VALUE rb_frame_count(VALUE self);
+
+__NAMESPACE_END_CVCAPTURE
+
+
+inline CvCapture *CVCAPTURE(VALUE object){
+ CvCapture *ptr;
+ Data_Get_Struct(object, CvCapture, ptr);
+ return ptr;
+}
+
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVCAPTURE_H
184 ext/cvchain.cpp
@@ -0,0 +1,184 @@
+/************************************************************
+
+ cvchain.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2007 Masakazu Yonekura
+
+************************************************************/
+#include "cvchain.h"
+/*
+ * Document-class: OpenCV::CvChain
+ *
+ * Freeman chain code.
+ * CvMat#find_contours(:method => :code)
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCHAIN
+
+#define APPROX_CHAIN_OPTION(op) NIL_P(op) ? rb_const_get(rb_class(), rb_intern("APPROX_CHAIN_OPTION")) : rb_funcall(rb_const_get(rb_class(), rb_intern("APPROX_CHAIN_OPTION")), rb_intern("merge"), 1, op)
+#define APPROX_CHAIN_METHOD(op) CVMETHOD("APPROX_CHAIN_METHOD", rb_hash_aref(op, ID2SYM(rb_intern("method"))), CV_CHAIN_APPROX_SIMPLE)
+#define APPROX_CHAIN_PARAMETER(op) NUM2INT(rb_hash_aref(op, ID2SYM(rb_intern("parameter"))))
+#define APPROX_CHAIN_MINIMAL_PARAMETER(op) NUM2INT(rb_hash_aref(op, ID2SYM(rb_intern("minimal_parameter"))))
+#define APPROX_CHAIN_RECURSIVE(op) ({VALUE _recursive = rb_hash_aref(op, ID2SYM(rb_intern("recursive"))); NIL_P(_recursive) ? 0 : _recursive == Qfalse ? 0 : 1;})
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if (rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ * cvseq = rb_define_class_under(opencv, "CvSeq");
+ * curve = rb_define_module_under(opencv, "Curve");
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+ VALUE cvseq = cCvSeq::rb_class();
+ VALUE curve = mCurve::rb_module();
+
+ rb_klass = rb_define_class_under(opencv, "CvChain", cvseq);
+ rb_include_module(rb_klass, curve);
+ VALUE approx_chain_option = rb_hash_new();
+ rb_define_const(rb_klass, "APPROX_CHAIN_OPTION", approx_chain_option);
+ rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("method")), ID2SYM(rb_intern("approx_simple")));
+ rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("parameter")), rb_float_new(0));
+ rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("minimal_parameter")), INT2FIX(0));
+ rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("recursive")), Qfalse);
+
+ rb_define_method(rb_klass, "origin", RUBY_METHOD_FUNC(rb_origin), 0);
+ rb_define_method(rb_klass, "origin=", RUBY_METHOD_FUNC(rb_set_origin), 0);
+ rb_define_method(rb_klass, "codes", RUBY_METHOD_FUNC(rb_codes), 0);
+ rb_define_method(rb_klass, "points", RUBY_METHOD_FUNC(rb_points), 0);
+ rb_define_method(rb_klass, "approx_chain", RUBY_METHOD_FUNC(rb_approx_chain), -1);
+ rb_define_alias(rb_klass, "approx", "approx_chain");
+}
+
+/*
+ * call-seq:
+ * origin -> cvpoint
+ *
+ * Return Freeman chain code origin.
+ */
+VALUE
+rb_origin(VALUE self)
+{
+ return cCvPoint::new_object(CVCHAIN(self)->origin);
+}
+
+/*
+ * call-seq:
+ * origin = point -> self
+ *
+ * Set Freeman chain code origin.
+ */
+VALUE
+rb_set_origin(VALUE self, VALUE origin)
+{
+ CVCHAIN(self)->origin = VALUE_TO_CVPOINT(origin);
+ return self;
+}
+
+/*
+ * call-seq:
+ * codes -> array(contain fixnum)
+ *
+ * Return Freeman chain codes.
+ */
+VALUE
+rb_codes(VALUE self)
+{
+ CvChain *chain = CVCHAIN(self);
+ CvChainPtReader reader;
+ CvPoint p = chain->origin;
+ VALUE ary = rb_ary_new2(chain->total);
+ cvStartReadChainPoints(chain, &reader);
+ for (int i = 0; i < chain->total; i++) {
+ CV_READ_SEQ_ELEM(reader.code, (*((CvSeqReader*)&(reader))));
+ rb_ary_store(ary, i, CHR2FIX(reader.code));
+ }
+ return ary;
+}
+
+/*
+ * call-seq:
+ * points -> array(contain cvpoint)
+ *
+ * Return points that represent by Freeman chain code.
+ */
+VALUE
+rb_points(VALUE self)
+{
+ CvChain *chain = CVCHAIN(self);
+ CvChainPtReader reader;
+ CvPoint p = chain->origin;
+ VALUE ary = rb_ary_new2(chain->total);
+ cvStartReadChainPoints(chain, &reader);
+ for (int i = 0; i < chain->total; i++) {
+ CV_READ_CHAIN_POINT(p, reader);
+ rb_ary_store(ary, i, cCvPoint::new_object(p));
+ }
+ return ary;
+}
+
+/*
+ * call-seq:
+ * approx_chain(<i>[approx_chain_option]</i>) -> cvcontour
+ *
+ * Approximates Freeman chain(s) with polygonal curve.
+ * <i>approx_chain_option</i> should be Hash include these keys.
+ * :method - Approximation method.
+ * :approx_none - translate all the points from the chain code into points;
+ * :approx_simple(default) - compress horizontal, vertical, and diagonal segments, that is,
+ * the function leaves only their ending points.
+ * :approx_tc89_l1
+ * :approx_tc89_kcos - apply one of the flavors of Teh-Chin chain approximation algorithm.
+ * If set the difference between the current pixel and seed pixel is considered,
+ * otherwise difference between neighbor pixels is considered (the range is floating).
+ * :parameter - Method parameter (not used now).
+ * :minimal_perimeter (default 0)
+ * Approximates only those contours whose perimeters are not less than minimal_perimeter. Other chains are removed from the resulting structure.
+ * :recursive (default false)
+ * If not nil or false, the function approximates all chains that access can be obtained to
+ * from self by h_next or v_next links. If 0, the single chain is approximated.
+ *
+ */
+VALUE
+rb_approx_chain(int argc, VALUE *argv, VALUE self)
+{
+ VALUE approx_chain_option, storage;
+ rb_scan_args(argc, argv, "01", &approx_chain_option);
+ approx_chain_option = APPROX_CHAIN_OPTION(approx_chain_option);
+ /* can't compile VC
+ storage = cCvMemStorage::new_object();
+ CvSeq *seq = cvApproxChains(CVSEQ(self), CVMEMSTORAGE(storage),
+ APPROX_CHAIN_METHOD(approx_chain_option),
+ APPROX_CHAIN_PARAMETER(approx_chain_option),
+ APPROX_CHAIN_MINIMAL_PARAMETER(approx_chain_option),
+ APPROX_CHAIN_RECURSIVE(approx_chain_option));
+
+ return cCvSeq::new_sequence(cCvContour::rb_class(), seq, cCvPoint::rb_class(), storage);
+ */
+ return Qnil;
+}
+
+VALUE
+new_object()
+{
+ VALUE storage = cCvMemStorage::new_object();
+ CvSeq *seq = cvCreateSeq(CV_SEQ_CHAIN_CONTOUR, sizeof(CvChain), sizeof(CvChainCode), CVMEMSTORAGE(storage));
+ VALUE object = cCvSeq::new_sequence(cCvChain::rb_class(), seq, cCvChainCode::rb_class(), storage);
+ return object;
+}
+
+__NAMESPACE_END_CVCHAIN
+__NAMESPACE_END_OPENCV
43 ext/cvchain.h
@@ -0,0 +1,43 @@
+/************************************************************
+
+ cvchain.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2007 Masakazu Yonekura
+
+************************************************************/
+#ifndef RUBY_OPENCV_CVCHAIN_H
+#define RUBY_OPENCV_CVCHAIN_H
+#include "opencv.h"
+
+#define __NAMESPACE_BEGIN_CVCHAIN namespace cCvChain{
+#define __NAMESPACE_END_CVCHAIN }
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCHAIN
+
+VALUE rb_class();
+
+void define_ruby_class();
+
+VALUE rb_origin(VALUE self);
+VALUE rb_set_origin(VALUE self, VALUE origin);
+VALUE rb_codes(VALUE self);
+VALUE rb_points(VALUE self);
+VALUE rb_approx_chain(int argc, VALUE *argv, VALUE self);
+
+VALUE new_object();
+
+__NAMESPACE_END_CVCHAIN
+
+inline CvChain*
+CVCHAIN(VALUE object){
+ CvChain *ptr;
+ Data_Get_Struct(object, CvChain, ptr);
+ return ptr;
+}
+
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVCHAIN_H
49 ext/cvchaincode.cpp
@@ -0,0 +1,49 @@
+/************************************************************
+
+ cvchaincode.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2007 Masakazu Yonekura
+
+************************************************************/
+#include "cvchaincode.h"
+/*
+ * Document-class: OpenCV::CvChainCode
+ *
+ * Freeman chain code.
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCHAINCODE
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if (rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+
+ rb_klass = rb_define_class_under(opencv, "CvChainCode", rb_cObject);
+}
+
+VALUE
+rb_to_i(VALUE self)
+{
+ return CHR2FIX(CVCHAINCODE(self)->code);
+}
+
+__NAMESPACE_END_CVCHAINCODE
+__NAMESPACE_END_OPENCV
43 ext/cvchaincode.h
@@ -0,0 +1,43 @@
+/************************************************************
+
+ opchaincode.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2008 Masakazu Yonekura
+
+ ************************************************************/
+#ifndef RUBY_OPENCV_CVCHAINCODE_H
+#define RUBY_OPENCV_CVCHAINCODE_H
+
+#include "opencv.h"
+
+#define __NAMESPACE_BEGIN_CVCHAINCODE namespace cCvChainCode{
+#define __NAMESPACE_END_CVCHAINCODE }
+
+typedef struct CvChainCode{
+ char code;
+}CvChainCode;
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCHAINCODE
+
+VALUE rb_class();
+
+void define_ruby_class();
+
+VALUE rb_to_i(VALUE self);
+
+__NAMESPACE_END_CVCHAINCODE
+
+inline CvChainCode*
+CVCHAINCODE(VALUE object){
+ CvChainCode *ptr;
+ Data_Get_Struct(object, CvChainCode, ptr);
+ return ptr;
+}
+
+
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVCHAINCODE_H
90 ext/cvcircle32f.cpp
@@ -0,0 +1,90 @@
+/************************************************************
+
+ cvcircle32f.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#include "cvcircle32f.h"
+/*
+ * Document-class: OpenCV::CvCircle32f
+ *
+ * Combination of center and radius.
+ *
+ * see CvMat#hough_circles
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCIRCLE32F
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if (rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+
+ rb_klass = rb_define_class_under(opencv, "CvCircle32f", rb_cObject);
+ rb_define_alloc_func(rb_klass, rb_allocate);
+ rb_define_method(rb_klass, "center", RUBY_METHOD_FUNC(rb_center), 0);
+ rb_define_method(rb_klass, "radius", RUBY_METHOD_FUNC(rb_radius), 0);
+
+ //rb_define_method(rb_klass, "to_s", RUBY_METHOD_FUNC(rb_to_s), 0);
+ rb_define_method(rb_klass, "to_ary", RUBY_METHOD_FUNC(rb_to_ary), 0);
+}
+
+VALUE
+rb_allocate(VALUE klass)
+{
+ CvCircle32f *ptr;
+ return Data_Make_Struct(klass, CvCircle32f, 0, -1, ptr);
+}
+
+/*
+ * Return parameter on center.
+ */
+VALUE
+rb_center(VALUE self)
+{
+ return cCvPoint2D32f::new_object(CVCIRCLE32F(self)->center);
+}
+
+/*
+ * Return parameter on radius.
+ */
+VALUE
+rb_radius(VALUE self)
+{
+ return rb_float_new(CVCIRCLE32F(self)->radius);
+}
+
+VALUE
+rb_to_ary(VALUE self)
+{
+ return rb_ary_new3(2, rb_center(self), rb_radius(self));
+}
+
+VALUE
+new_object(CvCircle32f circle32f)
+{
+ VALUE object = rb_allocate(rb_klass);
+ *CVCIRCLE32F(object) = circle32f;
+ return object;
+}
+
+__NAMESPACE_END_CVCIRCLE32F
+__NAMESPACE_END_OPENCV
53 ext/cvcircle32f.h
@@ -0,0 +1,53 @@
+/************************************************************
+
+ cvcircle32f.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2007 Masakazu Yonekura
+
+************************************************************/
+#ifndef RUBY_OPENCV_CVCIRCLE32F_H
+#define RUBY_OPENCV_CVCIRCLE32F_H
+
+#include "opencv.h"
+
+#define __NAMESPACE_BEGIN_CVCIRCLE32F namespace cCvCircle32f{
+#define __NAMESPACE_END_CVCIRCLE32F }
+
+__NAMESPACE_BEGIN_OPENCV
+
+typedef struct CvCircle32f{
+ CvPoint2D32f center;
+ float radius;
+} CvCircle32f;
+
+__NAMESPACE_BEGIN_CVCIRCLE32F
+
+VALUE rb_class();
+
+void define_ruby_class();
+
+VALUE rb_allocate(VALUE klass);
+VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
+VALUE rb_center(VALUE self);
+VALUE rb_radius(VALUE self);
+
+VALUE rb_to_s(VALUE self);
+VALUE rb_to_ary(VALUE self);
+
+VALUE new_object(CvCircle32f circle32f);
+
+__NAMESPACE_END_CVCIRCLE32F
+
+inline CvCircle32f*
+CVCIRCLE32F(VALUE object)
+{
+ CvCircle32f *ptr;
+ Data_Get_Struct(object, CvCircle32f, ptr);
+ return ptr;
+}
+
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVCIRCLE32F_H
230 ext/cvcondensation.cpp
@@ -0,0 +1,230 @@
+/************************************************************
+
+ cvcondensation.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#include "cvcondensation.h"
+/*
+ * Document-class: OpenCV::CvConDensation
+ *
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCONDENSATION
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if(rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+ rb_klass = rb_define_class_under(opencv, "CvConDensation", rb_cObject);
+ rb_define_method(rb_klass, "dp", RUBY_METHOD_FUNC(rb_dp), 0);
+ rb_define_method(rb_klass, "mp", RUBY_METHOD_FUNC(rb_mp), 0);
+ rb_define_method(rb_klass, "dynamic_matrix", RUBY_METHOD_FUNC(rb_dynamic_matrix), 0);
+ rb_define_method(rb_klass, "confidence", RUBY_METHOD_FUNC(rb_confidence), 0);
+ rb_define_method(rb_klass, "cumulative", RUBY_METHOD_FUNC(rb_cumulative), 0);
+ rb_define_method(rb_klass, "state", RUBY_METHOD_FUNC(rb_state), 0);
+ rb_define_method(rb_klass, "samples_num", RUBY_METHOD_FUNC(rb_samples_num), 0);
+ rb_define_method(rb_klass, "init_sample_set", RUBY_METHOD_FUNC(rb_init_sample_set), 2);
+ rb_define_method(rb_klass, "update_by_time", RUBY_METHOD_FUNC(rb_update_by_time), 0);
+ rb_define_alias(rb_klass, "update", "update_by_time");
+ rb_define_method(rb_klass, "each_sample", RUBY_METHOD_FUNC(rb_each_sample), 0);
+ rb_define_method(rb_klass, "calculate_confidence", RUBY_METHOD_FUNC(rb_calculate_confidence), 0);
+}
+
+/*
+ * call-seq:
+ * dp -> int
+ *
+ * Return dimension of state vector
+ */
+VALUE
+rb_dp(VALUE self)
+{
+ return INT2FIX(CVCONDENSATION(self)->DP);
+}
+
+/*
+ * call-seq:
+ * mp -> int
+ *
+ * Return demension of measurement vector.
+ */
+VALUE
+rb_mp(VALUE self)
+{
+ return INT2FIX(CVCONDENSATION(self)->MP);
+}
+
+/*
+ * call-seq:
+ * dynamic_matrix -> mat
+ *
+ * Return matrix of the linear Dynamics system.
+ */
+VALUE
+rb_dynamic_matrix(VALUE self)
+{
+ CvConDensation *cd = CVCONDENSATION(self);
+ return DEPEND_OBJECT(cCvMat::rb_class(), cvInitMatHeader(ALLOC(CvMat), cd->DP, cd->DP, CV_MAKETYPE(CV_32F, 1), cd->DynamMatr), self);
+}
+
+/*
+ * call-seq:
+ * confidence -> mat
+ *
+ * Return confidence for each sample.
+ */
+VALUE
+rb_confidence(VALUE self)
+{
+ CvConDensation *cd = CVCONDENSATION(self);
+ return DEPEND_OBJECT(cCvMat::rb_class(), cvInitMatHeader(ALLOC(CvMat), cd->SamplesNum, 1, CV_MAKETYPE(CV_32F, 1), cd->flConfidence), self);
+}
+
+/*
+ * call-seq:
+ * cumulative -> mat
+ *
+ * Return cumulative confidence.
+ */
+VALUE
+rb_cumulative(VALUE self)
+{
+ CvConDensation *cd = CVCONDENSATION(self);
+ return DEPEND_OBJECT(cCvMat::rb_class(), cvInitMatHeader(ALLOC(CvMat), cd->SamplesNum, 1, CV_MAKETYPE(CV_32F, 1), cd->flCumulative), self);
+}
+
+/*
+ * call-seq:
+ * state -> mat
+ *
+ * Return vector of state
+ */
+VALUE
+rb_state(VALUE self)
+{
+ CvConDensation *cd = CVCONDENSATION(self);
+ return DEPEND_OBJECT(cCvMat::rb_class(), cvInitMatHeader(ALLOC(CvMat), cd->DP, 1, CV_MAKETYPE(CV_32F, 1), cd->State), self);
+}
+
+/*
+ * call-seq:
+ * samples_num -> int
+ *
+ * Return number of the samples
+ */
+VALUE
+rb_samples_num(VALUE self)
+{
+ return INT2FIX(CVCONDENSATION(self)->SamplesNum);
+}
+
+/*
+ * call-seq:
+ * init_sample_set(upper, lower)
+ *
+ * Initializes sample set for ConDensation algorithm.
+ * Fills the samples with values within specified(lower to upper) ranges.
+ */
+VALUE
+rb_init_sample_set(VALUE self, VALUE lower, VALUE upper)
+{
+ CvConDensation *cd = CVCONDENSATION(self);
+ CvMat *lower_bound = CVMAT(lower), *upper_bound = CVMAT(upper), lb_stub, ub_stub;
+ int lower_type = lower_bound->type, upper_type = lower_bound->type;
+ if (lower_type != CV_32FC1 || lower_bound->cols != 1) {
+ if (CV_MAT_DEPTH(lower_type) == CV_32F) {
+ lower_bound = cvReshape(lower_bound, &lb_stub, 1, lower_bound->rows * lower_bound->cols);
+ } else {
+ lower = cCvMat::new_object(cvSize(lower_bound->rows * lower_bound->cols, 1), CV_MAKETYPE(CV_32S, 1));
+ cvConvertScale(lower_bound, CVARR(lower));
+ lower_bound = CVMAT(lower);
+ }
+ }
+ if (upper_type != CV_32FC1 || upper_bound->cols != 1) {
+ if (CV_MAT_DEPTH(upper_type) == CV_32F) {
+ upper_bound = cvReshape(upper_bound, &ub_stub, 1, upper_bound->rows * upper_bound->cols);
+ } else {
+ upper = cCvMat::new_object(cvSize(upper_bound->rows * upper_bound->cols, 1), CV_MAKETYPE(CV_32F, 1));
+ cvConvertScale(upper_bound, CVARR(upper));
+ upper_bound = CVMAT(upper);
+ }
+ }
+ if (lower_bound->rows != cd->DP || upper_bound->rows != cd->DP) {
+ rb_raise(rb_eTypeError, "sample matrix step unmatch.");
+ }
+ cvConDensInitSampleSet(cd, lower_bound, upper_bound);
+ return self;
+}
+
+/*
+ * call-seq:
+ * update_by_time
+ *
+ * Estimates subsequent model state.
+ */
+VALUE
+rb_update_by_time(VALUE self)
+{
+ cvConDensUpdateByTime(CVCONDENSATION(self));
+ return self;
+}
+
+/*
+ * call-seq:
+ * each_sample{|mat| ... }
+ *
+ * Evaluate each sample by given block.
+ */
+VALUE
+rb_each_sample(VALUE self)
+{
+ CvConDensation *cd = CVCONDENSATION(self);
+ if (rb_block_given_p()) {
+ for (int i = 0; i < cd->SamplesNum; i++) {
+ rb_yield(DEPEND_OBJECT(cCvMat::rb_class(), cvInitMatHeader(ALLOC(CvMat), cd->DP, 1, CV_MAKETYPE(CV_32F, 1), cd->flSamples[i]), self));
+ }
+ }
+ return self;
+}
+
+/*
+ * call-seq:
+ * calculate_confidence{|value| ... }
+ *
+ * Evalute each sample by given block, then return value set to confidence.
+ */
+VALUE
+rb_calculate_confidence(VALUE self)
+{
+ VALUE value;
+ CvConDensation *cd = CVCONDENSATION(self);
+ if (rb_block_given_p()) {
+ for (int i = 0; i < cd->SamplesNum; i++) {
+ value = rb_yield(DEPEND_OBJECT(cCvMat::rb_class(), cvInitMatHeader(ALLOC(CvMat), cd->DP, 1, CV_MAKETYPE(CV_32F, 1), cd->flSamples[i]), self));
+ cd->flConfidence[i] = NUM2DBL(value);
+ }
+ }
+ return self;
+}
+
+__NAMESPACE_END_CVCONDENSATION
+__NAMESPACE_END_OPENCV
49 ext/cvcondensation.h
@@ -0,0 +1,49 @@
+/************************************************************
+
+ cvcondensation.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2006 Masakazu Yonekura
+
+************************************************************/
+#ifndef RUBY_OPENCV_CVCONDENSATION_H
+#define RUBY_OPENCV_CVCONDENSATION_H
+
+#include "opencv.h"
+
+#define __NAMESPACE_BEGIN_CVCONDENSATION namespace cCvConDensation{
+#define __NAMESPACE_END_CVCONDENSATION }
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCONDENSATION
+
+VALUE rb_class();
+void define_ruby_class();
+
+VALUE rb_dp(VALUE self);
+VALUE rb_mp(VALUE self);
+VALUE rb_dynamic_matrix(VALUE self);
+VALUE rb_state(VALUE self);
+VALUE rb_confidence(VALUE self);
+VALUE rb_cumulative(VALUE self);
+VALUE rb_samples_num(VALUE self);
+
+VALUE rb_init_sample_set(VALUE self, VALUE lower, VALUE upper);
+VALUE rb_update_by_time(VALUE self);
+VALUE rb_each_sample(VALUE self);
+VALUE rb_calculate_confidence(VALUE self);
+
+__NAMESPACE_END_CVCONDENSATION
+
+inline CvConDensation*
+CVCONDENSATION(VALUE object)
+{
+ CvConDensation *ptr;
+ Data_Get_Struct(object, CvConDensation, ptr);
+ return ptr;
+}
+
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVCONDENSATION_H
115 ext/cvconnectedcomp.cpp
@@ -0,0 +1,115 @@
+/************************************************************
+
+ cvconnectedcomp.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2007 Masakazu Yonekura
+
+************************************************************/
+#include "cvconnectedcomp.h"
+/*
+ * Document-class: OpenCV::CvConnectedComp
+ *
+ * see CvMat#flood_fill
+ *
+ * C structure is here.
+ * typedef struct CvConnectedComp
+ * {
+ * double area;
+ * CvScalar value;
+ * CvRect rect;
+ * CvSeq* contour;
+ * } CvConnectedComp;
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCONNECTEDCOMP
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if (rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+
+ rb_klass = rb_define_class_under(opencv, "CvConnectedComp", rb_cObject);
+ rb_define_alloc_func(rb_klass, rb_allocate);
+ rb_define_method(rb_klass, "area", RUBY_METHOD_FUNC(rb_area), 0);
+ rb_define_method(rb_klass, "value", RUBY_METHOD_FUNC(rb_value), 0);
+ rb_define_method(rb_klass, "rect", RUBY_METHOD_FUNC(rb_rect), 0);
+ rb_define_method(rb_klass, "rect=", RUBY_METHOD_FUNC(rb_set_rect), 0);
+}
+
+VALUE
+rb_allocate(VALUE klass)
+{
+ CvConnectedComp *ptr;
+ return Data_Make_Struct(klass, CvConnectedComp, 0, -1, ptr);
+}
+
+/*
+ * Return area of connected component.
+ */
+VALUE
+rb_area(VALUE self)
+{
+ return rb_float_new(CVCONNECTEDCOMP(self)->area);
+}
+
+/*
+ * Return average color of the connected component.
+ */
+VALUE
+rb_value(VALUE self)
+{
+ return REFER_OBJECT(cCvScalar::rb_class(), &CVCONNECTEDCOMP(self)->value, self);
+}
+
+/*
+ * Return ROI of the component.
+ */
+VALUE
+rb_rect(VALUE self)
+{
+ return REFER_OBJECT(cCvRect::rb_class(), &CVCONNECTEDCOMP(self)->rect, self);
+}
+
+/*
+ * Set ROI of the component.
+ */
+VALUE
+rb_set_rect(VALUE self, VALUE rect)
+{
+ CVCONNECTEDCOMP(self)->rect = VALUE_TO_CVRECT(rect);
+ return self;
+}
+
+VALUE
+new_object()
+{
+ return rb_allocate(rb_klass);
+}
+
+VALUE
+new_object(CvConnectedComp comp)
+{
+ VALUE object = rb_allocate(rb_klass);
+ *CVCONNECTEDCOMP(object) = comp;
+ return object;
+}
+
+__NAMESPACE_END_CVCONNECTEDCOMP
+__NAMESPACE_END_OPENCV
46 ext/cvconnectedcomp.h
@@ -0,0 +1,46 @@
+/************************************************************
+
+ cvconnectedcomp.h -
+
+ $Author: lsxi $
+
+ Copyright (C) 2005-2007 Masakazu Yonekura
+
+************************************************************/
+#ifndef RUBY_OPENCV_CVCONNECTEDCOMP_H
+#define RUBY_OPENCV_CVCONNECTEDCOMP_H
+
+#include "opencv.h"
+
+#define __NAMESPACE_BEGIN_CVCONNECTEDCOMP namespace cCvConnectedComp{
+#define __NAMESPACE_END_CVCONNECTEDCOMP }
+
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCONNECTEDCOMP
+
+VALUE rb_class();
+
+void define_ruby_class();
+
+VALUE rb_allocate(VALUE klass);
+
+VALUE rb_area(VALUE self);
+VALUE rb_value(VALUE self);
+VALUE rb_rect(VALUE self);
+VALUE rb_set_rect(VALUE self, VALUE rect);
+VALUE rb_contour(VALUE self);
+
+VALUE new_object();
+VALUE new_object(CvConnectedComp comp);
+
+__NAMESPACE_END_CVCONNECTEDCOMP
+
+inline CvConnectedComp *CVCONNECTEDCOMP(VALUE object){
+ CvConnectedComp *ptr;
+ Data_Get_Struct(object, CvConnectedComp, ptr);
+ return ptr;
+}
+
+__NAMESPACE_END_OPENCV
+
+#endif // RUBY_OPENCV_CVCONNECTEDCOMP_H
219 ext/cvcontour.cpp
@@ -0,0 +1,219 @@
+/************************************************************
+
+ cvcontour.cpp -
+
+ $Author: lsxi $
+
+ Copyright (C) 2007 Masakazu Yonekura
+
+************************************************************/
+#include "cvcontour.h"
+/*
+ * Document-class: OpenCV::CvContour
+ *
+ * Contour.
+ * CvMat#find_contours
+ */
+__NAMESPACE_BEGIN_OPENCV
+__NAMESPACE_BEGIN_CVCONTOUR
+
+#define APPROX_POLY_OPTION(op) NIL_P(op) ? rb_const_get(rb_class(), rb_intern("APPROX_OPTION")) : rb_funcall(rb_const_get(rb_class(), rb_intern("APPROX_OPTION")), rb_intern("merge"), 1, op)
+#define APPROX_POLY_METHOD(op) CVMETHOD("APPROX_POLY_METHOD", rb_hash_aref(op, ID2SYM(rb_intern("method"))), CV_POLY_APPROX_DP)
+#define APPROX_POLY_ACCURACY(op) NUM2DBL(rb_hash_aref(op, ID2SYM(rb_intern("accuracy"))))
+#define APPROX_POLY_RECURSIVE(op) ({VALUE _recursive = rb_hash_aref(op, ID2SYM(rb_intern("recursive"))); NIL_P(_recursive) ? 0 : _recursive == Qfalse ? 0 : 1;})
+
+VALUE rb_klass;
+
+VALUE
+rb_class()
+{
+ return rb_klass;
+}
+
+void
+define_ruby_class()
+{
+ if (rb_klass)
+ return;
+ /*
+ * opencv = rb_define_module("OpenCV");
+ * cvseq = rb_define_class_under(opencv, "CvSeq");
+ * curve = rb_define_module_under(opencv, "Curve");
+ * pointset = rb_define_module_under(opencv, "PointSet");
+ *
+ * note: this comment is used by rdoc.
+ */
+ VALUE opencv = rb_module_opencv();
+ VALUE cvseq = cCvSeq::rb_class();
+ VALUE curve = mCurve::rb_module();
+ VALUE pointset = mPointSet::rb_module();
+
+ rb_klass = rb_define_class_under(opencv, "CvContour", cvseq);
+ rb_include_module(rb_klass, curve);
+ rb_include_module(rb_klass, pointset);
+
+ VALUE approx_option = rb_hash_new();
+ rb_define_const(rb_klass, "APPROX_OPTION", approx_option);
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("method")), INT2FIX(CV_POLY_APPROX_DP));
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("accuracy")), rb_float_new(1.0));
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("recursive")), Qfalse);
+
+ rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
+ rb_define_method(rb_klass, "rect", RUBY_METHOD_FUNC(rb_rect), 0);
+ rb_define_method(rb_klass, "color", RUBY_METHOD_FUNC(rb_color), 0);
+ rb_define_method(rb_klass, "color=", RUBY_METHOD_FUNC(rb_set_color), 1);
+ rb_define_method(rb_klass, "reserved", RUBY_METHOD_FUNC(rb_reserved), 0);
+ rb_define_method(rb_klass, "approx_poly", RUBY_METHOD_FUNC(rb_approx_poly), -1);
+ rb_define_alias(rb_klass, "approx", "approx_poly");
+ rb_define_method(rb_klass, "bounding_rect", RUBY_METHOD_FUNC(rb_bounding_rect), 0);
+ rb_define_method(rb_klass, "create_tree", RUBY_METHOD_FUNC(rb_create_tree), -1);
+ rb_define_method(rb_klass, "in?", RUBY_METHOD_FUNC(rb_in_q), 1);
+ rb_define_method(rb_klass, "measure_distance", RUBY_METHOD_FUNC(rb_measure_distance), 1);
+}
+
+VALUE
+rb_initialize(int argc, VALUE *argv, VALUE self)
+{
+ /*
+ VALUE storage;
+ CvSeq *seq = 0;
+ rb_scan_args(argc, argv, "01", &storage);
+
+ storage = CHECK_CVMEMSTORAGE(storage);
+ seq = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint), CVMEMSTORAGE(storage));
+ DATA_PTR(self) = seq;
+ resist_root_object(seq, storage);
+ st_insert(cCvSeq::seqblock_klass, (st_data_t)seq, (st_data_t)klass);
+ */
+ return self;
+}
+
+VALUE
+rb_rect(VALUE self)
+{
+ return cCvRect::new_object(CVCONTOUR(self)->rect);
+}
+
+VALUE
+rb_color(VALUE self)
+{
+ return INT2NUM(CVCONTOUR(self)->color);
+}
+
+VALUE
+rb_set_color(VALUE self, VALUE color)
+{
+ CVCONTOUR(self)->color = NUM2INT(color);
+ return self;
+}
+
+VALUE
+rb_reserved(VALUE self)
+{
+ return rb_ary_new3(3,
+ INT2NUM(CVCONTOUR(self)->reserved[0]),
+ INT2NUM(CVCONTOUR(self)->reserved[1]),
+ INT2NUM(CVCONTOUR(self)->reserved[2]));
+}
+
+/*
+ * call-seq:
+ * approx_poly(<i>approx_poly_option</i>) -> cvcontour
+ *
+ * Approximates polygonal curve(s) with desired precision.
+ * <i>approx_poly_option</i> should be Hash include these keys.
+ * :method - Approximation method.
+ * :dp - corresponds to Douglas-Peucker algorithm.
+ * :accuracy - approximation accuracy. (high-accuracy will create more simple contours)
+ * :recursive - (default false)
+ * If not nil or false, the function approximates all chains that access can be obtained to
+ * from self by h_next or v_next links. If 0, approximated this one.
+ */
+
+VALUE
+rb_approx_poly(int argc, VALUE *argv, VALUE self)
+{
+ VALUE approx_poly_option, storage;
+ rb_scan_args(argc, argv, "01", &approx_poly_option);
+ approx_poly_option = APPROX_POLY_OPTION(approx_poly_option);
+ storage = cCvMemStorage::new_object();
+ /*
+ CvSeq *contour = cvApproxPoly(CVCONTOUR(self), sizeof(CvContour), CVMEMSTORAGE(storage),
+ APPROX_POLY_METHOD(approx_poly_option),
+ APPROX_POLY_ACCURACY(approx_poly_option),
+ APPROX_POLY_RECURSIVE(approx_poly_option));
+ return cCvSeq::new_sequence(cCvContour::rb_class(), contour, cCvPoint::rb_class(), storage);
+ */
+ return Qnil;
+}
+
+/*
+ * call-seq:
+ * bounding_rect -> rect
+ *
+ * Calculates up-right bounding rectangle of point set.
+ *
+ */
+VALUE
+rb_bounding_rect(VALUE self)
+{
+ return cCvRect::new_object(cvBoundingRect(CVCONTOUR(self), 1));
+}
+
+/*
+ * call-seq:
+ * create_tree([threshold = 0.0]) -> cvcontourtree
+ *
+ * Creates hierarchical representation of contour.
+ * If the parameter <i>threshold</i> is less than or equal to 0,
+ * the method creates full binary tree representation.
+ * If the threshold is greater than 0, the function creates
+ * representation with the precision threshold:
+ */
+VALUE
+rb_create_tree(int argc, VALUE *argv, VALUE self)
+{
+ VALUE threshold, storage;
+ rb_scan_args(argc, argv, "01", &threshold);