From 1d258996bab739c5492e459179650923b36547bb Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 30 Apr 2017 04:07:04 +0900 Subject: [PATCH] add Mat#blur! --- ext/opencv/mat.cpp | 1 + ext/opencv/mat_imgproc.cpp | 42 +++++++++++++++++++++++++++++--------- ext/opencv/mat_imgproc.hpp | 1 + test/test_mat_imgproc.rb | 38 ++++++++++++++++++++++++++++++++++ 4 files changed, 72 insertions(+), 10 deletions(-) diff --git a/ext/opencv/mat.cpp b/ext/opencv/mat.cpp index 8da673b..a5c185f 100644 --- a/ext/opencv/mat.cpp +++ b/ext/opencv/mat.cpp @@ -1252,6 +1252,7 @@ namespace rubyopencv { 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, "cvt_color!", RUBY_METHOD_FUNC(rb_cvt_color_bang), -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, "blur!", RUBY_METHOD_FUNC(rb_blur_bang), -1); // in ext/opencv/mat_imgproc.cpp rb_define_method(rb_klass, "gaussian_blur", RUBY_METHOD_FUNC(rb_gaussian_blur), -1); // in ext/opencv/mat_imgproc.cpp rb_define_method(rb_klass, "median_blur", RUBY_METHOD_FUNC(rb_median_blur), 1); // in ext/opencv/mat_imgproc.cpp rb_define_method(rb_klass, "threshold", RUBY_METHOD_FUNC(rb_threshold), 3); // in ext/opencv/mat_imgproc.cpp diff --git a/ext/opencv/mat_imgproc.cpp b/ext/opencv/mat_imgproc.cpp index 1dba29b..456d4da 100644 --- a/ext/opencv/mat_imgproc.cpp +++ b/ext/opencv/mat_imgproc.cpp @@ -301,6 +301,18 @@ namespace rubyopencv { return self; } + cv::Mat* rb_blur_internal(int argc, VALUE *argv, VALUE self, cv::Mat* destptr) { + 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::blur(*selfptr, *destptr, *(Size::obj2size(ksize)), anchor_value, border_type_value); + + return destptr; + } + /* * Blurs an image using the normalized box filter. * @@ -311,22 +323,32 @@ namespace rubyopencv { * @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 = new cv::Mat(); + cv::Mat* destptr = new cv::Mat(); try { - cv::blur(*selfptr, *dstptr, *(Size::obj2size(ksize)), anchor_value, border_type_value); + rb_blur_internal(argc, argv, self, destptr); } catch (cv::Exception& e) { - delete dstptr; + delete destptr; Error::raise(e); } - return mat2obj(dstptr, CLASS_OF(self)); + return mat2obj(destptr, CLASS_OF(self)); + } + + /* + * @overload blur!(ksize, anchor = Point.new(-1, -1), border_type = BORDER_DEFAULT) + * @see #blue + */ + VALUE rb_blur_bang(int argc, VALUE *argv, VALUE self) { + cv::Mat* destptr = obj2mat(self); + try { + rb_blur_internal(argc, argv, self, destptr); + } + catch (cv::Exception& e) { + Error::raise(e); + } + + return self; } /* diff --git a/ext/opencv/mat_imgproc.hpp b/ext/opencv/mat_imgproc.hpp index 3a99b26..3b555d5 100644 --- a/ext/opencv/mat_imgproc.hpp +++ b/ext/opencv/mat_imgproc.hpp @@ -17,6 +17,7 @@ namespace rubyopencv { VALUE rb_resize(int argc, VALUE *argv, VALUE self); VALUE rb_resize_bang(int argc, VALUE *argv, VALUE self); VALUE rb_blur(int argc, VALUE *argv, VALUE self); + VALUE rb_blur_bang(int argc, VALUE *argv, VALUE self); VALUE rb_gaussian_blur(int argc, VALUE *argv, VALUE self); VALUE rb_median_blur(VALUE self, VALUE ksize); VALUE rb_threshold(VALUE self, VALUE threshold, VALUE max_value, VALUE threshold_type); diff --git a/test/test_mat_imgproc.rb b/test/test_mat_imgproc.rb index d432a02..c60f3dc 100755 --- a/test/test_mat_imgproc.rb +++ b/test/test_mat_imgproc.rb @@ -390,6 +390,44 @@ class TestCvMat < OpenCVTestCase # Cv::wait_key end + def test_blur_bang + m0 = Cv::imread(FILENAME_LENA256x256, -1) + m1 = m0.clone + m2 = m0.clone + m3 = m0.clone + + ksize = Cv::Size.new(3, 3) + anchor = Cv::Point.new(1, 1) + m0.blur!(ksize) + m0.blur!(ksize, anchor) + m0.blur!(ksize, anchor, Cv::BORDER_REPLICATE) + + blurs = [m1, m2, m3] + 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') + # m0 = Cv::imread(FILENAME_LENA256x256, -1) + # m0.blur!(ksize, anchor, Cv::BORDER_REPLICATE) + # w.show(m0) + # Cv::wait_key + end + def test_gaussian_blur m0 = Cv::imread(FILENAME_LENA256x256, -1)