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

implemented CvContour#point_polygon_test, and tested functions of CvContour

This commit is contained in:
ser1zw 2011-02-15 04:05:26 +09:00
parent ff3351ea64
commit a9b92b2239
4 changed files with 97 additions and 1 deletions

View file

@ -74,6 +74,7 @@ define_ruby_class()
rb_define_method(rb_klass, "create_tree", RUBY_METHOD_FUNC(rb_create_tree), -1);
rb_define_method(rb_klass, "in?", RUBY_METHOD_FUNC(rb_in_q), 1);
rb_define_method(rb_klass, "measure_distance", RUBY_METHOD_FUNC(rb_measure_distance), 1);
rb_define_method(rb_klass, "point_polygon_test", RUBY_METHOD_FUNC(rb_point_polygon_test), 2);
}
VALUE
@ -206,7 +207,7 @@ rb_create_tree(int argc, VALUE *argv, VALUE self)
* call-seq:
* in?(<i>point</i>) -> true or nil or false
*
* Determines whether the <i>point</i> is inside contour(true), outside(false), or lies on an edge(nil).
* Determines whether the <i>point</i> is inside contour(true), outside(false) or lies on an edge(nil).
*/
VALUE
rb_in_q(VALUE self, VALUE point)
@ -227,6 +228,35 @@ rb_measure_distance(VALUE self, VALUE point)
return rb_float_new(cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 1));
}
/*
* call-seq:
* point_polygon_test(<i>point, measure_dist</i>) -> float
*
* Determines whether the point is inside a contour, outside, or lies on an edge (or coinsides with a vertex).
* It returns positive, negative or zero value, correspondingly. When measure_dist = false or 0, the return value is +1, -1 and 0, respectively. When measure_dist = true or 1, it is a signed distance between the point and the nearest contour edge.
*/
VALUE
rb_point_polygon_test(VALUE self, VALUE point, VALUE measure_dist)
{
int measure_dist_flag;
double dist;
if (measure_dist == Qtrue)
measure_dist_flag = 1;
else if (measure_dist == Qfalse)
measure_dist_flag = 0;
else
measure_dist_flag = NUM2INT(measure_dist);
dist = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), measure_dist_flag);
/* cvPointPolygonTest returns 100, -100 or 0 when measure_dist = 0 */
if ((!measure_dist_flag) && ((int)dist) != 0)
dist = (dist > 0) ? 1 : -1;
return rb_float_new(dist);
}
VALUE new_object()
{
VALUE object = rb_allocate(rb_klass);

View file

@ -31,6 +31,7 @@ VALUE rb_bounding_rect(VALUE self);
VALUE rb_create_tree(int argc, VALUE *argv, VALUE self);
VALUE rb_in_q(VALUE self, VALUE point);
VALUE rb_measure_distance(VALUE self, VALUE point);
VALUE rb_point_polygon_test(VALUE self, VALUE point, VALUE measure_dist);
VALUE new_object();
__NAMESPACE_END_CVCONTOUR

View file

@ -72,6 +72,12 @@ class OpenCVTestCase < Test::Unit::TestCase
Digest::MD5.hexdigest(img.data)
end
unless Test::Unit::TestCase.instance_methods.map {|m| m.to_sym }.include? :assert_false
def assert_false(actual, message = nil)
assert_equal(false, actual, message)
end
end
alias original_assert_in_delta assert_in_delta
def assert_cvscalar_equal(expected, actual, message = nil)

View file

@ -33,5 +33,64 @@ class TestCvContour < OpenCVTestCase
assert_equal(Array, reserved.class)
assert_equal(3, reserved.size)
end
def test_approx_poly
flunk('FIXME: CvContour#approx_poly is not implemented yet.')
end
def test_bounding_rect
mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
(j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
}
contours = mat0.find_contours
rect = contours.bounding_rect
assert_equal(CvRect, rect.class)
assert_equal(32, rect.x)
assert_equal(32, rect.y)
assert_equal(65, rect.width)
assert_equal(65, rect.height)
end
def test_create_tree
flunk('FIXME: CvContour#create_tree is not tested yet.')
end
def test_in
mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
(j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
}
contour = mat0.find_contours
assert(contour.in? CvPoint.new(64, 64))
assert_false(contour.in? CvPoint.new(0, 0))
assert_nil(contour.in? CvPoint.new(64, 32))
end
def test_measure_distance
mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
(j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
}
contour = mat0.find_contours
assert_in_delta(-0.7071, contour.measure_distance(CvPoint.new(63, 32)), 0.01)
assert_in_delta(31.01, contour.measure_distance(CvPoint.new(64, 64)), 0.01)
end
def test_point_polygon_test
mat0 = create_cvmat(128, 128, :cv8u, 1) { |j, i|
(j - 64) ** 2 + (i - 64) ** 2 <= (32 ** 2) ? CvColor::White : CvColor::Black
}
contour = mat0.find_contours
assert_equal(1, contour.point_polygon_test(CvPoint.new(64, 64), 0))
assert_equal(1, contour.point_polygon_test(CvPoint.new(64, 64), false))
assert_equal(-1, contour.point_polygon_test(CvPoint.new(0, 0), 0))
assert_equal(-1, contour.point_polygon_test(CvPoint.new(0, 0), false))
assert_equal(0, contour.point_polygon_test(CvPoint.new(64, 32), 0))
assert_equal(0, contour.point_polygon_test(CvPoint.new(64, 32), false))
assert_in_delta(-0.7071, contour.point_polygon_test(CvPoint.new(63, 32), 1), 0.01)
assert_in_delta(-0.7071, contour.point_polygon_test(CvPoint.new(63, 32), true), 0.01)
assert_in_delta(31.01, contour.point_polygon_test(CvPoint.new(64, 64), 1), 0.01)
assert_in_delta(31.01, contour.point_polygon_test(CvPoint.new(64, 64), true), 0.01)
end
end