From 8cd64f243ecb884ed018e9998116bac5690819aa Mon Sep 17 00:00:00 2001 From: ser1zw Date: Sun, 20 Mar 2011 22:53:56 +0900 Subject: [PATCH] tested CvCircle32f and CvMat#hough_circle_gradient --- ext/cvcircle32f.cpp | 29 +++++++++++++++++++-- ext/cvcircle32f.h | 3 +-- ext/cvmat.cpp | 2 +- test/test_cvcircle32f.rb | 41 ++++++++++++++++++++++++++++++ test/test_cvmat_imageprocessing.rb | 21 +++++++++++++++ 5 files changed, 91 insertions(+), 5 deletions(-) create mode 100755 test/test_cvcircle32f.rb diff --git a/ext/cvcircle32f.cpp b/ext/cvcircle32f.cpp index 68a754e..ed44f09 100644 --- a/ext/cvcircle32f.cpp +++ b/ext/cvcircle32f.cpp @@ -42,8 +42,7 @@ define_ruby_class() rb_define_alloc_func(rb_klass, rb_allocate); rb_define_method(rb_klass, "center", RUBY_METHOD_FUNC(rb_center), 0); rb_define_method(rb_klass, "radius", RUBY_METHOD_FUNC(rb_radius), 0); - - //rb_define_method(rb_klass, "to_s", RUBY_METHOD_FUNC(rb_to_s), 0); + rb_define_method(rb_klass, "[]", RUBY_METHOD_FUNC(rb_aref), 1); rb_define_method(rb_klass, "to_ary", RUBY_METHOD_FUNC(rb_to_ary), 0); rb_define_alias(rb_klass, "to_a", "to_ary"); } @@ -73,6 +72,32 @@ rb_radius(VALUE self) return rb_float_new(CVCIRCLE32F(self)->radius); } +/* + * call-seq: + * [index] + * + * Return value of index dimension. + */ +VALUE +rb_aref(VALUE self, VALUE index) +{ + switch (NUM2INT(index)) { + case 0: + return DBL2NUM(CVCIRCLE32F(self)->center.x); + break; + case 1: + return DBL2NUM(CVCIRCLE32F(self)->center.y); + break; + case 2: + return DBL2NUM(CVCIRCLE32F(self)->radius); + break; + default: + rb_raise(rb_eIndexError, "index should be 0...3"); + break; + } + return Qnil; +} + VALUE rb_to_ary(VALUE self) { diff --git a/ext/cvcircle32f.h b/ext/cvcircle32f.h index cccfa36..d2bd141 100644 --- a/ext/cvcircle32f.h +++ b/ext/cvcircle32f.h @@ -32,8 +32,7 @@ VALUE rb_allocate(VALUE klass); VALUE rb_initialize(int argc, VALUE *argv, VALUE self); VALUE rb_center(VALUE self); VALUE rb_radius(VALUE self); - -VALUE rb_to_s(VALUE self); +VALUE rb_aref(VALUE self, VALUE index); VALUE rb_to_ary(VALUE self); VALUE new_object(CvCircle32f circle32f); diff --git a/ext/cvmat.cpp b/ext/cvmat.cpp index 3b6539a..80ed23f 100644 --- a/ext/cvmat.cpp +++ b/ext/cvmat.cpp @@ -4491,7 +4491,7 @@ rb_hough_circles_gradient(int argc, VALUE *argv, VALUE self) { SUPPORT_8UC1_ONLY(self); VALUE dp, min_dist, threshold_canny, threshold_accumulate, min_radius, max_radius, storage; - rb_scan_args(argc, argv, "24", &dp, &min_dist, &threshold_canny, &threshold_accumulate, &min_radius, &max_radius); + rb_scan_args(argc, argv, "42", &dp, &min_dist, &threshold_canny, &threshold_accumulate, &min_radius, &max_radius); storage = cCvMemStorage::new_object(); CvSeq *seq = cvHoughCircles(CVARR(self), CVMEMSTORAGE(storage), CV_HOUGH_GRADIENT, NUM2DBL(dp), NUM2DBL(min_dist), diff --git a/test/test_cvcircle32f.rb b/test/test_cvcircle32f.rb new file mode 100755 index 0000000..786cfde --- /dev/null +++ b/test/test_cvcircle32f.rb @@ -0,0 +1,41 @@ +#!/usr/bin/env ruby +# -*- mode: ruby; coding: utf-8-unix -*- +require 'test/unit' +require 'opencv' +require File.expand_path(File.dirname(__FILE__)) + '/helper' + +include OpenCV + +# Tests for OpenCV::CvCircle32f +class TestCvCircle32f < OpenCVTestCase + def setup + @circle = CvCircle32f.new + end + + def test_initialize + assert_equal(CvCircle32f, @circle.class) + end + + def test_center + assert_equal(CvPoint2D32f, @circle.center.class) + end + + def test_radius + assert_equal(Float, @circle.radius.class) + end + + def test_aref + assert_equal(Float, @circle[0].class) + assert_equal(Float, @circle[1].class) + assert_equal(Float, @circle[2].class) + assert_raise(IndexError) { + @circle[3] + } + end + + def test_to_ary + assert_equal(Array, @circle.to_ary.class) + assert_equal(2, @circle.to_ary.size) + end +end + diff --git a/test/test_cvmat_imageprocessing.rb b/test/test_cvmat_imageprocessing.rb index 525f5bf..bce5106 100755 --- a/test/test_cvmat_imageprocessing.rb +++ b/test/test_cvmat_imageprocessing.rb @@ -1370,5 +1370,26 @@ class TestCvMat_imageprocessing < OpenCVTestCase # } # snap mat0 end + + def test_hough_circles_gradient + mat0 = CvMat.load(FILENAME_LINES, CV_LOAD_IMAGE_ANYCOLOR | CV_LOAD_IMAGE_ANYDEPTH) + # make a binary image + mat = CvMat.new(mat0.rows, mat0.cols, :cv8u, 1) + (mat0.rows * mat0.cols).times { |i| + mat[i] = (mat0[i][0] <= 100) ? CvScalar.new(0) : CvScalar.new(255); + } + + [mat.hough_circles_gradient(1.5, 40, 100, 50, 10, 50), + mat.hough_circles_gradient(1.5, 40, 100, 50)].each { |seq| + assert_equal(2, seq.size) + } + + # Uncomment the following lines to show the result + # seq = mat.hough_circles_gradient(1.5, 40, 100, 50, 10, 50) + # seq.each { |circle| + # mat0.circle!(circle.center, circle.radius, :color => CvColor::Red, :thickness => 2) + # } + # snap mat0 + end end