diff --git a/ext/opencv/mat.cpp b/ext/opencv/mat.cpp index e17455e..9b6a5ba 100644 --- a/ext/opencv/mat.cpp +++ b/ext/opencv/mat.cpp @@ -1260,6 +1260,7 @@ namespace rubyopencv { rb_define_method(rb_klass, "threshold", RUBY_METHOD_FUNC(rb_threshold), 3); // in ext/opencv/mat_imgproc.cpp rb_define_method(rb_klass, "threshold!", RUBY_METHOD_FUNC(rb_threshold_bang), 3); // in ext/opencv/mat_imgproc.cpp rb_define_method(rb_klass, "adaptive_threshold", RUBY_METHOD_FUNC(rb_adaptive_threshold), 5); // in ext/opencv/mat_imgproc.cpp + rb_define_method(rb_klass, "adaptive_threshold!", RUBY_METHOD_FUNC(rb_adaptive_threshold_bang), 5); // 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 48e86cf..d8071a5 100644 --- a/ext/opencv/mat_imgproc.cpp +++ b/ext/opencv/mat_imgproc.cpp @@ -519,6 +519,15 @@ namespace rubyopencv { return ret; } + cv::Mat* rb_adaptive_threshold_internal(VALUE self, VALUE max_value, VALUE adaptive_method, VALUE threshold_type, + VALUE block_size, VALUE delta, cv::Mat* destptr) { + cv::Mat* selfptr = obj2mat(self); + cv::adaptiveThreshold(*selfptr, *destptr, NUM2DBL(max_value), NUM2INT(adaptive_method), + NUM2INT(threshold_type), NUM2INT(block_size), NUM2DBL(delta)); + + return destptr; + } + /* * Applies an adaptive threshold to an array. * @@ -536,19 +545,31 @@ namespace rubyopencv { */ VALUE rb_adaptive_threshold(VALUE self, VALUE max_value, VALUE adaptive_method, VALUE threshold_type, VALUE block_size, VALUE delta) { - cv::Mat* selfptr = obj2mat(self); - cv::Mat* dstptr = NULL; + cv::Mat* destptr = new cv::Mat(); try { - dstptr = new cv::Mat(); - cv::adaptiveThreshold(*selfptr, *dstptr, NUM2DBL(max_value), NUM2INT(adaptive_method), - NUM2INT(threshold_type), NUM2INT(block_size), NUM2DBL(delta)); + rb_adaptive_threshold_internal(self, max_value, adaptive_method, threshold_type, + block_size, delta, destptr); } catch (cv::Exception& e) { - delete dstptr; + delete destptr; Error::raise(e); } - return mat2obj(dstptr, CLASS_OF(self)); + return mat2obj(destptr, CLASS_OF(self)); + } + + VALUE rb_adaptive_threshold_bang(VALUE self, VALUE max_value, VALUE adaptive_method, VALUE threshold_type, + VALUE block_size, VALUE delta) { + cv::Mat* destptr = obj2mat(self); + try { + rb_adaptive_threshold_internal(self, max_value, adaptive_method, threshold_type, + block_size, delta, 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 50577b8..d95a3f9 100644 --- a/ext/opencv/mat_imgproc.hpp +++ b/ext/opencv/mat_imgproc.hpp @@ -26,6 +26,8 @@ namespace rubyopencv { VALUE rb_threshold_bang(VALUE self, VALUE threshold, VALUE max_value, VALUE threshold_type); VALUE rb_adaptive_threshold(VALUE self, VALUE max_value, VALUE adaptive_method, VALUE threshold_type, VALUE block_size, VALUE delta); + VALUE rb_adaptive_threshold_bang(VALUE self, VALUE max_value, VALUE adaptive_method, + VALUE threshold_type, VALUE block_size, VALUE delta); } } diff --git a/test/test_mat_imgproc.rb b/test/test_mat_imgproc.rb index 6b008fa..e7bd72b 100755 --- a/test/test_mat_imgproc.rb +++ b/test/test_mat_imgproc.rb @@ -703,4 +703,44 @@ class TestCvMat < OpenCVTestCase # w.show(Cv::hconcat([m0, m])) # Cv::wait_key end + + def test_adaptive_threshold_bang + m0 = Cv::Mat.new(2, 2, Cv::CV_8U) + m0[0, 0] = Cv::Scalar.new(10) + m0[0, 1] = Cv::Scalar.new(20) + m0[1, 0] = Cv::Scalar.new(30) + m0[1, 1] = Cv::Scalar.new(40) + + expected = "" + m = m0.clone + m.adaptive_threshold!(255, Cv::ADAPTIVE_THRESH_MEAN_C, Cv::THRESH_BINARY, 3, 0) + assert_equal(expected, m.to_s) + + m = m0.clone + m.adaptive_threshold!(255, Cv::ADAPTIVE_THRESH_GAUSSIAN_C, Cv::THRESH_BINARY, 3, 0) + assert_equal(expected, m.to_s) + + assert_raise(TypeError) { + m0.adaptive_threshold!(DUMMY_OBJ, Cv::ADAPTIVE_THRESH_MEAN_C, Cv::THRESH_BINARY, 3, 0) + } + assert_raise(TypeError) { + m0.adaptive_threshold!(255, DUMMY_OBJ, Cv::THRESH_BINARY, 3, 0) + } + assert_raise(TypeError) { + m0.adaptive_threshold!(255, Cv::ADAPTIVE_THRESH_MEAN_C, DUMMY_OBJ, 3, 0) + } + assert_raise(TypeError) { + m0.adaptive_threshold!(DUMMY_OBJ, Cv::ADAPTIVE_THRESH_MEAN_C, Cv::THRESH_BINARY, DUMMY_OBJ, 0) + } + assert_raise(TypeError) { + m0.adaptive_threshold!(DUMMY_OBJ, Cv::ADAPTIVE_THRESH_MEAN_C, Cv::THRESH_BINARY, 3, DUMMY_OBJ) + } + + # m0 = Cv::imread(FILENAME_LENA256x256, 0) + # m = m0.clone + # m.adaptive_threshold!(255, Cv::ADAPTIVE_THRESH_MEAN_C, Cv::THRESH_BINARY, 25, 0) + # w = Window.new('Original | Binary') + # w.show(Cv::hconcat([m0, m])) + # Cv::wait_key + end end