2010-12-31 02:38:46 -05:00
|
|
|
#!/usr/bin/env ruby
|
2011-01-01 13:04:27 -05:00
|
|
|
# -*- mode: ruby; coding: utf-8-unix -*-
|
2010-12-31 02:38:46 -05:00
|
|
|
require 'test/unit'
|
|
|
|
require 'digest/md5'
|
|
|
|
require 'opencv'
|
2011-01-01 13:04:27 -05:00
|
|
|
require File.expand_path(File.dirname(__FILE__)) + '/helper'
|
2010-12-31 02:38:46 -05:00
|
|
|
|
|
|
|
include OpenCV
|
|
|
|
|
|
|
|
# Tests to run first; check the handful of basic operations that the later tests rely on
|
2011-01-01 13:04:27 -05:00
|
|
|
class TestPreliminary < OpenCVTestCase
|
|
|
|
def test_assert_array_equal
|
|
|
|
assert_array_equal([1, 2, 3, 4], [1, 2, 3, 4])
|
|
|
|
|
|
|
|
# Uncomment the following line to check the fail case
|
|
|
|
# assert_array_equal([1, 2, 3, 4], [1, 2, 3, 0])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_cvscalar_equal
|
|
|
|
assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), CvScalar.new(1, 2, 3, 4))
|
|
|
|
assert_cvscalar_equal(CvScalar.new(0.1, 0.2, 0.3, 0.4), CvScalar.new(0.1, 0.2, 0.3, 0.4))
|
|
|
|
|
|
|
|
# Uncomment the following lines to check the fail cases
|
|
|
|
# assert_cvscalar_equal(CvScalar.new(1, 2, 3, 4), CvScalar.new(1, 2, 3, 0))
|
|
|
|
# assert_cvscalar_equal(CvScalar.new(0.1, 0.2, 0.3, 0.4), CvScalar.new(0.1, 0.2, 0.3, 0.0))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_assert_in_delta
|
|
|
|
assert_in_delta(1, 0.9999, 0.1)
|
|
|
|
assert_in_delta(CvScalar.new(1, 2, 3, 4), CvScalar.new(1.01, 2.01, 3.01, 4.01), 0.1)
|
|
|
|
assert_in_delta(CvScalar.new(1, 2, 3, 4), [1.01, 2.01, 3.01, 4.01], 0.1)
|
|
|
|
assert_in_delta([1, 2, 3, 4], CvScalar.new(1.01, 2.01, 3.01, 4.01), 0.1)
|
|
|
|
assert_in_delta([1, 2, 3, 4], [1.01, 2.01, 3.01, 4.01], 0.1)
|
|
|
|
|
|
|
|
# Uncomment the following lines to check the fail cases
|
|
|
|
# assert_in_delta(1, 0.009, 0.1)
|
|
|
|
# assert_in_delta(CvScalar.new(1, 2, 3, 4), CvScalar.new(1.01, 2.01, 3.01, 4.01), 0.001)
|
|
|
|
# assert_in_delta(CvScalar.new(1, 2, 3, 4), [1.01, 2.01, 3.01, 4.01], 0.001)
|
|
|
|
# assert_in_delta([1, 2, 3, 4], CvScalar.new(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
|
|
|
|
|
2011-01-02 23:47:47 -05:00
|
|
|
def test_assert_each_cvscalar
|
2011-01-03 00:21:38 -05:00
|
|
|
mat1 = CvMat.new(5, 5, :cv32f, 4)
|
|
|
|
mat2 = CvMat.new(5, 5, :cv32f, 4)
|
2011-01-02 23:47:47 -05:00
|
|
|
c = 0
|
|
|
|
mat.height.times { |j|
|
|
|
|
mat.width.times { |i|
|
2011-01-03 00:21:38 -05:00
|
|
|
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)
|
2011-01-02 23:47:47 -05:00
|
|
|
c += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(mat1, 0.001) { |j, i, n|
|
2011-01-02 23:47:47 -05:00
|
|
|
CvScalar.new(n * 0.1, n * 0.2, n * 0.3, n * 0.4)
|
|
|
|
}
|
2011-01-03 00:21:38 -05:00
|
|
|
assert_each_cvscalar(mat2) { |j, i, n|
|
|
|
|
CvScalar.new(c, c, c, c)
|
|
|
|
}
|
|
|
|
|
2011-01-02 23:47:47 -05:00
|
|
|
# Uncomment the following lines to check the fail cases
|
2011-01-03 00:21:38 -05:00
|
|
|
# assert_each_cvscalar(mat1, 0.001) { |j, i, n|
|
2011-01-02 23:47:47 -05:00
|
|
|
# CvScalar.new(n * 0.1, n * 0.2, n * 0.3, 0)
|
|
|
|
# }
|
2011-01-03 00:21:38 -05:00
|
|
|
# assert_each_cvscalar(mat1, 0.001) { |j, i, n|
|
2011-01-02 23:47:47 -05:00
|
|
|
# CvScalar.new(1, 2, 3, 4)
|
|
|
|
# }
|
2011-01-03 00:21:38 -05:00
|
|
|
# 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)
|
|
|
|
# }
|
2011-01-02 23:47:47 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-01-01 13:04:27 -05:00
|
|
|
def test_create_cvmat
|
|
|
|
mat = create_cvmat(3, 4)
|
|
|
|
assert_equal(3, mat.height)
|
|
|
|
assert_equal(4, mat.width)
|
|
|
|
assert_equal(:cv8u, mat.depth)
|
|
|
|
assert_equal(4, mat.channel)
|
2011-01-02 23:47:47 -05:00
|
|
|
c = 0
|
|
|
|
mat.height.times { |j|
|
|
|
|
mat.width.times { |i|
|
|
|
|
s = CvScalar.new(c + 1, c + 1, c + 1, c + 1)
|
|
|
|
assert_cvscalar_equal(s, mat[i, j])
|
2011-01-01 13:04:27 -05:00
|
|
|
c += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mat = create_cvmat(2, 3, :cv16s, 2)
|
|
|
|
assert_equal(2, mat.height)
|
|
|
|
assert_equal(3, mat.width)
|
|
|
|
assert_equal(:cv16s, mat.depth)
|
|
|
|
assert_equal(2, mat.channel)
|
2011-01-02 23:47:47 -05:00
|
|
|
c = 0
|
|
|
|
mat.height.times { |j|
|
|
|
|
mat.width.times { |i|
|
|
|
|
s = CvScalar.new(c + 1, c + 1, 0, 0)
|
|
|
|
assert_cvscalar_equal(s, mat[i, j])
|
2011-01-01 13:04:27 -05:00
|
|
|
c += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mat = create_cvmat(2, 3, :cv16u, 3) { |j, i, c|
|
|
|
|
n = j + i + c
|
|
|
|
CvScalar.new(n, n, n, 0)
|
|
|
|
}
|
|
|
|
assert_equal(2, mat.height)
|
|
|
|
assert_equal(3, mat.width)
|
|
|
|
assert_equal(:cv16u, mat.depth)
|
|
|
|
assert_equal(3, mat.channel)
|
2011-01-02 23:47:47 -05:00
|
|
|
c = 0
|
|
|
|
mat.height.times { |j|
|
|
|
|
mat.width.times { |i|
|
2011-01-01 13:04:27 -05:00
|
|
|
n = j + i + c
|
|
|
|
assert_cvscalar_equal(CvScalar.new(n, n, n, 0), mat[i, j])
|
|
|
|
c += 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-12-31 02:38:46 -05:00
|
|
|
def test_lena
|
|
|
|
# Check that the lena jpg image has loaded correctly
|
|
|
|
img = get_sample('lena.jpg', false)
|
|
|
|
# snap(img) # uncomment this line to view the image, when regilding
|
|
|
|
assert_equal('2980cef5ac9bc061a5ab3f04775f3cf0', hash_img(img))
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_load
|
|
|
|
assert_raise(ArgumentError) { IplImage::load }
|
|
|
|
assert_raise(TypeError) { IplImage::load(4) }
|
|
|
|
assert_raise(ArgumentError) { IplImage::load('foo.jpg', 1, 1) }
|
|
|
|
assert_raise(StandardError) { IplImage::load('foo.jpg', 'foobar') }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_types
|
|
|
|
assert_equal(IplImage.new(7, 5, CV_8U, 1).class, IplImage)
|
|
|
|
assert_equal(CvMat.new(5, 7, CV_32F).class, CvMat)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|