From 7e068bc34b9c086d0a866e851f41cde433dd8b4a Mon Sep 17 00:00:00 2001 From: ser1zw Date: Thu, 6 Jan 2011 01:16:10 +0900 Subject: [PATCH] modified CvMat, and added some tests --- ext/cvmat.cpp | 13 +++++++++---- test/test_cvmat.rb | 39 +++++++++++++++++++++++++++------------ test/test_cvscalar.rb | 17 +++++++++++++++++ 3 files changed, 53 insertions(+), 16 deletions(-) diff --git a/ext/cvmat.cpp b/ext/cvmat.cpp index 6a6a475..d3874ae 100644 --- a/ext/cvmat.cpp +++ b/ext/cvmat.cpp @@ -417,11 +417,13 @@ rb_allocate(VALUE klass) * Number of channel is set by channel. channel should be 1..4. * */ +#include VALUE rb_initialize(int argc, VALUE *argv, VALUE self) { VALUE row, column, depth, channel; rb_scan_args(argc, argv, "22", &row, &column, &depth, &channel); + DATA_PTR(self) = cvCreateMat(FIX2INT(row), FIX2INT(column), CV_MAKETYPE(CVMETHOD("DEPTH", depth, CV_8U), argc < 4 ? 3 : FIX2INT(channel))); return self; @@ -1633,7 +1635,7 @@ rb_add(int argc, VALUE *argv, VALUE self) { VALUE val, mask, dest; rb_scan_args(argc, argv, "11", &val, &mask); - dest = new_object(cvGetSize(CVARR(self)), cvGetElemType(CVARR(self))); + dest = copy(self); if (rb_obj_is_kind_of(val, rb_klass)) cvAdd(CVARR(self), CVARR(val), CVARR(dest), MASK(mask)); else @@ -1657,11 +1659,14 @@ rb_sub(int argc, VALUE *argv, VALUE self) { VALUE val, mask, dest; rb_scan_args(argc, argv, "11", &val, &mask); - dest = new_object(cvGetSize(CVARR(self)), cvGetElemType(CVARR(self))); - if (rb_obj_is_kind_of(val, rb_klass)) + + dest = copy(self); + if (rb_obj_is_kind_of(val, rb_klass)) { cvSub(CVARR(self), CVARR(val), CVARR(dest), MASK(mask)); - else + } + else { cvSubS(CVARR(self), VALUE_TO_CVSCALAR(val), CVARR(dest), MASK(mask)); + } return dest; } diff --git a/test/test_cvmat.rb b/test/test_cvmat.rb index 91aaece..514cc5d 100755 --- a/test/test_cvmat.rb +++ b/test/test_cvmat.rb @@ -15,9 +15,28 @@ class TestCvMat < OpenCVTestCase assert_equal(20, m.cols) assert_equal(:cv8u, m.depth) assert_equal(3, m.channel) - # assert_each_cvscalar(m) { - # CvScalar.new(0, 0, 0, 0) - # } + + depth_table = { + CV_8U => :cv8u, + CV_8S => :cv8s, + CV_16U => :cv16u, + CV_16S => :cv16s, + CV_32S => :cv32s, + CV_32F => :cv32f, + CV_64F => :cv64f + } + + [CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F, + :cv8u, :cv8s, :cv16u, :cv16s, :cv32s, :cv32f, :cv64f].each { |depth| + [1, 2, 3, 4].each { |ch| + m = CvMat.new(10, 20, depth, ch) + assert_equal(10, m.rows) + assert_equal(20, m.cols) + depth = depth_table[depth] unless depth.is_a? Symbol + assert_equal(depth, m.depth) + assert_equal(ch, m.channel) + } + } end def test_DRAWING_OPTION @@ -855,7 +874,6 @@ class TestCvMat < OpenCVTestCase (i < 3 and j < 2) ? 1 : 0 } - flunk('FIXME: Tests of CvMat + CvMat with Mask often (but not always) fails. Is initializing required...?') m4 = m1.add(m2, mask) assert_equal(m1.height, m4.height) assert_equal(m1.width, m4.width) @@ -865,14 +883,13 @@ class TestCvMat < OpenCVTestCase if i < 3 and j < 2 s = CvScalar.new(n * 1.1, n * 2.2, n * 3.3, n * 4.4) else - s = CvScalar.new(0) + s = m1[j, i] end assert_in_delta(s, m4[j, i], 0.001) n += 1 } } - flunk('FIXME: Tests of CvMat + CvScalar with Mask often (but not always) fails. Is initializing required...?') # CvMat + CvScalar with Mask m4 = m1.add(s1, mask) assert_equal(m1.height, m4.height) @@ -883,7 +900,7 @@ class TestCvMat < OpenCVTestCase if i < 3 and j < 2 s = CvScalar.new(n * 0.1 + 1, n * 0.2 + 2, n * 0.3 + 3, n * 0.4 + 4) else - s = CvScalar.new(0) + s = m1[j, i] end assert_in_delta(s, m4[j, i], 0.001) n += 1 @@ -943,7 +960,6 @@ class TestCvMat < OpenCVTestCase (i < 3 and j < 2) ? 1 : 0 } - flunk('FIXME: Tests of CvMat - CvMat with Mask often (but not always) fails. Is initializing required...?') # CvMat - CvMat with Mask m4 = m1.sub(m2, mask) assert_equal(m1.height, m4.height) @@ -954,16 +970,15 @@ class TestCvMat < OpenCVTestCase if i < 3 and j < 2 s = CvScalar.new(-n * 0.9, -n * 1.8, -n * 2.7, -n * 3.6) else - s = CvScalar.new(0) + s = m1[j, i] end assert_in_delta(s, m4[j, i], 0.001) n += 1 } } - flunk('FIXME: Tests of CvMat - CvScalar with Mask often (but not always) fails. Is initializing required...?') # CvMat - CvScalar with Mask - m4 = m1.add(s1, mask) + m4 = m1.sub(s1, mask) assert_equal(m1.height, m4.height) assert_equal(m1.width, m4.width) n = 0 @@ -972,7 +987,7 @@ class TestCvMat < OpenCVTestCase if i < 3 and j < 2 s = CvScalar.new(n * 0.1 - 1, n * 0.2 - 2, n * 0.3 - 3, n * 0.4 - 4) else - s = CvScalar.new(0) + s = m1[j, i] end assert_in_delta(s, m4[j, i], 0.001) n += 1 diff --git a/test/test_cvscalar.rb b/test/test_cvscalar.rb index f57b124..6ac3226 100755 --- a/test/test_cvscalar.rb +++ b/test/test_cvscalar.rb @@ -8,6 +8,23 @@ include OpenCV # Tests for OpenCV::CvScalar class TestCvScalar < OpenCVTestCase + def test_initialize + s = CvScalar.new + assert_in_delta([0, 0, 0, 0], s, 0.01) + + s = CvScalar.new(1.1) + assert_in_delta([1.1, 0, 0, 0], s, 0.01) + + s = CvScalar.new(1.1, 2.2) + assert_in_delta([1.1, 2.2, 0, 0], s, 0.01) + + s = CvScalar.new(1.1, 2.2, 3.3) + assert_in_delta([1.1, 2.2, 3.3, 0], s, 0.01) + + s = CvScalar.new(1.1, 2.2, 3.3, 4.4) + assert_in_delta([1.1, 2.2, 3.3, 4.4], s, 0.01) + end + def test_aref assert_in_delta([0, 0, 0, 0], CvScalar.new, 0.01) assert_in_delta([10, 20, 30, 40], CvScalar.new(10, 20, 30, 40), 0.01)