ruby-opencv 0.0.8.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (230) hide show
  1. data/Gemfile +8 -0
  2. data/History.txt +5 -0
  3. data/License.txt +30 -0
  4. data/Manifest.txt +229 -0
  5. data/README.rdoc +149 -0
  6. data/Rakefile +32 -0
  7. data/examples/alpha_blend.rb +21 -0
  8. data/examples/box.png +0 -0
  9. data/examples/box_in_scene.png +0 -0
  10. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  11. data/examples/contours/bitmap-contours.png +0 -0
  12. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  13. data/examples/contours/contour_retrieval_modes.rb +139 -0
  14. data/examples/contours/rotated-boxes-with-detected-bounding-rectangles.jpg +0 -0
  15. data/examples/contours/rotated-boxes.jpg +0 -0
  16. data/examples/convexhull.rb +47 -0
  17. data/examples/face_detect.rb +20 -0
  18. data/examples/find_obj.rb +169 -0
  19. data/examples/houghcircle.rb +22 -0
  20. data/examples/inpaint.png +0 -0
  21. data/examples/inpaint.rb +57 -0
  22. data/examples/lenna-rotated.jpg +0 -0
  23. data/examples/lenna.jpg +0 -0
  24. data/examples/match_kdtree.rb +88 -0
  25. data/examples/matching_to_many_images.rb +16 -0
  26. data/examples/matching_to_many_images/query.png +0 -0
  27. data/examples/matching_to_many_images/train/1.png +0 -0
  28. data/examples/matching_to_many_images/train/2.png +0 -0
  29. data/examples/matching_to_many_images/train/3.png +0 -0
  30. data/examples/matching_to_many_images/train/trainImages.txt +3 -0
  31. data/examples/paint.rb +70 -0
  32. data/examples/snake.rb +43 -0
  33. data/examples/stuff.jpg +0 -0
  34. data/examples/tiffany.jpg +0 -0
  35. data/ext/opencv/GPATH +0 -0
  36. data/ext/opencv/GSYMS +0 -0
  37. data/ext/opencv/curve.cpp +112 -0
  38. data/ext/opencv/curve.h +34 -0
  39. data/ext/opencv/cvavgcomp.cpp +67 -0
  40. data/ext/opencv/cvavgcomp.h +39 -0
  41. data/ext/opencv/cvbox2d.cpp +197 -0
  42. data/ext/opencv/cvbox2d.h +61 -0
  43. data/ext/opencv/cvcapture.cpp +499 -0
  44. data/ext/opencv/cvcapture.h +72 -0
  45. data/ext/opencv/cvchain.cpp +233 -0
  46. data/ext/opencv/cvchain.h +46 -0
  47. data/ext/opencv/cvcircle32f.cpp +116 -0
  48. data/ext/opencv/cvcircle32f.h +52 -0
  49. data/ext/opencv/cvcondensation.cpp +282 -0
  50. data/ext/opencv/cvcondensation.h +49 -0
  51. data/ext/opencv/cvconnectedcomp.cpp +143 -0
  52. data/ext/opencv/cvconnectedcomp.h +49 -0
  53. data/ext/opencv/cvcontour.cpp +296 -0
  54. data/ext/opencv/cvcontour.h +48 -0
  55. data/ext/opencv/cvcontourtree.cpp +91 -0
  56. data/ext/opencv/cvcontourtree.h +41 -0
  57. data/ext/opencv/cvconvexitydefect.cpp +103 -0
  58. data/ext/opencv/cvconvexitydefect.h +42 -0
  59. data/ext/opencv/cverror.cpp +159 -0
  60. data/ext/opencv/cverror.h +28 -0
  61. data/ext/opencv/cvfeaturetree.cpp +125 -0
  62. data/ext/opencv/cvfeaturetree.h +55 -0
  63. data/ext/opencv/cvfont.cpp +208 -0
  64. data/ext/opencv/cvfont.h +64 -0
  65. data/ext/opencv/cvhaarclassifiercascade.cpp +168 -0
  66. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  67. data/ext/opencv/cvhistogram.cpp +546 -0
  68. data/ext/opencv/cvhistogram.h +73 -0
  69. data/ext/opencv/cvhumoments.cpp +139 -0
  70. data/ext/opencv/cvhumoments.h +51 -0
  71. data/ext/opencv/cvline.cpp +154 -0
  72. data/ext/opencv/cvline.h +54 -0
  73. data/ext/opencv/cvmat.cpp +5848 -0
  74. data/ext/opencv/cvmat.h +284 -0
  75. data/ext/opencv/cvmatnd.cpp +44 -0
  76. data/ext/opencv/cvmatnd.h +28 -0
  77. data/ext/opencv/cvmemstorage.cpp +68 -0
  78. data/ext/opencv/cvmemstorage.h +53 -0
  79. data/ext/opencv/cvmoments.cpp +287 -0
  80. data/ext/opencv/cvmoments.h +75 -0
  81. data/ext/opencv/cvpoint.cpp +228 -0
  82. data/ext/opencv/cvpoint.h +64 -0
  83. data/ext/opencv/cvpoint2d32f.cpp +211 -0
  84. data/ext/opencv/cvpoint2d32f.h +63 -0
  85. data/ext/opencv/cvpoint3d32f.cpp +245 -0
  86. data/ext/opencv/cvpoint3d32f.h +66 -0
  87. data/ext/opencv/cvrect.cpp +333 -0
  88. data/ext/opencv/cvrect.h +79 -0
  89. data/ext/opencv/cvscalar.cpp +236 -0
  90. data/ext/opencv/cvscalar.h +71 -0
  91. data/ext/opencv/cvseq.cpp +599 -0
  92. data/ext/opencv/cvseq.h +74 -0
  93. data/ext/opencv/cvsize.cpp +221 -0
  94. data/ext/opencv/cvsize.h +65 -0
  95. data/ext/opencv/cvsize2d32f.cpp +209 -0
  96. data/ext/opencv/cvsize2d32f.h +64 -0
  97. data/ext/opencv/cvslice.cpp +120 -0
  98. data/ext/opencv/cvslice.h +61 -0
  99. data/ext/opencv/cvsparsemat.cpp +44 -0
  100. data/ext/opencv/cvsparsemat.h +28 -0
  101. data/ext/opencv/cvsurfparams.cpp +199 -0
  102. data/ext/opencv/cvsurfparams.h +58 -0
  103. data/ext/opencv/cvsurfpoint.cpp +223 -0
  104. data/ext/opencv/cvsurfpoint.h +52 -0
  105. data/ext/opencv/cvtermcriteria.cpp +192 -0
  106. data/ext/opencv/cvtermcriteria.h +71 -0
  107. data/ext/opencv/cvtwopoints.cpp +116 -0
  108. data/ext/opencv/cvtwopoints.h +51 -0
  109. data/ext/opencv/cvutils.cpp +194 -0
  110. data/ext/opencv/cvutils.h +29 -0
  111. data/ext/opencv/cvvideowriter.cpp +137 -0
  112. data/ext/opencv/cvvideowriter.h +43 -0
  113. data/ext/opencv/gui.cpp +68 -0
  114. data/ext/opencv/gui.h +30 -0
  115. data/ext/opencv/iplconvkernel.cpp +192 -0
  116. data/ext/opencv/iplconvkernel.h +71 -0
  117. data/ext/opencv/iplimage.cpp +644 -0
  118. data/ext/opencv/iplimage.h +73 -0
  119. data/ext/opencv/lib/opencv.rb +3 -0
  120. data/ext/opencv/lib/opencv/psyched_yaml.rb +22 -0
  121. data/ext/opencv/lib/opencv/version.rb +3 -0
  122. data/ext/opencv/mouseevent.cpp +181 -0
  123. data/ext/opencv/mouseevent.h +56 -0
  124. data/ext/opencv/opencv.cpp +722 -0
  125. data/ext/opencv/opencv.h +400 -0
  126. data/ext/opencv/pointset.cpp +274 -0
  127. data/ext/opencv/pointset.h +68 -0
  128. data/ext/opencv/trackbar.cpp +121 -0
  129. data/ext/opencv/trackbar.h +69 -0
  130. data/ext/opencv/window.cpp +357 -0
  131. data/ext/opencv/window.h +66 -0
  132. data/extconf.rb +75 -0
  133. data/images/CvMat_sobel.png +0 -0
  134. data/images/CvMat_sub_rect.png +0 -0
  135. data/images/CvSeq_relationmap.png +0 -0
  136. data/images/face_detect_from_lena.jpg +0 -0
  137. data/ruby-opencv.gemspec +43 -0
  138. data/test/helper.rb +166 -0
  139. data/test/log.txt +55 -0
  140. data/test/runner.rb +30 -0
  141. data/test/samples/airplane.jpg +0 -0
  142. data/test/samples/baboon.jpg +0 -0
  143. data/test/samples/baboon200.jpg +0 -0
  144. data/test/samples/baboon200_rotated.jpg +0 -0
  145. data/test/samples/blank0.jpg +0 -0
  146. data/test/samples/blank1.jpg +0 -0
  147. data/test/samples/blank2.jpg +0 -0
  148. data/test/samples/blank3.jpg +0 -0
  149. data/test/samples/blank4.jpg +0 -0
  150. data/test/samples/blank5.jpg +0 -0
  151. data/test/samples/blank6.jpg +0 -0
  152. data/test/samples/blank7.jpg +0 -0
  153. data/test/samples/blank8.jpg +0 -0
  154. data/test/samples/blank9.jpg +0 -0
  155. data/test/samples/cat.jpg +0 -0
  156. data/test/samples/chessboard.jpg +0 -0
  157. data/test/samples/contours.jpg +0 -0
  158. data/test/samples/fruits.jpg +0 -0
  159. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  160. data/test/samples/inpaint-mask.bmp +0 -0
  161. data/test/samples/lena-256x256.jpg +0 -0
  162. data/test/samples/lena-32x32.jpg +0 -0
  163. data/test/samples/lena-eyes.jpg +0 -0
  164. data/test/samples/lena-inpaint.jpg +0 -0
  165. data/test/samples/lena.jpg +0 -0
  166. data/test/samples/lines.jpg +0 -0
  167. data/test/samples/messy0.jpg +0 -0
  168. data/test/samples/messy1.jpg +0 -0
  169. data/test/samples/movie_sample.avi +0 -0
  170. data/test/samples/one_way_train_0000.jpg +0 -0
  171. data/test/samples/one_way_train_0001.jpg +0 -0
  172. data/test/samples/partially_blank0.jpg +0 -0
  173. data/test/samples/partially_blank1.jpg +0 -0
  174. data/test/samples/smooth0.jpg +0 -0
  175. data/test/samples/smooth1.jpg +0 -0
  176. data/test/samples/smooth2.jpg +0 -0
  177. data/test/samples/smooth3.jpg +0 -0
  178. data/test/samples/smooth4.jpg +0 -0
  179. data/test/samples/smooth5.jpg +0 -0
  180. data/test/samples/smooth6.jpg +0 -0
  181. data/test/samples/str-cv-rotated.jpg +0 -0
  182. data/test/samples/str-cv.jpg +0 -0
  183. data/test/samples/str-ov.jpg +0 -0
  184. data/test/samples/stuff.jpg +0 -0
  185. data/test/test_curve.rb +43 -0
  186. data/test/test_cvavgcomp.rb +24 -0
  187. data/test/test_cvbox2d.rb +76 -0
  188. data/test/test_cvcapture.rb +183 -0
  189. data/test/test_cvchain.rb +108 -0
  190. data/test/test_cvcircle32f.rb +41 -0
  191. data/test/test_cvconnectedcomp.rb +61 -0
  192. data/test/test_cvcontour.rb +150 -0
  193. data/test/test_cvcontourtree.rb +43 -0
  194. data/test/test_cverror.rb +50 -0
  195. data/test/test_cvfeaturetree.rb +65 -0
  196. data/test/test_cvfont.rb +58 -0
  197. data/test/test_cvhaarclassifiercascade.rb +63 -0
  198. data/test/test_cvhistogram.rb +271 -0
  199. data/test/test_cvhumoments.rb +83 -0
  200. data/test/test_cvline.rb +50 -0
  201. data/test/test_cvmat.rb +2947 -0
  202. data/test/test_cvmat_drawing.rb +349 -0
  203. data/test/test_cvmat_dxt.rb +150 -0
  204. data/test/test_cvmat_imageprocessing.rb +2025 -0
  205. data/test/test_cvmat_matching.rb +57 -0
  206. data/test/test_cvmoments.rb +180 -0
  207. data/test/test_cvpoint.rb +75 -0
  208. data/test/test_cvpoint2d32f.rb +75 -0
  209. data/test/test_cvpoint3d32f.rb +93 -0
  210. data/test/test_cvrect.rb +144 -0
  211. data/test/test_cvscalar.rb +113 -0
  212. data/test/test_cvseq.rb +295 -0
  213. data/test/test_cvsize.rb +75 -0
  214. data/test/test_cvsize2d32f.rb +75 -0
  215. data/test/test_cvslice.rb +31 -0
  216. data/test/test_cvsurfparams.rb +57 -0
  217. data/test/test_cvsurfpoint.rb +66 -0
  218. data/test/test_cvtermcriteria.rb +56 -0
  219. data/test/test_cvtwopoints.rb +40 -0
  220. data/test/test_cvvideowriter.rb +58 -0
  221. data/test/test_iplconvkernel.rb +54 -0
  222. data/test/test_iplimage.rb +236 -0
  223. data/test/test_mouseevent.rb +17 -0
  224. data/test/test_opencv.rb +324 -0
  225. data/test/test_pointset.rb +126 -0
  226. data/test/test_preliminary.rb +130 -0
  227. data/test/test_trackbar.rb +47 -0
  228. data/test/test_window.rb +115 -0
  229. data/test/videowriter_result.avi +0 -0
  230. metadata +394 -0
