diff --git a/ext/opencv/cvcontourtree.cpp b/ext/opencv/cvcontourtree.cpp index 382c4e1..90a1435 100644 --- a/ext/opencv/cvcontourtree.cpp +++ b/ext/opencv/cvcontourtree.cpp @@ -14,11 +14,11 @@ * Contour tree. CvContour#create_tree * * C structure is here. - * typedef struct CvContourTree{ + * typedef struct CvContourTree { * CV_SEQUENCE_FIELDS() * CvPoint p1; * CvPoint p2; - * }CvContourTree; + * } CvContourTree; * */ __NAMESPACE_BEGIN_OPENCV @@ -49,8 +49,7 @@ define_ruby_class() rb_klass = rb_define_class_under(opencv, "CvContourTree", cvseq); rb_define_method(rb_klass, "p1", RUBY_METHOD_FUNC(rb_p1), 0); rb_define_method(rb_klass, "p2", RUBY_METHOD_FUNC(rb_p2), 0); - rb_define_method(rb_klass, "contour", RUBY_METHOD_FUNC(rb_contour), 0); - + rb_define_method(rb_klass, "contour", RUBY_METHOD_FUNC(rb_contour), 1); } VALUE @@ -74,13 +73,17 @@ rb_p2(VALUE self) * used for reconstruction, so it is possible to build approximated contour. */ VALUE -rb_contour(int argc, VALUE *argv, VALUE self) +rb_contour(VALUE self, VALUE criteria) { - VALUE criteria, storage; - rb_scan_args(argc, argv, "01", &criteria); - storage = cCvMemStorage::new_object(); - CvSeq *contour = cvContourFromContourTree(CVCONTOURTREE(self), CVMEMSTORAGE(storage), - VALUE_TO_CVTERMCRITERIA(criteria)); + VALUE storage = cCvMemStorage::new_object(); + CvSeq *contour = NULL; + try { + contour = cvContourFromContourTree(CVCONTOURTREE(self), CVMEMSTORAGE(storage), + VALUE_TO_CVTERMCRITERIA(criteria)); + } + catch (cv::Exception& e) { + raise_cverror(e); + } return cCvSeq::new_sequence(cCvContour::rb_class(), contour, cCvPoint::rb_class(), storage); } diff --git a/ext/opencv/cvcontourtree.h b/ext/opencv/cvcontourtree.h index 1b2f900..685500f 100644 --- a/ext/opencv/cvcontourtree.h +++ b/ext/opencv/cvcontourtree.h @@ -11,7 +11,7 @@ #define RUBY_OPENCV_CVCONTOURTREE_H #include "opencv.h" -#define __NAMESPACE_BEGIN_CVCONTOURTREE namespace cCvContourTree{ +#define __NAMESPACE_BEGIN_CVCONTOURTREE namespace cCvContourTree { #define __NAMESPACE_END_CVCONTOURTREE } __NAMESPACE_BEGIN_OPENCV @@ -23,7 +23,7 @@ void define_ruby_class(); VALUE rb_p1(VALUE self); VALUE rb_p2(VALUE self); -VALUE rb_contour(int argc, VALUE *argv, VALUE self); +VALUE rb_contour(VALUE self, VALUE criteria); VALUE new_object(); diff --git a/test/test_cvcontour.rb b/test/test_cvcontour.rb index def6de0..57dd53b 100755 --- a/test/test_cvcontour.rb +++ b/test/test_cvcontour.rb @@ -58,7 +58,16 @@ class TestCvContour < OpenCVTestCase end def test_create_tree - flunk('FIXME: CvContour#create_tree is not tested yet.') + mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i| + (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black + } + contour = mat0.find_contours + tree = contour.create_tree + assert_equal(CvContourTree, tree.class) + assert_equal(34, tree.p1.x) + assert_equal(53, tree.p1.y) + assert_equal(0, tree.p2.x) + assert_equal(0, tree.p2.y) end def test_in diff --git a/test/test_cvcontourtree.rb b/test/test_cvcontourtree.rb new file mode 100755 index 0000000..c4ffd97 --- /dev/null +++ b/test/test_cvcontourtree.rb @@ -0,0 +1,43 @@ +#!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8-unix -*- +require 'test/unit' +require 'opencv' +require File.expand_path(File.dirname(__FILE__)) + '/helper' + +include OpenCV + +# Tests for OpenCV::CvContourTree +class TestCvContourTree < OpenCVTestCase + def setup + @tree = CvContourTree.new(CvPoint) + end + + def test_initialize + tree = CvContourTree.new(CvPoint) + assert_equal(CvContourTree, tree.class) + assert(tree.is_a? CvSeq) + end + + def test_p1 + assert_equal(CvPoint, @tree.p1.class) + end + + def test_p2 + assert_equal(CvPoint, @tree.p2.class) + end + + def test_contour + mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i| + (j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black + } + contour = mat0.find_contours + tree = contour.create_tree + contour = tree.contour(CvTermCriteria.new(100, 0.01)) + assert_equal(CvContour, contour.class) + + assert_raise(CvStsBadArg) { + tree.contour(CvTermCriteria.new(0, 0)) + } + end +end +