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

added structuring element shape constants for IplConvKernel

This commit is contained in:
ser1zw 2011-01-29 18:04:32 +09:00
parent d82a1506cc
commit a9717d18a8
5 changed files with 48 additions and 24 deletions

View file

@ -85,32 +85,26 @@ rb_initialize(int argc, VALUE *argv, VALUE self)
{
VALUE shape, rows, cols, anchor_x, anchor_y, values;
rb_scan_args(argc, argv, "51", &cols, &rows, &anchor_x, &anchor_y, &shape, &values);
Check_Type(shape, T_SYMBOL);
const char *shape_name = rb_id2name(SYM2ID(shape));
int shape_type = 0;
int shape_type;
int _cols = NUM2INT(cols);
int _rows = NUM2INT(rows);
int num_values;
int *_values;
if (!strcmp(shape_name, "rect"))
shape_type = CV_SHAPE_RECT;
else if (!strcmp(shape_name, "cross"))
shape_type = CV_SHAPE_CROSS;
else if (!strcmp(shape_name, "ellipse"))
shape_type = CV_SHAPE_ELLIPSE;
else if (!strcmp(shape_name, "custom")) {
const int INVALID_SHAPE = -1;
shape_type = CVMETHOD("STRUCTURING_ELEMENT_SHAPE", shape, INVALID_SHAPE);
if (shape_type == INVALID_SHAPE)
rb_raise(rb_eTypeError, "argument 1 (shape) should be :rect or :cross or :ellipse or :custom.");
if (shape_type == CV_SHAPE_CUSTOM) {
if (NIL_P(values))
rb_raise(rb_eArgError, "argument 6 (values) should not be nil when the shape is :custom.");
shape_type = CV_SHAPE_CUSTOM;
num_values = RARRAY_LEN(values);
_values = ALLOCA_N(int, num_values);
VALUE *values_ptr = RARRAY_PTR(values);
for (int i = 0; i < num_values; i++)
_values[i] = NUM2INT(values_ptr[i]);
}
else
rb_raise(rb_eTypeError, "argument 1 (shape) should be :rect or :cross or :ellipse or :custom.");
DATA_PTR(self) = cvCreateStructuringElementEx(_cols, _rows, NUM2INT(anchor_x), NUM2INT(anchor_y), shape_type, _values);
DATA_PTR(self) = cvCreateStructuringElementEx(_cols, _rows, NUM2INT(anchor_x), NUM2INT(anchor_y),shape_type, _values);
return self;
}

View file

@ -187,6 +187,12 @@ define_ruby_module()
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));
/* Shape of the structuring elements */
rb_define_const(rb_module, "CV_SHAPE_RECT", INT2FIX(CV_SHAPE_RECT));
rb_define_const(rb_module, "CV_SHAPE_CROSS", INT2FIX(CV_SHAPE_CROSS));
rb_define_const(rb_module, "CV_SHAPE_ELLIPSE", INT2FIX(CV_SHAPE_ELLIPSE));
rb_define_const(rb_module, "CV_SHAPE_CUSTOM", INT2FIX(CV_SHAPE_CUSTOM));
VALUE inversion_method = rb_hash_new();
/* {:lu, :svd, :svd_sym(:svd_symmetric)}: Inversion method */
rb_define_const(rb_module, "INVERSION_METHOD", inversion_method);
@ -241,6 +247,14 @@ define_ruby_module()
RESIST_CVMETHOD(connectivity, "aa", CV_AA);
RESIST_CVMETHOD(connectivity, "anti_alias", CV_AA);
VALUE structuring_element_shape = rb_hash_new();
/* {:rect, :cross, :ellipse, :custom}: Shape of the structuring elements */
rb_define_const(rb_module, "STRUCTURING_ELEMENT_SHAPE", structuring_element_shape);
RESIST_CVMETHOD(structuring_element_shape, "rect", CV_SHAPE_RECT);
RESIST_CVMETHOD(structuring_element_shape, "cross", CV_SHAPE_CROSS);
RESIST_CVMETHOD(structuring_element_shape, "ellipse", CV_SHAPE_ELLIPSE);
RESIST_CVMETHOD(structuring_element_shape, "custom", CV_SHAPE_CUSTOM);
VALUE retrieval_mode = rb_hash_new();
/* {:external, :list, :ccomp, :tree}: Retrieval mode */
rb_define_const(rb_module, "RETRIEVAL_MODE", retrieval_mode);

View file

@ -249,11 +249,11 @@ CVMETHOD(const char *name, VALUE method, int ifnone = 0)
return ifnone;
}else{
return FIX2INT(value);
}if (rb_obj_is_kind_of(value, rb_cNumeric))
default:
rb_raise(rb_eTypeError, "");
}
default:
rb_raise(rb_eTypeError, "");
}
return 0;
return ifnone;
}
inline int

View file

@ -9,17 +9,21 @@ include OpenCV
# Tests for OpenCV::IplConvKernel
class TestIplConvKernel < OpenCVTestCase
def test_initialize
[:rect, :cross, :ellipse].each { |sym|
kernel = IplConvKernel.new(5, 5, 2, 2, sym)
[:rect, :cross, :ellipse, CV_SHAPE_RECT, CV_SHAPE_CROSS, CV_SHAPE_ELLIPSE].each { |shape|
kernel = IplConvKernel.new(5, 5, 2, 2, shape)
assert_not_nil(kernel)
}
values = [1] * 25
kernel = IplConvKernel.new(5, 5, 2, 2, :custom, values)
assert_not_nil(kernel)
[:custom, CV_SHAPE_CUSTOM].each { |shape|
kernel = IplConvKernel.new(5, 5, 2, 2, shape, values)
assert_not_nil(kernel)
}
assert_raise(ArgumentError) {
IplConvKernel.new(5, 5, 2, 2, :custom)
[:custom, CV_SHAPE_CUSTOM].each { |shape|
assert_raise(ArgumentError) {
IplConvKernel.new(5, 5, 2, 2, shape)
}
}
assert_raise(TypeError) {

View file

@ -23,6 +23,12 @@ class TestOpenCV < OpenCVTestCase
assert_equal(1, CV_LOAD_IMAGE_COLOR)
assert_equal(2, CV_LOAD_IMAGE_ANYDEPTH)
assert_equal(4, CV_LOAD_IMAGE_ANYCOLOR)
# Structuring element shapes
assert_equal(0, CV_SHAPE_RECT)
assert_equal(1, CV_SHAPE_CROSS)
assert_equal(2, CV_SHAPE_ELLIPSE)
assert_equal(100, CV_SHAPE_CUSTOM)
end
def test_symbols
@ -89,6 +95,12 @@ class TestOpenCV < OpenCVTestCase
assert_equal(3, MATCH_TEMPLATE_METHOD[:ccorr_normed])
assert_equal(4, MATCH_TEMPLATE_METHOD[:ccoeff])
assert_equal(5, MATCH_TEMPLATE_METHOD[:ccoeff_normed])
# Structuring element shapes
assert_equal(0, STRUCTURING_ELEMENT_SHAPE[:rect])
assert_equal(1, STRUCTURING_ELEMENT_SHAPE[:cross])
assert_equal(2, STRUCTURING_ELEMENT_SHAPE[:ellipse])
assert_equal(100, STRUCTURING_ELEMENT_SHAPE[:custom])
end
def test_cvt_color_funcs