mirror of
https://github.com/ruby-opencv/ruby-opencv
synced 2023-03-27 23:22:12 -04:00
added some tests
This commit is contained in:
parent
9a3067caaf
commit
9a3a49b917
3 changed files with 225 additions and 14 deletions
|
@ -25,7 +25,6 @@ class OpenCVTestCase < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
alias original_assert_in_delta assert_in_delta
|
alias original_assert_in_delta assert_in_delta
|
||||||
# alias original_assert_equal assert_equal
|
|
||||||
|
|
||||||
def assert_cvscalar_equal(expected, actual, message = nil)
|
def assert_cvscalar_equal(expected, actual, message = nil)
|
||||||
assert_equal(CvScalar, actual.class, message)
|
assert_equal(CvScalar, actual.class, message)
|
||||||
|
@ -66,5 +65,36 @@ class OpenCVTestCase < Test::Unit::TestCase
|
||||||
}
|
}
|
||||||
m
|
m
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def assert_each_cvscalar(actual, delta, &block)
|
||||||
|
raise unless block_given?
|
||||||
|
count = 0
|
||||||
|
actual.height.times { |j|
|
||||||
|
actual.width.times { |i|
|
||||||
|
expected = block.call(j, i, count)
|
||||||
|
if delta == 0
|
||||||
|
expected = expected.to_ary if expected.is_a? CvScalar
|
||||||
|
assert_array_equal(expected, actual[i, j].to_ary)
|
||||||
|
else
|
||||||
|
assert_in_delta(expected, actual[i, j], delta)
|
||||||
|
end
|
||||||
|
count += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def print_cvmat(mat)
|
||||||
|
s = []
|
||||||
|
mat.height.times { |j|
|
||||||
|
a = []
|
||||||
|
mat.width.times { |i|
|
||||||
|
tmp = mat[i, j].to_ary.map {|m| m.to_i }.join(',')
|
||||||
|
#tmp = mat[i, j].to_ary.map {|m| m.to_f.round(2) }.join(',')
|
||||||
|
a << "[#{tmp}]"
|
||||||
|
}
|
||||||
|
s << a.join(' ')
|
||||||
|
}
|
||||||
|
puts s.join("\n")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -692,7 +692,7 @@ class TestCvMat < OpenCVTestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_mix_channels
|
def test_mix_channels
|
||||||
flunk('CvMat.mix_channels is not implemented yet.')
|
flunk('FIXME: CvMat.mix_channels is not implemented yet.')
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_rand_shuffle
|
def test_rand_shuffle
|
||||||
|
@ -969,6 +969,161 @@ class TestCvMat < OpenCVTestCase
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_mul
|
||||||
|
m1 = create_cvmat(3, 3, :cv32f)
|
||||||
|
s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4)
|
||||||
|
m2 = create_cvmat(3, 3, :cv32f) { s1 }
|
||||||
|
|
||||||
|
# CvMat * CvMat
|
||||||
|
m3 = m1.mul(m2)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = c + 1
|
||||||
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# CvMat * CvMat * scale
|
||||||
|
scale = 2.5
|
||||||
|
m3 = m1.mul(m2, scale)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = (c + 1) * scale
|
||||||
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# CvMat * CvScalar
|
||||||
|
scale = 2.5
|
||||||
|
m3 = m1.mul(s1)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = c + 1
|
||||||
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# CvMat * CvScalar * scale
|
||||||
|
m3 = m1.mul(s1, scale)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = (c + 1) * scale
|
||||||
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_mat_mul
|
||||||
|
flunk('FIXME: CvMat#mat_mul is not implemented yet.')
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_div
|
||||||
|
m1 = create_cvmat(3, 3, :cv32f)
|
||||||
|
s1 = CvScalar.new(0.1, 0.2, 0.3, 0.4)
|
||||||
|
m2 = create_cvmat(3, 3, :cv32f) { s1 }
|
||||||
|
|
||||||
|
# CvMat / CvMat
|
||||||
|
m3 = m1.div(m2)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = c + 1
|
||||||
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# scale * CvMat / CvMat
|
||||||
|
scale = 2.5
|
||||||
|
m3 = m1.div(m2, scale)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = (c + 1) * scale
|
||||||
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# CvMat / CvScalar
|
||||||
|
scale = 2.5
|
||||||
|
m3 = m1.div(s1)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = c + 1
|
||||||
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# scale * CvMat / CvScalar
|
||||||
|
m3 = m1.div(s1, scale)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = (c + 1) * scale
|
||||||
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Alias
|
||||||
|
m3 = m1 / m2
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = c + 1
|
||||||
|
CvScalar.new(n / 0.1, n / 0.2, n / 0.3, n / 0.4)
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_and
|
||||||
|
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.and(m2)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0) { |j, i, c|
|
||||||
|
n = c + 1
|
||||||
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# CvMat & CvMat with mask
|
||||||
|
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|
|
||||||
|
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.and(s1)
|
||||||
|
assert_equal(m1.height, m3.height)
|
||||||
|
assert_equal(m1.width, m3.width)
|
||||||
|
assert_each_cvscalar(m3, 0.001) { |j, i, c|
|
||||||
|
n = c + 1
|
||||||
|
CvScalar.new(n & 1, n & 2, n & 3, n & 4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# CvMat & CvScalar with mask
|
||||||
|
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|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
# def test_avg_sdv
|
# def test_avg_sdv
|
||||||
# m = CvMat.new(1, 8, CV_32F)
|
# m = CvMat.new(1, 8, CV_32F)
|
||||||
# [2, 4, 4, 4, 5, 5, 7, 9].each_with_index { |v, i|
|
# [2, 4, 4, 4, 5, 5, 7, 9].each_with_index { |v, i|
|
||||||
|
|
|
@ -40,16 +40,41 @@ class TestPreliminary < OpenCVTestCase
|
||||||
# assert_in_delta([1, 2, 3, 4], [1.01, 2.01, 3.01, 4.01], 0.001)
|
# assert_in_delta([1, 2, 3, 4], [1.01, 2.01, 3.01, 4.01], 0.001)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_assert_each_cvscalar
|
||||||
|
mat = 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)
|
||||||
|
c += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_each_cvscalar(mat, 0.001) { |j, i, n|
|
||||||
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
||||||
|
}
|
||||||
|
|
||||||
|
# Uncomment the following lines to check the fail cases
|
||||||
|
# assert_each_cvscalar(mat, 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|
|
||||||
|
# CvScalar.new(1, 2, 3, 4)
|
||||||
|
# }
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
def test_create_cvmat
|
def test_create_cvmat
|
||||||
mat = create_cvmat(3, 4)
|
mat = create_cvmat(3, 4)
|
||||||
assert_equal(3, mat.height)
|
assert_equal(3, mat.height)
|
||||||
assert_equal(4, mat.width)
|
assert_equal(4, mat.width)
|
||||||
assert_equal(:cv8u, mat.depth)
|
assert_equal(:cv8u, mat.depth)
|
||||||
assert_equal(4, mat.channel)
|
assert_equal(4, mat.channel)
|
||||||
c = 1
|
c = 0
|
||||||
mat.height { |j|
|
mat.height.times { |j|
|
||||||
mat.width { |i|
|
mat.width.times { |i|
|
||||||
assert_cvscalar_equal(CvScalar.new(c, c, c, c), mat[i, j])
|
s = CvScalar.new(c + 1, c + 1, c + 1, c + 1)
|
||||||
|
assert_cvscalar_equal(s, mat[i, j])
|
||||||
c += 1
|
c += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -59,10 +84,11 @@ class TestPreliminary < OpenCVTestCase
|
||||||
assert_equal(3, mat.width)
|
assert_equal(3, mat.width)
|
||||||
assert_equal(:cv16s, mat.depth)
|
assert_equal(:cv16s, mat.depth)
|
||||||
assert_equal(2, mat.channel)
|
assert_equal(2, mat.channel)
|
||||||
c = 1
|
c = 0
|
||||||
mat.height { |j|
|
mat.height.times { |j|
|
||||||
mat.width { |i|
|
mat.width.times { |i|
|
||||||
assert_cvscalar_equal(CvScalar.new(c, c, 0, 0), mat[i, j])
|
s = CvScalar.new(c + 1, c + 1, 0, 0)
|
||||||
|
assert_cvscalar_equal(s, mat[i, j])
|
||||||
c += 1
|
c += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -75,9 +101,9 @@ class TestPreliminary < OpenCVTestCase
|
||||||
assert_equal(3, mat.width)
|
assert_equal(3, mat.width)
|
||||||
assert_equal(:cv16u, mat.depth)
|
assert_equal(:cv16u, mat.depth)
|
||||||
assert_equal(3, mat.channel)
|
assert_equal(3, mat.channel)
|
||||||
c = 1
|
c = 0
|
||||||
mat.height { |j|
|
mat.height.times { |j|
|
||||||
mat.width { |i|
|
mat.width.times { |i|
|
||||||
n = j + i + c
|
n = j + i + c
|
||||||
assert_cvscalar_equal(CvScalar.new(n, n, n, 0), mat[i, j])
|
assert_cvscalar_equal(CvScalar.new(n, n, n, 0), mat[i, j])
|
||||||
c += 1
|
c += 1
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue