diff --git a/ext/opencv/mat.cpp b/ext/opencv/mat.cpp index 7f34c61..bd47de6 100644 --- a/ext/opencv/mat.cpp +++ b/ext/opencv/mat.cpp @@ -736,6 +736,29 @@ namespace rubyopencv { return mat2obj(retptr, CLASS_OF(self)); } + /* + * Computes a dot-product of two vectors. + * + * @overload dot(value) + * @param value [Mat] Another dot-product operand. + * @return [Number] Dot product + * @opencv_func cv::Mat::dot + */ + VALUE rb_dot(VALUE self, VALUE other) { + cv::Mat* selfptr = obj2mat(self); + VALUE ret = Qnil; + + try { + cv::Mat* mat = obj2mat(other); + ret = DBL2NUM(selfptr->dot(*mat)); + } + catch (cv::Exception& e) { + Error::raise(e); + } + + return ret; + } + /* * Sets all or some of the array elements to the specified value. * @@ -879,6 +902,7 @@ namespace rubyopencv { rb_define_method(rb_klass, "*", RUBY_METHOD_FUNC(rb_mul), 1); rb_define_method(rb_klass, "/", RUBY_METHOD_FUNC(rb_div), 1); rb_define_method(rb_klass, "diag", RUBY_METHOD_FUNC(rb_diag), -1); + rb_define_method(rb_klass, "dot", RUBY_METHOD_FUNC(rb_dot), 1); rb_define_method(rb_klass, "clone", RUBY_METHOD_FUNC(rb_clone), 0); diff --git a/test/test_mat.rb b/test/test_mat.rb index 8975617..dff32c5 100755 --- a/test/test_mat.rb +++ b/test/test_mat.rb @@ -353,6 +353,22 @@ class TestMat < OpenCVTestCase } end + def test_dot + m0 = Mat.new(2, 1, CV_8U); + m0[0, 0] = Scalar.new(1) + m0[1, 0] = Scalar.new(2) + m1 = Mat.new(2, 1, CV_8U); + m1[0, 0] = Scalar.new(3) + m1[1, 0] = Scalar.new(4) + + a = m0.dot(m1) + assert_in_delta(11.0, a, 0.01) + + assert_raise(TypeError) { + m0.dot(DUMMY_OBJ) + } + end + def test_cvt_color m = Mat.new(1, 1, CV_32FC3) m[0, 0] = Scalar.new(1.0, 2.0, 3.0)