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

implemented and tested CvMat#match_shapes

This commit is contained in:
ser1zw 2011-04-03 02:12:42 +09:00
parent dfc9bfacd1
commit b3e2d7e017
8 changed files with 118 additions and 18 deletions

View file

@ -381,6 +381,7 @@ void define_ruby_class()
rb_define_method(rb_klass, "equalize_hist", RUBY_METHOD_FUNC(rb_equalize_hist), 0);
rb_define_method(rb_klass, "match_template", RUBY_METHOD_FUNC(rb_match_template), -1);
rb_define_method(rb_klass, "match_shapes", RUBY_METHOD_FUNC(rb_match_shapes), -1);
rb_define_method(rb_klass, "match_shapes_i1", RUBY_METHOD_FUNC(rb_match_shapes_i1), -1);
rb_define_method(rb_klass, "match_shapes_i2", RUBY_METHOD_FUNC(rb_match_shapes_i2), -1);
rb_define_method(rb_klass, "match_shapes_i3", RUBY_METHOD_FUNC(rb_match_shapes_i3), -1);
@ -4665,23 +4666,56 @@ rb_match_template(int argc, VALUE *argv, VALUE self)
return result;
}
/*
* call-seq:
* match_shapes(<i>object, method</i>) -> float
*
* Compares two shapes(self and object). <i>object</i> should be CvMat or CvContour.
*
* A - object1, B - object2:
* * method=CV_CONTOUR_MATCH_I1
* I1(A,B)=sumi=1..7abs(1/mAi - 1/mBi)
* * method=CV_CONTOUR_MATCH_I2
* I2(A,B)=sumi=1..7abs(mAi - mBi)
* * method=CV_CONTOUR_MATCH_I3
* I3(A,B)=sumi=1..7abs(mAi - mBi)/abs(mAi)
*/
VALUE
rb_match_shapes(int argc, VALUE *argv, VALUE self)
{
VALUE object, method, param;
rb_scan_args(argc, argv, "21", &object, &method, &param);
int method_flag = CVMETHOD("COMPARISON_METHOD", method);
if (!(rb_obj_is_kind_of(object, cCvMat::rb_class()) || rb_obj_is_kind_of(object, cCvContour::rb_class())))
rb_raise(rb_eTypeError, "argument 1 (shape) should be %s or %s",
rb_class2name(cCvMat::rb_class()), rb_class2name(cCvContour::rb_class()));
return rb_float_new(cvMatchShapes(CVARR(self), CVARR(object), method_flag));
}
inline VALUE
rb_match_shapes_internal(int argc, VALUE *argv, VALUE self, int method)
{
VALUE object;
rb_scan_args(argc, argv, "10", &object);
if (!(rb_obj_is_kind_of(object, cCvMat::rb_class()) || rb_obj_is_kind_of(object, cCvContour::rb_class())))
rb_raise(rb_eTypeError, "argument 1 (shape) should be %s or %s",
rb_class2name(cCvMat::rb_class()), rb_class2name(cCvContour::rb_class()));
return rb_float_new(cvMatchShapes(CVARR(self), CVARR(object), method));
}
/*
* call-seq:
* match_shapes_i1(<i>object</i>) -> float
*
* Compares two shapes(self and object). <i>object</i> should be CvMat or CvContour.
*
* A ~ object1, B - object2):
* A - object1, B - object2:
* I1(A,B)=sumi=1..7abs(1/mAi - 1/mBi)
*/
VALUE
rb_match_shapes_i1(int argc, VALUE *argv, VALUE self)
{
VALUE object;
rb_scan_args(argc, argv, "10", &object);
if ((!(rb_obj_is_kind_of(object, cCvMat::rb_class()))) && (!(rb_obj_is_kind_of(object, cCvContour::rb_class()))))
rb_raise(rb_eTypeError, "argument 1 (shape) should be %s or %s", rb_class2name(cCvMat::rb_class()), rb_class2name(cCvContour::rb_class()));
return rb_float_new(cvMatchShapes(CVARR(self), CVARR(object), CV_CONTOURS_MATCH_I1));
return rb_match_shapes_internal(argc, argv, self, CV_CONTOURS_MATCH_I1);
}
/*
@ -4690,17 +4724,13 @@ rb_match_shapes_i1(int argc, VALUE *argv, VALUE self)
*
* Compares two shapes(self and object). <i>object</i> should be CvMat or CvContour.
*
* A ~ object1, B - object2):
* A - object1, B - object2:
* I2(A,B)=sumi=1..7abs(mAi - mBi)
*/
VALUE
rb_match_shapes_i2(int argc, VALUE *argv, VALUE self)
{
VALUE object;
rb_scan_args(argc, argv, "10", &object);
if ((!(rb_obj_is_kind_of(object, cCvMat::rb_class()))) && (!(rb_obj_is_kind_of(object, cCvContour::rb_class()))))
rb_raise(rb_eTypeError, "argument 1 (shape) should be %s or %s", rb_class2name(cCvMat::rb_class()), rb_class2name(cCvContour::rb_class()));
return rb_float_new(cvMatchShapes(CVARR(self), CVARR(object), CV_CONTOURS_MATCH_I2));
return rb_match_shapes_internal(argc, argv, self, CV_CONTOURS_MATCH_I2);
}
/*
@ -4709,17 +4739,13 @@ rb_match_shapes_i2(int argc, VALUE *argv, VALUE self)
*
* Compares two shapes(self and object). <i>object</i> should be CvMat or CvContour.
*
* A ~ object1, B - object2):
* A - object1, B - object2:
* I3(A,B)=sumi=1..7abs(mAi - mBi)/abs(mAi)
*/
VALUE
rb_match_shapes_i3(int argc, VALUE *argv, VALUE self)
{
VALUE object;
rb_scan_args(argc, argv, "10", &object);
if ((!(rb_obj_is_kind_of(object, cCvMat::rb_class()))) && (!(rb_obj_is_kind_of(object, cCvContour::rb_class()))))
rb_raise(rb_eTypeError, "argument 1 (shape) should be %s or %s", rb_class2name(cCvMat::rb_class()), rb_class2name(cCvContour::rb_class()));
return rb_float_new(cvMatchShapes(CVARR(self), CVARR(object), CV_CONTOURS_MATCH_I3));
return rb_match_shapes_internal(argc, argv, self, CV_CONTOURS_MATCH_I3);
}

