Class: OpenCV::CvContour

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

Overview

Contour

Constant Summary collapse

APPROX_OPTION =
approx_option

Instance Method Summary collapse

Methods included from Curve

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

Methods included from PointSet

#check_contour_convexity, #contour_area, #convex_hull2, #convexity_defects, #fit_ellipse2, #min_area_rect2, #min_enclosing_circle

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(seq_flags = CV_SEQ_ELTYPE_POINT|CV_SEQ_KIND_GENERIC, storage = nil) ⇒ CvContour

Constructor

Examples:

seq = CvContour.new(CV_SEQ_ELTYPE_POINT | CV_SEQ_KIND_CURVE)
seq << CvPoint.new(1, 2)
seq << 3 #=> TypeError

Parameters:

  • seq_flags (Fixnum) (defaults to: CV_SEQ_ELTYPE_POINT|CV_SEQ_KIND_GENERIC)

    Flags of the created sequence, which are combinations of the element types and sequence types.

    • Element type:

    • Sequence type:

      • CV_SEQ_KIND_GENERIC: Generic sequence

      • CV_SEQ_KIND_CURVE: Curve

  • storage (CvMemStorage) (defaults to: nil)

    Sequence location


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'ext/opencv/cvcontour.cpp', line 66

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE seq_flags_value, storage_value;
  rb_scan_args(argc, argv, "02", &seq_flags_value, &storage_value);

  int seq_flags = 0;
  if (NIL_P(seq_flags_value)) {
    seq_flags = CV_SEQ_ELTYPE_POINT | CV_SEQ_KIND_GENERIC;
  }
  else {
    Check_Type(seq_flags_value, T_FIXNUM);
    seq_flags = FIX2INT(seq_flags_value);
  }
  storage_value = CHECK_CVMEMSTORAGE(storage_value);

  try {
    DATA_PTR(self) = (CvContour*)cCvSeq::create_seq(seq_flags, sizeof(CvContour), storage_value);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }

  return self;
}

Instance Method Details

#approx_poly(options) ⇒ CvContour? Also known as: approx

Approximates polygonal curves with desired precision

Parameters:

  • options (Hash)

    Parameters

Options Hash (options):

  • :method (Symbol)

    Approximation method (default :dp)

    • :dp - Douglas-Peucker algorithm.

  • :accuracy (Number)

    Parameter specifying the approximation accuracy. This is the maximum distance between the original curve and its approximation.

  • :recursive (Boolean)

    Recursion flag. If true, the function approximates all the contours accessible from curve by h_next and v_next links.

Returns:

  • (CvContour)

    Result of the approximation

  • (nil)

    Approximation faied


154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'ext/opencv/cvcontour.cpp', line 154

