From c0bff63c92673052663ab4a7669f684368da87dd Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sat, 19 Jan 2013 21:58:16 +0900 Subject: [PATCH 01/15] change CvMat#get_rows, CvMat#get_cols --- ext/opencv/cvmat.cpp | 118 ++++++++++++++++++++----------------------- ext/opencv/cvmat.h | 4 +- test/test_cvmat.rb | 81 +++++++++++++++++++---------- 3 files changed, 113 insertions(+), 90 deletions(-) diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index 8861a4e..aafdcf1 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -225,8 +225,8 @@ void define_ruby_class() rb_define_method(rb_klass, "to_CvMat", RUBY_METHOD_FUNC(rb_to_CvMat), 0); rb_define_method(rb_klass, "sub_rect", RUBY_METHOD_FUNC(rb_sub_rect), -2); rb_define_alias(rb_klass, "subrect", "sub_rect"); - rb_define_method(rb_klass, "get_rows", RUBY_METHOD_FUNC(rb_get_rows), -2); - rb_define_method(rb_klass, "get_cols", RUBY_METHOD_FUNC(rb_get_cols), -2); + rb_define_method(rb_klass, "get_rows", RUBY_METHOD_FUNC(rb_get_rows), -1); + rb_define_method(rb_klass, "get_cols", RUBY_METHOD_FUNC(rb_get_cols), 1); rb_define_method(rb_klass, "each_row", RUBY_METHOD_FUNC(rb_each_row), 0); rb_define_method(rb_klass, "each_col", RUBY_METHOD_FUNC(rb_each_col), 0); rb_define_alias(rb_klass, "each_column", "each_col"); @@ -1058,77 +1058,71 @@ rb_sub_rect(VALUE self, VALUE args) return DEPEND_OBJECT(rb_klass, mat, self); } -/* - * call-seq: - * get_rows(n) -> Return row - * get_rows(n1, n2, ...) -> Return Array of row - * - * Return row(or rows) of matrix. - * argument should be Fixnum or CvSlice compatible object. - */ -VALUE -rb_get_rows(VALUE self, VALUE args) -{ - int len = RARRAY_LEN(args); - if (len < 1) - rb_raise(rb_eArgError, "wrong number of argument.(more than 1)"); - VALUE ary = rb_ary_new2(len); - for (int i = 0; i < len; ++i) { - VALUE value = rb_ary_entry(args, i); - - CvMat* row = NULL; - try { - if (FIXNUM_P(value)) - row = cvGetRow(CVARR(self), RB_CVALLOC(CvMat), FIX2INT(value)); - else { - CvSlice slice = VALUE_TO_CVSLICE(value); - row = cvGetRows(CVARR(self), RB_CVALLOC(CvMat), slice.start_index, slice.end_index); - } +void +rb_get_range_index(VALUE index, int* start, int *end) { + if (rb_obj_is_kind_of(index, rb_cRange)) { + *start = NUM2INT(rb_funcall3(index, rb_intern("begin"), 0, NULL)); + *end = NUM2INT(rb_funcall3(index, rb_intern("end"), 0, NULL)); + if (rb_funcall3(index, rb_intern("exclude_end?"), 0, NULL) == Qfalse) { + (*end)++; } - catch (cv::Exception& e) { - if (row != NULL) - cvReleaseMat(&row); - raise_cverror(e); - } - rb_ary_store(ary, i, DEPEND_OBJECT(rb_klass, row, self)); } - return RARRAY_LEN(ary) > 1 ? ary : rb_ary_entry(ary, 0); + else { + *start = NUM2INT(index); + *end = *start + 1; + } } /* * call-seq: - * get_cols(n) -> Return column - * get_cols(n1, n2, ...) -> Return Array of columns + * get_rows(row, delta_row = 1) -> cvmat * - * Return column(or columns) of matrix. - * argument should be Fixnum or CvSlice compatible object. + * Return row(or rows) of matrix. + * argument row should be Fixnum or Range object. */ VALUE -rb_get_cols(VALUE self, VALUE args) +rb_get_rows(int argc, VALUE* argv, VALUE self) { - int len = RARRAY_LEN(args); - if (len < 1) - rb_raise(rb_eArgError, "wrong number of argument.(more than 1)"); - VALUE ary = rb_ary_new2(len); - for (int i = 0; i < len; ++i) { - VALUE value = rb_ary_entry(args, i); - CvMat* col = NULL; - try { - if (FIXNUM_P(value)) - col = cvGetCol(CVARR(self), RB_CVALLOC(CvMat), FIX2INT(value)); - else { - CvSlice slice = VALUE_TO_CVSLICE(value); - col = cvGetCols(CVARR(self), RB_CVALLOC(CvMat), slice.start_index, slice.end_index); - } - } - catch (cv::Exception& e) { - if (col != NULL) - cvReleaseMat(&col); - raise_cverror(e); - } - rb_ary_store(ary, i, DEPEND_OBJECT(rb_klass, col, self)); + VALUE row_val, delta_val; + rb_scan_args(argc, argv, "11", &row_val, &delta_val); + + int start, end; + rb_get_range_index(row_val, &start, &end); + int delta = NIL_P(delta_val) ? 1 : NUM2INT(delta_val); + CvMat* submat = RB_CVALLOC(CvMat); + try { + cvGetRows(CVARR(self), submat, start, end, delta); } - return RARRAY_LEN(ary) > 1 ? ary : rb_ary_entry(ary, 0); + catch (cv::Exception& e) { + cvFree(&submat); + raise_cverror(e); + } + + return DEPEND_OBJECT(rb_klass, submat, self); +} + +/* + * call-seq: + * get_cols(col) -> cvmat + * + * Return column(or columns) of matrix. + * argument col should be Fixnum or Range object. + */ +VALUE +rb_get_cols(VALUE self, VALUE col) +{ + int start, end; + rb_get_range_index(col, &start, &end); + CvMat* submat = RB_CVALLOC(CvMat); + try { + cvGetCols(CVARR(self), submat, start, end); + } + catch (cv::Exception& e) { + cvFree(&submat); + raise_cverror(e); + } + + return DEPEND_OBJECT(rb_klass, submat, self); } /* diff --git a/ext/opencv/cvmat.h b/ext/opencv/cvmat.h index 4138454..fae26c6 100644 --- a/ext/opencv/cvmat.h +++ b/ext/opencv/cvmat.h @@ -58,8 +58,8 @@ VALUE rb_square_q(VALUE self); VALUE rb_to_CvMat(VALUE self); VALUE rb_to_IplImage(VALUE self); VALUE rb_sub_rect(VALUE self, VALUE args); -VALUE rb_get_rows(VALUE self, VALUE args); -VALUE rb_get_cols(VALUE self, VALUE args); +VALUE rb_get_rows(int argc, VALUE* argv, VALUE self); +VALUE rb_get_cols(VALUE self, VALUE col); VALUE rb_each_row(VALUE self); VALUE rb_each_col(VALUE self); VALUE rb_diag(int argc, VALUE *argv, VALUE self); diff --git a/test/test_cvmat.rb b/test/test_cvmat.rb index 3cf0973..daa19b8 100755 --- a/test/test_cvmat.rb +++ b/test/test_cvmat.rb @@ -439,48 +439,77 @@ class TestCvMat < OpenCVTestCase end def test_get_rows - m1 = create_cvmat(10, 20) + m1 = create_cvmat(10, 20) { |j, i, c| CvScalar.new(c) } - m2 = m1.get_rows(2) - assert_equal(1, m2.height) - assert_equal(m1.width, m2.width) - m1.width.times { |i| - assert_cvscalar_equal(m1[2, i], m2[i]) + row = 2 + m2 = m1.get_rows(row) + assert_equal(1, m2.rows) + assert_equal(m1.cols, m2.cols) + m1.cols.times { |i| + assert_cvscalar_equal(m1[row, i], m2[i]) } - m2, m3 = m1.get_rows(1, 2) - [m2, m3].each { |m| - assert_equal(1, m.height) - assert_equal(m1.width, m.width) + row1 = 3..7 + row2 = 2...8 + [row1, row2].each { |row| + m3 = m1.get_rows(row) + w = (row.exclude_end?) ? row.last - row.begin : row.last - row.begin + 1 + assert_equal(w, m3.rows) + assert_equal(m1.cols, m3.cols) + + m3.rows.times { |j| + m3.cols.times { |i| + assert_cvscalar_equal(m1[row.begin + j, i], m3[j, i]) + } + } } - m1.width.times { |i| - assert_cvscalar_equal(m1[1, i], m2[i]) - assert_cvscalar_equal(m1[2, i], m3[i]) + + [row1, row2].each { |row| + delta = 2 + m3 = m1.get_rows(row, 2) + w = (((row.exclude_end?) ? row.last - row.begin : row.last - row.begin + 1).to_f / delta).ceil + assert_equal(w, m3.rows) + assert_equal(m1.cols, m3.cols) + + m3.rows.times { |j| + m3.cols.times { |i| + assert_cvscalar_equal(m1[row.begin + j * delta, i], m3[j, i]) + } + } } assert_raise(TypeError) { m1.get_rows(DUMMY_OBJ) } + assert_raise(TypeError) { + m1.get_rows(1, DUMMY_OBJ) + } end def test_get_cols - m1 = create_cvmat(10, 20) + m1 = create_cvmat(10, 20) { |j, i, c| CvScalar.new(c) } - m2 = m1.get_cols(2) - assert_equal(1, m2.width) - assert_equal(m1.height, m2.height) + col = 2 + m2 = m1.get_cols(col) + assert_equal(m1.rows, m2.rows) + assert_equal(1, m2.cols) m1.height.times { |j| - assert_cvscalar_equal(m1[j, 2], m2[j]) + assert_cvscalar_equal(m1[j, col], m2[j]) } - m2, m3 = m1.get_cols(1, 2) - [m2, m3].each { |m| - assert_equal(1, m.width) - assert_equal(m1.height, m.height) - } - m1.height.times { |j| - assert_cvscalar_equal(m1[j, 1], m2[j]) - assert_cvscalar_equal(m1[j, 2], m3[j]) + col1 = 3..7 + col2 = 2...8 + [col1, col2].each { |col| + m3 = m1.get_cols(col) + w = (col.exclude_end?) ? col.last - col.begin : col.last - col.begin + 1 + assert_equal(m1.rows, m3.rows) + assert_equal(w, m3.cols) + + m3.rows.times { |j| + m3.cols.times { |i| + assert_cvscalar_equal(m1[j, col.begin + i], m3[j, i]) + } + } } assert_raise(TypeError) { From 79f0bef6b391d1958a0636e420cbeb4d3dbf8764 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 02:13:37 +0900 Subject: [PATCH 02/15] change utility functions' name REGISTER_CVMETHOD -> REGISTER_HASH LOOKUP_CVMETHOD -> LOOKUP_HASH --- ext/opencv/cvchain.cpp | 8 +- ext/opencv/cvcontour.cpp | 6 +- ext/opencv/cvfont.cpp | 2 +- ext/opencv/cvfont.h | 10 +- ext/opencv/cvhaarclassifiercascade.cpp | 12 +-- ext/opencv/cvmat.cpp | 62 ++++++------ ext/opencv/opencv.cpp | 126 ++++++++++++------------- ext/opencv/opencv.h | 4 +- 8 files changed, 115 insertions(+), 115 deletions(-) diff --git a/ext/opencv/cvchain.cpp b/ext/opencv/cvchain.cpp index 51413b7..19cb1d2 100644 --- a/ext/opencv/cvchain.cpp +++ b/ext/opencv/cvchain.cpp @@ -18,10 +18,10 @@ __NAMESPACE_BEGIN_OPENCV __NAMESPACE_BEGIN_CVCHAIN #define APPROX_CHAIN_OPTION(op) rb_get_option_table(rb_klass, "APPROX_CHAIN_OPTION", op) -#define APPROX_CHAIN_METHOD(op) CVMETHOD("APPROX_CHAIN_METHOD", LOOKUP_CVMETHOD(op, "method"), CV_CHAIN_APPROX_SIMPLE) -#define APPROX_CHAIN_PARAMETER(op) NUM2INT(LOOKUP_CVMETHOD(op, "parameter")) -#define APPROX_CHAIN_MINIMAL_PERIMETER(op) NUM2INT(LOOKUP_CVMETHOD(op, "minimal_perimeter")) -#define APPROX_CHAIN_RECURSIVE(op) TRUE_OR_FALSE(LOOKUP_CVMETHOD(op, "recursive")) +#define APPROX_CHAIN_METHOD(op) CVMETHOD("APPROX_CHAIN_METHOD", LOOKUP_HASH(op, "method"), CV_CHAIN_APPROX_SIMPLE) +#define APPROX_CHAIN_PARAMETER(op) NUM2INT(LOOKUP_HASH(op, "parameter")) +#define APPROX_CHAIN_MINIMAL_PERIMETER(op) NUM2INT(LOOKUP_HASH(op, "minimal_perimeter")) +#define APPROX_CHAIN_RECURSIVE(op) TRUE_OR_FALSE(LOOKUP_HASH(op, "recursive")) VALUE rb_klass; diff --git a/ext/opencv/cvcontour.cpp b/ext/opencv/cvcontour.cpp index b6dfd2b..96d95e0 100644 --- a/ext/opencv/cvcontour.cpp +++ b/ext/opencv/cvcontour.cpp @@ -18,9 +18,9 @@ __NAMESPACE_BEGIN_OPENCV __NAMESPACE_BEGIN_CVCONTOUR #define APPROX_POLY_OPTION(op) rb_get_option_table(rb_klass, "APPROX_OPTION", op) -#define APPROX_POLY_METHOD(op) CVMETHOD("APPROX_POLY_METHOD", LOOKUP_CVMETHOD(op, "method"), CV_POLY_APPROX_DP) -#define APPROX_POLY_ACCURACY(op) NUM2DBL(LOOKUP_CVMETHOD(op, "accuracy")) -#define APPROX_POLY_RECURSIVE(op) TRUE_OR_FALSE(LOOKUP_CVMETHOD(op, "recursive")) +#define APPROX_POLY_METHOD(op) CVMETHOD("APPROX_POLY_METHOD", LOOKUP_HASH(op, "method"), CV_POLY_APPROX_DP) +#define APPROX_POLY_ACCURACY(op) NUM2DBL(LOOKUP_HASH(op, "accuracy")) +#define APPROX_POLY_RECURSIVE(op) TRUE_OR_FALSE(LOOKUP_HASH(op, "recursive")) VALUE rb_allocate(VALUE klass); void cvcontour_free(void *ptr); diff --git a/ext/opencv/cvfont.cpp b/ext/opencv/cvfont.cpp index ef8d937..9c96034 100644 --- a/ext/opencv/cvfont.cpp +++ b/ext/opencv/cvfont.cpp @@ -23,7 +23,7 @@ VALUE rb_klass; int rb_font_option_line_type(VALUE font_option) { - VALUE line_type = LOOKUP_CVMETHOD(font_option, "line_type"); + VALUE line_type = LOOKUP_HASH(font_option, "line_type"); if (FIXNUM_P(line_type)) { return FIX2INT(line_type); } diff --git a/ext/opencv/cvfont.h b/ext/opencv/cvfont.h index 330ab6a..31e34cb 100644 --- a/ext/opencv/cvfont.h +++ b/ext/opencv/cvfont.h @@ -19,11 +19,11 @@ __NAMESPACE_BEGIN_OPENCV __NAMESPACE_BEGIN_CVFONT #define FONT_OPTION(op) rb_get_option_table(rb_klass, "FONT_OPTION", op) -#define FO_ITALIC(op) TRUE_OR_FALSE(LOOKUP_CVMETHOD(op, "italic")) -#define FO_HSCALE(op) NUM2DBL(LOOKUP_CVMETHOD(op, "hscale")) -#define FO_VSCALE(op) NUM2DBL(LOOKUP_CVMETHOD(op, "vscale")) -#define FO_SHEAR(op) NUM2DBL(LOOKUP_CVMETHOD(op, "shear")) -#define FO_THICKNESS(op) NUM2INT(LOOKUP_CVMETHOD(op, "thickness")) +#define FO_ITALIC(op) TRUE_OR_FALSE(LOOKUP_HASH(op, "italic")) +#define FO_HSCALE(op) NUM2DBL(LOOKUP_HASH(op, "hscale")) +#define FO_VSCALE(op) NUM2DBL(LOOKUP_HASH(op, "vscale")) +#define FO_SHEAR(op) NUM2DBL(LOOKUP_HASH(op, "shear")) +#define FO_THICKNESS(op) NUM2INT(LOOKUP_HASH(op, "thickness")) #define FO_LINE_TYPE(op) rb_font_option_line_type(op) VALUE rb_class(); diff --git a/ext/opencv/cvhaarclassifiercascade.cpp b/ext/opencv/cvhaarclassifiercascade.cpp index 1bbb77f..fc2fda0 100644 --- a/ext/opencv/cvhaarclassifiercascade.cpp +++ b/ext/opencv/cvhaarclassifiercascade.cpp @@ -138,14 +138,14 @@ rb_detect_objects(int argc, VALUE *argv, VALUE self) storage_val = cCvMemStorage::new_object(); } else { - scale_factor = IF_DBL(LOOKUP_CVMETHOD(options, "scale_factor"), 1.1); - flags = IF_INT(LOOKUP_CVMETHOD(options, "flags"), 0); - min_neighbors = IF_INT(LOOKUP_CVMETHOD(options, "min_neighbors"), 3); - VALUE min_size_val = LOOKUP_CVMETHOD(options, "min_size"); + scale_factor = IF_DBL(LOOKUP_HASH(options, "scale_factor"), 1.1); + flags = IF_INT(LOOKUP_HASH(options, "flags"), 0); + min_neighbors = IF_INT(LOOKUP_HASH(options, "min_neighbors"), 3); + VALUE min_size_val = LOOKUP_HASH(options, "min_size"); min_size = NIL_P(min_size_val) ? cvSize(0, 0) : VALUE_TO_CVSIZE(min_size_val); - VALUE max_size_val = LOOKUP_CVMETHOD(options, "max_size"); + VALUE max_size_val = LOOKUP_HASH(options, "max_size"); max_size = NIL_P(max_size_val) ? cvSize(0, 0) : VALUE_TO_CVSIZE(max_size_val); - storage_val = CHECK_CVMEMSTORAGE(LOOKUP_CVMETHOD(options, "storage")); + storage_val = CHECK_CVMEMSTORAGE(LOOKUP_HASH(options, "storage")); } VALUE result = Qnil; diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index aafdcf1..e568099 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -46,49 +46,49 @@ __NAMESPACE_BEGIN_OPENCV __NAMESPACE_BEGIN_CVMAT #define DRAWING_OPTION(opt) rb_get_option_table(rb_klass, "DRAWING_OPTION", opt) -#define DO_COLOR(opt) VALUE_TO_CVSCALAR(LOOKUP_CVMETHOD(opt, "color")) -#define DO_THICKNESS(opt) NUM2INT(LOOKUP_CVMETHOD(opt, "thickness")) +#define DO_COLOR(opt) VALUE_TO_CVSCALAR(LOOKUP_HASH(opt, "color")) +#define DO_THICKNESS(opt) NUM2INT(LOOKUP_HASH(opt, "thickness")) #define DO_LINE_TYPE(opt) rb_drawing_option_line_type(opt) -#define DO_SHIFT(opt) NUM2INT(LOOKUP_CVMETHOD(opt, "shift")) -#define DO_IS_CLOSED(opt) TRUE_OR_FALSE(LOOKUP_CVMETHOD(opt, "is_closed")) +#define DO_SHIFT(opt) NUM2INT(LOOKUP_HASH(opt, "shift")) +#define DO_IS_CLOSED(opt) TRUE_OR_FALSE(LOOKUP_HASH(opt, "is_closed")) #define GOOD_FEATURES_TO_TRACK_OPTION(opt) rb_get_option_table(rb_klass, "GOOD_FEATURES_TO_TRACK_OPTION", opt) -#define GF_MAX(opt) NUM2INT(LOOKUP_CVMETHOD(opt, "max")) -#define GF_MASK(opt) MASK(LOOKUP_CVMETHOD(opt, "mask")) -#define GF_BLOCK_SIZE(opt) NUM2INT(LOOKUP_CVMETHOD(opt, "block_size")) -#define GF_USE_HARRIS(opt) TRUE_OR_FALSE(LOOKUP_CVMETHOD(opt, "use_harris")) -#define GF_K(opt) NUM2DBL(LOOKUP_CVMETHOD(opt, "k")) +#define GF_MAX(opt) NUM2INT(LOOKUP_HASH(opt, "max")) +#define GF_MASK(opt) MASK(LOOKUP_HASH(opt, "mask")) +#define GF_BLOCK_SIZE(opt) NUM2INT(LOOKUP_HASH(opt, "block_size")) +#define GF_USE_HARRIS(opt) TRUE_OR_FALSE(LOOKUP_HASH(opt, "use_harris")) +#define GF_K(opt) NUM2DBL(LOOKUP_HASH(opt, "k")) #define FLOOD_FILL_OPTION(opt) rb_get_option_table(rb_klass, "FLOOD_FILL_OPTION", opt) -#define FF_CONNECTIVITY(opt) NUM2INT(LOOKUP_CVMETHOD(opt, "connectivity")) -#define FF_FIXED_RANGE(opt) TRUE_OR_FALSE(LOOKUP_CVMETHOD(opt, "fixed_range")) -#define FF_MASK_ONLY(opt) TRUE_OR_FALSE(LOOKUP_CVMETHOD(opt, "mask_only")) +#define FF_CONNECTIVITY(opt) NUM2INT(LOOKUP_HASH(opt, "connectivity")) +#define FF_FIXED_RANGE(opt) TRUE_OR_FALSE(LOOKUP_HASH(opt, "fixed_range")) +#define FF_MASK_ONLY(opt) TRUE_OR_FALSE(LOOKUP_HASH(opt, "mask_only")) #define FIND_CONTOURS_OPTION(opt) rb_get_option_table(rb_klass, "FIND_CONTOURS_OPTION", opt) -#define FC_MODE(opt) NUM2INT(LOOKUP_CVMETHOD(opt, "mode")) -#define FC_METHOD(opt) NUM2INT(LOOKUP_CVMETHOD(opt, "method")) -#define FC_OFFSET(opt) VALUE_TO_CVPOINT(LOOKUP_CVMETHOD(opt, "offset")) +#define FC_MODE(opt) NUM2INT(LOOKUP_HASH(opt, "mode")) +#define FC_METHOD(opt) NUM2INT(LOOKUP_HASH(opt, "method")) +#define FC_OFFSET(opt) VALUE_TO_CVPOINT(LOOKUP_HASH(opt, "offset")) #define OPTICAL_FLOW_HS_OPTION(opt) rb_get_option_table(rb_klass, "OPTICAL_FLOW_HS_OPTION", opt) -#define HS_LAMBDA(opt) NUM2DBL(LOOKUP_CVMETHOD(opt, "lambda")) -#define HS_CRITERIA(opt) VALUE_TO_CVTERMCRITERIA(LOOKUP_CVMETHOD(opt, "criteria")) +#define HS_LAMBDA(opt) NUM2DBL(LOOKUP_HASH(opt, "lambda")) +#define HS_CRITERIA(opt) VALUE_TO_CVTERMCRITERIA(LOOKUP_HASH(opt, "criteria")) #define OPTICAL_FLOW_BM_OPTION(opt) rb_get_option_table(rb_klass, "OPTICAL_FLOW_BM_OPTION", opt) -#define BM_BLOCK_SIZE(opt) VALUE_TO_CVSIZE(LOOKUP_CVMETHOD(opt, "block_size")) -#define BM_SHIFT_SIZE(opt) VALUE_TO_CVSIZE(LOOKUP_CVMETHOD(opt, "shift_size")) -#define BM_MAX_RANGE(opt) VALUE_TO_CVSIZE(LOOKUP_CVMETHOD(opt, "max_range")) +#define BM_BLOCK_SIZE(opt) VALUE_TO_CVSIZE(LOOKUP_HASH(opt, "block_size")) +#define BM_SHIFT_SIZE(opt) VALUE_TO_CVSIZE(LOOKUP_HASH(opt, "shift_size")) +#define BM_MAX_RANGE(opt) VALUE_TO_CVSIZE(LOOKUP_HASH(opt, "max_range")) #define FIND_FUNDAMENTAL_MAT_OPTION(opt) rb_get_option_table(rb_klass, "FIND_FUNDAMENTAL_MAT_OPTION", opt) -#define FFM_WITH_STATUS(opt) TRUE_OR_FALSE(LOOKUP_CVMETHOD(opt, "with_status")) -#define FFM_MAXIMUM_DISTANCE(opt) NUM2DBL(LOOKUP_CVMETHOD(opt, "maximum_distance")) -#define FFM_DESIRABLE_LEVEL(opt) NUM2DBL(LOOKUP_CVMETHOD(opt, "desirable_level")) +#define FFM_WITH_STATUS(opt) TRUE_OR_FALSE(LOOKUP_HASH(opt, "with_status")) +#define FFM_MAXIMUM_DISTANCE(opt) NUM2DBL(LOOKUP_HASH(opt, "maximum_distance")) +#define FFM_DESIRABLE_LEVEL(opt) NUM2DBL(LOOKUP_HASH(opt, "desirable_level")) VALUE rb_klass; int rb_drawing_option_line_type(VALUE drawing_option) { - VALUE line_type = LOOKUP_CVMETHOD(drawing_option, "line_type"); + VALUE line_type = LOOKUP_HASH(drawing_option, "line_type"); if (FIXNUM_P(line_type)) { return FIX2INT(line_type); } @@ -2676,9 +2676,9 @@ rb_mul_transposed(int argc, VALUE *argv, VALUE self) if (rb_scan_args(argc, argv, "01", &options) > 0) { Check_Type(options, T_HASH); - _delta = LOOKUP_CVMETHOD(options, "delta"); - _scale = LOOKUP_CVMETHOD(options, "scale"); - _order = LOOKUP_CVMETHOD(options, "order"); + _delta = LOOKUP_HASH(options, "delta"); + _scale = LOOKUP_HASH(options, "scale"); + _order = LOOKUP_HASH(options, "order"); } CvArr* delta = NIL_P(_delta) ? NULL : CVARR_WITH_CHECK(_delta); @@ -4582,15 +4582,15 @@ rb_adaptive_threshold(int argc, VALUE *argv, VALUE self) double param1 = 5; if (!NIL_P(options)) { Check_Type(options, T_HASH); - threshold_type = CVMETHOD("THRESHOLD_TYPE", LOOKUP_CVMETHOD(options, "threshold_type"), + threshold_type = CVMETHOD("THRESHOLD_TYPE", LOOKUP_HASH(options, "threshold_type"), CV_THRESH_BINARY); - adaptive_method = CVMETHOD("ADAPTIVE_METHOD", LOOKUP_CVMETHOD(options, "adaptive_method"), + adaptive_method = CVMETHOD("ADAPTIVE_METHOD", LOOKUP_HASH(options, "adaptive_method"), CV_ADAPTIVE_THRESH_MEAN_C); - VALUE _block_size = LOOKUP_CVMETHOD(options, "block_size"); + VALUE _block_size = LOOKUP_HASH(options, "block_size"); if (!NIL_P(_block_size)) { block_size = NUM2INT(_block_size); } - VALUE _param1 = LOOKUP_CVMETHOD(options, "param1"); + VALUE _param1 = LOOKUP_HASH(options, "param1"); if (!NIL_P(_param1)) { param1 = NUM2INT(_param1); } diff --git a/ext/opencv/opencv.cpp b/ext/opencv/opencv.cpp index 643e17a..c19bbf4 100644 --- a/ext/opencv/opencv.cpp +++ b/ext/opencv/opencv.cpp @@ -341,129 +341,129 @@ define_ruby_module() VALUE inversion_method = rb_hash_new(); /* {:lu, :svd, :svd_sym(:svd_symmetric)}: Inversion method */ rb_define_const(rb_module, "INVERSION_METHOD", inversion_method); - REGISTER_CVMETHOD(inversion_method, "lu", CV_LU); - REGISTER_CVMETHOD(inversion_method, "svd", CV_SVD); - REGISTER_CVMETHOD(inversion_method, "svd_sym", CV_SVD_SYM); - REGISTER_CVMETHOD(inversion_method, "svd_symmetric", CV_SVD_SYM); + REGISTER_HASH(inversion_method, "lu", CV_LU); + REGISTER_HASH(inversion_method, "svd", CV_SVD); + REGISTER_HASH(inversion_method, "svd_sym", CV_SVD_SYM); + REGISTER_HASH(inversion_method, "svd_symmetric", CV_SVD_SYM); VALUE homography_calc_method = rb_hash_new(); /* {:all, :ransac, :lmeds}: Methods used to computed homography matrix */ rb_define_const(rb_module, "HOMOGRAPHY_CALC_METHOD", homography_calc_method); - REGISTER_CVMETHOD(homography_calc_method, "all", 0); - REGISTER_CVMETHOD(homography_calc_method, "ransac", CV_RANSAC); - REGISTER_CVMETHOD(homography_calc_method, "lmeds", CV_LMEDS); + REGISTER_HASH(homography_calc_method, "all", 0); + REGISTER_HASH(homography_calc_method, "ransac", CV_RANSAC); + REGISTER_HASH(homography_calc_method, "lmeds", CV_LMEDS); VALUE depth = rb_hash_new(); /* {:cv8u, :cv8s, :cv16u, :cv16s, :cv32s, :cv32f, :cv64f}: Depth of each pixel. */ rb_define_const(rb_module, "DEPTH", depth); - REGISTER_CVMETHOD(depth, "cv8u", CV_8U); - REGISTER_CVMETHOD(depth, "cv8s", CV_8S); - REGISTER_CVMETHOD(depth, "cv16u", CV_16U); - REGISTER_CVMETHOD(depth, "cv16s", CV_16S); - REGISTER_CVMETHOD(depth, "cv32s", CV_32S); - REGISTER_CVMETHOD(depth, "cv32f", CV_32F); - REGISTER_CVMETHOD(depth, "cv64f", CV_64F); + REGISTER_HASH(depth, "cv8u", CV_8U); + REGISTER_HASH(depth, "cv8s", CV_8S); + REGISTER_HASH(depth, "cv16u", CV_16U); + REGISTER_HASH(depth, "cv16s", CV_16S); + REGISTER_HASH(depth, "cv32s", CV_32S); + REGISTER_HASH(depth, "cv32f", CV_32F); + REGISTER_HASH(depth, "cv64f", CV_64F); VALUE connectivity = rb_hash_new(); /* {:aa(:anti_alias)}: Determined by the closeness of pixel values */ rb_define_const(rb_module, "CONNECTIVITY", connectivity); - REGISTER_CVMETHOD(connectivity, "aa", CV_AA); - REGISTER_CVMETHOD(connectivity, "anti_alias", CV_AA); + REGISTER_HASH(connectivity, "aa", CV_AA); + REGISTER_HASH(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); - REGISTER_CVMETHOD(structuring_element_shape, "rect", CV_SHAPE_RECT); - REGISTER_CVMETHOD(structuring_element_shape, "cross", CV_SHAPE_CROSS); - REGISTER_CVMETHOD(structuring_element_shape, "ellipse", CV_SHAPE_ELLIPSE); - REGISTER_CVMETHOD(structuring_element_shape, "custom", CV_SHAPE_CUSTOM); + REGISTER_HASH(structuring_element_shape, "rect", CV_SHAPE_RECT); + REGISTER_HASH(structuring_element_shape, "cross", CV_SHAPE_CROSS); + REGISTER_HASH(structuring_element_shape, "ellipse", CV_SHAPE_ELLIPSE); + REGISTER_HASH(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); - REGISTER_CVMETHOD(retrieval_mode, "external", CV_RETR_EXTERNAL); - REGISTER_CVMETHOD(retrieval_mode, "list", CV_RETR_LIST); - REGISTER_CVMETHOD(retrieval_mode, "ccomp", CV_RETR_CCOMP); - REGISTER_CVMETHOD(retrieval_mode, "tree", CV_RETR_TREE); + REGISTER_HASH(retrieval_mode, "external", CV_RETR_EXTERNAL); + REGISTER_HASH(retrieval_mode, "list", CV_RETR_LIST); + REGISTER_HASH(retrieval_mode, "ccomp", CV_RETR_CCOMP); + REGISTER_HASH(retrieval_mode, "tree", CV_RETR_TREE); VALUE approx_chain_method = rb_hash_new(); /* {:code, :approx_none, :approx_simple, :apporx_tc89_11, :approx_tc89_kcos}: Approximation method */ rb_define_const(rb_module, "APPROX_CHAIN_METHOD", approx_chain_method); - REGISTER_CVMETHOD(approx_chain_method, "code", CV_CHAIN_CODE); - REGISTER_CVMETHOD(approx_chain_method, "approx_none", CV_CHAIN_APPROX_NONE); - REGISTER_CVMETHOD(approx_chain_method, "approx_simple", CV_CHAIN_APPROX_SIMPLE); - REGISTER_CVMETHOD(approx_chain_method, "approx_tc89_l1", CV_CHAIN_APPROX_TC89_L1); - REGISTER_CVMETHOD(approx_chain_method, "approx_tc89_kcos", CV_CHAIN_APPROX_TC89_KCOS); + REGISTER_HASH(approx_chain_method, "code", CV_CHAIN_CODE); + REGISTER_HASH(approx_chain_method, "approx_none", CV_CHAIN_APPROX_NONE); + REGISTER_HASH(approx_chain_method, "approx_simple", CV_CHAIN_APPROX_SIMPLE); + REGISTER_HASH(approx_chain_method, "approx_tc89_l1", CV_CHAIN_APPROX_TC89_L1); + REGISTER_HASH(approx_chain_method, "approx_tc89_kcos", CV_CHAIN_APPROX_TC89_KCOS); VALUE approx_poly_method = rb_hash_new(); /* {:dp}: Approximation method (polygon) */ rb_define_const(rb_module, "APPROX_POLY_METHOD", approx_poly_method); - REGISTER_CVMETHOD(approx_poly_method, "dp", CV_POLY_APPROX_DP); + REGISTER_HASH(approx_poly_method, "dp", CV_POLY_APPROX_DP); VALUE match_template_method = rb_hash_new(); /* {:sqdiff, :sqdiff_normed, :ccorr, :ccorr_normed, :ccoeff, :ccoeff_normed}: Match template method */ rb_define_const(rb_module, "MATCH_TEMPLATE_METHOD", match_template_method); - REGISTER_CVMETHOD(match_template_method, "sqdiff", CV_TM_SQDIFF); - REGISTER_CVMETHOD(match_template_method, "sqdiff_normed", CV_TM_SQDIFF_NORMED); - REGISTER_CVMETHOD(match_template_method, "ccorr", CV_TM_CCORR); - REGISTER_CVMETHOD(match_template_method, "ccorr_normed", CV_TM_CCORR_NORMED); - REGISTER_CVMETHOD(match_template_method, "ccoeff", CV_TM_CCOEFF); - REGISTER_CVMETHOD(match_template_method, "ccoeff_normed", CV_TM_CCOEFF_NORMED); + REGISTER_HASH(match_template_method, "sqdiff", CV_TM_SQDIFF); + REGISTER_HASH(match_template_method, "sqdiff_normed", CV_TM_SQDIFF_NORMED); + REGISTER_HASH(match_template_method, "ccorr", CV_TM_CCORR); + REGISTER_HASH(match_template_method, "ccorr_normed", CV_TM_CCORR_NORMED); + REGISTER_HASH(match_template_method, "ccoeff", CV_TM_CCOEFF); + REGISTER_HASH(match_template_method, "ccoeff_normed", CV_TM_CCOEFF_NORMED); VALUE morphological_operation = rb_hash_new(); /* {:open, :close, :gradient, :tophat, :blackhat}: Types of morphological operations */ rb_define_const(rb_module, "MORPHOLOGICAL_OPERATION", morphological_operation); - REGISTER_CVMETHOD(morphological_operation, "open", CV_MOP_OPEN); - REGISTER_CVMETHOD(morphological_operation, "close", CV_MOP_CLOSE); - REGISTER_CVMETHOD(morphological_operation, "gradient", CV_MOP_GRADIENT); - REGISTER_CVMETHOD(morphological_operation, "tophat", CV_MOP_TOPHAT); - REGISTER_CVMETHOD(morphological_operation, "blackhat", CV_MOP_BLACKHAT); + REGISTER_HASH(morphological_operation, "open", CV_MOP_OPEN); + REGISTER_HASH(morphological_operation, "close", CV_MOP_CLOSE); + REGISTER_HASH(morphological_operation, "gradient", CV_MOP_GRADIENT); + REGISTER_HASH(morphological_operation, "tophat", CV_MOP_TOPHAT); + REGISTER_HASH(morphological_operation, "blackhat", CV_MOP_BLACKHAT); VALUE smoothing_type = rb_hash_new(); /* {:blur_no_scale, :blur, :gaussian, :median, :bilateral}: Types of smoothing */ rb_define_const(rb_module, "SMOOTHING_TYPE", smoothing_type); - REGISTER_CVMETHOD(smoothing_type, "blur_no_scale", CV_BLUR_NO_SCALE); - REGISTER_CVMETHOD(smoothing_type, "blur", CV_BLUR); - REGISTER_CVMETHOD(smoothing_type, "gaussian", CV_GAUSSIAN); - REGISTER_CVMETHOD(smoothing_type, "median", CV_MEDIAN); - REGISTER_CVMETHOD(smoothing_type, "bilateral", CV_BILATERAL); + REGISTER_HASH(smoothing_type, "blur_no_scale", CV_BLUR_NO_SCALE); + REGISTER_HASH(smoothing_type, "blur", CV_BLUR); + REGISTER_HASH(smoothing_type, "gaussian", CV_GAUSSIAN); + REGISTER_HASH(smoothing_type, "median", CV_MEDIAN); + REGISTER_HASH(smoothing_type, "bilateral", CV_BILATERAL); VALUE adaptive_method = rb_hash_new(); /* {:mean_c, :gaussian_c}: Adaptive thresholding algorithm */ rb_define_const(rb_module, "ADAPTIVE_METHOD", adaptive_method); - REGISTER_CVMETHOD(adaptive_method, "mean_c", CV_ADAPTIVE_THRESH_MEAN_C); - REGISTER_CVMETHOD(adaptive_method, "gaussian_c", CV_ADAPTIVE_THRESH_GAUSSIAN_C); + REGISTER_HASH(adaptive_method, "mean_c", CV_ADAPTIVE_THRESH_MEAN_C); + REGISTER_HASH(adaptive_method, "gaussian_c", CV_ADAPTIVE_THRESH_GAUSSIAN_C); VALUE threshold_type = rb_hash_new(); /* {:binary, :binary_inv, :trunc, :tozero, :tozero_inv, :otsu} : Thresholding types */ rb_define_const(rb_module, "THRESHOLD_TYPE", threshold_type); - REGISTER_CVMETHOD(threshold_type, "binary", CV_THRESH_BINARY); - REGISTER_CVMETHOD(threshold_type, "binary_inv", CV_THRESH_BINARY_INV); - REGISTER_CVMETHOD(threshold_type, "trunc", CV_THRESH_TRUNC); - REGISTER_CVMETHOD(threshold_type, "tozero", CV_THRESH_TOZERO); - REGISTER_CVMETHOD(threshold_type, "tozero_inv", CV_THRESH_TOZERO_INV); - REGISTER_CVMETHOD(threshold_type, "otsu", CV_THRESH_OTSU); + REGISTER_HASH(threshold_type, "binary", CV_THRESH_BINARY); + REGISTER_HASH(threshold_type, "binary_inv", CV_THRESH_BINARY_INV); + REGISTER_HASH(threshold_type, "trunc", CV_THRESH_TRUNC); + REGISTER_HASH(threshold_type, "tozero", CV_THRESH_TOZERO); + REGISTER_HASH(threshold_type, "tozero_inv", CV_THRESH_TOZERO_INV); + REGISTER_HASH(threshold_type, "otsu", CV_THRESH_OTSU); VALUE hough_transform_method = rb_hash_new(); /* {:standard, :probabilistic, :multi_scale} : Hough transform method */ rb_define_const(rb_module, "HOUGH_TRANSFORM_METHOD", hough_transform_method); - REGISTER_CVMETHOD(hough_transform_method, "standard", CV_HOUGH_STANDARD); - REGISTER_CVMETHOD(hough_transform_method, "probabilistic", CV_HOUGH_PROBABILISTIC); - REGISTER_CVMETHOD(hough_transform_method, "multi_scale", CV_HOUGH_MULTI_SCALE); - REGISTER_CVMETHOD(hough_transform_method, "gradient", CV_HOUGH_GRADIENT); + REGISTER_HASH(hough_transform_method, "standard", CV_HOUGH_STANDARD); + REGISTER_HASH(hough_transform_method, "probabilistic", CV_HOUGH_PROBABILISTIC); + REGISTER_HASH(hough_transform_method, "multi_scale", CV_HOUGH_MULTI_SCALE); + REGISTER_HASH(hough_transform_method, "gradient", CV_HOUGH_GRADIENT); VALUE inpaint_method = rb_hash_new(); /* {:ns, :telea} : Inpaint method */ rb_define_const(rb_module, "INPAINT_METHOD", inpaint_method); - REGISTER_CVMETHOD(inpaint_method, "ns", CV_INPAINT_NS); - REGISTER_CVMETHOD(inpaint_method, "telea", CV_INPAINT_TELEA); + REGISTER_HASH(inpaint_method, "ns", CV_INPAINT_NS); + REGISTER_HASH(inpaint_method, "telea", CV_INPAINT_TELEA); VALUE comparison_method = rb_hash_new(); /* Comparison method */ rb_define_const(rb_module, "COMPARISON_METHOD", comparison_method); - REGISTER_CVMETHOD(comparison_method, "i1", CV_CONTOURS_MATCH_I1); - REGISTER_CVMETHOD(comparison_method, "i2", CV_CONTOURS_MATCH_I2); - REGISTER_CVMETHOD(comparison_method, "i3", CV_CONTOURS_MATCH_I3); + REGISTER_HASH(comparison_method, "i1", CV_CONTOURS_MATCH_I1); + REGISTER_HASH(comparison_method, "i2", CV_CONTOURS_MATCH_I2); + REGISTER_HASH(comparison_method, "i3", CV_CONTOURS_MATCH_I3); /* color convert methods */ rb_define_module_function(rb_module, "BGR2BGRA", RUBY_METHOD_FUNC(rb_BGR2BGRA), 1); diff --git a/ext/opencv/opencv.h b/ext/opencv/opencv.h index 18c283a..5c3358e 100644 --- a/ext/opencv/opencv.h +++ b/ext/opencv/opencv.h @@ -147,8 +147,8 @@ extern "C" { #define IF_DEPTH(val, ifnone) NIL_P(val) ? ifnone : NUM2INT(val) -#define REGISTER_CVMETHOD(hash, str, value) rb_hash_aset(hash, ID2SYM(rb_intern(str)), INT2FIX(value)) -#define LOOKUP_CVMETHOD(hash, key_as_cstr) (rb_hash_lookup(hash, ID2SYM(rb_intern(key_as_cstr)))) +#define REGISTER_HASH(hash, str, value) rb_hash_aset(hash, ID2SYM(rb_intern(str)), INT2FIX(value)) +#define LOOKUP_HASH(hash, key_as_cstr) (rb_hash_lookup(hash, ID2SYM(rb_intern(key_as_cstr)))) #define maxint(a,b) ({int _a = (a), _b = (b); _a > _b ? _a : _b; }) From 6331e8dc53ccec72b87c2e7d5dfafe424412d4fd Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 02:37:19 +0900 Subject: [PATCH 03/15] replace rb_hash_aref to rb_hash_lookup (or LOOKUP_HASH) --- ext/opencv/cvcapture.cpp | 2 +- ext/opencv/cvfont.cpp | 2 +- ext/opencv/cvmat.cpp | 20 ++++++++++---------- ext/opencv/opencv.h | 2 +- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/ext/opencv/cvcapture.cpp b/ext/opencv/cvcapture.cpp index dd11630..7fafaf6 100644 --- a/ext/opencv/cvcapture.cpp +++ b/ext/opencv/cvcapture.cpp @@ -132,7 +132,7 @@ rb_open(int argc, VALUE *argv, VALUE self) capture = cvCaptureFromCAM(FIX2INT(device)); break; case T_SYMBOL: { - VALUE cap_index = rb_hash_aref(rb_const_get(rb_class(), rb_intern("INTERFACE")), device); + VALUE cap_index = rb_hash_lookup(rb_const_get(rb_class(), rb_intern("INTERFACE")), device); if (NIL_P(cap_index)) rb_raise(rb_eArgError, "undefined interface."); capture = cvCaptureFromCAM(NUM2INT(cap_index)); diff --git a/ext/opencv/cvfont.cpp b/ext/opencv/cvfont.cpp index 9c96034..ca3a428 100644 --- a/ext/opencv/cvfont.cpp +++ b/ext/opencv/cvfont.cpp @@ -135,7 +135,7 @@ rb_initialize(int argc, VALUE *argv, VALUE self) VALUE face, font_option; rb_scan_args(argc, argv, "11", &face, &font_option); Check_Type(face, T_SYMBOL); - face = rb_hash_aref(rb_const_get(cCvFont::rb_class(), rb_intern("FACE")), face); + face = rb_hash_lookup(rb_const_get(cCvFont::rb_class(), rb_intern("FACE")), face); if (NIL_P(face)) { rb_raise(rb_eArgError, "undefined face."); } diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index e568099..997cdb4 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -753,8 +753,8 @@ rb_height(VALUE self) VALUE rb_depth(VALUE self) { - return rb_hash_aref(rb_funcall(rb_const_get(rb_module_opencv(), rb_intern("DEPTH")), rb_intern("invert"), 0), - INT2FIX(CV_MAT_DEPTH(CVMAT(self)->type))); + return rb_hash_lookup(rb_funcall(rb_const_get(rb_module_opencv(), rb_intern("DEPTH")), rb_intern("invert"), 0), + INT2FIX(CV_MAT_DEPTH(CVMAT(self)->type))); } /* @@ -1612,8 +1612,8 @@ VALUE rb_reshape(VALUE self, VALUE hash) { Check_Type(hash, T_HASH); - VALUE channel = rb_hash_aref(hash, ID2SYM(rb_intern("channel"))); - VALUE rows = rb_hash_aref(hash, ID2SYM(rb_intern("rows"))); + VALUE channel = LOOKUP_HASH(hash, "channel"); + VALUE rows = LOOKUP_HASH(hash, "rows"); CvMat *mat = NULL; try { mat = cvReshape(CVARR(self), RB_CVALLOC(CvMat), NIL_P(channel) ? 0 : NUM2INT(channel), @@ -1869,9 +1869,9 @@ rb_convert_scale(VALUE self, VALUE hash) { Check_Type(hash, T_HASH); CvMat* self_ptr = CVMAT(self); - VALUE depth = rb_hash_aref(hash, ID2SYM(rb_intern("depth"))), - scale = rb_hash_aref(hash, ID2SYM(rb_intern("scale"))), - shift = rb_hash_aref(hash, ID2SYM(rb_intern("shift"))); + VALUE depth = LOOKUP_HASH(hash, "depth"); + VALUE scale = LOOKUP_HASH(hash, "scale"); + VALUE shift = LOOKUP_HASH(hash, "shift"); VALUE dest = Qnil; try { @@ -1899,8 +1899,8 @@ rb_convert_scale_abs(VALUE self, VALUE hash) { Check_Type(hash, T_HASH); CvMat* self_ptr = CVMAT(self); - VALUE scale = rb_hash_aref(hash, ID2SYM(rb_intern("scale"))), - shift = rb_hash_aref(hash, ID2SYM(rb_intern("shift"))); + VALUE scale = LOOKUP_HASH(hash, "scale"); + VALUE shift = LOOKUP_HASH(hash, "shift"); VALUE dest = Qnil; try { dest = new_mat_kind_object(cvGetSize(self_ptr), self, CV_8U, CV_MAT_CN(CVMAT(self)->type)); @@ -5407,7 +5407,7 @@ rb_match_descriptors(int argc, VALUE *argv, VALUE self) VALUE _matches = rb_hash_new(); for (size_t i=0; i Date: Sun, 20 Jan 2013 02:53:24 +0900 Subject: [PATCH 04/15] move pyr_segmentation to IplImage --- ext/opencv/cvmat.cpp | 37 ----------------------------- ext/opencv/cvmat.h | 1 - ext/opencv/iplimage.cpp | 38 +++++++++++++++++++++++++++++- ext/opencv/iplimage.h | 2 ++ test/test_cvmat_imageprocessing.rb | 38 ------------------------------ test/test_iplimage.rb | 33 ++++++++++++++++++++++++++ 6 files changed, 72 insertions(+), 77 deletions(-) diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index 997cdb4..5287e4f 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -381,7 +381,6 @@ void define_ruby_class() rb_define_method(rb_klass, "draw_contours!", RUBY_METHOD_FUNC(rb_draw_contours_bang), -1); rb_define_method(rb_klass, "draw_chessboard_corners", RUBY_METHOD_FUNC(rb_draw_chessboard_corners), 3); rb_define_method(rb_klass, "draw_chessboard_corners!", RUBY_METHOD_FUNC(rb_draw_chessboard_corners_bang), 3); - rb_define_method(rb_klass, "pyr_segmentation", RUBY_METHOD_FUNC(rb_pyr_segmentation), 3); rb_define_method(rb_klass, "pyr_mean_shift_filtering", RUBY_METHOD_FUNC(rb_pyr_mean_shift_filtering), -1); rb_define_method(rb_klass, "watershed", RUBY_METHOD_FUNC(rb_watershed), 1); @@ -4959,42 +4958,6 @@ rb_draw_chessboard_corners_bang(VALUE self, VALUE pattern_size, VALUE corners, V return self; } -/* - * call-seq: - * pyr_segmentation(level, threshold1, threshold2) -> [cvmat, cvseq(include cvconnectedcomp)] - * - * Does image segmentation by pyramids. - * The pyramid builds up to the level level. - * The links between any pixel a on leveli and - * its candidate father pixel b on the adjacent level are established if - * p(c(a),c(b)) < threshold1. After the connected components are defined, they are joined into several clusters. Any two segments A and B belong to the same cluster, if - * p(c(A),c(B)) < threshold2. The input image has only one channel, then - * p(c^2,c^2)=|c^2-c^2|. If the input image has three channels (red, green and blue), then - * p(c^2,c^2)=0,3*(c^2 r-c^2 r)+0.59*(c^2 g-c^2 g)+0,11*(c^2 b-c^2 b) . There may be more than one connected component per a cluster. - * - * Return segmented image and sequence of connected components. - * support single-channel or 3-channel 8bit unsigned image only - */ -VALUE -rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2) -{ - IplImage* self_ptr = IPLIMAGE(self); - CvSeq *comp = NULL; - VALUE storage = cCvMemStorage::new_object(); - VALUE dest = Qnil; - try { - dest = cIplImage::new_object(cvGetSize(self_ptr), cvGetElemType(self_ptr)); - cvPyrSegmentation(self_ptr, IPLIMAGE(dest), CVMEMSTORAGE(storage), &comp, - NUM2INT(level), NUM2DBL(threshold1), NUM2DBL(threshold2)); - } - catch (cv::Exception& e) { - raise_cverror(e); - } - if (!comp) - comp = cvCreateSeq(CV_SEQ_CONNECTED_COMP, sizeof(CvSeq), sizeof(CvConnectedComp), CVMEMSTORAGE(storage)); - return rb_ary_new3(2, dest, cCvSeq::new_sequence(cCvSeq::rb_class(), comp, cCvConnectedComp::rb_class(), storage)); -} - /* * call-seq: * pyr_mean_shift_filtering(sp, sr[,max_level = 1][termcrit = CvTermCriteria.new(5,1)]) -> cvmat diff --git a/ext/opencv/cvmat.h b/ext/opencv/cvmat.h index fae26c6..0878d2b 100644 --- a/ext/opencv/cvmat.h +++ b/ext/opencv/cvmat.h @@ -204,7 +204,6 @@ VALUE rb_draw_contours(int argc, VALUE *argv, VALUE self); VALUE rb_draw_contours_bang(int argc, VALUE *argv, VALUE self); VALUE rb_draw_chessboard_corners(VALUE self, VALUE pattern_size, VALUE corners, VALUE pattern_was_found); VALUE rb_draw_chessboard_corners_bang(VALUE self, VALUE pattern_size, VALUE corners, VALUE pattern_was_found); -VALUE rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2); VALUE rb_pyr_mean_shift_filtering(int argc, VALUE *argv, VALUE self); VALUE rb_watershed(VALUE self, VALUE markers); VALUE rb_moments(int argc, VALUE *argv, VALUE self); diff --git a/ext/opencv/iplimage.cpp b/ext/opencv/iplimage.cpp index cf1d60e..baccc66 100644 --- a/ext/opencv/iplimage.cpp +++ b/ext/opencv/iplimage.cpp @@ -60,7 +60,7 @@ define_ruby_class() rb_define_method(rb_klass, "set_coi", RUBY_METHOD_FUNC(rb_set_coi), 1); rb_define_alias(rb_klass, "coi=", "set_coi"); rb_define_method(rb_klass, "reset_coi", RUBY_METHOD_FUNC(rb_reset_coi), 0); - + rb_define_method(rb_klass, "pyr_segmentation", RUBY_METHOD_FUNC(rb_pyr_segmentation), 3); rb_define_method(rb_klass, "smoothness", RUBY_METHOD_FUNC(rb_smoothness), -1); rb_define_singleton_method(rb_klass, "decode_image", RUBY_METHOD_FUNC(rb_decode_image), -1); @@ -591,6 +591,42 @@ high_pass_range(const IplImage *pImage, float lostPercentage, int &outLow, int & outLow = (int)(lostPercentage * outHigh); } +/* + * call-seq: + * pyr_segmentation(level, threshold1, threshold2) -> [iplimage, cvseq(include cvconnectedcomp)] + * + * Does image segmentation by pyramids. + * The pyramid builds up to the level level. + * The links between any pixel a on leveli and + * its candidate father pixel b on the adjacent level are established if + * p(c(a),c(b)) < threshold1. After the connected components are defined, they are joined into several clusters. Any two segments A and B belong to the same cluster, if + * p(c(A),c(B)) < threshold2. The input image has only one channel, then + * p(c^2,c^2)=|c^2-c^2|. If the input image has three channels (red, green and blue), then + * p(c^2,c^2)=0,3*(c^2 r-c^2 r)+0.59*(c^2 g-c^2 g)+0,11*(c^2 b-c^2 b) . There may be more than one connected component per a cluster. + * + * Return segmented image and sequence of connected components. + * support single-channel or 3-channel 8bit unsigned image only + */ +VALUE +rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2) +{ + IplImage* self_ptr = IPLIMAGE(self); + CvSeq *comp = NULL; + VALUE storage = cCvMemStorage::new_object(); + VALUE dest = Qnil; + try { + dest = cIplImage::new_object(cvGetSize(self_ptr), cvGetElemType(self_ptr)); + cvPyrSegmentation(self_ptr, IPLIMAGE(dest), CVMEMSTORAGE(storage), &comp, + NUM2INT(level), NUM2DBL(threshold1), NUM2DBL(threshold2)); + } + catch (cv::Exception& e) { + raise_cverror(e); + } + if (!comp) { + comp = cvCreateSeq(CV_SEQ_CONNECTED_COMP, sizeof(CvSeq), sizeof(CvConnectedComp), CVMEMSTORAGE(storage)); + } + return rb_ary_new3(2, dest, cCvSeq::new_sequence(cCvSeq::rb_class(), comp, cCvConnectedComp::rb_class(), storage)); +} VALUE new_object(int width, int height, int type) diff --git a/ext/opencv/iplimage.h b/ext/opencv/iplimage.h index b564794..d1c53fd 100644 --- a/ext/opencv/iplimage.h +++ b/ext/opencv/iplimage.h @@ -38,6 +38,8 @@ VALUE rb_get_coi(VALUE self); VALUE rb_set_coi(VALUE self, VALUE coi); VALUE rb_reset_coi(VALUE self); +VALUE rb_pyr_segmentation(VALUE self, VALUE level, VALUE threshold1, VALUE threshold2); + VALUE rb_smoothness(int argc, VALUE *argv, VALUE self); typedef enum { SMOOTH = 1, BLANK = 2, MESSY = 3 } Smoothness; Smoothness compute_smoothness(const IplImage *pFourierImage, const double lowFreqRatio, const double blankDensity, const double messyDensity, const double highFreqRatio, double &outLowDensity, double &outHighDensity); diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 3844a29..0f78c11 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1316,44 +1316,6 @@ class TestCvMat_imageprocessing < OpenCVTestCase } end - def test_pyr_segmentation - mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) - mat1, seq1 = mat0.pyr_segmentation(4, 255, 50) - assert_equal('ebd9bad0bbc90b1d4a25289b7d59c958', hash_img(mat1)) - assert_equal(5, seq1.total) - - img0 = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) - img0.set_roi(CvRect.new(0, 0, 256, 512)) - img2, seq2 = img0.pyr_segmentation(2, 255, 50) - assert_equal('963b26f51b14f175fbbf128e9b9e979f', hash_img(img2)) - assert_equal(11, seq2.total) - - assert_raise(CvStsAssert) { - img0.pyr_segmentation(-1, 255, 50) - } - assert_raise(CvStsAssert) { - img0.pyr_segmentation(1000, 255, 50) - } - assert_raise(CvStsAssert) { - img0.pyr_segmentation(4, -1, 50) - } - assert_raise(CvStsAssert) { - img0.pyr_segmentation(4, 255, -1) - } - assert_raise(TypeError) { - img0.pyr_segmentation(DUMMY_OBJ, 255, 50) - } - assert_raise(TypeError) { - img0.pyr_segmentation(4, DUMMY_OBJ, 50) - } - assert_raise(TypeError) { - img0.pyr_segmentation(4, 255, DUMMY_OBJ) - } - assert_raise(CvBadDepth) { - IplImage.new(10, 10, :cv32f, 2).pyr_segmentation(4, 255, 50) - } - end - def test_pyr_mean_shift_filtering mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) mat1 = mat0.pyr_mean_shift_filtering(30, 30) diff --git a/test/test_iplimage.rb b/test/test_iplimage.rb index c6eb3ee..0228709 100755 --- a/test/test_iplimage.rb +++ b/test/test_iplimage.rb @@ -198,6 +198,39 @@ class TestIplImage < OpenCVTestCase should_classify_images_as image, :blank end end + + def test_pyr_segmentation + img0 = IplImage.load(FILENAME_CAT, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) + img0.set_roi(CvRect.new(0, 0, 256, 512)) + img1, seq1 = img0.pyr_segmentation(2, 255, 50) + assert_equal('963b26f51b14f175fbbf128e9b9e979f', hash_img(img1)) + assert_equal(11, seq1.total) + + assert_raise(CvStsAssert) { + img0.pyr_segmentation(-1, 255, 50) + } + assert_raise(CvStsAssert) { + img0.pyr_segmentation(1000, 255, 50) + } + assert_raise(CvStsAssert) { + img0.pyr_segmentation(4, -1, 50) + } + assert_raise(CvStsAssert) { + img0.pyr_segmentation(4, 255, -1) + } + assert_raise(TypeError) { + img0.pyr_segmentation(DUMMY_OBJ, 255, 50) + } + assert_raise(TypeError) { + img0.pyr_segmentation(4, DUMMY_OBJ, 50) + } + assert_raise(TypeError) { + img0.pyr_segmentation(4, 255, DUMMY_OBJ) + } + assert_raise(CvBadDepth) { + IplImage.new(10, 10, :cv32f, 2).pyr_segmentation(4, 255, 50) + } + end end From d68e7ddf1b2ec6edff7f2f5431c578de7493b329 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 03:12:17 +0900 Subject: [PATCH 05/15] update CvMat#moments --- ext/opencv/cvmat.cpp | 12 ++++-------- test/test_cvmat_imageprocessing.rb | 8 ++------ 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index 5287e4f..7a6ab8e 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -5033,7 +5033,7 @@ rb_watershed(VALUE self, VALUE markers) /* * call-seq: - * moments -> array(include CvMoments) + * moments -> cvmoments * * Calculates moments. */ @@ -5042,14 +5042,10 @@ rb_moments(int argc, VALUE *argv, VALUE self) { VALUE is_binary; rb_scan_args(argc, argv, "01", &is_binary); - IplImage image = *IPLIMAGE(self); - VALUE moments = rb_ary_new(); + CvArr *self_ptr = CVARR(self); + VALUE moments = Qnil; try { - int cn = CV_MAT_CN(cvGetElemType(CVARR(self))); - for (int i = 1; i <= cn; ++i) { - cvSetImageCOI(&image, i); - rb_ary_push(moments, cCvMoments::new_object(&image, TRUE_OR_FALSE(is_binary, 0))); - } + moments = cCvMoments::new_object(self_ptr, TRUE_OR_FALSE(is_binary, 0)); } catch (cv::Exception& e) { raise_cverror(e); diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 0f78c11..51b46bd 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1367,9 +1367,7 @@ class TestCvMat_imageprocessing < OpenCVTestCase moments2 = mat.moments(false) moments3 = mat.moments(true) - [moments1, moments2].each { |moments| - assert_equal(1, moments.size) - m = moments[0] + [moments1, moments2].each { |m| assert_in_delta(2221056, m.spatial(0, 0), 0.1) assert_in_delta(2221056, m.central(0, 0), 0.1) assert_in_delta(1, m.normalized_central(0, 0), 0.1) @@ -1408,9 +1406,7 @@ class TestCvMat_imageprocessing < OpenCVTestCase assert_in_delta(0.000671, m.inv_sqrt_m00, 0.000001) } - assert_equal(1, moments3.size) - m = moments3[0] - + m = moments3 assert_in_delta(10240, m.spatial(0, 0), 0.1) assert_in_delta(10240, m.central(0, 0), 0.1) assert_in_delta(1, m.normalized_central(0, 0), 0.1) From 7fe82c5cc80471c94fcd3c691af828a7e5b9ab48 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 03:35:00 +0900 Subject: [PATCH 06/15] update tests of CvMat#corner_harris --- test/helper.rb | 9 +++++++++ test/test_cvmat_imageprocessing.rb | 12 ++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index da58720..6704d72 100755 --- a/test/helper.rb +++ b/test/helper.rb @@ -143,5 +143,14 @@ class OpenCVTestCase < Test::Unit::TestCase } puts s.join("\n") end + + def count_threshold(mat, threshold, &block) + n = 0 + block = lambda { |a, b| a > b } unless block_given? + (mat.rows * mat.cols).times { |i| + n += 1 if block.call(mat[i][0], threshold) + } + n + end end diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 51b46bd..05e7352 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -165,10 +165,14 @@ class TestCvMat_imageprocessing < OpenCVTestCase mat3 = mat0.corner_harris(3, 3, 0.04) mat4 = mat0.corner_harris(3, 7, 0.01) - assert_equal('fbb4e04c86f906c83fe17fd148675f90', hash_img(mat1)) - assert_equal('fbb4e04c86f906c83fe17fd148675f90', hash_img(mat2)) - assert_equal('fbb4e04c86f906c83fe17fd148675f90', hash_img(mat3)) - assert_equal('6515d75f6223806f077cebc7b3927a13', hash_img(mat4)) + [mat1, mat2, mat3].each { |mat| + assert_equal(mat0.rows, mat.rows) + assert_equal(mat0.cols, mat.cols) + assert_in_delta(0, count_threshold(mat, 10), 10) + } + assert_equal(mat0.rows, mat4.rows) + assert_equal(mat0.cols, mat4.cols) + assert_in_delta(90, count_threshold(mat4, 10), 10) # Uncomment the following lines to show the images # snap(['original', mat0], ['corner_harris(3)', mat1], ['corner_harris(3,3)', mat2], From 7d224ce817a13eb5b80a4d90ff3fe93490c19467 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 03:40:11 +0900 Subject: [PATCH 07/15] update tests of CvMat#inpaint --- test/test_cvmat_imageprocessing.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 05e7352..7fbb148 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1556,11 +1556,11 @@ class TestCvMat_imageprocessing < OpenCVTestCase [CV_INPAINT_NS, :ns].each { |method| result_ns = mat.inpaint(method, mask, 10) - assert_equal('d3df4dda8642c83512fb417ffa5e1457', hash_img(result_ns)) + assert_in_delta(14000, count_threshold(result_ns, 128), 1000) } [CV_INPAINT_TELEA, :telea].each { |method| result_telea = mat.inpaint(method, mask, 10) - assert_equal('d45bec22d03067578703f2ec68567167', hash_img(result_telea)) + assert_in_delta(13500, count_threshold(result_telea, 128), 1000) } # Uncomment the following lines to show the results From 5b67b19d2b108f9da982e72fc07d601b615583d7 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 03:56:06 +0900 Subject: [PATCH 08/15] update tests of CvMat#optical_flow_* --- test/test_cvmat_imageprocessing.rb | 54 +++++++++++++++--------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 7fbb148..e0657be 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1798,30 +1798,30 @@ class TestCvMat_imageprocessing < OpenCVTestCase [curr.optical_flow_hs(prev, nil, nil, :lambda => 0.0005, :criteria => CvTermCriteria.new(1, 0.001)), curr.optical_flow_hs(prev)].each { |velx, vely| - assert_equal('d437cd896365c509b5d16fd5f2d7e498', hash_img(velx)) - assert_equal('36ccbcafcd58b4d07dab058fb60eede6', hash_img(vely)) + assert_in_delta(60, count_threshold(velx, 1), 20) + assert_in_delta(50, count_threshold(vely, 1), 20) } velx, vely = curr.optical_flow_hs(prev, nil, nil, :lambda => 0.001) - assert_equal('6a0133bcfa5057ef3d607429753d1f27', hash_img(velx)) - assert_equal('dffbc0e267b08f3ca0098f27ecf61f6e', hash_img(vely)) + assert_in_delta(60, count_threshold(velx, 1), 20) + assert_in_delta(50, count_threshold(vely, 1), 20) velx, vely = curr.optical_flow_hs(prev, nil, nil, :criteria => CvTermCriteria.new(10, 0.01)) - assert_equal('5dc2bb2aaee70383da8b12c99cbf388b', hash_img(velx)) - assert_equal('b53cec7a363ba7491cde61540813b827', hash_img(vely)) + assert_in_delta(130, count_threshold(velx, 1), 20) + assert_in_delta(110, count_threshold(vely, 1), 20) prev_velx, prev_vely = curr.optical_flow_hs(prev) velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely) - assert_equal('08b50f3d4cb4cbbe443fada293e6af02', hash_img(velx)) - assert_equal('4d302c8267388995ec85a4a26da5ffcc', hash_img(vely)) + assert_in_delta(70, count_threshold(velx, 1), 20) + assert_in_delta(60, count_threshold(vely, 1), 20) velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely, :lambda => 0.001) - assert_equal('8bb08ee394719a70e24bfb8b428662cd', hash_img(velx)) - assert_equal('f6c09e73160f0792e4527cdeea0e5573', hash_img(vely)) + assert_in_delta(80, count_threshold(velx, 1), 20) + assert_in_delta(70, count_threshold(vely, 1), 20) velx, vely = curr.optical_flow_hs(prev, prev_velx, prev_vely, :criteria => CvTermCriteria.new(10, 0.01)) - assert_equal('c32a8483e3aec3cd6c33bceeefb8d2f2', hash_img(velx)) - assert_equal('da33e266aece70ed69dcf074acd8fd4e', hash_img(vely)) + assert_in_delta(150, count_threshold(velx, 1), 20) + assert_in_delta(130, count_threshold(vely, 1), 20) assert_raise(TypeError) { curr.optical_flow_hs(DUMMY_OBJ) @@ -1858,12 +1858,12 @@ class TestCvMat_imageprocessing < OpenCVTestCase } velx, vely = curr.optical_flow_lk(prev, CvSize.new(3, 3)) - assert_equal('13333362f0daf6ad732006bd2a32e177', hash_img(velx)) - assert_equal('45dc42034ab606dd61e34a5adc6a1c1b', hash_img(vely)) + assert_in_delta(100, count_threshold(velx, 1), 20) + assert_in_delta(90, count_threshold(vely, 1), 20) velx, vely = curr.optical_flow_lk(prev, CvSize.new(5, 5)) - assert_equal('d83c57805f9c074d0ad33a7522a75952', hash_img(velx)) - assert_equal('df8fb2010b00d89090e7d9653781a68d', hash_img(vely)) + assert_in_delta(180, count_threshold(velx, 1), 20) + assert_in_delta(150, count_threshold(vely, 1), 20) assert_raise(TypeError) { curr.optical_flow_lk(DUMMY_OBJ, CvSize.new(3, 3)) @@ -1896,26 +1896,26 @@ class TestCvMat_imageprocessing < OpenCVTestCase [curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(4, 4), :shift_size => CvSize.new(1, 1), :max_range => CvSize.new(4, 4)), curr.optical_flow_bm(prev)].each { |velx, vely| - assert_equal('08e73a6fa9af7684a5eddc4f30fd46e7', hash_img(velx)) - assert_equal('aabaf1b7393b950c2297f567b6f57d5d', hash_img(vely)) + assert_in_delta(350, count_threshold(velx, 1), 30) + assert_in_delta(250, count_threshold(vely, 1), 30) } - + velx, vely = curr.optical_flow_bm(prev, nil, nil, :block_size => CvSize.new(3, 3)) - assert_equal('fe540dc1f0aec2d70b774286eafa3602', hash_img(velx)) - assert_equal('c7cc0f055fe4708396ba6046c0f1c6b5', hash_img(vely)) + assert_in_delta(280, count_threshold(velx, 1), 30) + assert_in_delta(200, count_threshold(vely, 1), 30) velx, vely = curr.optical_flow_bm(prev, nil, nil, :shift_size => CvSize.new(2, 2)) - assert_equal('00ad7854afb6eb4e46f0fb31c312c5eb', hash_img(velx)) - assert_equal('33215ec0bfa7f6b1424d6fadb2a48e0f', hash_img(vely)) + assert_in_delta(80, count_threshold(velx, 1), 30) + assert_in_delta(60, count_threshold(vely, 1), 30) velx, vely = curr.optical_flow_bm(prev, nil, nil, :max_range => CvSize.new(5, 5)) - assert_equal('ac10837eeee45fd80a24695cfaf9cfc7', hash_img(velx)) - assert_equal('8c7011c26ac53eaf1fae1aa9324e5979', hash_img(vely)) + assert_in_delta(400, count_threshold(velx, 1), 30) + assert_in_delta(300, count_threshold(vely, 1), 30) prev_velx, prev_vely = curr.optical_flow_bm(prev) velx, vely = curr.optical_flow_bm(prev, prev_velx, prev_vely) - assert_equal('6ad6b7a5c935379c0df4b9ec5666f3de', hash_img(velx)) - assert_equal('b317b0b9d4fdb0e5cd40beb0dd4143b4', hash_img(vely)) + assert_in_delta(350, count_threshold(velx, 1), 30) + assert_in_delta(270, count_threshold(vely, 1), 30) assert_raise(TypeError) { curr.optical_flow_bm(DUMMY_OBJ) From 4f6cdf9adbb9c2c52005a2d5e68515765f1eb1a6 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 04:11:31 +0900 Subject: [PATCH 09/15] update tests of CvMat#match_template --- test/test_cvmat_imageprocessing.rb | 33 ++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index e0657be..01f78d3 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1601,43 +1601,59 @@ class TestCvMat_imageprocessing < OpenCVTestCase def test_match_template mat = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) templ = CvMat.load(FILENAME_LENA_EYES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) - + + expected_pt = CvPoint.new(100, 120) + # sqdiff result = mat.match_template(templ) - assert_equal('88663ec44be797ca883fc87bb6d7c09b', hash_img(result)) + pt = result.min_max_loc[2] + assert_in_delta(expected_pt.x, pt.x, 20) + assert_in_delta(expected_pt.y, pt.y, 20) + [CV_TM_SQDIFF, :sqdiff].each { |method| result = mat.match_template(templ, method) - assert_equal('88663ec44be797ca883fc87bb6d7c09b', hash_img(result)) + assert_in_delta(expected_pt.x, pt.x, 20) + assert_in_delta(expected_pt.y, pt.y, 20) } # sqdiff_normed [CV_TM_SQDIFF_NORMED, :sqdiff_normed].each { |method| result = mat.match_template(templ, method) - assert_equal('75c812f87184b2ccd8f83b70a8436356', hash_img(result)) + pt = result.min_max_loc[2] + assert_in_delta(expected_pt.x, pt.x, 20) + assert_in_delta(expected_pt.y, pt.y, 20) } # ccorr [CV_TM_CCORR, :ccorr].each { |method| result = mat.match_template(templ, method) - assert_equal('6ebe7e48edf8fc64bcc0fd7f1e96555c', hash_img(result)) + pt = result.min_max_loc[3] + assert_in_delta(110, pt.x, 20) + assert_in_delta(60, pt.y, 20) } # ccorr_normed [CV_TM_CCORR_NORMED, :ccorr_normed].each { |method| result = mat.match_template(templ, method) - assert_equal('4cf8ebcec870f8295d615a9aa345ae4d', hash_img(result)) + pt = result.min_max_loc[3] + assert_in_delta(expected_pt.x, pt.x, 20) + assert_in_delta(expected_pt.y, pt.y, 20) } # ccoeff [CV_TM_CCOEFF, :ccoeff].each { |method| result = mat.match_template(templ, method) - assert_equal('248a391c5a1e1dbcf7a19f3310b5cd7a', hash_img(result)) + pt = result.min_max_loc[3] + assert_in_delta(expected_pt.x, pt.x, 20) + assert_in_delta(expected_pt.y, pt.y, 20) } # ccoeff_normed [CV_TM_CCOEFF_NORMED, :ccoeff_normed].each { |method| result = mat.match_template(templ, method) - assert_equal('27a4e9b45ed648848f0498356bd2c5b5', hash_img(result)) + pt = result.min_max_loc[3] + assert_in_delta(expected_pt.x, pt.x, 20) + assert_in_delta(expected_pt.y, pt.y, 20) } # Uncomment the following lines to show the result @@ -1647,7 +1663,6 @@ class TestCvMat_imageprocessing < OpenCVTestCase # mat.rectangle!(pt1, pt2, :color => CvColor::Black, :thickness => 3) # snap mat, templ, result - assert_raise(TypeError) { mat.match_template(DUMMY_OBJ) } From bde231fd9600353fcb731d69cadb0fc9ca2dd7c5 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 04:15:12 +0900 Subject: [PATCH 10/15] update tests of CvMat#pre_corner_detect --- test/test_cvmat_imageprocessing.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 01f78d3..76bf7b6 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -115,9 +115,9 @@ class TestCvMat_imageprocessing < OpenCVTestCase mat2 = mat0.pre_corner_detect(3) mat3 = mat0.pre_corner_detect(5) - assert_equal('1ec909dfa2e497c7f454e39aefd764f5', hash_img(mat1)) - assert_equal('1ec909dfa2e497c7f454e39aefd764f5', hash_img(mat2)) - assert_equal('42e7443ffd389d15343d3c6bdc42f553', hash_img(mat3)) + assert_in_delta(0, count_threshold(mat1, 0.1), 30) + assert_in_delta(0, count_threshold(mat2, 0.1), 30) + assert_in_delta(380, count_threshold(mat3, 0.1), 30) # Uncomment the following lines to show the images # snap(['original', mat0], ['pre_coner_detect', mat1], From 4f1c87bcabbb9c5f2691f716c869e9d6fed96bc7 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 04:29:37 +0900 Subject: [PATCH 11/15] update tests of CvMat#pyr_up --- test/helper.rb | 10 ++++++++++ test/test_cvmat_imageprocessing.rb | 14 ++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/test/helper.rb b/test/helper.rb index 6704d72..b861181 100755 --- a/test/helper.rb +++ b/test/helper.rb @@ -152,5 +152,15 @@ class OpenCVTestCase < Test::Unit::TestCase } n end + + def color_hists(mat) + hists = [0] * mat.channel + (mat.rows * mat.cols).times { |i| + hists.size.times { |c| + hists[c] += mat[i][c] + } + } + hists + end end diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 76bf7b6..096052d 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1138,8 +1138,18 @@ class TestCvMat_imageprocessing < OpenCVTestCase mat1 = mat0.pyr_up mat2 = mat0.pyr_up(:gaussian_5x5) - assert_equal('02430c6cf143d3d104e25bc829f1fa93', hash_img(mat1)) - assert_equal('02430c6cf143d3d104e25bc829f1fa93', hash_img(mat2)) + [mat1, mat2].each { |mat| + assert_equal(mat0.cols * 2, mat.cols) + assert_equal(mat0.rows * 2, mat.rows) + assert_equal(mat0.depth, mat.depth) + assert_equal(mat0.channel, mat.channel) + b, g, r = color_hists(mat) + assert_in_delta(27500000, b, 1000000) + assert_in_delta(26000000, g, 1000000) + assert_in_delta(47000000, r, 1000000) + } + # Uncomment the following lines to show the result + # snap mat0, mat1, mat2 assert_raise(TypeError) { mat0.pyr_up(DUMMY_OBJ) From bef15254173c13ed3361ed24e7663f9d7b562b3a Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 04:34:15 +0900 Subject: [PATCH 12/15] update tests of CvMat#pyr_mean_shift_filtering --- test/test_cvmat_imageprocessing.rb | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 096052d..6f90bd3 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1336,9 +1336,12 @@ class TestCvMat_imageprocessing < OpenCVTestCase mat2 = mat0.pyr_mean_shift_filtering(30, 30, 2) mat3 = mat0.pyr_mean_shift_filtering(30, 30, nil, CvTermCriteria.new(3, 0.01)) - assert_equal('6887e96bc5dfd552f76ac5411b394775', hash_img(mat1)) - assert_equal('3cd9c4983fcabeafa04be200d5e08841', hash_img(mat2)) - assert_equal('e37f0157f93fe2a98312ae6b768e8295', hash_img(mat3)) + [mat1, mat2, mat3].each { |mat| + b, g, r = color_hists(mat) + assert_in_delta(6900000, b, 100000) + assert_in_delta(6500000, g, 100000) + assert_in_delta(11800000, r, 100000) + } assert_raise(TypeError) { mat0.pyr_mean_shift_filtering(DUMMY_OBJ, 30) From bdf229b6d41544c2577392b0b33af8052143eef4 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 04:41:39 +0900 Subject: [PATCH 13/15] update tests of CvMat#log_polar --- test/test_cvmat_imageprocessing.rb | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 6f90bd3..594bd48 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -562,14 +562,26 @@ class TestCvMat_imageprocessing < OpenCVTestCase mat0 = CvMat.load(FILENAME_FRUITS, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) mat1 = mat0.log_polar(CvSize.new(255, 255), CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40) - assert_equal('d0425614b2f6e63ab2b6ef6637b4efcb', hash_img(mat1)) - mat1 = mat0.log_polar(CvSize.new(255, 255), CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40, + mat2 = mat0.log_polar(CvSize.new(255, 255), CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS) - assert_equal('d0425614b2f6e63ab2b6ef6637b4efcb', hash_img(mat1)) - - mat2 = mat1.log_polar(mat0.size, CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40, + mat3 = mat1.log_polar(mat0.size, CvPoint2D32f.new(mat0.width / 2, mat0.height / 2), 40, CV_INTER_LINEAR | CV_WARP_FILL_OUTLIERS | CV_WARP_INVERSE_MAP) - assert_equal('52587e593fec1b0383731be53147e8cd', hash_img(mat2)) + + [mat1, mat2].each { |mat| + assert_equal(mat0.depth, mat.depth) + assert_equal(mat0.channel, mat.channel) + b, g, r = color_hists(mat) + assert_in_delta(4000000, b, 100000) + assert_in_delta(5860000, g, 100000) + assert_in_delta(7700000, r, 100000) + } + + b, g, r = color_hists(mat3) + assert_equal(mat0.depth, mat3.depth) + assert_equal(mat0.channel, mat3.channel) + assert_in_delta(11200000, b, 1000000) + assert_in_delta(20800000, g, 1000000) + assert_in_delta(26900000, r, 1000000) # Uncomment the following line to show the results # snap mat0, mat1, mat2 From a129a14bb681d5bd6f7bba1d7c0431cd12c07dd1 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 04:58:13 +0900 Subject: [PATCH 14/15] modify CvMat#morphology --- ext/opencv/cvmat.cpp | 41 +++++++++++++++++++---------------------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/ext/opencv/cvmat.cpp b/ext/opencv/cvmat.cpp index 7a6ab8e..90fc2d0 100644 --- a/ext/opencv/cvmat.cpp +++ b/ext/opencv/cvmat.cpp @@ -4192,9 +4192,26 @@ rb_dilate_bang(int argc, VALUE *argv, VALUE self) return self; } +/* + * call-seq: + * morpholohy(operation[,element = nil][,iteration = 1]) -> cvmat + * + * Performs advanced morphological transformations. + * operation + * Type of morphological operation, one of: + * CV_MOP_OPEN - opening + * CV_MOP_CLOSE - closing + * CV_MOP_GRADIENT - morphological gradient + * CV_MOP_TOPHAT - top hat + * CV_MOP_BLACKHAT - black hat + */ VALUE -rb_morphology_internal(VALUE element, VALUE iteration, int operation, VALUE self) +rb_morphology(int argc, VALUE *argv, VALUE self) { + VALUE element, iteration, operation_val; + rb_scan_args(argc, argv, "12", &operation_val, &element, &iteration); + + int operation = CVMETHOD("MORPHOLOGICAL_OPERATION", operation_val, -1); CvArr* self_ptr = CVARR(self); CvSize size = cvGetSize(self_ptr); VALUE dest = new_mat_kind_object(size, self); @@ -4212,28 +4229,8 @@ rb_morphology_internal(VALUE element, VALUE iteration, int operation, VALUE self catch (cv::Exception& e) { raise_cverror(e); } - return dest; -} -/* - * call-seq: - * morpholohy(operation[,element = nil][,iteration = 1]) -> cvmat - * - * Performs advanced morphological transformations. - * operation - * Type of morphological operation, one of: - * CV_MOP_OPEN - opening - * CV_MOP_CLOSE - closing - * CV_MOP_GRADIENT - morphological gradient - * CV_MOP_TOPHAT - top hat - * CV_MOP_BLACKHAT - black hat - */ -VALUE -rb_morphology(int argc, VALUE *argv, VALUE self) -{ - VALUE element, iteration, operation; - rb_scan_args(argc, argv, "12", &operation, &element, &iteration); - return rb_morphology_internal(element, iteration, CVMETHOD("MORPHOLOGICAL_OPERATION", operation, -1), self); + return dest; } /* From 8e0f3285029a45a2319b44f6701aa9e453ecee6b Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Jan 2013 04:59:09 +0900 Subject: [PATCH 15/15] ignore .RUBYLIBDIR.* --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 6d09bed..a4e164c 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,5 @@ log.txt videowriter_result.avi examples/contours/rotated-boxes-with-detected-bounding-rectangles.jpg Gemfile.lock +.RUBYLIBDIR.* +