From c9e0877f3686b6f7a049379a210ef6058d697734 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Fri, 31 Dec 2010 16:38:46 +0900 Subject: [PATCH] added some tests --- test/test_opencv.rb | 56 +++++++++++++++++++++++++++++++++++++++- test/test_preliminary.rb | 53 +++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+), 1 deletion(-) create mode 100755 test/test_preliminary.rb diff --git a/test/test_opencv.rb b/test/test_opencv.rb index 4c57e7f..8d5dd7e 100755 --- a/test/test_opencv.rb +++ b/test/test_opencv.rb @@ -1,6 +1,60 @@ #!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8-unix -*- $:.unshift(File.dirname(__FILE__) + '/../lib') - +require 'test/unit' +require 'digest/md5' require 'opencv' +include OpenCV + +class TestOpenCV < Test::Unit::TestCase + # mat_types = [CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4, + # CV_8SC1, CV_8SC2, CV_8SC3, CV_8SC4, + # CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4, + # CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4, + # CV_32SC1, CV_32SC2, CV_32SC3, CV_32SC4, + # CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4, + # CV_64FC1, CV_64FC2, CV_64FC3, CV_64FC4] + + # mat_types_single = [CV_8UC1, CV_8SC1, + # CV_16UC1, CV_16SC1, + # CV_32SC1, CV_32FC1, + # CV_64FC1] + + def setup + @depths = [CV_8U, CV_8S, + CV_16U, CV_16S, + CV_32S, CV_32F, + CV_64F] + @depthsize = { + CV_8U => 1, + CV_8S => 1, + CV_16U => 2, + CV_16S => 2, + CV_32S => 4, + CV_32F => 4, + CV_64F => 8 + } + end + + def get_sample(filename, iscolor = nil) + IplImage::load('samples/' + filename, iscolor) + end + + def snap(*images) + win = [] + images.size.times { |i| win << GUI::Window.new("snap-#{i}") } + win.each_with_index { |w, i| w.show images[i] } + + GUI::wait_key + GUI::Window::destroy_all + end + + def hash_img(img) + # Compute a hash for an image, useful for image comparisons + Digest::MD5.hexdigest(img.data) + end +end + + diff --git a/test/test_preliminary.rb b/test/test_preliminary.rb new file mode 100755 index 0000000..32d1a55 --- /dev/null +++ b/test/test_preliminary.rb @@ -0,0 +1,53 @@ +#!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8-unix -*- + +require 'test/unit' +require 'digest/md5' +require 'opencv' +require 'pp' +require_relative 'test_opencv' + +include OpenCV + +# Tests to run first; check the handful of basic operations that the later tests rely on +class TestPreliminary < TestOpenCV + 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 + + def test_to_s + [1, 4, 64, 512, 640].each { |w| + [1, 4, 64, 480, 512].each { |h| + [1, 2, 3, 4].each { |c| + @depths.each { |d| + expected_size = w * h * c * @depthsize[d] + + mat = CvMat.new(w, h, d, c) + assert_equal(expected_size, mat.data.size) + + # img = IplImage.new(w, h, d, c) + # expected_size += 4 - (expected_size % 4) unless (expected_size % 4) == 0 + # assert_equal(expected_size, img.data.to_s.size) + } + } + } + } + end +end + +