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

add Cv::threshold

This commit is contained in:
ser1zw 2016-08-13 01:34:48 +09:00
parent 45e10202cf
commit a544115b55
5 changed files with 101 additions and 0 deletions

View file

@ -1246,6 +1246,7 @@ namespace rubyopencv {
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, "threshold", RUBY_METHOD_FUNC(rb_threshold), 3); // in ext/opencv/mat_imgproc.cpp
rb_define_method(rb_klass, "save", RUBY_METHOD_FUNC(rb_save), -1);

View file

@ -265,5 +265,51 @@ namespace rubyopencv {
return mat2obj(dstptr, CLASS_OF(self));
}
/*
* Applies a fixed-level threshold to each array element.
*
* @overload threshold(threshold, max_value, type)
* @param threshold [Number] Threshold value
* @param max_value [Number] Maximum value to use with the <tt>THRESH_BINARY</tt> and
* <tt>THRESH_BINARY_INV</tt> thresholding types.
* @param threshold_type [Integer] Thresholding type
* * THRESH_BINARY
* * THRESH_BINARY_INV
* * THRESH_TRUNC
* * THRESH_TOZERO
* * THRESH_TOZERO_INV
* * THRESH_MASK
* * THRESH_OTSU
* * THRESH_TRIANGLE
* @return [Mat] Output array of the same size and type as <tt>self</tt>.
* @return [Array<Mat, Number>] Output array and optimal threshold when using
* <tt>THRESH_OTSU</tt> or <tt>THRESH_TRIANGLE</tt>.
* @opencv_func cv::threshold
*/
VALUE rb_threshold(VALUE self, VALUE threshold, VALUE max_value, VALUE threshold_type) {
cv::Mat* selfptr = obj2mat(self);
cv::Mat* dstptr = NULL;
double optimal_threshold = 0.0;
int threshold_type_value = NUM2INT(threshold_type);
try {
dstptr = new cv::Mat();
optimal_threshold = cv::threshold(*selfptr, *dstptr, NUM2DBL(threshold), NUM2DBL(max_value), threshold_type_value);
}
catch (cv::Exception& e) {
delete dstptr;
Error::raise(e);
}
VALUE ret = Qnil;
VALUE dst = mat2obj(dstptr, CLASS_OF(self));
if ((threshold_type_value & cv::THRESH_OTSU) || (threshold_type_value & cv::THRESH_TRIANGLE)) {
ret = rb_assoc_new(dst, DBL2NUM(optimal_threshold));
}
else {
ret = dst;
}
return ret;
}
}
}

View file

@ -14,6 +14,7 @@ namespace rubyopencv {
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);
VALUE rb_threshold(VALUE self, VALUE threshold, VALUE max_value, VALUE threshold_type);
}
}

View file

@ -377,5 +377,14 @@ namespace rubyopencv {
rb_define_const(rb_module, "CAP_PROP_SETTINGS", INT2FIX(cv::CAP_PROP_SETTINGS));
rb_define_const(rb_module, "CAP_PROP_BUFFERSIZE", INT2FIX(cv::CAP_PROP_BUFFERSIZE));
rb_define_const(rb_module, "CAP_PROP_AUTOFOCUS", INT2FIX(cv::CAP_PROP_AUTOFOCUS));
rb_define_const(rb_module, "THRESH_BINARY", INT2FIX(cv::THRESH_BINARY));
rb_define_const(rb_module, "THRESH_BINARY_INV", INT2FIX(cv::THRESH_BINARY_INV));
rb_define_const(rb_module, "THRESH_TRUNC", INT2FIX(cv::THRESH_TRUNC));
rb_define_const(rb_module, "THRESH_TOZERO", INT2FIX(cv::THRESH_TOZERO));
rb_define_const(rb_module, "THRESH_TOZERO_INV", INT2FIX(cv::THRESH_TOZERO_INV));
rb_define_const(rb_module, "THRESH_MASK", INT2FIX(cv::THRESH_MASK));
rb_define_const(rb_module, "THRESH_OTSU", INT2FIX(cv::THRESH_OTSU));
rb_define_const(rb_module, "THRESH_TRIANGLE", INT2FIX(cv::THRESH_TRIANGLE));
}
}

View file

@ -237,4 +237,48 @@ class TestCvMat < OpenCVTestCase
# m2 = m.median_blur(9)
# snap(['Original', m], ['Median blur', m2])
end
def test_threshold
m0 = Cv::Mat.zeros(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)
m = m0.threshold(25, 255, Cv::THRESH_BINARY)
expected = "<Cv::Mat:2x2,depth=0,channels=1,\n[ 0, 0;\n 255, 255]>"
assert_equal(expected, m.to_s)
m = m0.threshold(25, 255, Cv::THRESH_BINARY_INV)
expected = "<Cv::Mat:2x2,depth=0,channels=1,\n[255, 255;\n 0, 0]>"
assert_equal(expected, m.to_s)
m = m0.threshold(25, 255, Cv::THRESH_TRUNC)
expected = "<Cv::Mat:2x2,depth=0,channels=1,\n[ 10, 20;\n 25, 25]>"
assert_equal(expected, m.to_s)
m = m0.threshold(25, 255, Cv::THRESH_TOZERO)
expected = "<Cv::Mat:2x2,depth=0,channels=1,\n[ 0, 0;\n 30, 40]>"
assert_equal(expected, m.to_s)
m = m0.threshold(25, 255, Cv::THRESH_TOZERO_INV)
expected = "<Cv::Mat:2x2,depth=0,channels=1,\n[ 10, 20;\n 0, 0]>"
assert_equal(expected, m.to_s)
m, optimal_threshold = m0.threshold(25, 255, Cv::THRESH_BINARY | Cv::THRESH_OTSU)
expected = "<Cv::Mat:2x2,depth=0,channels=1,\n[ 0, 0;\n 255, 255]>"
assert_equal(expected, m.to_s)
assert_in_delta(20, optimal_threshold, 0.1)
m, optimal_threshold = m0.threshold(25, 255, Cv::THRESH_BINARY | Cv::THRESH_TRIANGLE)
expected = "<Cv::Mat:2x2,depth=0,channels=1,\n[ 0, 255;\n 255, 255]>"
assert_equal(expected, m.to_s)
assert_in_delta(12, optimal_threshold, 0.1)
# m0 = Cv::imread(FILENAME_LENA256x256, 0)
# m = m0.threshold(127, 255, Cv::THRESH_BINARY)
# w = Window.new('Original | Binary')
# w.show(Cv::hconcat([m0, m]))
# Cv::wait_key
end
end