From 4f8c9bfa10dd22085311c079d98b6e973c62fea1 Mon Sep 17 00:00:00 2001 From: Martin Pilch Date: Fri, 17 Jun 2016 14:57:12 +0200 Subject: [PATCH] Implemented match_shapes for CvContour --- ext/opencv/cvcontour.cpp | 35 ++++++++++++++++++++++++++++++++++- ext/opencv/cvcontour.h | 3 +++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/ext/opencv/cvcontour.cpp b/ext/opencv/cvcontour.cpp index f3409b4..02b0174 100644 --- a/ext/opencv/cvcontour.cpp +++ b/ext/opencv/cvcontour.cpp @@ -292,9 +292,42 @@ rb_point_polygon_test(VALUE self, VALUE point, VALUE measure_dist) return rb_float_new(dist); } +/* + * call-seq: + * match_shapes(object, method) -> float + * + * Compares two shapes(self and object). object should be CvContour. + * + * A - object1, B - object2: + * * method=CV_CONTOURS_MATCH_I1 + * I1(A,B)=sumi=1..7abs(1/mAi - 1/mBi) + * * method=CV_CONTOURS_MATCH_I2 + * I2(A,B)=sumi=1..7abs(mAi - mBi) + * * method=CV_CONTOURS_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, ¶m); + int method_flag = CVMETHOD("COMPARISON_METHOD", method); + if (!rb_obj_is_kind_of(object, cCvContour::rb_class())) + rb_raise(rb_eTypeError, "argument 1 (shape) should be %s", + rb_class2name(cCvContour::rb_class())); + double result = 0; + try { + result = cvMatchShapes(CVARR(self), CVARR(object), method_flag); + } + catch (cv::Exception& e) { + raise_cverror(e); + } + return rb_float_new(result); +} + VALUE new_object() { - VALUE object = rb_allocate(rb_klass); +VALUE object = rb_allocate(rb_klass); rb_initialize(0, NULL, object); return object; } diff --git a/ext/opencv/cvcontour.h b/ext/opencv/cvcontour.h index 4654654..5cdc031 100644 --- a/ext/opencv/cvcontour.h +++ b/ext/opencv/cvcontour.h @@ -33,6 +33,9 @@ VALUE rb_in_q(VALUE self, VALUE point); VALUE rb_measure_distance(VALUE self, VALUE point); VALUE rb_point_polygon_test(VALUE self, VALUE point, VALUE measure_dist); +/* Matching*/ +VALUE rb_match_shapes(int argc, VALUE *argv, VALUE self); + VALUE new_object(); __NAMESPACE_END_CVCONTOUR