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

add CvMat.add_weighted

This commit is contained in:
ser1zw 2012-01-04 03:28:02 +09:00
parent e752f57d5b
commit b02022287e
3 changed files with 72 additions and 0 deletions

View file

@ -242,6 +242,7 @@ void define_ruby_class()
rb_define_alias(rb_klass, "*", "mat_mul");
rb_define_method(rb_klass, "div", RUBY_METHOD_FUNC(rb_div), -1);
rb_define_alias(rb_klass, "/", "div");
rb_define_singleton_method(rb_klass, "add_weighted", RUBY_METHOD_FUNC(rb_add_weighted), 5);
rb_define_method(rb_klass, "and", RUBY_METHOD_FUNC(rb_and), -1);
rb_define_alias(rb_klass, "&", "and");
rb_define_method(rb_klass, "or", RUBY_METHOD_FUNC(rb_or), -1);
@ -1927,6 +1928,39 @@ rb_div(int argc, VALUE *argv, VALUE self)
return dest;
}
/*
* call-seq:
* add_weighted(src1, alpha, src2, beta, gamma)
*
* Computes the weighted sum of two arrays.
*
* src1 - The first source array
* alpha - Weight for the first array elements
* src2 - The second source array
* beta - Weight for the second array elements
* gamma - Added to each sum
*
* The function calculates the weighted sum of two arrays as follows:
* dst(I)=src1(I)*alpha+src2(I)*beta+gamma
* All the arrays must have the same type and the same size (or ROI size).
* For types that have limited range this operation is saturating.
*/
VALUE
rb_add_weighted(VALUE klass, VALUE src1, VALUE alpha, VALUE src2, VALUE beta, VALUE gamma)
{
CvArr* src1_ptr = CVARR_WITH_CHECK(src1);
VALUE dst = new_mat_kind_object(cvGetSize(src1_ptr), src1);
try {
cvAddWeighted(src1_ptr, NUM2DBL(alpha),
CVARR_WITH_CHECK(src2), NUM2DBL(beta),
NUM2DBL(gamma), CVARR(dst));
}
catch (cv::Exception& e) {
raise_cverror(e);
}
return dst;
}
/*
* call-seq:
* and(<i>val[,mask]</i>) -> cvmat

View file

@ -107,6 +107,7 @@ VALUE rb_le(VALUE self, VALUE val);
VALUE rb_ne(VALUE self, VALUE val);
VALUE rb_in_range(VALUE self, VALUE min, VALUE max);
VALUE rb_abs_diff(VALUE self, VALUE val);
VALUE rb_add_weighted(VALUE klass, VALUE src1, VALUE alpha, VALUE src2, VALUE beta, VALUE gamma);
/* Statistics */
VALUE rb_count_non_zero(VALUE self);
VALUE rb_sum(VALUE self);

View file

@ -1352,6 +1352,43 @@ class TestCvMat < OpenCVTestCase
}
end
def test_add_weighted
m1 = create_cvmat(3, 2, :cv8u) { |j, i, c| c + 1 }
m2 = create_cvmat(3, 2, :cv8u) { |j, i, c| (c + 1) * 10 }
a = 2.0
b = 0.1
g = 100
m3 = CvMat.add_weighted(m1, a, m2, b, g)
assert_equal(m1.class, m3.class)
assert_equal(m1.rows, m3.rows)
assert_equal(m1.cols, m3.cols)
assert_equal(m1.depth, m3.depth)
assert_equal(m1.channel, m3.channel)
m1.rows.times { |j|
m1.cols.times { |i|
expected = m1[j, i][0] * a + m2[j, i][0] * b + g
assert_equal(expected, m3[j, i][0])
}
}
assert_raise(TypeError) {
CvMat.add_weighted(DUMMY_OBJ, a, m2, b, g)
}
assert_raise(TypeError) {
CvMat.add_weighted(m1, DUMMY_OBJ, m2, b, g)
}
assert_raise(TypeError) {
CvMat.add_weighted(m1, a, DUMMY_OBJ, b, g)
}
assert_raise(TypeError) {
CvMat.add_weighted(m1, a, m2, DUMMY_OBJ, g)
}
assert_raise(TypeError) {
CvMat.add_weighted(m1, a, m2, b, DUMMY_OBJ)
}
end
def test_and
m1 = create_cvmat(6, 4)
s1 = CvScalar.new(1, 2, 3, 4)