Class: OpenCV::CvRect

Inherits:
Object
  • Object
show all
Defined in:
ext/opencv/cvrect.cpp,
ext/opencv/cvrect.cpp,
ext/opencv/cvavgcomp.cpp

Overview

This class have coordinate of top-left point(x, y) and size, width and height.

C stracture is here, very simple.

typdef struct CvRect {
  int x;
  int y;
  int width;
  int height;
}

Direct Known Subclasses

CvAvgComp

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#newCvRect.new(0, 0, 0, 0) #new(obj) ⇒ CvRect.new(obj.x.to_i] #new(x, y, width, height) ⇒ Object

Create new rectangle area. (x, y) is top-left point, and width, height is size of area. It is dropped below the decimal point.

new() is same as new(0, 0, 0, 0)

new(obj) is same as new(obj.x.to_i, obj.y.to_i, obj.width.to_i, obj.height.to_i)

Overloads:

  • #newCvRect.new(0, 0, 0, 0)
  • #new(obj) ⇒ CvRect.new(obj.x.to_i]

    Returns CvRect.new(obj.x.to_i].



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/opencv/cvrect.cpp', line 103

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  CvRect *self_ptr = CVRECT(self);
  switch (argc) {
  case 0:
    break;
  case 1: {
    CvRect rect = VALUE_TO_CVRECT(argv[0]);
    self_ptr->x = rect.x;
    self_ptr->y = rect.y;
    self_ptr->width = rect.width;
    self_ptr->height = rect.height;
    break;
  }
  case 4:
    self_ptr->x = NUM2INT(argv[0]);
    self_ptr->y = NUM2INT(argv[1]);
    self_ptr->width = NUM2INT(argv[2]);
    self_ptr->height = NUM2INT(argv[3]);
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
    break;
  }
  return self;
}

Class Method Details

.combatible?(obj) ⇒ Boolean

Return compatibility to CvRect. Return true if object have method #x and #y and #width and #height.

For example.

class MyRect
  def x
    1
  end
  def y
    2
  end
  def width
    10
  end
  def height
    20
  end
end
mr = MyRect.new
CvRect.compatible?(mp)  #=> true
CvRect.new(mp)          #=> same as CvRect(1, 2, 10, 20)

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
# File 'ext/opencv/cvrect.cpp', line 62

VALUE
rb_compatible_q(VALUE klass, VALUE object)
{
  return (rb_respond_to(object, rb_intern("x")) &&
	  rb_respond_to(object, rb_intern("y")) &&
	  rb_respond_to(object, rb_intern("width")) &&
	  rb_respond_to(object, rb_intern("height"))) ? Qtrue : Qfalse;
}

.max_rect(rect1, rect2) ⇒ Object

Finds bounding rectangle for given rectangles.



77
78
79
80
81
# File 'ext/opencv/cvrect.cpp', line 77

VALUE
rb_max_rect(VALUE klass, VALUE rect1, VALUE rect2)
{
  return cCvRect::new_object(cvMaxRect(CVRECT(rect1), CVRECT(rect2)));
}

Instance Method Details

#bottom_leftObject

Return bottom-left point of rectangle.



272
273
274
275
276
277
278
# File 'ext/opencv/cvrect.cpp', line 272

VALUE
rb_bottom_left(VALUE self)
{
  CvRect* rect = CVRECT(self);
  return cCvPoint::new_object(cvPoint(rect->x,
				      rect->y + rect->height));
}

#bottom_rightObject

Return bottom-right point of rectangle.



283
284
285
286
287
288
289
# File 'ext/opencv/cvrect.cpp', line 283

VALUE
rb_bottom_right(VALUE self)
{
  CvRect* rect = CVRECT(self);
  return cCvPoint::new_object(cvPoint(rect->x + rect->width,
				      rect->y + rect->height));
}

#centerObject

Return center point of rectangle.



226
227
228
229
230
231
232
# File 'ext/opencv/cvrect.cpp', line 226

