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

added constants for IplImage.load, and tested IplImage

This commit is contained in:
ser1zw 2011-01-14 02:42:29 +09:00
parent 2a256fda4a
commit 0ebc5b70f9
7 changed files with 158 additions and 37 deletions

View file

@ -79,7 +79,7 @@ rb_allocate(VALUE klass)
* Number of channel is set by <i>channel</i>. <i>channel</i> should be 1..4.
*
* note: width = col, height = row, on CvMat. It is noted not to make a mistake
* because the order of arguument is differenct to CvMat.
* because the order of argument is differenct to CvMat.
*/
VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
@ -94,12 +94,12 @@ rb_initialize(int argc, VALUE *argv, VALUE self)
/*
* call-seq:
* IplImage::load(<i>filename[,iscolor = nil]</i>)
* IplImage::load(<i>filename[,iscolor = CV_LOAD_IMAGE_COLOR]</i>)
*
* Load an image from file.
* iscolor = true, the loaded image is forced to be color 3-channel image.
* iscolor = false, the loaded image is forced to be grayscale.
* iscolor = nil, the loaded image will be loaded as is (depend on the file).
* iscolor = CV_LOAD_IMAGE_COLOR, the loaded image is forced to be a 3-channel color image
* iscolor = CV_LOAD_IMAGE_GRAYSCALE, the loaded image is forced to be grayscale
* iscolor = CV_LOAD_IMAGE_UNCHANGED, the loaded image will be loaded as is.
* Currently the following file format are supported.
* * Windows bitmaps - BMP,DIB
* * JPEG files - JPEG,JPG,JPE
@ -114,21 +114,16 @@ rb_load_image(int argc, VALUE *argv, VALUE self)
VALUE filename, iscolor;
rb_scan_args(argc, argv, "11", &filename, &iscolor);
Check_Type(filename, T_STRING);
int _iscolor;
switch (TYPE(iscolor)) {
case T_FALSE:
_iscolor = 0;
break;
case T_TRUE:
_iscolor = 1;
break;
case T_NIL:
_iscolor = -1;
break;
default:
rb_warn("argument 2 should be true(color)/false(non-color) or nil(auto).");
_iscolor = -1;
if (TYPE(iscolor) == T_NIL) {
_iscolor = CV_LOAD_IMAGE_COLOR;
}
else {
Check_Type(iscolor, T_FIXNUM);
_iscolor = FIX2INT(iscolor);
}
IplImage *image;
if ((image = cvLoadImage(StringValueCStr(filename), _iscolor)) == NULL) {
rb_raise(rb_eStandardError, "file does not exist or invalid format image.");

View file

@ -168,6 +168,13 @@ define_ruby_module()
/* 6: 64bit floating-point */
rb_define_const(rb_module, "CV_64F", INT2FIX(CV_64F));
/* Color types of loaded images */
rb_define_const(rb_module, "CV_LOAD_IMAGE_UNCHANGED", INT2FIX(CV_LOAD_IMAGE_UNCHANGED));
rb_define_const(rb_module, "CV_LOAD_IMAGE_GRAYSCALE", INT2FIX(CV_LOAD_IMAGE_GRAYSCALE));
rb_define_const(rb_module, "CV_LOAD_IMAGE_COLOR", INT2FIX(CV_LOAD_IMAGE_COLOR));
rb_define_const(rb_module, "CV_LOAD_IMAGE_ANYDEPTH", INT2FIX(CV_LOAD_IMAGE_ANYDEPTH));
rb_define_const(rb_module, "CV_LOAD_IMAGE_ANYCOLOR", INT2FIX(CV_LOAD_IMAGE_ANYCOLOR));
VALUE inversion_method = rb_hash_new();
/* {:lu, :svd, :svd_sym(:svd_symmetric)}: Inversion method */
rb_define_const(rb_module, "INVERSION_METHOD", inversion_method);

View file

@ -36,10 +36,6 @@ class OpenCVTestCase < Test::Unit::TestCase
end
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}") }

View file

@ -2,7 +2,6 @@
# -*- mode: ruby; coding: utf-8-unix -*-
require 'test/unit'
require 'opencv'
require 'pp'
require File.expand_path(File.dirname(__FILE__)) + '/helper'
include OpenCV

131
test/test_iplimage.rb Executable file
View file