@@ -0,0 +1,287 @@
1
+ /************************************************************
2
+
3
+ cvmoments.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2007 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvmoments.h"
11
+ /*
12
+ * Document-class: OpenCV::CvMoments
13
+ *
14
+ * moments
15
+ */
16
+ __NAMESPACE_BEGIN_OPENCV
17
+ __NAMESPACE_BEGIN_CVMOMENTS
18
+
19
+ #define DEFINE_CVMOMENTS_ACCESSOR(elem) \
20
+ rb_define_method(rb_klass, #elem, RUBY_METHOD_FUNC(rb_##elem), 0)
21
+
22
+ #define CVMOMENTS_ACCESSOR(elem) \
23
+ VALUE rb_##elem(VALUE self) { return DBL2NUM(CVMOMENTS(self)->elem); }
24
+
25
+ VALUE rb_klass;
26
+
27
+ VALUE
28
+ rb_class()
29
+ {
30
+ return rb_klass;
31
+ }
32
+
33
+ void
34
+ define_ruby_class()
35
+ {
36
+ if (rb_klass)
37
+ return;
38
+ /*
39
+ * opencv = rb_define_module("OpenCV");
40
+ *
41
+ * note: this comment is used by rdoc.
42
+ */
43
+ VALUE opencv = rb_module_opencv();
44
+
45
+ rb_klass = rb_define_class_under(opencv, "CvMoments", rb_cObject);
46
+ rb_define_alloc_func(rb_klass, rb_allocate);
47
+ rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
48
+ rb_define_method(rb_klass, "spatial", RUBY_METHOD_FUNC(rb_spatial), 2);
49
+ rb_define_method(rb_klass, "central", RUBY_METHOD_FUNC(rb_central), 2);
50
+ rb_define_method(rb_klass, "normalized_central", RUBY_METHOD_FUNC(rb_normalized_central), 2);
51
+ rb_define_method(rb_klass, "hu", RUBY_METHOD_FUNC(rb_hu), 0);
52
+ rb_define_method(rb_klass, "gravity_center", RUBY_METHOD_FUNC(rb_gravity_center), 0);
53
+ rb_define_method(rb_klass, "angle", RUBY_METHOD_FUNC(rb_angle), 0);
54
+
55
+ DEFINE_CVMOMENTS_ACCESSOR(m00);
56
+ DEFINE_CVMOMENTS_ACCESSOR(m10);
57
+ DEFINE_CVMOMENTS_ACCESSOR(m01);
58
+ DEFINE_CVMOMENTS_ACCESSOR(m20);
59
+ DEFINE_CVMOMENTS_ACCESSOR(m11);
60
+ DEFINE_CVMOMENTS_ACCESSOR(m02);
61
+ DEFINE_CVMOMENTS_ACCESSOR(m30);
62
+ DEFINE_CVMOMENTS_ACCESSOR(m21);
63
+ DEFINE_CVMOMENTS_ACCESSOR(m12);
64
+ DEFINE_CVMOMENTS_ACCESSOR(m03);
65
+
66
+ DEFINE_CVMOMENTS_ACCESSOR(mu20);
67
+ DEFINE_CVMOMENTS_ACCESSOR(mu11);
68
+ DEFINE_CVMOMENTS_ACCESSOR(mu02);
69
+ DEFINE_CVMOMENTS_ACCESSOR(mu30);
70
+ DEFINE_CVMOMENTS_ACCESSOR(mu21);
71
+ DEFINE_CVMOMENTS_ACCESSOR(mu12);
72
+ DEFINE_CVMOMENTS_ACCESSOR(mu03);
73
+
74
+ DEFINE_CVMOMENTS_ACCESSOR(inv_sqrt_m00);
75
+ }
76
+
77
+ VALUE
78
+ rb_allocate(VALUE klass)
79
+ {
80
+ CvMoments *ptr;
81
+ return Data_Make_Struct(klass, CvMoments, 0, -1, ptr);
82
+ }
83
+ /*
84
+ * call-seq:
85
+ * CvMoments.new(<i>src[,is_binary = nil]</i>)
86
+ *
87
+ * Calculates all moments up to third order of a polygon or rasterized shape.
88
+ * <i>src</i> should be CvMat or CvPolygon.
89
+ *
90
+ * If is_binary = true, all the zero pixel values are treated as zeroes, all the others are treated as 1's.
91
+ */
92
+ VALUE
93
+ rb_initialize(int argc, VALUE *argv, VALUE self)
94
+ {
95
+ VALUE src, is_binary;
96
+ rb_scan_args(argc, argv, "02", &src, &is_binary);
97
+ if (!NIL_P(src)) {
98
+ if (rb_obj_is_kind_of(src, cCvMat::rb_class()) || rb_obj_is_kind_of(src, cCvSeq::rb_class())) {
99
+ try {
100
+ cvMoments(CVARR(src), CVMOMENTS(self), TRUE_OR_FALSE(is_binary, 0));
101
+ }
102
+ catch (cv::Exception& e) {
103
+ raise_cverror(e);
104
+ }
105
+ }
106
+ else
107
+ rb_raise(rb_eTypeError, "argument 1 (src) should be %s or %s.",
108
+ rb_class2name(cCvMat::rb_class()), rb_class2name(cCvSeq::rb_class()));
109
+ }
110
+ return self;
111
+ }
112
+
113
+ CVMOMENTS_ACCESSOR(m00);
114
+ CVMOMENTS_ACCESSOR(m10);
115
+ CVMOMENTS_ACCESSOR(m01);
116
+ CVMOMENTS_ACCESSOR(m20);
117
+ CVMOMENTS_ACCESSOR(m11);
118
+ CVMOMENTS_ACCESSOR(m02);
119
+ CVMOMENTS_ACCESSOR(m30);
120
+ CVMOMENTS_ACCESSOR(m21);
121
+ CVMOMENTS_ACCESSOR(m12);
122
+ CVMOMENTS_ACCESSOR(m03);
123
+
124
+ CVMOMENTS_ACCESSOR(mu20);
125
+ CVMOMENTS_ACCESSOR(mu11);
126
+ CVMOMENTS_ACCESSOR(mu02);
127
+ CVMOMENTS_ACCESSOR(mu30);
128
+ CVMOMENTS_ACCESSOR(mu21);
129
+ CVMOMENTS_ACCESSOR(mu12);
130
+ CVMOMENTS_ACCESSOR(mu03);
131
+
132
+ CVMOMENTS_ACCESSOR(inv_sqrt_m00);
133
+
134
+ /*
135
+ * call-seq:
136
+ * spatial -> float
137
+ *
138
+ * Retrieves spatial moment.
139
+ *
140
+ * which in case of image moments is defined as:
141
+ * Mx_order,y_order=sumx,y(I(x,y)*xx_order*yy_order)
142
+ * where I(x,y) is the intensity of the pixel (x, y).
143
+ */
144
+ VALUE
145
+ rb_spatial(VALUE self, VALUE x_order, VALUE y_order)
146
+ {
147
+ double result = 0;
148
+ try {
149
+ result = cvGetSpatialMoment(CVMOMENTS(self), NUM2INT(x_order), NUM2INT(y_order));
150
+ }
151
+ catch (cv::Exception& e) {
152
+ raise_cverror(e);
153
+ }
154
+ return rb_float_new(result);
155
+ }
156
+
157
+ /*
158
+ * call-seq:
159
+ * central -> float
160
+ *
161
+ * Retrieves central moment.
162
+ *
163
+ * which in case of image moments is defined as:
164
+ * μx_order,y_order=sumx,y(I(x,y)*(x-xc)x_order*(y-yc)y_order),
165
+ * where xc=M10/M00, yc=M01/M00 - coordinates of the gravity center
166
+ */
167
+ VALUE
168
+ rb_central(VALUE self, VALUE x_order, VALUE y_order)
169
+ {
170
+ double result = 0;
171
+ try {
172
+ result = cvGetCentralMoment(CVMOMENTS(self), NUM2INT(x_order), NUM2INT(y_order));
173
+ }
174
+ catch (cv::Exception& e) {
175
+ raise_cverror(e);
176
+ }
177
+ return rb_float_new(result);
178
+ }
179
+
180
+ /*
181
+ * call-seq:
182
+ * normalized_central -> float
183
+ *
184
+ * Retrieves normalized central moment.
185
+ *
186
+ * ηx_order,y_order= μx_order,y_order/M00((y_order+x_order)/2+1)
187
+ */
188
+ VALUE
189
+ rb_normalized_central(VALUE self, VALUE x_order, VALUE y_order)
190
+ {
191
+ double result = 0;
192
+ try {
193
+ result = cvGetNormalizedCentralMoment(CVMOMENTS(self), NUM2INT(x_order), NUM2INT(y_order));
194
+ }
195
+ catch (cv::Exception& e) {
196
+ raise_cverror(e);
197
+ }
198
+ return rb_float_new(result);
199
+ }
200
+
201
+ /*
202
+ * call-seq:
203
+ * hu -> cvhumoments
204
+ *
205
+ * Calculates seven Hu invariants.
206
+ *
207
+ * seven Hu invariants that are defined as:
208
+ * h1=η20+η02
209
+ * h2=(η20-η02)²+4η11²
210
+ * h3=(η30-3η12)²+ (3η21-η03)²
211
+ * h4=(η30+η12)²+ (η21+η03)²
212
+ * h5=(η30-3η12)(η30+η12)[(η30+η12)²-3(η21+η03)²]+(3η21-η03)(η21+η03)[3(η30+η12)²-(η21+η03)²]
213
+ * h6=(η20-η02)[(η30+η12)²- (η21+η03)²]+4η11(η30+η12)(η21+η03)
214
+ * h7=(3η21-η03)(η21+η03)[3(η30+η12)²-(η21+η03)²]-(η30-3η12)(η21+η03)[3(η30+η12)²-(η21+η03)²]
215
+ * 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.
216
+ */
217
+ VALUE
218
+ rb_hu(VALUE self)
219
+ {
220
+ return cCvHuMoments::new_object(CVMOMENTS(self));
221
+ }
222
+
223
+ /*
224
+ * call-seq:
225
+ * gravity_center -> cvpoint2d32f
226
+ *
227
+ * Return gravity center.
228
+ */
229
+ VALUE
230
+ rb_gravity_center(VALUE self)
231
+ {
232
+ CvMoments *moments = CVMOMENTS(self);
233
+ CvPoint2D32f point;
234
+ double m00 = 0, m01 = 0, m10 = 0;
235
+ try {
236
+ m00 = cvGetSpatialMoment(moments, 0, 0);
237
+ m10 = cvGetSpatialMoment(moments, 1, 0);
238
+ m01 = cvGetSpatialMoment(moments, 0, 1);
239
+ point = cvPoint2D32f(m10 / m00, m01 / m00);
240
+ }
241
+ catch (cv::Exception& e) {
242
+ raise_cverror(e);
243
+ }
244
+ return cCvPoint2D32f::new_object(point);
245
+ }
246
+
247
+ /*
248
+ * call-seq:
249
+ * angle -> float
250
+ *
251
+ * Return angle.
252
+ */
253
+ VALUE
254
+ rb_angle(VALUE self)
255
+ {
256
+ CvMoments *moments = CVMOMENTS(self);
257
+ double m11 = 0, m20 = 0, m02 = 0;
258
+ try {
259
+ m11 = cvGetCentralMoment(moments, 1, 1);
260
+ m20 = cvGetCentralMoment(moments, 2, 0);
261
+ m02 = cvGetCentralMoment(moments, 0, 2);
262
+ }
263
+ catch (cv::Exception& e) {
264
+ raise_cverror(e);
265
+ }
266
+ double mangle = 0.5 * atan(2 * m11 / (m20 - m02));
267
+ if (cvIsNaN(mangle) || cvIsInf(mangle))
268
+ return Qnil;
269
+ else
270
+ return rb_float_new(mangle);
271
+ }
272
+
273
+ VALUE
274
+ new_object(CvArr *arr, int is_binary = 0)
275
+ {
276
+ VALUE object = rb_allocate(rb_klass);
277
+ try {
278
+ cvMoments(arr, CVMOMENTS(object), is_binary);
279
+ }
280
+ catch (cv::Exception& e) {
281
+ raise_cverror(e);
282
+ }
283
+ return object;
284
+ }
285
+
286
+ __NAMESPACE_END_CVMOMENTS
287
+ __NAMESPACE_END_OPENCV
@@ -0,0 +1,75 @@
1
+ /************************************************************
2
+
3
+ cvmoments.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2007 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_CVMOMENTS_H
11
+ #define RUBY_OPENCV_CVMOMENTS_H
12
+
13
+ #include "opencv.h"
14
+
15
+ #define __NAMESPACE_BEGIN_CVMOMENTS namespace cCvMoments {
16
+ #define __NAMESPACE_END_CVMOMENTS }
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_CVMOMENTS
20
+
21
+ VALUE rb_class();
22
+
23
+ void define_ruby_class();
24
+
25
+ VALUE rb_allocate(VALUE klass);
26
+ VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
27
+ VALUE rb_spatial(VALUE self, VALUE x_order, VALUE y_order);
28
+ VALUE rb_central(VALUE self, VALUE x_order, VALUE y_order);
29
+ VALUE rb_normalized_central(VALUE self, VALUE x_order, VALUE y_order);
30
+ VALUE rb_hu(VALUE self);
31
+ VALUE rb_gravity_center(VALUE self);
32
+ VALUE rb_angle(VALUE self);
33
+
34
+ VALUE rb_m00(VALUE self);
35
+ VALUE rb_m10(VALUE self);
36
+ VALUE rb_m01(VALUE self);
37
+ VALUE rb_m20(VALUE self);
38
+ VALUE rb_m11(VALUE self);
39
+ VALUE rb_m02(VALUE self);
40
+ VALUE rb_m30(VALUE self);
41
+ VALUE rb_m21(VALUE self);
42
+ VALUE rb_m12(VALUE self);
43
+ VALUE rb_m03(VALUE self);
44
+ VALUE rb_mu20(VALUE self);
45
+ VALUE rb_mu11(VALUE self);
46
+ VALUE rb_mu02(VALUE self);
47
+ VALUE rb_mu30(VALUE self);
48
+ VALUE rb_mu21(VALUE self);
49
+ VALUE rb_mu12(VALUE self);
50
+ VALUE rb_mu03(VALUE self);
51
+ VALUE rb_inv_sqrt_m00(VALUE self);
52
+
53
+ VALUE new_object(CvArr *arr, int is_binary);
54
+
55
+ __NAMESPACE_END_CVMOMENTS
56
+
57
+ inline CvMoments*
58
+ CVMOMENTS(VALUE object)
59
+ {
60
+ CvMoments *ptr;
61
+ Data_Get_Struct(object, CvMoments, ptr);
62
+ return ptr;
63
+ }
64
+
65
+ inline CvMoments*
66
+ CVMOMENTS_WITH_CHECK(VALUE object)
67
+ {
68
+ if (!rb_obj_is_kind_of(object, cCvMoments::rb_class()))
69
+ raise_typeerror(object, cCvMoments::rb_class());
70
+ return CVMOMENTS(object);
71
+ }
72
+
73
+ __NAMESPACE_END_OPENCV
74
+
75
+ #endif // RUBY_OPENCV_CVMOMENTS_H
@@ -0,0 +1,228 @@
1
+ /************************************************************
2
+
3
+ cvpoint.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvpoint.h"
11
+ /*
12
+ * Document-class: OpenCV::CvPoint
13
+ *
14
+ * This class means one point on X axis Y axis.
15
+ * X and Y takes the value of the Fixnum. see also CvPoint2D32F
16
+ *
17
+ * C structure is here, very simple.
18
+ * typdef struct CvPoint {
19
+ * int x;
20
+ * int y;
21
+ * }
22
+ */
23
+ __NAMESPACE_BEGIN_OPENCV
24
+ __NAMESPACE_BEGIN_CVPOINT
25
+
26
+ VALUE rb_klass;
27
+
28
+ VALUE
29
+ rb_class()
30
+ {
31
+ return rb_klass;
32
+ }
33
+
34
+ void
35
+ define_ruby_class()
36
+ {
37
+ if (rb_klass)
38
+ return;
39
+ /*
40
+ * opencv = rb_define_module("OpenCV");
41
+ *
42
+ * note: this comment is used by rdoc.
43
+ */
44
+ VALUE opencv = rb_module_opencv();
45
+
46
+ rb_klass = rb_define_class_under(opencv, "CvPoint", rb_cObject);
47
+ rb_define_alloc_func(rb_klass, rb_allocate);
48
+ rb_define_singleton_method(rb_klass, "compatible?", RUBY_METHOD_FUNC(rb_compatible_q), 1);
49
+ rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
50
+ rb_define_method(rb_klass, "x", RUBY_METHOD_FUNC(rb_x), 0);
51
+ rb_define_method(rb_klass, "x=", RUBY_METHOD_FUNC(rb_set_x), 1);
52
+ rb_define_method(rb_klass, "y", RUBY_METHOD_FUNC(rb_y), 0);
53
+ rb_define_method(rb_klass, "y=", RUBY_METHOD_FUNC(rb_set_y), 1);
54
+
55
+ rb_define_method(rb_klass, "to_s", RUBY_METHOD_FUNC(rb_to_s), 0);
56
+ rb_define_method(rb_klass, "to_ary", RUBY_METHOD_FUNC(rb_to_ary), 0);
57
+ rb_define_alias(rb_klass, "to_a", "to_ary");
58
+ }
59
+
60
+ /*
61
+ * call-seq:
62
+ * combatible?(obj)
63
+ *
64
+ * Return compatibility to CvPoint. Return true if object have method #x and #y.
65
+ *
66
+ * For example.
67
+ * class MyPoint
68
+ * def x
69
+ * 1
70
+ * end
71
+ * def y
72
+ * 2
73
+ * end
74
+ * end
75
+ * mp = MyPoint.new
76
+ * CvPoint.compatible?(mp) #=> true
77
+ * CvPoint.new(mp) #=> same as CvPoint(1, 2)
78
+ */
79
+ VALUE
80
+ rb_compatible_q(VALUE klass, VALUE object)
81
+ {
82
+ return (rb_respond_to(object, rb_intern("x")) && rb_respond_to(object, rb_intern("y"))) ? Qtrue : Qfalse;
83
+ }
84
+
85
+ VALUE
86
+ rb_allocate(VALUE klass)
87
+ {
88
+ CvPoint *ptr;
89
+ return Data_Make_Struct(klass, CvPoint, 0, -1, ptr);
90
+ }
91
+
92
+ /*
93
+ * call-seq:
94
+ * new -> CvPoint.new(0, 0)
95
+ * new(obj) -> CvPoint.new(obj.x.to_i, obj.y.to_i)
96
+ * new(x, y)
97
+ *
98
+ * Create new 2D-coordinate, (x, y). It is dropped below the decimal point.
99
+ *
100
+ * new() is same as new(0, 0)
101
+ *
102
+ * new(obj) is same as new(obj.x.to_i, obj.y.to_i)
103
+ */
104
+ VALUE
105
+ rb_initialize(int argc, VALUE *argv, VALUE self)
106
+ {
107
+ CvPoint* self_ptr = CVPOINT(self);
108
+ switch (argc) {
109
+ case 0:
110
+ break;
111
+ case 1: {
112
+ CvPoint point = VALUE_TO_CVPOINT(argv[0]);
113
+ self_ptr->x = point.x;
114
+ self_ptr->y = point.y;
115
+ break;
116
+ }
117
+ case 2:
118
+ self_ptr->x = NUM2INT(argv[0]);
119
+ self_ptr->y = NUM2INT(argv[1]);
120
+ break;
121
+ default:
122
+ rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
123
+ break;
124
+ }
125
+ return self;
126
+ }
127
+
128
+ /*
129
+ * Return parameter on x-axis.
130
+ */
131
+ VALUE
132
+ rb_x(VALUE self)
133
+ {
134
+ return INT2NUM(CVPOINT(self)->x);
135
+ }
136
+
137
+ /*
138
+ * call-seq:
139
+ * x = val
140
+ *
141
+ * Set x-axis parameter, return self.
142
+ * It is dropped below the decimal point.
143
+ * pt = CvPoint.new
144
+ * pt.x = 1.1
145
+ * pt.x #=> 1
146
+ * pt.x = 100.9
147
+ * pt.x #=> 100
148
+ */
149
+ VALUE
150
+ rb_set_x(VALUE self, VALUE x)
151
+ {
152
+ CVPOINT(self)->x = NUM2INT(x);
153
+ return self;
154
+ }
155
+
156
+ /*
157
+ * Return parameter on y-axis.
158
+ */
159
+ VALUE
160
+ rb_y(VALUE self)
161
+ {
162
+ return INT2NUM(CVPOINT(self)->y);
163
+ }
164
+
165
+ /*
166
+ * call-seq:
167
+ * y = val
168
+ *
169
+ * Set y-axis parameter, return self.
170
+ * It is dropped below the decimal point.
171
+ */
172
+ VALUE
173
+ rb_set_y(VALUE self, VALUE y)
174
+ {
175
+ CVPOINT(self)->y = NUM2INT(y);
176
+ return self;
177
+ }
178
+
179
+ /*
180
+ * call-seq:
181
+ * to_s -> "<OpenCV::CvPoint:(self.x,self.y)>"
182
+ *
183
+ * Return x and y by String.
184
+ */
185
+ VALUE
186
+ rb_to_s(VALUE self)
187
+ {
188
+ const int i = 4;
189
+ VALUE str[i];
190
+ str[0] = rb_str_new2("<%s:(%d,%d)>");
191
+ str[1] = rb_str_new2(rb_class2name(CLASS_OF(self)));
192
+ str[2] = rb_x(self);
193
+ str[3] = rb_y(self);
194
+ return rb_f_sprintf(i, str);
195
+ }
196
+
197
+ /*
198
+ * call-seq:
199
+ * to_ary -> [x, y]
200
+ *
201
+ * Return x and y by Array.
202
+ */
203
+ VALUE
204
+ rb_to_ary(VALUE self)
205
+ {
206
+ CvPoint* self_ptr = CVPOINT(self);
207
+ return rb_ary_new3(2, INT2NUM(self_ptr->x), INT2NUM(self_ptr->y));
208
+ }
209
+
210
+ VALUE
211
+ new_object()
212
+ {
213
+ VALUE object = rb_allocate(rb_klass);
214
+ *CVPOINT(object) = cvPoint(0, 0);
215
+ return object;
216
+ }
217
+
218
+
219
+ VALUE
220
+ new_object(CvPoint point)
221
+ {
222
+ VALUE object = rb_allocate(rb_klass);
223
+ *CVPOINT(object) = point;
224
+ return object;
225
+ }
226
+
227
+ __NAMESPACE_END_CVPOINT
228
+ __NAMESPACE_END_OPENCV