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

added some tests for CvMat

This commit is contained in:
ser1zw 2010-12-31 21:34:36 +09:00
parent 8eef415d46
commit e3a6dc658a
3 changed files with 266 additions and 34 deletions

View file

@ -10,6 +10,18 @@ include OpenCV
# Tests for OpenCV::CvMat
class TestCvMat < TestOpenCV
def make_cvmat(h, w, depth = :cv8u, channel = 3)
m = CvMat.new(h, w, depth, channel)
count = 1
h.times { |j|
w.times { |i|
m[i, j] = count
count += 1
}
}
m
end
def test_DRAWING_OPTION
CvMat::DRAWING_OPTION[:color].to_ary.each { |c|
assert_in_delta(0, c, 0.01)
@ -61,19 +73,239 @@ class TestCvMat < TestOpenCV
assert_in_delta(0.99, CvMat::FIND_FUNDAMENTAL_MAT_OPTION[:desirable_level], 0.01)
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|
m[i] = CvScalar.new(v, 0, 0, 0)
# m[i][0] = v
}
avg = m.avg
assert_in_delta(avg[0], 5.0, 0.01)
avg, sdv = m.avg_sdv
assert_in_delta(avg[0], 5.0, 0.01)
assert_in_delta(sdv[0], 2.0, 0.01)
def test_to_s
m = CvMat.new(10, 20)
assert_equal('<OpenCV::CvMat:20x10,depth=cv8u,channel=3>', m.to_s)
m = CvMat.new(10, 20, :cv16s)
assert_equal('<OpenCV::CvMat:20x10,depth=cv16s,channel=3>', m.to_s)
m = CvMat.new(10, 20, :cv32f, 1)
assert_equal('<OpenCV::CvMat:20x10,depth=cv32f,channel=1>', m.to_s)
end
def test_parent
m1 = CvMat.new(10, 20)
assert((not m1.has_parent?))
assert_nil(m1.parent)
flunk('TODO: resolve unexpected ABORT of CvMat#to_CvMat')
# m2 = m1.to_CvMat
# assert(m2.has_parent?)
# assert_same(m1, m2.parent)
end
def test_inside
m = CvMat.new(20, 10)
assert(m.inside? CvPoint.new(0, 0))
assert(m.inside? CvPoint.new(9, 19))
assert((not m.inside? CvPoint.new(10, 0)))
assert((not m.inside? CvPoint.new(0, 20)))
assert((not m.inside? CvPoint.new(10, 20)))
end
def test_to_IplConvKernel
kernel = CvMat.new(10, 20).to_IplConvKernel(CvPoint.new(2, 3))
assert_equal(10, kernel.rows)
assert_equal(20, kernel.cols)
assert_equal(2, kernel.anchor.x)
assert_equal(3, kernel.anchor.y)
assert_equal(2, kernel.anchor_x)
assert_equal(3, kernel.anchor_y)
end
def test_create_mask
mask = CvMat.new(10, 20).create_mask
assert_equal(20, mask.width)
assert_equal(10, mask.height)
assert_equal(:cv8u, mask.depth)
assert_equal(1, mask.channel)
end
def test_fields
m = CvMat.new(20, 10)
assert_equal(10, m.width)
assert_equal(10, m.columns)
assert_equal(10, m.cols)
assert_equal(20, m.height)
assert_equal(20, m.rows)
assert_equal(:cv8u, m.depth)
assert_equal(3, m.channel)
m = CvMat.new(20, 10, :cv16s, 1)
assert_equal(10, m.width)
assert_equal(10, m.columns)
assert_equal(10, m.cols)
assert_equal(20, m.height)
assert_equal(20, m.rows)
assert_equal(:cv16s, m.depth)
assert_equal(1, m.channel)
end
def test_clone
m1 = make_cvmat(10, 20)
m2 = m1.clone
assert_equal(m1.data, m2.data)
end
def test_copy
m1 = make_cvmat(10, 20, CV_32F, 1)
m2 = m1.copy
assert_equal(m1.data, m2.data)
m2 = CvMat.new(10, 20, CV_32F, 1)
m1.copy(m2)
m1.height.times { |j|
m1.width.times { |i|
assert(is_same_float_array(m1[i, j].to_ary, m2[i, j].to_ary))
}
}
assert_equal(m1.data, m2.data)
m2 = CvMat.new(1, 2, CV_32F, 1)
assert_raise(CvUnmatchedSizes) {
m1.copy(m2)
}
m2 = CvMat.new(10, 20, CV_32F, 3)
assert_raise(CvUnmatchedFormats) {
m1.copy(m2)
}
m2 = CvMat.new(10, 20, CV_8U, 1)
assert_raise(CvUnmatchedFormats) {
m1.copy(m2)
}
a = m1.copy(2)
assert_equal(2, a.size)
a.each { |m|
m1.height.times { |j|
m1.width.times { |i|
assert(is_same_float_array(m1[i, j].to_ary, m[i, j].to_ary))
}
}
}
assert_nil(m1.copy(-1))
end
def test_convert_depth
m = CvMat.new(10, 20, :cv32f)
assert_equal(:cv8u, m.to_8u.depth)
assert_equal(:cv8s, m.to_8s.depth)
assert_equal(:cv16u, m.to_16u.depth)
assert_equal(:cv16s, m.to_16s.depth)
assert_equal(:cv32s, m.to_32s.depth)
assert_equal(:cv32f, m.to_32f.depth)
assert_equal(:cv64f, m.to_64f.depth)
end
def test_vector
m = CvMat.new(1, 2)
assert(m.vector?)
m = CvMat.new(2, 2)
assert((not m.vector?))
end
def test_square
m = CvMat.new(2, 2)
assert(m.square?)
m = CvMat.new(1, 2)
assert((not m.square?))
end
def test_to_CvMat
flunk('TODO: resolve unexpected ABORT of CvMat#to_CvMat')
# m1 = CvMat.new(2, 2)
# m2 = m1.to_CvMat
# assert_same(m1, m2.parent)
end
def test_sub_rect
m1 = make_cvmat(10, 10)
assert_raise(ArgumentError) {
m1.sub_rect
}
m2 = m1.sub_rect(CvRect.new(0, 0, 2, 3))
assert_equal(2, m2.width)
assert_equal(3, m2.height)
m2.height.times { |j|
m2.width.times { |i|
assert(is_same_float_array(m1[i, j].to_ary, m2[i, j].to_ary))
}
}
topleft = CvPoint.new(2, 3)
m2 = m1.sub_rect(topleft, CvSize.new(4, 5))
assert_equal(4, m2.width)
assert_equal(5, m2.height)
m2.height.times { |j|
m2.width.times { |i|
assert(is_same_float_array(m1[topleft.x + i, topleft.y + j].to_ary, m2[i, j].to_ary))
}
}
topleft = CvPoint.new(1, 2)
m2 = m1.sub_rect(topleft.x, topleft.y, 3, 4)
assert_equal(3, m2.width)
assert_equal(4, m2.height)
m2.height.times { |j|
m2.width.times { |i|
assert(is_same_float_array(m1[topleft.x + i, topleft.y + j].to_ary, m2[i, j].to_ary))
}
}
end
def test_slice_width
m1 = make_cvmat(10, 40, :cv32f, 1)
ml, mr = m1.slice_width(2)
[ml, mr].each { |m|
assert_equal(10, m.height)
assert_equal(20, m.width)
assert_equal(:cv32f, m.depth)
assert_equal(1, m.channel)
}
ml.height.times { |j|
ml.width.times { |i|
assert(is_same_float_array(m1[i, j].to_ary, ml[i, j].to_ary))
assert(is_same_float_array(m1[20 + i, j].to_ary, mr[i, j].to_ary))
}
}
end
def test_slice_height
m1 = make_cvmat(10, 20, :cv32f, 1)
mt, mb = m1.slice_height(2)
[mt, mb].each { |m|
assert_equal(5, m.height)
assert_equal(20, m.width)
assert_equal(:cv32f, m.depth)
assert_equal(1, m.channel)
}
mt.height.times { |j|
mt.width.times { |i|
assert(is_same_float_array(m1[i, j].to_ary, mt[i, j].to_ary))
assert(is_same_float_array(m1[i, 5 + j].to_ary, mb[i, j].to_ary))
}
}
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|
# m[i] = CvScalar.new(v, 0, 0, 0)
# }
# avg = m.avg
# assert_in_delta(avg[0], 5.0, 0.01)
# avg, sdv = m.avg_sdv
# assert_in_delta(avg[0], 5.0, 0.01)
# assert_in_delta(sdv[0], 2.0, 0.01)
# end
end

