diff --git a/ext/opencv/cvconnectedcomp.cpp b/ext/opencv/cvconnectedcomp.cpp index 3bbb704..5f3a259 100644 --- a/ext/opencv/cvconnectedcomp.cpp +++ b/ext/opencv/cvconnectedcomp.cpp @@ -47,6 +47,7 @@ define_ruby_class() rb_klass = rb_define_class_under(opencv, "CvConnectedComp", rb_cObject); rb_define_alloc_func(rb_klass, rb_allocate); + rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1); 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); @@ -75,6 +76,22 @@ rb_allocate(VALUE klass) return Data_Make_Struct(klass, CvConnectedComp, 0, cvconnectedcomp_free, ptr); } +VALUE +rb_initialize(int argc, VALUE *argv, VALUE self) +{ + VALUE area, value, rect, contour; + rb_scan_args(argc, argv, "04", &area, &value, &rect, &contour); + + if (!NIL_P(area)) + CVCONNECTEDCOMP(self)->area = NUM2DBL(area); + if (!NIL_P(value)) + CVCONNECTEDCOMP(self)->value = *CVSCALAR(value); + if (!NIL_P(rect)) + CVCONNECTEDCOMP(self)->rect = *CVRECT(rect); + if (!NIL_P(contour)) + CVCONNECTEDCOMP(self)->contour = CVSEQ(contour); +} + /* * Return area of connected component. */ diff --git a/ext/opencv/cvconnectedcomp.h b/ext/opencv/cvconnectedcomp.h index 4ebe878..d9e4ebf 100644 --- a/ext/opencv/cvconnectedcomp.h +++ b/ext/opencv/cvconnectedcomp.h @@ -24,6 +24,7 @@ void define_ruby_class(); VALUE rb_allocate(VALUE klass); +VALUE rb_initialize(int argc, VALUE *argv, VALUE self); VALUE rb_area(VALUE self); VALUE rb_value(VALUE self); VALUE rb_rect(VALUE self); diff --git a/test/test_cvconnectedcomp.rb b/test/test_cvconnectedcomp.rb index 59def9f..66255f1 100755 --- a/test/test_cvconnectedcomp.rb +++ b/test/test_cvconnectedcomp.rb @@ -9,24 +9,20 @@ include OpenCV # Tests for OpenCV::CvConnectedComp class TestCvConnectedComp < OpenCVTestCase def setup - mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i, c| - if (i >= 32 and i < 96) and (j >= 32 and j < 96) - CvScalar.new(255) - elsif (i >= 16 and i < 112) and (j >= 16 and j < 112) - CvScalar.new(192) - else - CvScalar.new(128) - end - } - - point = CvPoint.new(20, 20) - @mat, @connected_comp, @mask = mat0.flood_fill(point, 0, CvScalar.new(0), CvScalar.new(64), - :connectivity => 8, :fixed_range => true, :mask_only => true) + @connected_comp = CvConnectedComp.new(9216, CvScalar.new(1, 2, 3, 4), + CvRect.new(1, 2, 3, 4), CvSeq.new(CvPoint)) end def test_initialize connected_comp = CvConnectedComp.new - assert_not_nil(connected_comp) + assert_equal(CvConnectedComp, connected_comp.class) + assert_not_nil(connected_comp.area) + assert_not_nil(connected_comp.value) + assert_not_nil(connected_comp.rect) + assert_not_nil(connected_comp.contour) + + connected_comp = CvConnectedComp.new(100, CvScalar.new(1, 2, 3, 4), + CvRect.new(1, 2, 3, 4), CvSeq.new(CvPoint)) assert_equal(CvConnectedComp, connected_comp.class) assert_not_nil(connected_comp.area) assert_not_nil(connected_comp.value) @@ -40,21 +36,21 @@ class TestCvConnectedComp < OpenCVTestCase def test_value assert_equal(CvScalar, @connected_comp.value.class) - assert_cvscalar_equal(CvScalar.new(220, 0, 0, 0), @connected_comp.value) + assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), @connected_comp.value) end def test_rect assert_equal(CvRect, @connected_comp.rect.class) - assert_equal(16, @connected_comp.rect.x) - assert_equal(16, @connected_comp.rect.y) - assert_equal(96, @connected_comp.rect.width) - assert_equal(96, @connected_comp.rect.height) - - @connected_comp.rect = CvRect.new(1, 2, 3, 4); assert_equal(1, @connected_comp.rect.x) assert_equal(2, @connected_comp.rect.y) assert_equal(3, @connected_comp.rect.width) assert_equal(4, @connected_comp.rect.height) + + @connected_comp.rect = CvRect.new(10, 20, 30, 40); + assert_equal(10, @connected_comp.rect.x) + assert_equal(20, @connected_comp.rect.y) + assert_equal(30, @connected_comp.rect.width) + assert_equal(40, @connected_comp.rect.height) end def test_contour