2008-08-19 11:01:28 -04:00
|
|
|
/************************************************************
|
|
|
|
|
|
|
|
cvcontour.cpp -
|
|
|
|
|
|
|
|
$Author: lsxi $
|
|
|
|
|
|
|
|
Copyright (C) 2007 Masakazu Yonekura
|
|
|
|
|
|
|
|
************************************************************/
|
|
|
|
#include "cvcontour.h"
|
|
|
|
/*
|
|
|
|
* Document-class: OpenCV::CvContour
|
|
|
|
*
|
|
|
|
* Contour.
|
|
|
|
* CvMat#find_contours
|
|
|
|
*/
|
|
|
|
__NAMESPACE_BEGIN_OPENCV
|
|
|
|
__NAMESPACE_BEGIN_CVCONTOUR
|
|
|
|
|
2011-10-30 14:28:06 -04:00
|
|
|
#define APPROX_POLY_OPTION(op) rb_get_option_table(rb_klass, "APPROX_OPTION", op)
|
|
|
|
#define APPROX_POLY_METHOD(op) CVMETHOD("APPROX_POLY_METHOD", LOOKUP_CVMETHOD(op, "method"), CV_POLY_APPROX_DP)
|
2012-05-02 11:41:14 -04:00
|
|
|
#define APPROX_POLY_ACCURACY(op) NUM2DBL(LOOKUP_CVMETHOD(op, "accuracy"))
|
|
|
|
#define APPROX_POLY_RECURSIVE(op) TRUE_OR_FALSE(LOOKUP_CVMETHOD(op, "recursive"))
|
2008-08-19 11:01:28 -04:00
|
|
|
|
2011-02-13 14:40:25 -05:00
|
|
|
VALUE rb_allocate(VALUE klass);
|
|
|
|
void cvcontour_free(void *ptr);
|
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
VALUE rb_klass;
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_class()
|
|
|
|
{
|
|
|
|
return rb_klass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
define_ruby_class()
|
|
|
|
{
|
|
|
|
if (rb_klass)
|
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* opencv = rb_define_module("OpenCV");
|
|
|
|
* cvseq = rb_define_class_under(opencv, "CvSeq");
|
|
|
|
* curve = rb_define_module_under(opencv, "Curve");
|
|
|
|
* pointset = rb_define_module_under(opencv, "PointSet");
|
|
|
|
*
|
|
|
|
* note: this comment is used by rdoc.
|
|
|
|
*/
|
|
|
|
VALUE opencv = rb_module_opencv();
|
|
|
|
VALUE cvseq = cCvSeq::rb_class();
|
|
|
|
VALUE curve = mCurve::rb_module();
|
|
|
|
VALUE pointset = mPointSet::rb_module();
|
|
|
|
|
|
|
|
rb_klass = rb_define_class_under(opencv, "CvContour", cvseq);
|
|
|
|
rb_include_module(rb_klass, curve);
|
|
|
|
rb_include_module(rb_klass, pointset);
|
2011-02-13 14:40:25 -05:00
|
|
|
|
|
|
|
rb_define_alloc_func(rb_klass, rb_allocate);
|
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
VALUE approx_option = rb_hash_new();
|
|
|
|
rb_define_const(rb_klass, "APPROX_OPTION", approx_option);
|
|
|
|
rb_hash_aset(approx_option, ID2SYM(rb_intern("method")), INT2FIX(CV_POLY_APPROX_DP));
|
2012-05-02 11:41:14 -04:00
|
|
|
rb_hash_aset(approx_option, ID2SYM(rb_intern("accuracy")), rb_float_new(1.0));
|
|
|
|
rb_hash_aset(approx_option, ID2SYM(rb_intern("recursive")), Qfalse);
|
2008-08-19 11:01:28 -04:00
|
|
|
|
|
|
|
rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
|
|
|
|
rb_define_method(rb_klass, "rect", RUBY_METHOD_FUNC(rb_rect), 0);
|
|
|
|
rb_define_method(rb_klass, "color", RUBY_METHOD_FUNC(rb_color), 0);
|
|
|
|
rb_define_method(rb_klass, "color=", RUBY_METHOD_FUNC(rb_set_color), 1);
|
|
|
|
rb_define_method(rb_klass, "reserved", RUBY_METHOD_FUNC(rb_reserved), 0);
|
|
|
|
rb_define_method(rb_klass, "approx_poly", RUBY_METHOD_FUNC(rb_approx_poly), -1);
|
|
|
|
rb_define_alias(rb_klass, "approx", "approx_poly");
|
|
|
|
rb_define_method(rb_klass, "bounding_rect", RUBY_METHOD_FUNC(rb_bounding_rect), 0);
|
|
|
|
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);
|
2011-02-14 14:05:26 -05:00
|
|
|
rb_define_method(rb_klass, "point_polygon_test", RUBY_METHOD_FUNC(rb_point_polygon_test), 2);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
2011-02-13 14:40:25 -05:00
|
|
|
VALUE
|
|
|
|
rb_allocate(VALUE klass)
|
|
|
|
{
|
2011-08-03 14:09:30 -04:00
|
|
|
CvContour *ptr;
|
2011-08-11 11:53:48 -04:00
|
|
|
return Data_Make_Struct(klass, CvContour, mark_root_object, unregister_object, ptr);
|
2011-02-13 14:40:25 -05:00
|
|
|
}
|
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
VALUE
|
|
|
|
rb_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2011-08-03 14:09:30 -04:00
|
|
|
VALUE storage;
|
|
|
|
rb_scan_args(argc, argv, "01", &storage);
|
|
|
|
|
|
|
|
if (NIL_P(storage))
|
|
|
|
storage = cCvMemStorage::new_object(0);
|
2011-02-13 14:40:25 -05:00
|
|
|
else
|
2011-08-03 14:09:30 -04:00
|
|
|
storage = CHECK_CVMEMSTORAGE(storage);
|
2011-07-21 11:47:54 -04:00
|
|
|
|
2011-08-03 14:09:30 -04:00
|
|
|
CvContour* contour = (CvContour*)DATA_PTR(self);
|
2011-07-21 11:47:54 -04:00
|
|
|
try {
|
2011-08-03 14:09:30 -04:00
|
|
|
contour = (CvContour*)cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvContour),
|
|
|
|
sizeof(CvPoint), CVMEMSTORAGE(storage));
|
2011-07-21 11:47:54 -04:00
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2011-08-11 11:53:48 -04:00
|
|
|
register_root_object((CvSeq*)contour, storage);
|
2011-08-03 14:09:30 -04:00
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_rect(VALUE self)
|
|
|
|
{
|
|
|
|
return cCvRect::new_object(CVCONTOUR(self)->rect);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_color(VALUE self)
|
|
|
|
{
|
|
|
|
return INT2NUM(CVCONTOUR(self)->color);
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_set_color(VALUE self, VALUE color)
|
|
|
|
{
|
|
|
|
CVCONTOUR(self)->color = NUM2INT(color);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_reserved(VALUE self)
|
|
|
|
{
|
|
|
|
return rb_ary_new3(3,
|
|
|
|
INT2NUM(CVCONTOUR(self)->reserved[0]),
|
|
|
|
INT2NUM(CVCONTOUR(self)->reserved[1]),
|
|
|
|
INT2NUM(CVCONTOUR(self)->reserved[2]));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* approx_poly(<i>approx_poly_option</i>) -> cvcontour
|
|
|
|
*
|
|
|
|
* Approximates polygonal curve(s) with desired precision.
|
|
|
|
* <i>approx_poly_option</i> should be Hash include these keys.
|
|
|
|
* :method - Approximation method.
|
|
|
|
* :dp - corresponds to Douglas-Peucker algorithm.
|
|
|
|
* :accuracy - approximation accuracy. (high-accuracy will create more simple contours)
|
|
|
|
* :recursive - (default false)
|
|
|
|
* If not nil or false, the function approximates all chains that access can be obtained to
|
|
|
|
* from self by h_next or v_next links. If 0, approximated this one.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_approx_poly(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
2012-04-30 16:41:06 -04:00
|
|
|
VALUE approx_poly_option;
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_scan_args(argc, argv, "01", &approx_poly_option);
|
|
|
|
approx_poly_option = APPROX_POLY_OPTION(approx_poly_option);
|
2012-04-30 16:41:06 -04:00
|
|
|
VALUE storage = cCvMemStorage::new_object();
|
|
|
|
CvSeq *contour = cvApproxPoly(CVCONTOUR(self), sizeof(CvContour), CVMEMSTORAGE(storage),
|
|
|
|
APPROX_POLY_METHOD(approx_poly_option),
|
2012-05-02 11:41:14 -04:00
|
|
|
APPROX_POLY_ACCURACY(approx_poly_option),
|
|
|
|
APPROX_POLY_RECURSIVE(approx_poly_option));
|
2012-04-30 16:41:06 -04:00
|
|
|
|
|
|
|
if (contour && contour->total > 0) {
|
2011-08-03 14:09:30 -04:00
|
|
|
return cCvSeq::new_sequence(cCvContour::rb_class(), contour, cCvPoint::rb_class(), storage);
|
2012-04-30 16:41:06 -04:00
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* bounding_rect -> rect
|
|
|
|
*
|
|
|
|
* Calculates up-right bounding rectangle of point set.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_bounding_rect(VALUE self)
|
|
|
|
{
|
2011-07-21 11:47:54 -04:00
|
|
|
CvRect rect;
|
|
|
|
try {
|
|
|
|
rect = cvBoundingRect(CVCONTOUR(self), 1);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
|
|
|
return cCvRect::new_object(rect);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* create_tree([threshold = 0.0]) -> cvcontourtree
|
|
|
|
*
|
|
|
|
* Creates hierarchical representation of contour.
|
|
|
|
* If the parameter <i>threshold</i> is less than or equal to 0,
|
|
|
|
* the method creates full binary tree representation.
|
|
|
|
* If the threshold is greater than 0, the function creates
|
|
|
|
* representation with the precision threshold:
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_create_tree(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
VALUE threshold, storage;
|
|
|
|
rb_scan_args(argc, argv, "01", &threshold);
|
|
|
|
storage = cCvMemStorage::new_object();
|
2011-07-21 11:47:54 -04:00
|
|
|
CvContourTree *tree = NULL;
|
|
|
|
try {
|
|
|
|
tree = cvCreateContourTree(CVSEQ(self), CVMEMSTORAGE(storage), IF_DBL(threshold, 0.0));
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
return cCvSeq::new_sequence(cCvContourTree::rb_class(), (CvSeq*)tree, cCvPoint::rb_class(), storage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* in?(<i>point</i>) -> true or nil or false
|
|
|
|
*
|
2011-02-14 14:05:26 -05:00
|
|
|
* Determines whether the <i>point</i> is inside contour(true), outside(false) or lies on an edge(nil).
|
2008-08-19 11:01:28 -04:00
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_in_q(VALUE self, VALUE point)
|
|
|
|
{
|
2011-07-21 11:47:54 -04:00
|
|
|
double n = 0;
|
|
|
|
try {
|
|
|
|
n = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 0);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
return n == 0 ? Qnil : n > 0 ? Qtrue : Qfalse;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* measure_distance(<i>point</i>) -> float
|
|
|
|
*
|
|
|
|
* Return distance between the point and the nearest contour edge.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_measure_distance(VALUE self, VALUE point)
|
|
|
|
{
|
2011-07-21 11:47:54 -04:00
|
|
|
double distance = 0;
|
|
|
|
try {
|
|
|
|
distance = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 1);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
|
|
|
return rb_float_new(distance);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
2011-02-14 14:05:26 -05:00
|
|
|
/*
|
|
|
|
* 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;
|
2011-08-03 14:09:30 -04:00
|
|
|
|
2011-02-14 14:05:26 -05:00
|
|
|
if (measure_dist == Qtrue)
|
|
|
|
measure_dist_flag = 1;
|
|
|
|
else if (measure_dist == Qfalse)
|
|
|
|
measure_dist_flag = 0;
|
|
|
|
else
|
|
|
|
measure_dist_flag = NUM2INT(measure_dist);
|
|
|
|
|
2011-08-03 14:09:30 -04:00
|
|
|
double dist = Qnil;
|
2011-07-21 11:47:54 -04:00
|
|
|
try {
|
|
|
|
dist = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), measure_dist_flag);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
|
|
|
|
2011-02-14 14:05:26 -05:00
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
VALUE new_object()
|
|
|
|
{
|
2011-02-13 14:40:25 -05:00
|
|
|
VALUE object = rb_allocate(rb_klass);
|
|
|
|
rb_initialize(0, NULL, object);
|
2008-08-19 11:01:28 -04:00
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
|
|
|
__NAMESPACE_END_CVCONTOUR
|
|
|
|
__NAMESPACE_END_OPENCV
|
|
|
|
|