1
0
Fork 0
mirror of https://github.com/ruby-opencv/ruby-opencv synced 2023-03-27 23:22:12 -04:00

modified CvMat#not, and added some tests

This commit is contained in:
ser1zw 2011-01-03 14:21:38 +09:00
parent 24804939a3
commit c7412ba528
4 changed files with 120 additions and 22 deletions

View file

@ -1780,7 +1780,7 @@ rb_xor(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))
cvXor(CVARR(self), CVARR(val), CVARR(dest), MASK(mask));
else
@ -1798,7 +1798,7 @@ rb_xor(int argc, VALUE *argv, VALUE self)
VALUE
rb_not(VALUE self)
{
VALUE dest = new_object(cvGetSize(CVARR(self)), cvGetElemType(CVARR(self)));
VALUE dest = copy(self);
cvNot(CVARR(self), CVARR(dest));
return dest;
}

View file

@ -66,7 +66,7 @@ class OpenCVTestCase < Test::Unit::TestCase
m
end
def assert_each_cvscalar(actual, delta, &block)
def assert_each_cvscalar(actual, delta = 0, &block)
raise unless block_given?
count = 0
actual.height.times { |j|

View file

@ -844,7 +844,7 @@ 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...?')
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)
@ -1083,7 +1083,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.and(m2)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
}
@ -1092,7 +1092,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.and(m2, mask)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
if i < 3 and j < 2
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
@ -1105,7 +1105,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.and(s1)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0.001) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
}
@ -1114,7 +1114,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.and(s1, mask)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
if i < 3 and j < 2
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
@ -1127,7 +1127,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1 & m2
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
}
@ -1135,7 +1135,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1 & s1
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0.001) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
}
@ -1154,7 +1154,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.or(m2)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
}
@ -1163,7 +1163,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.or(m2, mask)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
if i < 3 and j < 2
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
@ -1176,7 +1176,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.or(s1)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0.001) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
}
@ -1185,7 +1185,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1.or(s1, mask)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
if i < 3 and j < 2
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
@ -1198,7 +1198,7 @@ class TestCvMat < OpenCVTestCase
m3 = m1 | m2
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
}
@ -1206,12 +1206,99 @@ class TestCvMat < OpenCVTestCase
m3 = m1 | s1
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3, 0.001) { |j, i, c|
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n | 1, n | 2, n | 3, n | 4)
}
end
def test_xor
m1 = create_cvmat(6, 4)
s1 = CvScalar.new(1, 2, 3, 4)
m2 = create_cvmat(6, 4) { s1 }
mask = create_cvmat(6, 4, :cv8u, 1) { |j, i, c|
s = (i < 3 and j < 2) ? 1 : 0
CvScalar.new(s)
}
# CvMat ^ CvMat
m3 = m1.xor(m2)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
}
# CvMat ^ CvMat with mask
m3 = m1.xor(m2, mask)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
if i < 3 and j < 2
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
else
CvScalar.new(n, n, n, n)
end
}
# CvMat ^ CvScalar
m3 = m1.xor(s1)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
}
# CvMat ^ CvScalar with mask
m3 = m1.xor(s1, mask)
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
if i < 3 and j < 2
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
else
CvScalar.new(n, n, n, n)
end
}
# Alias
m3 = m1 ^ m2
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
}
m3 = m1 ^ s1
assert_equal(m1.height, m3.height)
assert_equal(m1.width, m3.width)
assert_each_cvscalar(m3) { |j, i, c|
n = c + 1
CvScalar.new(n ^ 1, n ^ 2, n ^ 3, n ^ 4)
}
end
def test_not
m1 = create_cvmat(6, 4, :cv8s)
m2 = m1.not;
m3 = m1.clone
m3.not!
[m2, m3].each { |m|
assert_equal(m1.height, m.height)
assert_equal(m1.width, m.width)
assert_each_cvscalar(m) { |j, i, c|
n = c + 1
CvScalar.new(~n, ~n, ~n, ~n)
}
}
end
# def test_avg_sdv
# m = CvMat.new(1, 8, CV_32F)
# [2, 4, 4, 4, 5, 5, 7, 9].each_with_index { |v, i|

View file

@ -41,26 +41,37 @@ class TestPreliminary < OpenCVTestCase
end
def test_assert_each_cvscalar
mat = CvMat.new(5, 5, :cv32f, 4)
mat1 = CvMat.new(5, 5, :cv32f, 4)
mat2 = CvMat.new(5, 5, :cv32f, 4)
c = 0
mat.height.times { |j|
mat.width.times { |i|
mat[i, j] = CvScalar.new(c * 0.1, c * 0.2, c * 0.3, c * 0.4)
mat1[i, j] = CvScalar.new(c * 0.1, c * 0.2, c * 0.3, c * 0.4)
mat2[i, j] = CvScalar.new(c, c, c, c)
c += 1
}
}
assert_each_cvscalar(mat, 0.001) { |j, i, n|
assert_each_cvscalar(mat1, 0.001) { |j, i, n|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
}
assert_each_cvscalar(mat2) { |j, i, n|
CvScalar.new(c, c, c, c)
}
# Uncomment the following lines to check the fail cases
# assert_each_cvscalar(mat, 0.001) { |j, i, n|
# assert_each_cvscalar(mat1, 0.001) { |j, i, n|
# CvScalar.new(n * 0.1, n * 0.2, n * 0.3, 0)
# }
# assert_each_cvscalar(mat, 0.001) { |j, i, n|
# assert_each_cvscalar(mat1, 0.001) { |j, i, n|
# CvScalar.new(1, 2, 3, 4)
# }
# assert_each_cvscalar(mat2) { |j, i, n|
# CvScalar.new(n * 0.1, n * 0.2, n * 0.3, 0)
# }
# assert_each_cvscalar(mat2) { |j, i, n|
# CvScalar.new(1, 2, 3, 0)
# }
end