From 6b6a771207e263f8785830e7ae984071a7375af6 Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 30 Jan 2011 01:39:57 +0900 Subject: [PATCH] added symbols for CvMat#morphology and CvMat#smooth --- ext/cvmat.cpp | 5 ++- ext/opencv.cpp | 19 ++++++++++ test/test_cvmat_imageprocessing.rb | 61 ++++++++++++++++++++++++------ test/test_opencv.rb | 14 +++++++ 4 files changed, 86 insertions(+), 13 deletions(-) diff --git a/ext/cvmat.cpp b/ext/cvmat.cpp index 7847c19..16e1592 100644 --- a/ext/cvmat.cpp +++ b/ext/cvmat.cpp @@ -3571,7 +3571,7 @@ rb_morphology(int argc, VALUE *argv, VALUE self) { VALUE element, iteration, operation; rb_scan_args(argc, argv, "12", &operation, &element, &iteration); - return rb_morphology_internal(element, iteration, FIX2INT(operation), self); + return rb_morphology_internal(element, iteration, CVMETHOD("MORPHOLOGICAL_OPERATION", operation, -1), self); } /* @@ -3654,7 +3654,8 @@ rb_smooth(int argc, VALUE *argv, VALUE self) { VALUE smoothtype, p1, p2, p3, p4; rb_scan_args(argc, argv, "14", &smoothtype, &p1, &p2, &p3, &p4); - int _smoothtype = FIX2INT(smoothtype); + int _smoothtype = CVMETHOD("SMOOTHING_TYPE", smoothtype, -1); + VALUE (*smooth_func)(int c, VALUE* v, VALUE s); switch (_smoothtype) { case CV_BLUR_NO_SCALE: diff --git a/ext/opencv.cpp b/ext/opencv.cpp index cbaa324..23eeb37 100644 --- a/ext/opencv.cpp +++ b/ext/opencv.cpp @@ -301,6 +301,25 @@ define_ruby_module() RESIST_CVMETHOD(match_template_method, "ccoeff", CV_TM_CCOEFF); RESIST_CVMETHOD(match_template_method, "ccoeff_normed", CV_TM_CCOEFF_NORMED); + VALUE morphological_operation = rb_hash_new(); + /* {:open, :close, :gradient, :tophat, :blackhat}: Types of morphological operations */ + rb_define_const(rb_module, "MORPHOLOGICAL_OPERATION", morphological_operation); + RESIST_CVMETHOD(morphological_operation, "open", CV_MOP_OPEN); + RESIST_CVMETHOD(morphological_operation, "close", CV_MOP_CLOSE); + RESIST_CVMETHOD(morphological_operation, "gradient", CV_MOP_GRADIENT); + RESIST_CVMETHOD(morphological_operation, "tophat", CV_MOP_TOPHAT); + RESIST_CVMETHOD(morphological_operation, "blackhat", CV_MOP_BLACKHAT); + + VALUE smoothing_type = rb_hash_new(); + /* {:blur_no_scale, :blur, :gaussian, :median, :bilateral}: Types of smoothing */ + rb_define_const(rb_module, "SMOOTHING_TYPE", smoothing_type); + RESIST_CVMETHOD(smoothing_type, "blur_no_scale", CV_BLUR_NO_SCALE); + RESIST_CVMETHOD(smoothing_type, "blur", CV_BLUR); + RESIST_CVMETHOD(smoothing_type, "gaussian", CV_GAUSSIAN); + RESIST_CVMETHOD(smoothing_type, "median", CV_MEDIAN); + RESIST_CVMETHOD(smoothing_type, "bilateral", CV_BILATERAL); + + /* color convert methods */ rb_define_module_function(rb_module, "BGR2BGRA", RUBY_METHOD_FUNC(rb_BGR2BGRA), 1); rb_define_module_function(rb_module, "RGB2RGBA", RUBY_METHOD_FUNC(rb_RGB2RGBA), 1); diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index ad361bd..289c654 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -493,6 +493,47 @@ class TestCvMat_imageprocessing < OpenCVTestCase assert_equal('9f02fc4438b1d69fea75a10dfd2b66b0', hash_img(mat6)) end + def test_morphology + mat0 = create_cvmat(64, 64, :cv8u, 1) { |j, i, c| + if i >= 8 and i < 56 and j >= 8 and j < 56 and (i + j) % 15 != 0 + CvScalar.new(255) + else + CvScalar.new(0) + end + } + + # Open + kernel = IplConvKernel.new(5, 5, 2, 2, :cross) + mat1 = mat0.morphology(CV_MOP_OPEN, kernel) + mat2 = mat0.morphology(:open, kernel) + assert_equal('63ccb07cb93efb1563657f51e3d89252', hash_img(mat1)) + assert_equal('63ccb07cb93efb1563657f51e3d89252', hash_img(mat2)) + + # Close + mat1 = mat0.morphology(CV_MOP_CLOSE, kernel) + mat2 = mat0.morphology(:close, kernel) + assert_equal('831c513d6ed86bce3f15c697de4a72f8', hash_img(mat1)) + assert_equal('831c513d6ed86bce3f15c697de4a72f8', hash_img(mat2)) + + # Gradient + mat1 = mat0.morphology(CV_MOP_GRADIENT, kernel) + mat2 = mat0.morphology(:gradient, kernel) + assert_equal('1e8007c211d6f464cf8584e8e83b3c35', hash_img(mat1)) + assert_equal('1e8007c211d6f464cf8584e8e83b3c35', hash_img(mat2)) + + # Top hat + mat1 = mat0.morphology(CV_MOP_TOPHAT, kernel) + mat2 = mat0.morphology(CV_MOP_TOPHAT, kernel) + assert_equal('1760c5b63a52df37069164fe3e901aa4', hash_img(mat1)) + assert_equal('1760c5b63a52df37069164fe3e901aa4', hash_img(mat2)) + + # Black hat + mat1 = mat0.morphology(CV_MOP_BLACKHAT, kernel) + mat2 = mat0.morphology(:blackhat, kernel) + assert_equal('18b1d51637b912a38133341ee006c6ff', hash_img(mat1)) + assert_equal('18b1d51637b912a38133341ee006c6ff', hash_img(mat2)) + end + def test_morphology_open mat0 = create_cvmat(64, 64, :cv8u, 1) { |j, i, c| if i >= 8 and i < 56 and j >= 8 and j < 56 and (i + j) % 15 != 0 @@ -506,12 +547,10 @@ class TestCvMat_imageprocessing < OpenCVTestCase mat2 = mat0.morphology_open(nil, 2) kernel = IplConvKernel.new(5, 5, 2, 2, :cross) mat3 = mat0.morphology_open(kernel) - mat4 = mat0.morphology(CV_MOP_OPEN, kernel) assert_equal('165c36ad069db33735f0d4c2823f43b7', hash_img(mat1)) assert_equal('e5af47b2827ed20450222321c1678ed3', hash_img(mat2)) assert_equal('63ccb07cb93efb1563657f51e3d89252', hash_img(mat3)) - assert_equal('63ccb07cb93efb1563657f51e3d89252', hash_img(mat4)) end def test_morphology_close @@ -603,9 +642,9 @@ class TestCvMat_imageprocessing < OpenCVTestCase # Blur no scale mat1 = mat0.smooth(CV_BLUR_NO_SCALE) - mat2 = mat0.smooth(CV_BLUR_NO_SCALE, 3, 3) + mat2 = mat0.smooth(:blur_no_scale, 3, 3) mat3 = mat0.smooth(CV_BLUR_NO_SCALE, 7, 7) - mat4 = CvMat.new(32, 32, :cv32f, 1).smooth_blur_no_scale + mat4 = CvMat.new(32, 32, :cv32f, 1).smooth(:blur_no_scale) [mat1, mat2, mat3].each { |m| assert_equal(1, m.channel) @@ -620,11 +659,11 @@ class TestCvMat_imageprocessing < OpenCVTestCase # Blur mat1 = mat0.smooth(CV_BLUR) - mat2 = mat0.smooth(CV_BLUR, 3, 3) + mat2 = mat0.smooth(:blur, 3, 3) mat3 = mat0.smooth(CV_BLUR, 7, 7) - mat4 = CvMat.new(32, 32, :cv16u, 1).smooth(CV_BLUR) + mat4 = CvMat.new(32, 32, :cv16u, 1).smooth(:blur) mat5 = CvMat.new(32, 32, :cv32f, 1).smooth(CV_BLUR) - mat6 = CvMat.new(32, 32, :cv8u, 3).smooth(CV_BLUR) + mat6 = CvMat.new(32, 32, :cv8u, 3).smooth(:blur) [mat1, mat2, mat3].each { |m| assert_equal(1, m.channel) @@ -643,9 +682,9 @@ class TestCvMat_imageprocessing < OpenCVTestCase # Gaussian mat1 = mat0.smooth(CV_GAUSSIAN) - mat2 = mat0.smooth(CV_GAUSSIAN, 3, 3) + mat2 = mat0.smooth(:gaussian, 3, 3) mat3 = mat0.smooth(CV_GAUSSIAN, 3, 3, 3) - mat4 = mat0.smooth(CV_GAUSSIAN, 3, 3, 3, 3) + mat4 = mat0.smooth(:gaussian, 3, 3, 3, 3) mat5 = mat0.smooth(CV_GAUSSIAN, 7, 7, 5, 3) mat6 = CvMat.new(32, 32, :cv16u, 1).smooth(CV_GAUSSIAN) @@ -684,7 +723,7 @@ class TestCvMat_imageprocessing < OpenCVTestCase } mat1 = mat0.smooth(CV_MEDIAN) - mat2 = mat0.smooth(CV_MEDIAN, 3) + mat2 = mat0.smooth(:median, 3) mat3 = mat0.smooth(CV_MEDIAN, 7) mat4 = CvMat.new(64, 64, :cv8u, 3).smooth(CV_MEDIAN) @@ -702,7 +741,7 @@ class TestCvMat_imageprocessing < OpenCVTestCase } mat1 = mat0.smooth(CV_BILATERAL) - mat2 = mat0.smooth(CV_BILATERAL, 3, 3) + mat2 = mat0.smooth(:bilateral, 3, 3) mat3 = mat0.smooth(CV_BILATERAL, 7, 7) mat4 = CvMat.new(64, 64, :cv8u, 3).smooth(CV_BILATERAL) flunk('FIXME: Cases of CvMat#smooth(CV_BILATERAL) are not tested yet.') diff --git a/test/test_opencv.rb b/test/test_opencv.rb index 78683d0..4e805d8 100755 --- a/test/test_opencv.rb +++ b/test/test_opencv.rb @@ -115,6 +115,20 @@ class TestOpenCV < OpenCVTestCase assert_equal(1, STRUCTURING_ELEMENT_SHAPE[:cross]) assert_equal(2, STRUCTURING_ELEMENT_SHAPE[:ellipse]) assert_equal(100, STRUCTURING_ELEMENT_SHAPE[:custom]) + + # Types of morphological operations + assert_equal(2, MORPHOLOGICAL_OPERATION[:open]) + assert_equal(3, MORPHOLOGICAL_OPERATION[:close]) + assert_equal(4, MORPHOLOGICAL_OPERATION[:gradient]) + assert_equal(5, MORPHOLOGICAL_OPERATION[:tophat]) + assert_equal(6, MORPHOLOGICAL_OPERATION[:blackhat]) + + # Types of the smoothing + assert_equal(0, SMOOTHING_TYPE[:blur_no_scale]) + assert_equal(1, SMOOTHING_TYPE[:blur]) + assert_equal(2, SMOOTHING_TYPE[:gaussian]) + assert_equal(3, SMOOTHING_TYPE[:median]) + assert_equal(4, SMOOTHING_TYPE[:bilateral]) end def test_cvt_color_funcs