2008-08-19 11:01:28 -04:00
|
|
|
/************************************************************
|
|
|
|
|
|
|
|
cvchain.cpp -
|
|
|
|
|
|
|
|
$Author: lsxi $
|
|
|
|
|
|
|
|
Copyright (C) 2007 Masakazu Yonekura
|
|
|
|
|
|
|
|
************************************************************/
|
|
|
|
#include "cvchain.h"
|
|
|
|
/*
|
|
|
|
* Document-class: OpenCV::CvChain
|
|
|
|
*
|
|
|
|
* Freeman chain code.
|
|
|
|
* CvMat#find_contours(:method => :code)
|
|
|
|
*/
|
|
|
|
__NAMESPACE_BEGIN_OPENCV
|
|
|
|
__NAMESPACE_BEGIN_CVCHAIN
|
|
|
|
|
2011-10-30 14:28:06 -04:00
|
|
|
#define APPROX_CHAIN_OPTION(op) rb_get_option_table(rb_klass, "APPROX_CHAIN_OPTION", op)
|
|
|
|
#define APPROX_CHAIN_METHOD(op) CVMETHOD("APPROX_CHAIN_METHOD", LOOKUP_CVMETHOD(op, "method"), CV_CHAIN_APPROX_SIMPLE)
|
|
|
|
#define APPROX_CHAIN_PARAMETER(op) NUM2INT(LOOKUP_CVMETHOD(op, "parameter"))
|
2012-04-30 16:19:58 -04:00
|
|
|
#define APPROX_CHAIN_MINIMAL_PERIMETER(op) NUM2INT(LOOKUP_CVMETHOD(op, "minimal_perimeter"))
|
2011-10-30 14:28:06 -04:00
|
|
|
#define APPROX_CHAIN_RECURSIVE(op) TRUE_OR_FALSE(LOOKUP_CVMETHOD(op, "recursive"))
|
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");
|
|
|
|
* note: this comment is used by rdoc.
|
|
|
|
*/
|
|
|
|
VALUE opencv = rb_module_opencv();
|
|
|
|
VALUE cvseq = cCvSeq::rb_class();
|
|
|
|
VALUE curve = mCurve::rb_module();
|
|
|
|
|
|
|
|
rb_klass = rb_define_class_under(opencv, "CvChain", cvseq);
|
|
|
|
rb_include_module(rb_klass, curve);
|
|
|
|
VALUE approx_chain_option = rb_hash_new();
|
|
|
|
rb_define_const(rb_klass, "APPROX_CHAIN_OPTION", approx_chain_option);
|
|
|
|
rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("method")), ID2SYM(rb_intern("approx_simple")));
|
|
|
|
rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("parameter")), rb_float_new(0));
|
2012-04-30 16:19:58 -04:00
|
|
|
rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("minimal_perimeter")), INT2FIX(0));
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_hash_aset(approx_chain_option, ID2SYM(rb_intern("recursive")), Qfalse);
|
|
|
|
|
2011-02-15 11:36:46 -05:00
|
|
|
rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_define_method(rb_klass, "origin", RUBY_METHOD_FUNC(rb_origin), 0);
|
2011-02-15 11:36:46 -05:00
|
|
|
rb_define_method(rb_klass, "origin=", RUBY_METHOD_FUNC(rb_set_origin), 1);
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_define_method(rb_klass, "codes", RUBY_METHOD_FUNC(rb_codes), 0);
|
|
|
|
rb_define_method(rb_klass, "points", RUBY_METHOD_FUNC(rb_points), 0);
|
2012-04-30 16:19:58 -04:00
|
|
|
rb_define_method(rb_klass, "approx_chains", RUBY_METHOD_FUNC(rb_approx_chains), -1);
|
|
|
|
rb_define_alias(rb_klass, "approx", "approx_chains");
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
2011-02-15 11:36:46 -05:00
|
|
|
VALUE
|
|
|
|
rb_allocate(VALUE klass)
|
|
|
|
{
|
2011-08-03 14:09:30 -04:00
|
|
|
CvChain *ptr;
|
|
|
|
return Data_Make_Struct(klass, CvChain, 0, 0, ptr);
|
2011-02-15 11:36:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
rb_initialize(int argc, VALUE *argv, VALUE self)
|
|
|
|
{
|
|
|
|
CvMemStorage *storage;
|
|
|
|
VALUE storage_value;
|
|
|
|
if (rb_scan_args(argc, argv, "01", &storage_value) > 0) {
|
|
|
|
storage_value = CHECK_CVMEMSTORAGE(storage_value);
|
|
|
|
storage = CVMEMSTORAGE(storage_value);
|
|
|
|
}
|
|
|
|
else
|
2011-04-27 11:36:34 -04:00
|
|
|
storage = rb_cvCreateMemStorage(0);
|
2011-07-21 11:47:54 -04:00
|
|
|
try {
|
|
|
|
DATA_PTR(self) = (CvChain*)cvCreateSeq(CV_SEQ_ELTYPE_CODE, sizeof(CvChain),
|
|
|
|
sizeof(char), storage);
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
2011-02-15 11:36:46 -05:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* origin -> cvpoint
|
|
|
|
*
|
|
|
|
* Return Freeman chain code origin.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_origin(VALUE self)
|
|
|
|
{
|
|
|
|
return cCvPoint::new_object(CVCHAIN(self)->origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* origin = point -> self
|
|
|
|
*
|
|
|
|
* Set Freeman chain code origin.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_set_origin(VALUE self, VALUE origin)
|
|
|
|
{
|
|
|
|
CVCHAIN(self)->origin = VALUE_TO_CVPOINT(origin);
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* codes -> array(contain fixnum)
|
|
|
|
*
|
|
|
|
* Return Freeman chain codes.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_codes(VALUE self)
|
|
|
|
{
|
|
|
|
CvChain *chain = CVCHAIN(self);
|
|
|
|
CvChainPtReader reader;
|
2011-05-23 11:25:58 -04:00
|
|
|
int total = chain->total;
|
|
|
|
VALUE ary = rb_ary_new2(total);
|
2011-07-21 11:47:54 -04:00
|
|
|
try {
|
|
|
|
cvStartReadChainPoints(chain, &reader);
|
|
|
|
for (int i = 0; i < total; ++i) {
|
|
|
|
CV_READ_SEQ_ELEM(reader.code, (*((CvSeqReader*)&(reader))));
|
|
|
|
rb_ary_store(ary, i, CHR2FIX(reader.code));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* points -> array(contain cvpoint)
|
|
|
|
*
|
|
|
|
* Return points that represent by Freeman chain code.
|
|
|
|
*/
|
|
|
|
VALUE
|
|
|
|
rb_points(VALUE self)
|
|
|
|
{
|
|
|
|
CvChain *chain = CVCHAIN(self);
|
|
|
|
CvChainPtReader reader;
|
|
|
|
CvPoint p = chain->origin;
|
2011-05-23 11:25:58 -04:00
|
|
|
int total = chain->total;
|
|
|
|
VALUE ary = rb_ary_new2(total);
|
2011-07-21 11:47:54 -04:00
|
|
|
try {
|
|
|
|
cvStartReadChainPoints(chain, &reader);
|
|
|
|
for (int i = 0; i < total; ++i) {
|
|
|
|
CV_READ_CHAIN_POINT(p, reader);
|
|
|
|
rb_ary_store(ary, i, cCvPoint::new_object(p));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* call-seq:
|
2012-04-30 16:19:58 -04:00
|
|
|
* approx_chains(<i>[approx_chain_option]</i>) -> cvcontour
|
2008-08-19 11:01:28 -04:00
|
|
|
*
|
|
|
|
* Approximates Freeman chain(s) with polygonal curve.
|
|
|
|
* <i>approx_chain_option</i> should be Hash include these keys.
|
|
|
|
* :method - Approximation method.
|
|
|
|
* :approx_none - translate all the points from the chain code into points;
|
|
|
|
* :approx_simple(default) - compress horizontal, vertical, and diagonal segments, that is,
|
|
|
|
* the function leaves only their ending points.
|
|
|
|
* :approx_tc89_l1
|
|
|
|
* :approx_tc89_kcos - apply one of the flavors of Teh-Chin chain approximation algorithm.
|
|
|
|
* If set the difference between the current pixel and seed pixel is considered,
|
|
|
|
* otherwise difference between neighbor pixels is considered (the range is floating).
|
|
|
|
* :parameter - Method parameter (not used now).
|
|
|
|
* :minimal_perimeter (default 0)
|
|
|
|
* Approximates only those contours whose perimeters are not less than minimal_perimeter. Other chains are removed from the resulting structure.
|
|
|
|
* :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, the single chain is approximated.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
VALUE
|
2012-04-30 16:19:58 -04:00
|
|
|
rb_approx_chains(int argc, VALUE *argv, VALUE self)
|
2008-08-19 11:01:28 -04:00
|
|
|
{
|
2012-04-30 16:19:58 -04:00
|
|
|
VALUE approx_chain_option;
|
2008-08-19 11:01:28 -04:00
|
|
|
rb_scan_args(argc, argv, "01", &approx_chain_option);
|
2012-04-30 16:19:58 -04:00
|
|
|
|
2008-08-19 11:01:28 -04:00
|
|
|
approx_chain_option = APPROX_CHAIN_OPTION(approx_chain_option);
|
2012-04-30 16:19:58 -04:00
|
|
|
VALUE storage = cCvMemStorage::new_object();
|
2008-08-19 11:01:28 -04:00
|
|
|
CvSeq *seq = cvApproxChains(CVSEQ(self), CVMEMSTORAGE(storage),
|
|
|
|
APPROX_CHAIN_METHOD(approx_chain_option),
|
2012-04-30 16:19:58 -04:00
|
|
|
APPROX_CHAIN_PARAMETER(approx_chain_option),
|
|
|
|
APPROX_CHAIN_MINIMAL_PERIMETER(approx_chain_option),
|
2008-08-19 11:01:28 -04:00
|
|
|
APPROX_CHAIN_RECURSIVE(approx_chain_option));
|
2012-04-30 16:19:58 -04:00
|
|
|
|
|
|
|
if (seq && seq->total > 0) {
|
|
|
|
return cCvSeq::new_sequence(cCvChain::rb_class(), seq, cCvPoint::rb_class(), storage);
|
|
|
|
}
|
2008-08-19 11:01:28 -04:00
|
|
|
return Qnil;
|
|
|
|
}
|
|
|
|
|
|
|
|
VALUE
|
|
|
|
new_object()
|
|
|
|
{
|
|
|
|
VALUE storage = cCvMemStorage::new_object();
|
2011-07-21 11:47:54 -04:00
|
|
|
CvSeq *seq = NULL;
|
|
|
|
try {
|
|
|
|
seq = cvCreateSeq(CV_SEQ_CHAIN_CONTOUR, sizeof(CvChain), sizeof(char), CVMEMSTORAGE(storage));
|
|
|
|
}
|
|
|
|
catch (cv::Exception& e) {
|
|
|
|
raise_cverror(e);
|
|
|
|
}
|
|
|
|
return cCvSeq::new_sequence(cCvChain::rb_class(), seq, T_FIXNUM, storage);
|
2008-08-19 11:01:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
__NAMESPACE_END_CVCHAIN
|
|
|
|
__NAMESPACE_END_OPENCV
|