Class: OpenCV::CvChain

Inherits:
CvSeq
  • Object
show all
Includes:
Curve
Defined in:
ext/opencv/cvchain.cpp,
ext/opencv/cvchain.cpp

Overview

Freeman chain code

Constant Summary collapse

APPROX_CHAIN_OPTION =
approx_chain_option

Instance Method Summary collapse

Methods included from Curve

#arc_length, #closed?, #convex?, #hole?, #simple?

Methods inherited from CvSeq

#[], #clear, #each, #each_index, #empty?, #first, #h_next, #h_prev, #insert, #last, #pop, #push, #remove, #shift, #total, #unshift, #v_next, #v_prev

Constructor Details

#new(storage = nil) ⇒ CvChain

Create a new chain code

Parameters:

  • storage (CvMemStorage, nil) (defaults to: nil)

    Sequence location (If storage is nil, allocates a new storage automatically)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'ext/opencv/cvchain.cpp', line 46

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
    storage = rb_cvCreateMemStorage(0);
  try {
    DATA_PTR(self) = (CvChain*)cvCreateSeq(CV_SEQ_ELTYPE_CODE, sizeof(CvChain),
					   sizeof(int), storage);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  CvSeq* self_ptr = CVSEQ(self);
  cCvSeq::register_elem_class(self_ptr, rb_cFixnum);
  register_root_object(self_ptr, storage_value);

  return self;
}

Instance Method Details

#approx_chain(options) ⇒ CvSeq<CvPoint> Also known as: approx

Approximates Freeman chains with a polygonal curve

Parameters:

  • options (Hash)

    Parameters

Options Hash (options):

  • :method (Symbol)

    Approximation method (see the description of CvMat#find_contours)

  • :minimal_perimeter (Number)

    Approximates only those contours whose perimeters are not less than minimal_perimeter. Other chains are removed from the resulting structure.

  • :recursive (Boolean)

    Recursion flag. If it is true, the function approximates all chains that can be obtained from chain by using the h_next or v_next links. Otherwise, the single input chain is approximated.

Returns:


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'ext/opencv/cvchain.cpp', line 163

VALUE
rb_approx_chains(int argc, VALUE *argv, VALUE self)
{
  VALUE approx_chain_option;
  rb_scan_args(argc, argv, "01", &approx_chain_option);

  approx_chain_option = APPROX_CHAIN_OPTION(approx_chain_option);
  VALUE storage = cCvMemStorage::new_object();
  CvSeq *seq = cvApproxChains(CVSEQ(self), CVMEMSTORAGE(storage),			      
			      APPROX_CHAIN_METHOD(approx_chain_option),
			      APPROX_CHAIN_PARAMETER(approx_chain_option),
			      APPROX_CHAIN_MINIMAL_PERIMETER(approx_chain_option),
			      APPROX_CHAIN_RECURSIVE(approx_chain_option));

  if (seq && seq->total > 0) {
    return cCvSeq::new_sequence(cCvChain::rb_class(), seq, cCvPoint::rb_class(), storage);
  }
  return Qnil;
}

#codesArray<Fixnum>

Returns the chain codes

Returns:

  • (Array<Fixnum>)

    Chain codes


102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'ext/opencv/cvchain.cpp', line 102

VALUE
rb_codes(VALUE self)
{
  CvChain *chain = CVCHAIN(self);
  CvChainPtReader reader;
  int total = chain->total;
  VALUE ary = rb_ary_new2(total);
  try {
    cvStartReadChainPoints(chain, &reader);
    for (int i = 0; i < total; ++i) {
      CV_READ_SEQ_ELEM(reader.code, (*((CvSeqReader*)&(reader))));
      rb_ary_store(ary, i, INT2FIX(reader.code));
    }
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return ary;
}

#originCvPoint

Returns Freeman chain code origin

Returns:

  • (CvPoint)

    Origin of the chain code


76
77
78
79
80
# File 'ext/opencv/cvchain.cpp', line 76

VALUE
rb_origin(VALUE self)
{
  return cCvPoint::new_object(CVCHAIN(self)->origin);
}

#origin=CvChain

Set Freeman chain code origin

Parameters:

  • value (CvPoint)

    Origin of the chain code

Returns:


88
89
90
91
92
93
# File 'ext/opencv/cvchain.cpp', line 88

VALUE
rb_set_origin(VALUE self, VALUE origin)
{
  CVCHAIN(self)->origin = VALUE_TO_CVPOINT(origin);  
  return self;
}

#pointsArray<CvPoint>

Returns the points of the chain codes

Returns:

  • (Array<CvPoint>)

    Points of the chain codes


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'ext/opencv/cvchain.cpp', line 129

VALUE
rb_points(VALUE self)
{
  CvChain *chain = CVCHAIN(self);
  CvChainPtReader reader;
  CvPoint p = chain->origin;
  int total = chain->total;
  VALUE ary = rb_ary_new2(total);
  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);
  }
  return ary;
}