@ -0,0 +1,131 @@
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8-unix -*-
require 'test/unit'
require 'opencv'
require File.expand_path(File.dirname(__FILE__)) + '/helper'
include OpenCV
# Tests for OpenCV::IplImage
class TestIplImage < OpenCVTestCase
FILENAME_CAT = File.expand_path(File.dirname(__FILE__)) + '/samples/cat.jpg'
def test_initialize
img = IplImage.new(10, 20)
assert_equal(10, img.width)
assert_equal(20, img.height)
assert_equal(:cv8u, img.depth)
assert_equal(3, img.channel)
img = IplImage.new(30, 40, :cv32f, 1)
assert_equal(30, img.width)
assert_equal(40, img.height)
assert_equal(:cv32f, img.depth)
assert_equal(1, img.channel)
end
def test_load
img = IplImage.load(FILENAME_CAT)
assert_equal(375, img.width)
assert_equal(500, img.height)
assert_equal(:cv8u, img.depth)
assert_equal(3, img.channel)
assert_equal('f2e4dc5d6d3fc285203762ff53d150c7', hash_img(img))
img = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_GRAYSCALE)
assert_equal(375, img.width)
assert_equal(500, img.height)
assert_equal(:cv8u, img.depth)
assert_equal(1, img.channel)
# The following test fails only when executed by test runner. (I don't know why...)
# $ ruby test/runner.rb #=> fail
# $ ruby test/test_iplimage.rb #=> pass
assert_equal('b1a0c1c5504961b62e15fa7d57a2e7e0', hash_img(img))
img = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR)
assert_equal(375, img.width)
assert_equal(500, img.height)
assert_equal(:cv8u, img.depth)
assert_equal(3, img.channel)
assert_equal('f2e4dc5d6d3fc285203762ff53d150c7', hash_img(img))
assert_raise(ArgumentError) {
IplImage.load
}
assert_raise(TypeError) {
IplImage.load(123)
}
assert_raise(TypeError) {
IplImage.load(FILENAME_CAT, 'foobar')
}
assert_raise(StandardError) {
IplImage.load('file/does/not/exist')
}
end
def test_roi
img = IplImage.new(20, 30)
rect = img.roi
assert_equal(0, rect.x)
assert_equal(0, rect.y)
assert_equal(img.width, rect.width)
assert_equal(img.height, rect.height)
img.set_roi(CvRect.new(2, 3, 10, 20))
rect = img.roi
assert_equal(2, rect.x)
assert_equal(3, rect.y)
assert_equal(10, rect.width)
assert_equal(20, rect.height)
img.reset_roi
rect = img.roi
assert_equal(0, rect.x)
assert_equal(0, rect.y)
assert_equal(img.width, rect.width)
assert_equal(img.height, rect.height)
img.set_roi(CvRect.new(1, 2, 5, 6)) {|image|
rect = image.roi
assert_equal(1, rect.x)
assert_equal(2, rect.y)
assert_equal(5, rect.width)
assert_equal(6, rect.height)
}
rect = img.roi
assert_equal(0, rect.x)
assert_equal(0, rect.y)
assert_equal(img.width, rect.width)
assert_equal(img.height, rect.height)
# Alias
img.roi = CvRect.new(4, 5, 11, 12)
rect = img.roi
assert_equal(4, rect.x)
assert_equal(5, rect.y)
assert_equal(11, rect.width)
assert_equal(12, rect.height)
end
def test_coi
img = IplImage.new(20, 30)
assert_equal(0, img.coi)
img.set_coi(1)
assert_equal(1, img.coi)
img.reset_coi
assert_equal(0, img.coi)
img.set_coi(2) {|image|
assert_equal(2, image.coi)
}
assert_equal(0, img.coi)
# Alias
img.coi = 1
assert_equal(1, img.coi)
end
end

View file

@ -16,6 +16,13 @@ class TestOpenCV < OpenCVTestCase
assert_equal(4, CV_32S)
assert_equal(5, CV_32F)
assert_equal(6, CV_64F)
# Load image flags
assert_equal(-1, CV_LOAD_IMAGE_UNCHANGED)
assert_equal(0, CV_LOAD_IMAGE_GRAYSCALE)
assert_equal(1, CV_LOAD_IMAGE_COLOR)
assert_equal(2, CV_LOAD_IMAGE_ANYDEPTH)
assert_equal(4, CV_LOAD_IMAGE_ANYCOLOR)
end
def test_symbols

View file

@ -122,20 +122,6 @@ class TestPreliminary < OpenCVTestCase
}
end
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)