VALUE
rb_approx_poly(int argc, VALUE *argv, VALUE self)
{
  VALUE approx_poly_option;
  rb_scan_args(argc, argv, "01", &approx_poly_option);  
  approx_poly_option = APPROX_POLY_OPTION(approx_poly_option);
  VALUE storage = cCvMemStorage::new_object();
  CvSeq *contour = cvApproxPoly(CVCONTOUR(self), sizeof(CvContour), CVMEMSTORAGE(storage),
        APPROX_POLY_METHOD(approx_poly_option),
        APPROX_POLY_ACCURACY(approx_poly_option),
        APPROX_POLY_RECURSIVE(approx_poly_option));

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

#bounding_rectCvRect

Calculates up-right bounding rectangle of point set.

Returns:

  • (CvRect)

    Bounding rectangle


178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/opencv/cvcontour.cpp', line 178

VALUE
rb_bounding_rect(VALUE self)
{
  CvRect rect;
  try {
    rect = cvBoundingRect(CVCONTOUR(self), 1);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return cCvRect::new_object(rect);
}

#colorNumber

Returns color of the contour

Returns:

  • (Number)

    Color of the contour


108
109
110
111
112
# File 'ext/opencv/cvcontour.cpp', line 108

VALUE
rb_color(VALUE self)
{
  return INT2NUM(CVCONTOUR(self)->color);
}

#color=Object

Set color of the contour

Parameters:

  • value (Number)

    Color of the contour


119
120
121
122
123
124
# File 'ext/opencv/cvcontour.cpp', line 119

VALUE
rb_set_color(VALUE self, VALUE color)
{
  CVCONTOUR(self)->color = NUM2INT(color);
  return self;
}

#create_tree(threshold = 0.0) ⇒ CvContourTree

Creates hierarchical representation of contour

Parameters:

  • threshold (Number) (defaults to: 0.0)

    If <= 0, the method creates full binary tree representation. If > 0, the method creates representation with the precision threshold.

Returns:


199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'ext/opencv/cvcontour.cpp', line 199

VALUE
rb_create_tree(int argc, VALUE *argv, VALUE self)
{
  VALUE threshold, storage;
  rb_scan_args(argc, argv, "01", &threshold);
  storage = cCvMemStorage::new_object();
  CvContourTree *tree = NULL;
  try {
    tree = cvCreateContourTree(CVSEQ(self), CVMEMSTORAGE(storage), IF_DBL(threshold, 0.0));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return cCvSeq::new_sequence(cCvContourTree::rb_class(), (CvSeq*)tree, cCvPoint::rb_class(), storage);
}

#in?(point) ⇒ Boolean

Performs a point-in-contour test. The method determines whether the point is inside a contour, outside, or lies on an edge (or coincides with a vertex).

Parameters:

Returns:

  • (Boolean)

    If the point is inside, returns true. If outside, returns false. If lies on an edge, returns nil.

  • (Boolean)

225
226
227
228
229
230
231
232
233
234
235
236
# File 'ext/opencv/cvcontour.cpp', line 225

VALUE
rb_in_q(VALUE self, VALUE point)
{
  double n = 0;
  try {
    n = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 0);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return n == 0 ? Qnil : n > 0 ? Qtrue : Qfalse;
}

#measure_distance(point) ⇒ Object

Calculates distance between a point and the nearest contour edgex

Parameters:

Returns:

  • Signed distance between the point and the nearest contour edge


245
246
247
248
249
250
251
252
253
254
255
256
# File 'ext/opencv/cvcontour.cpp', line 245

VALUE
rb_measure_distance(VALUE self, VALUE point)
{
  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);
}

#point_polygon_test(point, measure_dist) ⇒ Number

Determines whether the point is inside a contour, outside, or lies on an edge (or coinsides with a vertex).

Parameters:

  • point (CvPoint2D32f)

    Point tested against the contour

  • measure_dist (Boolean)

    If true, the method estimates the signed distance from the point to the nearest contour edge. Otherwise, the function only checks if the point is inside a contour or not.

Returns:

  • (Number)

    When measure_dist = false, the return value is +1, -1 and 0, respectively. When measure_dist = true, it is a signed distance between the point and the nearest contour edge.


268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'ext/opencv/cvcontour.cpp', line 268

VALUE
rb_point_polygon_test(VALUE self, VALUE point, VALUE measure_dist)
{
  int measure_dist_flag;

  if (measure_dist == Qtrue)
    measure_dist_flag = 1;
  else if (measure_dist == Qfalse)
    measure_dist_flag = 0;
  else
    measure_dist_flag = NUM2INT(measure_dist);

  double dist = Qnil;
  try {
    dist = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), measure_dist_flag);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  
  /* 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);
}

#rectCvRect

Returns bounding box of the contour

Returns:

  • (CvRect)

    Bounding box of the contour


97
98
99
100
101
# File 'ext/opencv/cvcontour.cpp', line 97

VALUE
rb_rect(VALUE self)
{
  return cCvRect::new_object(CVCONTOUR(self)->rect);
}

#reservedArray<Number>

Returns reserved region values of the contour

Returns:

  • (Array<Number>)

    Reserved region values of the contour


131
132
133
134
135
136
137
138
# File 'ext/opencv/cvcontour.cpp', line 131

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]));
}