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

add Cv::Mat#sobel!

This commit is contained in:
ser1zw 2017-04-30 03:03:55 +09:00
parent da4e452100
commit 668e7b842a
4 changed files with 87 additions and 21 deletions

View file

@ -1232,6 +1232,7 @@ namespace rubyopencv {
rb_define_method(rb_klass, "to_s", RUBY_METHOD_FUNC(rb_to_s), 0);
rb_define_method(rb_klass, "sobel", RUBY_METHOD_FUNC(rb_sobel), -1); // in ext/opencv/mat_imgproc.cpp
rb_define_method(rb_klass, "sobel!", RUBY_METHOD_FUNC(rb_sobel_bang), -1); // in ext/opencv/mat_imgproc.cpp
rb_define_method(rb_klass, "canny", RUBY_METHOD_FUNC(rb_canny), -1); // in ext/opencv/mat_imgproc.cpp
rb_define_method(rb_klass, "laplacian", RUBY_METHOD_FUNC(rb_laplacian), -1); // in ext/opencv/mat_imgproc.cpp

View file

@ -11,6 +11,21 @@
*/
namespace rubyopencv {
namespace Mat {
cv::Mat* rb_sobel_internal(int argc, VALUE *argv, VALUE self, cv::Mat* destptr) {
VALUE ddepth, dx, dy, ksize, scale, delta, border_type;
rb_scan_args(argc, argv, "34", &ddepth ,&dx, &dy, &ksize, &scale, &delta, &border_type);
int ksize_value = NIL_P(ksize) ? 3 : NUM2INT(ksize);
double scale_value = NIL_P(scale) ? 1.0 : NUM2DBL(scale);
double delta_value = NIL_P(delta) ? 0.0 : NUM2DBL(delta);
int border_type_value = NIL_P(border_type) ? cv::BORDER_DEFAULT : NUM2INT(border_type);
cv::Mat* selfptr = obj2mat(self);
cv::Sobel(*selfptr, *destptr, NUM2INT(ddepth), NUM2INT(dx), NUM2INT(dy),
ksize_value, scale_value, delta_value, border_type_value);
return destptr;
}
/*
* Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator.
*
@ -26,27 +41,32 @@ namespace rubyopencv {
* @opencv_func cv::Sobel
*/
VALUE rb_sobel(int argc, VALUE *argv, VALUE self) {
VALUE ddepth, dx, dy, ksize, scale, delta, border_type;
rb_scan_args(argc, argv, "34", &ddepth ,&dx, &dy, &ksize, &scale, &delta, &border_type);
int ksize_value = NIL_P(ksize) ? 3 : NUM2INT(ksize);
double scale_value = NIL_P(scale) ? 1.0 : NUM2DBL(scale);
double delta_value = NIL_P(delta) ? 0.0 : NUM2DBL(delta);
int border_type_value = NIL_P(border_type) ? cv::BORDER_DEFAULT : NUM2INT(border_type);
cv::Mat* selfptr = obj2mat(self);
cv::Mat* destptr = new cv::Mat();
try {
cv::Sobel(*selfptr, *destptr, NUM2INT(ddepth), NUM2INT(dx), NUM2INT(dy),
ksize_value, scale_value, delta_value, border_type_value);
rb_sobel_internal(argc, argv, self, destptr);
}
catch (cv::Exception& e) {
delete destptr;
Error::raise(e);
}
return mat2obj(destptr, CLASS_OF(self));
}
/*
* @overload sobel!(ddepth, dx, dy, ksize = 3, scale = 1, delta = 0, border_type = BORDER_DEFAULT)
* @see #sobel
*/
VALUE rb_sobel_bang(int argc, VALUE *argv, VALUE self) {
cv::Mat* destptr = obj2mat(self);
try {
rb_sobel_internal(argc, argv, self, destptr);
}
catch (cv::Exception& e) {
Error::raise(e);
}
return self;
}
/**
* Finds edges in an image using the [Canny86] algorithm.
*

View file

@ -7,6 +7,7 @@
namespace rubyopencv {
namespace Mat {
VALUE rb_sobel(int argc, VALUE *argv, VALUE self);
VALUE rb_sobel_bang(int argc, VALUE *argv, VALUE self);
VALUE rb_canny(int argc, VALUE *argv, VALUE self);
VALUE rb_laplacian(int argc, VALUE *argv, VALUE self);
VALUE rb_cvt_color(int argc, VALUE *argv, VALUE self);

View file

@ -10,12 +10,12 @@ class TestCvMat < OpenCVTestCase
m0 = Cv::imread(FILENAME_LENA256x256, 0)
sobel = []
sobel << m0.sobel(CV_32F, 1, 1)
sobel << m0.sobel(CV_32F, 1, 1, 5, 0.5, 32, BORDER_CONSTANT)
sobel << m0.sobel(CV_8U, 1, 1)
sobel << m0.sobel(CV_8U, 1, 1, 5, 0.5, 32, BORDER_CONSTANT)
sobel.each { |m|
assert_equal(m0.rows, m.rows)
assert_equal(m0.cols, m.cols)
assert_equal(CV_32F, m.depth)
assert_equal(CV_8U, m.depth)
assert_equal(m0.dims, m.dims)
assert_equal(m0.channels, m.channels)
}
@ -24,26 +24,70 @@ class TestCvMat < OpenCVTestCase
m0.sobel(DUMMY_OBJ, 1, 1, 5, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel(CV_32F, DUMMY_OBJ, 1, 5, 0.5, 32, BORDER_CONSTANT)
m0.sobel(CV_8U, DUMMY_OBJ, 1, 5, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel(CV_32F, 1, DUMMY_OBJ, 5, 0.5, 32, BORDER_CONSTANT)
m0.sobel(CV_8U, 1, DUMMY_OBJ, 5, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel(CV_32F, 1, 1, DUMMY_OBJ, 0.5, 32, BORDER_CONSTANT)
m0.sobel(CV_8U, 1, 1, DUMMY_OBJ, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel(CV_32F, 1, 1, 5, DUMMY_OBJ, 32, BORDER_CONSTANT)
m0.sobel(CV_8U, 1, 1, 5, DUMMY_OBJ, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel(CV_32F, 1, 1, 5, 0.5, DUMMY_OBJ, BORDER_CONSTANT)
m0.sobel(CV_8U, 1, 1, 5, 0.5, DUMMY_OBJ, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel(CV_32F, 1, 1, 5, 0.5, 32, DUMMY_OBJ)
m0.sobel(CV_8U, 1, 1, 5, 0.5, 32, DUMMY_OBJ)
}
# w = Window.new('Sobel')
# w.show(m0.sobel(CV_32F, 1, 1))
# w.show(m0.sobel(CV_8U, 1, 1))
# Cv::wait_key
end
def test_sobel_bang
m0 = Cv::imread(FILENAME_LENA256x256, 0)
m1 = m0.clone
m0.sobel!(CV_8U, 1, 1)
m1.sobel!(CV_8U, 1, 1, 5, 0.5, 32, BORDER_CONSTANT)
sobel = [m0, m1]
sobel.each { |m|
assert_equal(m0.rows, m.rows)
assert_equal(m0.cols, m.cols)
assert_equal(CV_8U, m.depth)
assert_equal(m0.dims, m.dims)
assert_equal(m0.channels, m.channels)
}
assert_raise(TypeError) {
m0.sobel!(DUMMY_OBJ, 1, 1, 5, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel!(CV_8U, DUMMY_OBJ, 1, 5, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel!(CV_8U, 1, DUMMY_OBJ, 5, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel!(CV_8U, 1, 1, DUMMY_OBJ, 0.5, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel!(CV_8U, 1, 1, 5, DUMMY_OBJ, 32, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel!(CV_8U, 1, 1, 5, 0.5, DUMMY_OBJ, BORDER_CONSTANT)
}
assert_raise(TypeError) {
m0.sobel!(CV_8U, 1, 1, 5, 0.5, 32, DUMMY_OBJ)
}
# w = Window.new('Sobel')
# m0 = Cv::imread(FILENAME_LENA256x256, 0)
# m0.sobel!(CV_8U, 1, 1)
# w.show(m0)
# Cv::wait_key
end