View file

@ -242,6 +242,7 @@ VALUE rb_inpaint_telea(VALUE self, VALUE mask, VALUE radius);
VALUE rb_equalize_hist(VALUE self);
/* Matching*/
VALUE rb_match_template(int argc, VALUE *argv, VALUE self);
VALUE rb_match_shapes(int argc, VALUE *argv, VALUE self);
VALUE rb_match_shapes_i1(int argc, VALUE *argv, VALUE self);
VALUE rb_match_shapes_i2(int argc, VALUE *argv, VALUE self);
VALUE rb_match_shapes_i3(int argc, VALUE *argv, VALUE self);

View file

@ -251,6 +251,11 @@ define_ruby_module()
rb_define_const(rb_module, "CV_TM_CCORR_NORMED", INT2FIX(CV_TM_CCORR_NORMED));
rb_define_const(rb_module, "CV_TM_CCOEFF", INT2FIX(CV_TM_CCOEFF));
rb_define_const(rb_module, "CV_TM_CCOEFF_NORMED", INT2FIX(CV_TM_CCOEFF_NORMED));
/* Comparison method */
rb_define_const(rb_module, "CV_CONTOURS_MATCH_I1", INT2FIX(CV_CONTOURS_MATCH_I1));
rb_define_const(rb_module, "CV_CONTOURS_MATCH_I2", INT2FIX(CV_CONTOURS_MATCH_I2));
rb_define_const(rb_module, "CV_CONTOURS_MATCH_I3", INT2FIX(CV_CONTOURS_MATCH_I3));
VALUE inversion_method = rb_hash_new();
/* {:lu, :svd, :svd_sym(:svd_symmetric)}: Inversion method */
@ -388,6 +393,13 @@ define_ruby_module()
RESIST_CVMETHOD(inpaint_method, "ns", CV_INPAINT_NS);
RESIST_CVMETHOD(inpaint_method, "telea", CV_INPAINT_TELEA);
VALUE comparison_method = rb_hash_new();
/* Comparison method */
rb_define_const(rb_module, "COMPARISON_METHOD", comparison_method);
RESIST_CVMETHOD(comparison_method, "i1", CV_CONTOURS_MATCH_I1);
RESIST_CVMETHOD(comparison_method, "i2", CV_CONTOURS_MATCH_I2);
RESIST_CVMETHOD(comparison_method, "i3", CV_CONTOURS_MATCH_I3);
/* color convert methods */
rb_define_module_function(rb_module, "BGR2BGRA", RUBY_METHOD_FUNC(rb_BGR2BGRA), 1);
rb_define_module_function(rb_module, "RGB2RGBA", RUBY_METHOD_FUNC(rb_RGB2RGBA), 1);

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
test/samples/str-cv.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
test/samples/str-ov.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

View file