VALUE
rb_center(VALUE self)
{
  CvRect *rect = CVRECT(self);  
  return cCvPoint2D32f::new_object(cvPoint2D32f((float)rect->x + (float)rect->width / 2.0,
                                                (float)rect->y + (float)rect->height / 2.0));
}

#heightObject

Return size of y-axis.



203
204
205
206
207
# File 'ext/opencv/cvrect.cpp', line 203

VALUE
rb_height(VALUE self)
{
  return INT2NUM(CVRECT(self)->height);
}

#height=(val) ⇒ Object

Set y-axis size, return self. It is dropped below the decimal point.



216
217
218
219
220
221
# File 'ext/opencv/cvrect.cpp', line 216

VALUE
rb_set_height(VALUE self, VALUE y)
{
  CVRECT(self)->height = NUM2INT(y);
  return self;
}

#pointsObject

Return 4 points (top-left, bottom-left, bottom-right, top-right)



237
238
239
240
241
242
243
244
245
246
247
# File 'ext/opencv/cvrect.cpp', line 237

VALUE
rb_points(VALUE self)
{
  CvRect *rect = CVRECT(self);
  return rb_ary_new3(4,
                     cCvPoint::new_object(cvPoint(rect->x, rect->y)),
                     cCvPoint::new_object(cvPoint(rect->x, rect->y + rect->height)),
                     cCvPoint::new_object(cvPoint(rect->x + rect->width, rect->y + rect->height)),
                     cCvPoint::new_object(cvPoint(rect->x + rect->width, rect->y))
                     );
}

#top_leftObject

Return top-left point of rectangle.



252
253
254
255
256
257
# File 'ext/opencv/cvrect.cpp', line 252

VALUE
rb_top_left(VALUE self)
{
  CvRect* rect = CVRECT(self);
  return cCvPoint::new_object(cvPoint(rect->x, rect->y));
}

#top_rightObject

Return top-right point of rectangle.



262
263
264
265
266
267
# File 'ext/opencv/cvrect.cpp', line 262

VALUE
rb_top_right(VALUE self)
{
  CvRect* rect = CVRECT(self);
  return cCvPoint::new_object(cvPoint(rect->x + rect->width, rect->y));
}

#widthObject

Return size of x-axis.



180
181
182
183
184
# File 'ext/opencv/cvrect.cpp', line 180

VALUE
rb_width(VALUE self)
{
  return INT2NUM(CVRECT(self)->width);
}

#width=(val) ⇒ Object

Set x-axis size, return self. It is dropped below the decimal point.



193
194
195
196
197
198
# File 'ext/opencv/cvrect.cpp', line 193

VALUE
rb_set_width(VALUE self, VALUE x)
{
  CVRECT(self)->width = NUM2INT(x);
  return self;
}

#xObject

Return parameter on x-axis of top-left point.



134
135
136
137
138
# File 'ext/opencv/cvrect.cpp', line 134

VALUE
rb_x(VALUE self)
{
  return INT2NUM(CVRECT(self)->x);
}

#x=(val) ⇒ Object

Set x-axis parameter of top-left point, return self. It is dropped below the decimal point.



147
148
149
150
151
152
# File 'ext/opencv/cvrect.cpp', line 147

VALUE
rb_set_x(VALUE self, VALUE x)
{
  CVRECT(self)->x = NUM2INT(x);
  return self;
}

#yObject

Return parameter on y-axis of top-left point.



157
158
159
160
161
# File 'ext/opencv/cvrect.cpp', line 157

VALUE
rb_y(VALUE self)
{
  return INT2NUM(CVRECT(self)->y);
}

#y=(val) ⇒ Object

Set y-axis parameter of top-left point, return self. It is dropped below the decimal point.



170
171
172
173
174
175
# File 'ext/opencv/cvrect.cpp', line 170

VALUE
rb_set_y(VALUE self, VALUE y)
{
  CVRECT(self)->y = NUM2INT(y);
  return self;
}