From 02d8cf8f32dcb6a0a550a0f8ca7d2b6d8744d6c9 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 1 May 2016 23:59:24 +0900 Subject: [PATCH] add Cv::Mat#median_blur --- ext/opencv/mat.cpp | 1 + ext/opencv/mat_imgproc.cpp | 24 ++++++++++++++++++++++++ ext/opencv/mat_imgproc.hpp | 1 + test/test_mat_imgproc.rb | 26 ++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) diff --git a/ext/opencv/mat.cpp b/ext/opencv/mat.cpp index 6d0c9ea..b7b8024 100644 --- a/ext/opencv/mat.cpp +++ b/ext/opencv/mat.cpp @@ -1068,6 +1068,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, "blur", RUBY_METHOD_FUNC(rb_blur), -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, "save", RUBY_METHOD_FUNC(rb_save), -1); diff --git a/ext/opencv/mat_imgproc.cpp b/ext/opencv/mat_imgproc.cpp index 33383dd..727f788 100644 --- a/ext/opencv/mat_imgproc.cpp +++ b/ext/opencv/mat_imgproc.cpp @@ -241,5 +241,29 @@ namespace rubyopencv { return mat2obj(dstptr, CLASS_OF(self)); } + + /* + * Blurs an image using the median filter. + * + * @overload median_blur(ksize) + * @param ksize [Integer] Aperture linear size; it must be odd and greater than 1, + * for example: 3, 5, 7 ... + * @return [Mat] Output array + * @opencv_func cv::medianBlur + */ + VALUE rb_median_blur(VALUE self, VALUE ksize) { + cv::Mat* selfptr = obj2mat(self); + cv::Mat* dstptr = NULL; + try { + dstptr = new cv::Mat(); + cv::medianBlur(*selfptr, *dstptr, NUM2INT(ksize)); + } + catch (cv::Exception& e) { + delete dstptr; + Error::raise(e); + } + + return mat2obj(dstptr, CLASS_OF(self)); + } } } diff --git a/ext/opencv/mat_imgproc.hpp b/ext/opencv/mat_imgproc.hpp index 70c4d9a..ef84357 100644 --- a/ext/opencv/mat_imgproc.hpp +++ b/ext/opencv/mat_imgproc.hpp @@ -13,6 +13,7 @@ namespace rubyopencv { VALUE rb_resize(int argc, VALUE *argv, VALUE self); VALUE rb_blur(int argc, VALUE *argv, VALUE self); VALUE rb_gaussian_blur(int argc, VALUE *argv, VALUE self); + VALUE rb_median_blur(VALUE self, VALUE ksize); } } diff --git a/test/test_mat_imgproc.rb b/test/test_mat_imgproc.rb index d06168a..b438f85 100755 --- a/test/test_mat_imgproc.rb +++ b/test/test_mat_imgproc.rb @@ -211,4 +211,30 @@ class TestCvMat < OpenCVTestCase # w.show(m0.gaussian_blur(ksize, 3, 5, Cv::BORDER_REPLICATE)) # Cv::wait_key end + + def test_median_blur + m0 = Cv::Mat.ones(3, 3, Cv::CV_8U) + m0[1, 1] = Cv::Scalar.new(0) + m1 = m0.median_blur(3) + + assert_equal(m0.class, m1.class) + assert_equal(m0.rows, m1.rows) + assert_equal(m0.cols, m1.cols) + assert_equal(m0.depth, m1.depth) + assert_equal(m0.dims, m1.dims) + assert_equal(m0.channels, m1.channels) + m1.rows.times { |r| + m1.cols.times { |c| + assert_equal(1, m1[r, c][0].to_i) + } + } + + assert_raise(TypeError) { + m0.median_blur(DUMMY_OBJ) + } + + # m = Cv::imread(FILENAME_LENA256x256, -1) + # m2 = m.median_blur(9) + # snap(['Original', m], ['Median blur', m2]) + end end