@ -15,6 +15,9 @@ class TestCvMat_imageprocessing < OpenCVTestCase
FILENAME_CONTOURS = File.expand_path(File.dirname(__FILE__)) + '/samples/contours.jpg'
FILENAME_LINES = File.expand_path(File.dirname(__FILE__)) + '/samples/lines.jpg'
FILENAME_LENA_EYES = File.expand_path(File.dirname(__FILE__)) + '/samples/lena-eyes.jpg'
FILENAME_STR_CV = File.expand_path(File.dirname(__FILE__)) + '/samples/str-cv.jpg'
FILENAME_STR_OV = File.expand_path(File.dirname(__FILE__)) + '/samples/str-ov.jpg'
FILENAME_STR_CV_ROTATED = File.expand_path(File.dirname(__FILE__)) + '/samples/str-cv-rotated.jpg'
def test_sobel
mat0 = CvMat.load(FILENAME_LENA256x256, CV_LOAD_IMAGE_GRAYSCALE)
@ -1513,5 +1516,53 @@ class TestCvMat_imageprocessing < OpenCVTestCase
# mat.rectangle!(pt1, pt2, :color => CvColor::Black, :thickness => 3)
# snap mat, templ, result
end
def test_match_shapes
mat_cv = CvMat.load(FILENAME_STR_CV, CV_LOAD_IMAGE_GRAYSCALE)
mat_ov = CvMat.load(FILENAME_STR_OV, CV_LOAD_IMAGE_GRAYSCALE)
mat_cv_rotated = CvMat.load(FILENAME_STR_CV_ROTATED, CV_LOAD_IMAGE_GRAYSCALE)
[CV_CONTOURS_MATCH_I1, :i1].each { |method|
assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
assert_in_delta(0.0010649, mat_cv.match_shapes(mat_ov, method), 0.00001)
}
[CV_CONTOURS_MATCH_I2, :i2].each { |method|
assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
assert_in_delta(0.0104650, mat_cv.match_shapes(mat_ov, method), 0.00001)
}
[CV_CONTOURS_MATCH_I3, :i3].each { |method|
assert_in_delta(0, mat_cv.match_shapes(mat_cv_rotated, method), 0.00001)
assert_in_delta(0.0033327, mat_cv.match_shapes(mat_ov, method), 0.00001)
}
end
def test_match_shapes_i1
mat_cv = CvMat.load(FILENAME_STR_CV, CV_LOAD_IMAGE_GRAYSCALE)
mat_ov = CvMat.load(FILENAME_STR_OV, CV_LOAD_IMAGE_GRAYSCALE)
mat_cv_rotated = CvMat.load(FILENAME_STR_CV_ROTATED, CV_LOAD_IMAGE_GRAYSCALE)
assert_in_delta(0, mat_cv.match_shapes_i1(mat_cv_rotated), 0.00001)
assert_in_delta(0.0010649, mat_cv.match_shapes_i1(mat_ov), 0.00001)
end
def test_match_shapes_i2
mat_cv = CvMat.load(FILENAME_STR_CV, CV_LOAD_IMAGE_GRAYSCALE)
mat_ov = CvMat.load(FILENAME_STR_OV, CV_LOAD_IMAGE_GRAYSCALE)
mat_cv_rotated = CvMat.load(FILENAME_STR_CV_ROTATED, CV_LOAD_IMAGE_GRAYSCALE)
assert_in_delta(0, mat_cv.match_shapes_i2(mat_cv_rotated), 0.00001)
assert_in_delta(0.0104650, mat_cv.match_shapes_i2(mat_ov), 0.00001)
end
def test_match_shapes_i3
mat_cv = CvMat.load(FILENAME_STR_CV, CV_LOAD_IMAGE_GRAYSCALE)
mat_ov = CvMat.load(FILENAME_STR_OV, CV_LOAD_IMAGE_GRAYSCALE)
mat_cv_rotated = CvMat.load(FILENAME_STR_CV_ROTATED, CV_LOAD_IMAGE_GRAYSCALE)
assert_in_delta(0, mat_cv.match_shapes_i3(mat_cv_rotated), 0.00001)
assert_in_delta(0.0033327, mat_cv.match_shapes_i3(mat_ov), 0.00001)
end
end

View file

@ -88,6 +88,11 @@ class TestOpenCV < OpenCVTestCase
assert_equal(3, CV_TM_CCORR_NORMED)
assert_equal(4, CV_TM_CCOEFF)
assert_equal(5, CV_TM_CCOEFF_NORMED)
# Comparison method
assert_equal(1, CV_CONTOURS_MATCH_I1)
assert_equal(2, CV_CONTOURS_MATCH_I2)
assert_equal(3, CV_CONTOURS_MATCH_I3)
end
def test_symbols
@ -192,6 +197,11 @@ class TestOpenCV < OpenCVTestCase
# Inpaint method
assert_equal(0, INPAINT_METHOD[:ns])
assert_equal(1, INPAINT_METHOD[:telea])
# Comparison method
assert_equal(1, COMPARISON_METHOD[:i1])
assert_equal(2, COMPARISON_METHOD[:i2])
assert_equal(3, COMPARISON_METHOD[:i3])
end
def test_cvt_color_funcs