diff --git a/ext/opencv/mat.cpp b/ext/opencv/mat.cpp index abffdaa..a9eebbc 100644 --- a/ext/opencv/mat.cpp +++ b/ext/opencv/mat.cpp @@ -895,7 +895,8 @@ namespace rubyopencv { rb_define_method(rb_klass, "resize", RUBY_METHOD_FUNC(rb_resize), -1); // in ext/opencv/mat_imgproc.cpp rb_define_method(rb_klass, "cvt_color", RUBY_METHOD_FUNC(rb_cvt_color), -1); // in ext/opencv/mat_imgproc.cpp - + rb_define_method(rb_klass, "blur", RUBY_METHOD_FUNC(rb_blur), -1); // in ext/opencv/mat_imgproc.cpp + rb_define_method(rb_klass, "save", RUBY_METHOD_FUNC(rb_save), -1); rb_define_method(rb_klass, "imencode", RUBY_METHOD_FUNC(rb_imencode), -1); diff --git a/ext/opencv/mat_imgproc.cpp b/ext/opencv/mat_imgproc.cpp index 03317f0..691f113 100644 --- a/ext/opencv/mat_imgproc.cpp +++ b/ext/opencv/mat_imgproc.cpp @@ -3,6 +3,7 @@ #include "mat.hpp" #include "size.hpp" +#include "point.hpp" #include "error.hpp" /* @@ -177,5 +178,32 @@ namespace rubyopencv { return mat2obj(destptr, CLASS_OF(self)); } + + /* + * Blurs an image using the normalized box filter. + * + * @overload blur(ksize, anchor = Point.new(-1, -1), border_type = BORDER_DEFAULT) + * @param ksize [Integer] Blurring kernel size. + * @param border_type [Integer] Border mode used to extrapolate pixels outside of the image. + * @return [Mat] Output array + * @opencv_func cv::blur + */ + VALUE rb_blur(int argc, VALUE *argv, VALUE self) { + VALUE ksize, anchor, border_type; + rb_scan_args(argc, argv, "12", &ksize, &anchor, &border_type); + cv::Point anchor_value = NIL_P(anchor) ? cv::Point(-1, -1) : *(Point::obj2point(anchor)); + int border_type_value = NIL_P(border_type) ? cv::BORDER_DEFAULT : NUM2INT(border_type); + + cv::Mat* selfptr = obj2mat(self); + cv::Mat* dstptr = empty_mat(); + try { + cv::blur(*selfptr, *dstptr, *(Size::obj2size(ksize)), anchor_value, border_type_value); + } + catch (cv::Exception& e) { + Error::raise(e); + } + + return mat2obj(dstptr, CLASS_OF(self)); + } } } diff --git a/ext/opencv/mat_imgproc.hpp b/ext/opencv/mat_imgproc.hpp index 835cc4d..57c8d39 100644 --- a/ext/opencv/mat_imgproc.hpp +++ b/ext/opencv/mat_imgproc.hpp @@ -11,6 +11,7 @@ namespace rubyopencv { VALUE rb_laplacian(int argc, VALUE *argv, VALUE self); VALUE rb_cvt_color(int argc, VALUE *argv, VALUE self); VALUE rb_resize(int argc, VALUE *argv, VALUE self); + VALUE rb_blur(int argc, VALUE *argv, VALUE self); } } diff --git a/ext/opencv/point.hpp b/ext/opencv/point.hpp index 7d1ef7c..edebb8c 100644 --- a/ext/opencv/point.hpp +++ b/ext/opencv/point.hpp @@ -5,6 +5,7 @@ namespace rubyopencv { namespace Point { void init(); cv::Point conpatible_obj2point(VALUE obj); + cv::Point* obj2point(VALUE obj); } } diff --git a/test/test_mat_imgproc.rb b/test/test_mat_imgproc.rb index 1a1c316..fe26095 100755 --- a/test/test_mat_imgproc.rb +++ b/test/test_mat_imgproc.rb @@ -148,4 +148,36 @@ class TestCvMat < OpenCVTestCase Cv::add_weighted(m0, 0.5, m1, 0.5, 32, DUMMY_OBJ) } end + + def test_blur + m0 = Cv::imread(FILENAME_LENA256x256, -1) + + ksize = Cv::Size.new(3, 3) + anchor = Cv::Point.new(1, 1) + blurs = [] + blurs << m0.blur(ksize) + blurs << m0.blur(ksize, anchor) + blurs << m0.blur(ksize, anchor, Cv::BORDER_REPLICATE) + blurs.each { |m| + assert_equal(m0.rows, m.rows) + assert_equal(m0.cols, m.cols) + assert_equal(m0.depth, m.depth) + assert_equal(m0.dims, m.dims) + assert_equal(m0.channels, m.channels) + } + + assert_raise(TypeError) { + m0.blur(DUMMY_OBJ) + } + assert_raise(TypeError) { + m0.blur(ksize, DUMMY_OBJ) + } + assert_raise(TypeError) { + m0.blur(ksize, anchor, DUMMY_OBJ) + } + + # w = Window.new('Blur') + # w.show(m0.blur(ksize, anchor, Cv::BORDER_REPLICATE)) + # Cv::wait_key + end end