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

add Mat#adaptive_threshold!

This commit is contained in:
ser1zw 2017-04-30 04:45:50 +09:00
parent 85e744c07d
commit aeda43902a
4 changed files with 71 additions and 7 deletions

View file

@ -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);

View file

@ -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;
}
}
}

View file

@ -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);
}
}

View file

@ -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 = "<Cv::Mat:2x2,depth=0,channels=1,\n[ 0, 0;\n 255, 255]>"
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