Class: OpenCV::CvMoments

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

Overview

moments

Instance Method Summary collapse

Constructor Details

#new(src[,is_binary = nil]) ⇒ Object

Calculates all moments up to third order of a polygon or rasterized shape. src should be CvMat or CvPolygon.

If is_binary = true, all the zero pixel values are treated as zeroes, all the others are treated as 1's.


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

VALUE
rb_initialize(int argc, VALUE *argv, VALUE self)
{
  VALUE src, is_binary;
  rb_scan_args(argc, argv, "02", &src, &is_binary);
  if (!NIL_P(src)) {
    if (rb_obj_is_kind_of(src, cCvMat::rb_class()) || rb_obj_is_kind_of(src, cCvSeq::rb_class())) {
      try {
	cvMoments(CVARR(src), CVMOMENTS(self), TRUE_OR_FALSE(is_binary, 0));
      }
      catch (cv::Exception& e) {
	raise_cverror(e);
      }
    }
    else
      rb_raise(rb_eTypeError, "argument 1 (src) should be %s or %s.",
	       rb_class2name(cCvMat::rb_class()), rb_class2name(cCvSeq::rb_class()));
  }
  return self;      
}

Instance Method Details

#angleFloat

Return angle.

Returns:

  • (Float)

209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'ext/opencv/cvmoments.cpp', line 209

VALUE
rb_angle(VALUE self)
{
  CvMoments *moments = CVMOMENTS(self);
  double m11 = 0, m20 = 0, m02 = 0;
  try {
    m11 = cvGetCentralMoment(moments, 1, 1);
    m20 = cvGetCentralMoment(moments, 2, 0);
    m02 = cvGetCentralMoment(moments, 0, 2);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  double mangle = 0.5 * atan(2 * m11 / (m20 - m02));
  if (cvIsNaN(mangle) || cvIsInf(mangle))
    return Qnil;
  else
    return rb_float_new(mangle);
}

#centralFloat

Retrieves central moment.

which in case of image moments is defined as:

μx_order,y_order=sumx,y(I(x,y)*(x-xc)x_order*(y-yc)y_order),
where xc=M10/M00, yc=M01/M00 - coordinates of the gravity center

Returns:

  • (Float)

123
124
125
126
127
128
129
130
131
132
133
134
# File 'ext/opencv/cvmoments.cpp', line 123

VALUE
rb_central(VALUE self, VALUE x_order, VALUE y_order)
{
  double result = 0;
  try {
    result = cvGetCentralMoment(CVMOMENTS(self), NUM2INT(x_order), NUM2INT(y_order));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return rb_float_new(result);
}

#gravity_centerObject

Return gravity center.


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'ext/opencv/cvmoments.cpp', line 185

VALUE
rb_gravity_center(VALUE self)
{
  CvMoments *moments = CVMOMENTS(self);
  CvPoint2D32f point;
  double m00 = 0, m01 = 0, m10 = 0;
  try {
    m00 = cvGetSpatialMoment(moments, 0, 0);
    m10 = cvGetSpatialMoment(moments, 1, 0);
    m01 = cvGetSpatialMoment(moments, 0, 1);
    point = cvPoint2D32f(m10 / m00, m01 / m00);
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return cCvPoint2D32f::new_object(point);
}

#huObject

Calculates seven Hu invariants.

seven Hu invariants that are defined as:

h1=η20+η02
h2=(η20-η02)²+4η11²
h3=(η30-3η12)²+ (3η21-η03)²
h4=(η30+η12)²+ (η21+η03)²
h5=(η30-3η12)(η30+η12)[(η30+η12)²-3(η21+η03)²]+(3η21-η03)(η21+η03)[3(η30+η12)²-(η21+η03)²]
h6=(η20-η02)[(η30+η12)²- (η21+η03)²]+4η11(η30+η12)(η21+η03)
h7=(3η21-η03)(η21+η03)[3(η30+η12)²-(η21+η03)²]-(η30-3η12)(η21+η03)[3(η30+η12)²-(η21+η03)²]

where ηi,j are normalized central moments of 2-nd and 3-rd orders. The computed values are proved to be invariant to the image scaling, rotation, and reflection except the seventh one, whose sign is changed by reflection.


173
174
175
176
177
# File 'ext/opencv/cvmoments.cpp', line 173

VALUE
rb_hu(VALUE self)
{
  return cCvHuMoments::new_object(CVMOMENTS(self));
}

#normalized_centralFloat

Retrieves normalized central moment.

ηx_order,y_order= μx_order,y_order/M00((y_order+x_order)/2+1)

Returns:

  • (Float)

144
145
146
147
148
149
150
151
152
153
154
155
# File 'ext/opencv/cvmoments.cpp', line 144

VALUE
rb_normalized_central(VALUE self, VALUE x_order, VALUE y_order)
{
  double result = 0;
  try {
    result = cvGetNormalizedCentralMoment(CVMOMENTS(self), NUM2INT(x_order), NUM2INT(y_order));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return rb_float_new(result);
}

#spatialFloat

Retrieves spatial moment.

which in case of image moments is defined as:

Mx_order,y_order=sumx,y(I(x,y)*xx_order*yy_order)
where I(x,y) is the intensity of the pixel (x, y).

Returns:

  • (Float)

100
101
102
103
104
105
106
107
108
109
110
111
# File 'ext/opencv/cvmoments.cpp', line 100

VALUE
rb_spatial(VALUE self, VALUE x_order, VALUE y_order)
{
  double result = 0;
  try {
    result = cvGetSpatialMoment(CVMOMENTS(self), NUM2INT(x_order), NUM2INT(y_order));
  }
  catch (cv::Exception& e) {
    raise_cverror(e);
  }
  return rb_float_new(result);
}