View file

@ -8,13 +8,6 @@ include OpenCV
# Tests for OpenCV::CvScalar
class TestCvScalar < TestOpenCV
def is_same_elems(a, b, delta = 0.01)
4.times { |i|
return false unless (a[i].to_f - b[i].to_f).abs <= delta.to_f
}
true
end
def test_aref
s = CvScalar.new
4.times { |i| assert_in_delta(0, s[i], 0.01) }
@ -62,22 +55,22 @@ class TestCvScalar < TestOpenCV
end
def test_cvcolor
assert(is_same_elems(CvColor::Black, CvScalar.new(0x0, 0x0, 0x0, 0)))
assert(is_same_elems(CvColor::Silver, CvScalar.new(0x0c, 0x0c, 0x0c, 0)))
assert(is_same_elems(CvColor::Gray, CvScalar.new(0x80, 0x80, 0x80, 0)))
assert(is_same_elems(CvColor::White, CvScalar.new(0xff, 0xff, 0xff, 0)))
assert(is_same_elems(CvColor::Maroon, CvScalar.new(0x0, 0x0, 0x80, 0)))
assert(is_same_elems(CvColor::Red, CvScalar.new(0x0, 0x0, 0xff, 0)))
assert(is_same_elems(CvColor::Purple, CvScalar.new(0x80, 0x0, 0x80, 0)))
assert(is_same_elems(CvColor::Fuchsia, CvScalar.new(0xff, 0x0, 0xff, 0)))
assert(is_same_elems(CvColor::Green, CvScalar.new(0x0, 0x80, 0x0, 0)))
assert(is_same_elems(CvColor::Lime, CvScalar.new(0x0, 0xff, 0x0, 0)))
assert(is_same_elems(CvColor::Olive, CvScalar.new(0x0, 0x80, 0x80, 0)))
assert(is_same_elems(CvColor::Yellow, CvScalar.new(0x0, 0xff, 0xff, 0)))
assert(is_same_elems(CvColor::Navy, CvScalar.new(0x80, 0x0, 0x0, 0)))
assert(is_same_elems(CvColor::Blue, CvScalar.new(0xff, 0x0, 0x0, 0)))
assert(is_same_elems(CvColor::Teal, CvScalar.new(0x80, 0x80, 0x0, 0)))
assert(is_same_elems(CvColor::Aqua, CvScalar.new(0xff, 0xff, 0x0, 0)))
assert(is_same_float_array(CvColor::Black, CvScalar.new(0x0, 0x0, 0x0, 0)))
assert(is_same_float_array(CvColor::Silver, CvScalar.new(0x0c, 0x0c, 0x0c, 0)))
assert(is_same_float_array(CvColor::Gray, CvScalar.new(0x80, 0x80, 0x80, 0)))
assert(is_same_float_array(CvColor::White, CvScalar.new(0xff, 0xff, 0xff, 0)))
assert(is_same_float_array(CvColor::Maroon, CvScalar.new(0x0, 0x0, 0x80, 0)))
assert(is_same_float_array(CvColor::Red, CvScalar.new(0x0, 0x0, 0xff, 0)))
assert(is_same_float_array(CvColor::Purple, CvScalar.new(0x80, 0x0, 0x80, 0)))
assert(is_same_float_array(CvColor::Fuchsia, CvScalar.new(0xff, 0x0, 0xff, 0)))
assert(is_same_float_array(CvColor::Green, CvScalar.new(0x0, 0x80, 0x0, 0)))
assert(is_same_float_array(CvColor::Lime, CvScalar.new(0x0, 0xff, 0x0, 0)))
assert(is_same_float_array(CvColor::Olive, CvScalar.new(0x0, 0x80, 0x80, 0)))
assert(is_same_float_array(CvColor::Yellow, CvScalar.new(0x0, 0xff, 0xff, 0)))
assert(is_same_float_array(CvColor::Navy, CvScalar.new(0x80, 0x0, 0x0, 0)))
assert(is_same_float_array(CvColor::Blue, CvScalar.new(0xff, 0x0, 0x0, 0)))
assert(is_same_float_array(CvColor::Teal, CvScalar.new(0x80, 0x80, 0x0, 0)))
assert(is_same_float_array(CvColor::Aqua, CvScalar.new(0xff, 0xff, 0x0, 0)))
end
end

View file

@ -54,6 +54,13 @@ class TestOpenCV < Test::Unit::TestCase
# Compute a hash for an image, useful for image comparisons
Digest::MD5.hexdigest(img.data)
end
def is_same_float_array(a, b, delta = 0.01)
4.times { |i|
return false unless (a[i].to_f - b[i].to_f).abs <= delta.to_f
}
true
end
end