afeld-opencv 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. data/.gitignore +23 -0
  2. data/Gemfile +6 -0
  3. data/History.txt +5 -0
  4. data/License.txt +30 -0
  5. data/Manifest.txt +217 -0
  6. data/README.rdoc +161 -0
  7. data/Rakefile +52 -0
  8. data/examples/alpha_blend.rb +21 -0
  9. data/examples/box.png +0 -0
  10. data/examples/box_in_scene.png +0 -0
  11. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  12. data/examples/contours/bitmap-contours.png +0 -0
  13. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  14. data/examples/contours/contour_retrieval_modes.rb +139 -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/paint.rb +70 -0
  26. data/examples/snake.rb +43 -0
  27. data/examples/stuff.jpg +0 -0
  28. data/examples/tiffany.jpg +0 -0
  29. data/ext/opencv/curve.cpp +112 -0
  30. data/ext/opencv/curve.h +34 -0
  31. data/ext/opencv/cvavgcomp.cpp +67 -0
  32. data/ext/opencv/cvavgcomp.h +39 -0
  33. data/ext/opencv/cvbox2d.cpp +197 -0
  34. data/ext/opencv/cvbox2d.h +61 -0
  35. data/ext/opencv/cvcapture.cpp +499 -0
  36. data/ext/opencv/cvcapture.h +72 -0
  37. data/ext/opencv/cvchain.cpp +230 -0
  38. data/ext/opencv/cvchain.h +46 -0
  39. data/ext/opencv/cvcircle32f.cpp +116 -0
  40. data/ext/opencv/cvcircle32f.h +52 -0
  41. data/ext/opencv/cvcondensation.cpp +282 -0
  42. data/ext/opencv/cvcondensation.h +49 -0
  43. data/ext/opencv/cvconnectedcomp.cpp +143 -0
  44. data/ext/opencv/cvconnectedcomp.h +49 -0
  45. data/ext/opencv/cvcontour.cpp +297 -0
  46. data/ext/opencv/cvcontour.h +48 -0
  47. data/ext/opencv/cvcontourtree.cpp +91 -0
  48. data/ext/opencv/cvcontourtree.h +41 -0
  49. data/ext/opencv/cvconvexitydefect.cpp +103 -0
  50. data/ext/opencv/cvconvexitydefect.h +42 -0
  51. data/ext/opencv/cverror.cpp +159 -0
  52. data/ext/opencv/cverror.h +28 -0
  53. data/ext/opencv/cvfeaturetree.cpp +125 -0
  54. data/ext/opencv/cvfeaturetree.h +55 -0
  55. data/ext/opencv/cvfont.cpp +208 -0
  56. data/ext/opencv/cvfont.h +64 -0
  57. data/ext/opencv/cvhaarclassifiercascade.cpp +168 -0
  58. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  59. data/ext/opencv/cvhistogram.cpp +546 -0
  60. data/ext/opencv/cvhistogram.h +73 -0
  61. data/ext/opencv/cvhumoments.cpp +139 -0
  62. data/ext/opencv/cvhumoments.h +51 -0
  63. data/ext/opencv/cvline.cpp +154 -0
  64. data/ext/opencv/cvline.h +54 -0
  65. data/ext/opencv/cvmat.cpp +5627 -0
  66. data/ext/opencv/cvmat.h +280 -0
  67. data/ext/opencv/cvmatnd.cpp +44 -0
  68. data/ext/opencv/cvmatnd.h +28 -0
  69. data/ext/opencv/cvmemstorage.cpp +68 -0
  70. data/ext/opencv/cvmemstorage.h +53 -0
  71. data/ext/opencv/cvmoments.cpp +287 -0
  72. data/ext/opencv/cvmoments.h +75 -0
  73. data/ext/opencv/cvpoint.cpp +228 -0
  74. data/ext/opencv/cvpoint.h +64 -0
  75. data/ext/opencv/cvpoint2d32f.cpp +211 -0
  76. data/ext/opencv/cvpoint2d32f.h +63 -0
  77. data/ext/opencv/cvpoint3d32f.cpp +246 -0
  78. data/ext/opencv/cvpoint3d32f.h +66 -0
  79. data/ext/opencv/cvrect.cpp +333 -0
  80. data/ext/opencv/cvrect.h +79 -0
  81. data/ext/opencv/cvscalar.cpp +236 -0
  82. data/ext/opencv/cvscalar.h +71 -0
  83. data/ext/opencv/cvseq.cpp +599 -0
  84. data/ext/opencv/cvseq.h +72 -0
  85. data/ext/opencv/cvsize.cpp +221 -0
  86. data/ext/opencv/cvsize.h +65 -0
  87. data/ext/opencv/cvsize2d32f.cpp +209 -0
  88. data/ext/opencv/cvsize2d32f.h +64 -0
  89. data/ext/opencv/cvslice.cpp +120 -0
  90. data/ext/opencv/cvslice.h +61 -0
  91. data/ext/opencv/cvsparsemat.cpp +44 -0
  92. data/ext/opencv/cvsparsemat.h +28 -0
  93. data/ext/opencv/cvsurfparams.cpp +199 -0
  94. data/ext/opencv/cvsurfparams.h +58 -0
  95. data/ext/opencv/cvsurfpoint.cpp +223 -0
  96. data/ext/opencv/cvsurfpoint.h +52 -0
  97. data/ext/opencv/cvtermcriteria.cpp +192 -0
  98. data/ext/opencv/cvtermcriteria.h +71 -0
  99. data/ext/opencv/cvtwopoints.cpp +116 -0
  100. data/ext/opencv/cvtwopoints.h +51 -0
  101. data/ext/opencv/cvutils.cpp +194 -0
  102. data/ext/opencv/cvutils.h +29 -0
  103. data/ext/opencv/cvvideowriter.cpp +137 -0
  104. data/ext/opencv/cvvideowriter.h +43 -0
  105. data/ext/opencv/gui.cpp +68 -0
  106. data/ext/opencv/gui.h +30 -0
  107. data/ext/opencv/iplconvkernel.cpp +192 -0
  108. data/ext/opencv/iplconvkernel.h +71 -0
  109. data/ext/opencv/iplimage.cpp +576 -0
  110. data/ext/opencv/iplimage.h +70 -0
  111. data/ext/opencv/lib/opencv.rb +3 -0
  112. data/ext/opencv/lib/opencv/psyched_yaml.rb +22 -0
  113. data/ext/opencv/lib/opencv/version.rb +3 -0
  114. data/ext/opencv/mouseevent.cpp +181 -0
  115. data/ext/opencv/mouseevent.h +56 -0
  116. data/ext/opencv/opencv.cpp +710 -0
  117. data/ext/opencv/opencv.h +400 -0
  118. data/ext/opencv/pointset.cpp +284 -0
  119. data/ext/opencv/pointset.h +69 -0
  120. data/ext/opencv/trackbar.cpp +121 -0
  121. data/ext/opencv/trackbar.h +69 -0
  122. data/ext/opencv/window.cpp +357 -0
  123. data/ext/opencv/window.h +66 -0
  124. data/extconf.rb +65 -0
  125. data/images/CvMat_sobel.png +0 -0
  126. data/images/CvMat_sub_rect.png +0 -0
  127. data/images/CvSeq_relationmap.png +0 -0
  128. data/images/face_detect_from_lena.jpg +0 -0
  129. data/test/helper.rb +146 -0
  130. data/test/runner.rb +30 -0
  131. data/test/samples/airplane.jpg +0 -0
  132. data/test/samples/baboon.jpg +0 -0
  133. data/test/samples/baboon200.jpg +0 -0
  134. data/test/samples/baboon200_rotated.jpg +0 -0
  135. data/test/samples/blank0.jpg +0 -0
  136. data/test/samples/blank1.jpg +0 -0
  137. data/test/samples/blank2.jpg +0 -0
  138. data/test/samples/blank3.jpg +0 -0
  139. data/test/samples/blank4.jpg +0 -0
  140. data/test/samples/blank5.jpg +0 -0
  141. data/test/samples/blank6.jpg +0 -0
  142. data/test/samples/blank7.jpg +0 -0
  143. data/test/samples/blank8.jpg +0 -0
  144. data/test/samples/blank9.jpg +0 -0
  145. data/test/samples/cat.jpg +0 -0
  146. data/test/samples/contours.jpg +0 -0
  147. data/test/samples/fruits.jpg +0 -0
  148. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  149. data/test/samples/inpaint-mask.bmp +0 -0
  150. data/test/samples/lena-256x256.jpg +0 -0
  151. data/test/samples/lena-32x32.jpg +0 -0
  152. data/test/samples/lena-eyes.jpg +0 -0
  153. data/test/samples/lena-inpaint.jpg +0 -0
  154. data/test/samples/lena.jpg +0 -0
  155. data/test/samples/lines.jpg +0 -0
  156. data/test/samples/messy0.jpg +0 -0
  157. data/test/samples/messy1.jpg +0 -0
  158. data/test/samples/movie_sample.avi +0 -0
  159. data/test/samples/one_way_train_0000.jpg +0 -0
  160. data/test/samples/one_way_train_0001.jpg +0 -0
  161. data/test/samples/partially_blank0.jpg +0 -0
  162. data/test/samples/partially_blank1.jpg +0 -0
  163. data/test/samples/smooth0.jpg +0 -0
  164. data/test/samples/smooth1.jpg +0 -0
  165. data/test/samples/smooth2.jpg +0 -0
  166. data/test/samples/smooth3.jpg +0 -0
  167. data/test/samples/smooth4.jpg +0 -0
  168. data/test/samples/smooth5.jpg +0 -0
  169. data/test/samples/smooth6.jpg +0 -0
  170. data/test/samples/str-cv-rotated.jpg +0 -0
  171. data/test/samples/str-cv.jpg +0 -0
  172. data/test/samples/str-ov.jpg +0 -0
  173. data/test/samples/stuff.jpg +0 -0
  174. data/test/test_curve.rb +43 -0
  175. data/test/test_cvavgcomp.rb +24 -0
  176. data/test/test_cvbox2d.rb +76 -0
  177. data/test/test_cvcapture.rb +183 -0
  178. data/test/test_cvchain.rb +108 -0
  179. data/test/test_cvcircle32f.rb +41 -0
  180. data/test/test_cvconnectedcomp.rb +61 -0
  181. data/test/test_cvcontour.rb +150 -0
  182. data/test/test_cvcontourtree.rb +43 -0
  183. data/test/test_cverror.rb +50 -0
  184. data/test/test_cvfeaturetree.rb +65 -0
  185. data/test/test_cvfont.rb +58 -0
  186. data/test/test_cvhaarclassifiercascade.rb +63 -0
  187. data/test/test_cvhistogram.rb +271 -0
  188. data/test/test_cvhumoments.rb +83 -0
  189. data/test/test_cvline.rb +50 -0
  190. data/test/test_cvmat.rb +2752 -0
  191. data/test/test_cvmat_drawing.rb +318 -0
  192. data/test/test_cvmat_dxt.rb +147 -0
  193. data/test/test_cvmat_imageprocessing.rb +1943 -0
  194. data/test/test_cvmoments.rb +180 -0
  195. data/test/test_cvpoint.rb +75 -0
  196. data/test/test_cvpoint2d32f.rb +75 -0
  197. data/test/test_cvpoint3d32f.rb +93 -0
  198. data/test/test_cvrect.rb +144 -0
  199. data/test/test_cvscalar.rb +113 -0
  200. data/test/test_cvseq.rb +295 -0
  201. data/test/test_cvsize.rb +75 -0
  202. data/test/test_cvsize2d32f.rb +75 -0
  203. data/test/test_cvslice.rb +31 -0
  204. data/test/test_cvsurfparams.rb +57 -0
  205. data/test/test_cvsurfpoint.rb +66 -0
  206. data/test/test_cvtermcriteria.rb +56 -0
  207. data/test/test_cvtwopoints.rb +40 -0
  208. data/test/test_cvvideowriter.rb +58 -0
  209. data/test/test_iplconvkernel.rb +54 -0
  210. data/test/test_iplimage.rb +156 -0
  211. data/test/test_mouseevent.rb +17 -0
  212. data/test/test_opencv.rb +340 -0
  213. data/test/test_pointset.rb +130 -0
  214. data/test/test_preliminary.rb +130 -0
  215. data/test/test_trackbar.rb +47 -0
  216. data/test/test_window.rb +115 -0
  217. metadata +399 -0
@@ -0,0 +1,236 @@
1
+ /************************************************************
2
+
3
+ cvscalar.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvscalar.h"
11
+ /*
12
+ * Document-class: OpenCV::CvScalar
13
+ *
14
+ * Element-value of one pixel.
15
+ * OpenCV supports the image of 4-channels in the maximum.
16
+ * Therefore, CvScalar has 4-values.
17
+ *
18
+ * C structure is here, very simple.
19
+ * typdef struct CvScalar {
20
+ * double val[4];
21
+ * } CvScalar;
22
+ *
23
+ * If obtain CvScalar-object from the method of CvMat(or IplImage),
24
+ * the channel outside the range is obtained as all 0.
25
+ *
26
+ * image = IplImage::load("opencv.jpg") #=> 3-channel 8bit-depth BGR image
27
+ * pixel = image[10, 20] #=> Get pixel value of (10, 20) of image. pixel is CvScalar-object.
28
+ * blue, green, red = pixel[0], pixel[1], pixel[2]
29
+ * # pixel[3] always 0.
30
+ *
31
+ * CvColor is alias of CvScalar.
32
+ */
33
+ __NAMESPACE_BEGIN_OPENCV
34
+ __NAMESPACE_BEGIN_CVSCALAR
35
+
36
+
37
+ VALUE rb_klass;
38
+
39
+ VALUE
40
+ rb_class()
41
+ {
42
+ return rb_klass;
43
+ }
44
+
45
+ void
46
+ define_ruby_class()
47
+ {
48
+ if (rb_klass)
49
+ return;
50
+ /*
51
+ * opencv = rb_define_module("OpenCV");
52
+ *
53
+ * note: this comment is used by rdoc.
54
+ */
55
+ VALUE opencv = rb_module_opencv();
56
+
57
+ rb_klass = rb_define_class_under(opencv, "CvScalar", rb_cObject);
58
+ /* CvScalar: class */
59
+ rb_define_const(opencv, "CvColor", rb_klass);
60
+ rb_define_alloc_func(rb_klass, rb_allocate);
61
+ rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
62
+ rb_define_method(rb_klass, "[]", RUBY_METHOD_FUNC(rb_aref), 1);
63
+ rb_define_method(rb_klass, "[]=", RUBY_METHOD_FUNC(rb_aset), 2);
64
+ rb_define_method(rb_klass, "sub", RUBY_METHOD_FUNC(rb_sub), -1);
65
+ rb_define_alias(rb_klass, "-", "sub");
66
+
67
+ rb_define_method(rb_klass, "to_s", RUBY_METHOD_FUNC(rb_to_s), 0);
68
+ rb_define_method(rb_klass, "to_ary", RUBY_METHOD_FUNC(rb_to_ary), 0);
69
+ rb_define_alias(rb_klass, "to_a", "to_ary");
70
+
71
+ rb_define_const(rb_klass, "Black", cCvScalar::new_object(cvScalar(0x0,0x0,0x0)));
72
+ rb_define_const(rb_klass, "Silver", cCvScalar::new_object(cvScalar(0x0c,0x0c,0x0c)));
73
+ rb_define_const(rb_klass, "Gray", cCvScalar::new_object(cvScalar(0x80,0x80,0x80)));
74
+ rb_define_const(rb_klass, "White", cCvScalar::new_object(cvScalar(0xff,0xff,0xff)));
75
+ rb_define_const(rb_klass, "Maroon", cCvScalar::new_object(cvScalar(0x0,0x0,0x80)));
76
+ rb_define_const(rb_klass, "Red", cCvScalar::new_object(cvScalar(0x0,0x0,0xff)));
77
+ rb_define_const(rb_klass, "Purple", cCvScalar::new_object(cvScalar(0x80,0x0,0x80)));
78
+ rb_define_const(rb_klass, "Fuchsia", cCvScalar::new_object(cvScalar(0xff,0x0,0xff)));
79
+ rb_define_const(rb_klass, "Green", cCvScalar::new_object(cvScalar(0x0,0x80,0x0)));
80
+ rb_define_const(rb_klass, "Lime", cCvScalar::new_object(cvScalar(0x0,0xff,0x0)));
81
+ rb_define_const(rb_klass, "Olive", cCvScalar::new_object(cvScalar(0x0,0x80,0x80)));
82
+ rb_define_const(rb_klass, "Yellow", cCvScalar::new_object(cvScalar(0x0,0xff,0xff)));
83
+ rb_define_const(rb_klass, "Navy", cCvScalar::new_object(cvScalar(0x80,0x0,0x0)));
84
+ rb_define_const(rb_klass, "Blue", cCvScalar::new_object(cvScalar(0xff,0x0,0x0)));
85
+ rb_define_const(rb_klass, "Teal", cCvScalar::new_object(cvScalar(0x80,0x80,0x0)));
86
+ rb_define_const(rb_klass, "Aqua", cCvScalar::new_object(cvScalar(0xff,0xff,0x0)));
87
+ }
88
+
89
+ VALUE
90
+ rb_allocate(VALUE klass)
91
+ {
92
+ CvScalar *ptr;
93
+ return Data_Make_Struct(klass, CvScalar, 0, -1, ptr);
94
+ }
95
+
96
+ /*
97
+ * call-seq:
98
+ * new(<i>[d1][,d2][,d3][,d4]</i>)
99
+ *
100
+ * Create new Scalar. Argument should be Fixnum (or nil as 0).
101
+ */
102
+ VALUE
103
+ rb_initialize(int argc, VALUE *argv, VALUE self)
104
+ {
105
+ VALUE val[4];
106
+ rb_scan_args(argc, argv, "04", &val[0], &val[1], &val[2], &val[3]);
107
+ CvScalar* self_ptr = CVSCALAR(self);
108
+ for (int i = 0; i < 4; ++i) {
109
+ self_ptr->val[i] = NIL_P(val[i]) ? 0 : NUM2DBL(val[i]);
110
+ }
111
+ return self;
112
+ }
113
+
114
+ /*
115
+ * call-seq:
116
+ * [<i>index</i>]
117
+ *
118
+ * Return value of <i>index</i> dimension.
119
+ */
120
+ VALUE
121
+ rb_aref(VALUE self, VALUE index)
122
+ {
123
+ int idx = NUM2INT(index);
124
+ if (idx < 0 || idx >= 4) {
125
+ rb_raise(rb_eIndexError, "scalar index should be 0...4");
126
+ }
127
+ return rb_float_new(CVSCALAR(self)->val[idx]);
128
+ }
129
+
130
+ /*
131
+ * call-seq:
132
+ * [<i>index</i>] = <i>value</i>
133
+ *
134
+ * Set value of <i>index</i> dimension to <i>value</i>
135
+ */
136
+ VALUE
137
+ rb_aset(VALUE self, VALUE index, VALUE value)
138
+ {
139
+ int idx = NUM2INT(index);
140
+ if (idx < 0 || idx >= 4) {
141
+ rb_raise(rb_eIndexError, "scalar index should be 0...4");
142
+ }
143
+ CVSCALAR(self)->val[idx] = NUM2DBL(value);
144
+ return self;
145
+ }
146
+
147
+ /*
148
+ * call-seq:
149
+ * sub(val[,mask])
150
+ *
151
+ * Return new CvScalar if <i>val</i> is CvScalar or compatible object.
152
+ * self[I] - val[I]
153
+ * Or return new CvMat if <i>val</i> is CvMat or subclass.
154
+ */
155
+ VALUE
156
+ rb_sub(int argc, VALUE *argv, VALUE self)
157
+ {
158
+ VALUE val, mask;
159
+ rb_scan_args(argc, argv, "11", &val, &mask);
160
+ if (rb_obj_is_kind_of(val, cCvMat::rb_class())) {
161
+ CvArr *val_ptr = CVARR(val);
162
+ VALUE dest = Qnil;
163
+ try {
164
+ dest = cCvMat::new_object(cvGetSize(val_ptr), cvGetElemType(val_ptr));
165
+ cvSubRS(val_ptr, *CVSCALAR(self), CVARR(dest), MASK(mask));
166
+ }
167
+ catch (cv::Exception& e) {
168
+ raise_cverror(e);
169
+ }
170
+ return dest;
171
+ }
172
+ else {
173
+ CvScalar *src = CVSCALAR(self);
174
+ CvScalar scl = VALUE_TO_CVSCALAR(val);
175
+ return new_object(cvScalar(src->val[0] - scl.val[0],
176
+ src->val[1] - scl.val[1],
177
+ src->val[2] - scl.val[2],
178
+ src->val[3] - scl.val[3]));
179
+ }
180
+ }
181
+
182
+ /*
183
+ * call-seq:
184
+ * to_s -> "<OpeCV::CvScalar:#{self[0]},#{self[1]},#{self[2]},#{self[3]}>"
185
+ *
186
+ * Return values by String.
187
+ */
188
+ VALUE
189
+ rb_to_s(VALUE self)
190
+ {
191
+ const int i = 6;
192
+ VALUE str[i];
193
+ str[0] = rb_str_new2("<%s:%g,%g,%g,%g>");
194
+ str[1] = rb_str_new2(rb_class2name(CLASS_OF(self)));
195
+ str[2] = rb_aref(self, INT2FIX(0));
196
+ str[3] = rb_aref(self, INT2FIX(1));
197
+ str[4] = rb_aref(self, INT2FIX(2));
198
+ str[5] = rb_aref(self, INT2FIX(3));
199
+ return rb_f_sprintf(i, str);
200
+ }
201
+
202
+ /*
203
+ * call-seq:
204
+ * to_ary -> [self[0],self[1],self[2],self[3]]
205
+ *
206
+ * Return values by Array.
207
+ */
208
+ VALUE
209
+ rb_to_ary(VALUE self)
210
+ {
211
+ return rb_ary_new3(4,
212
+ rb_aref(self, INT2FIX(0)),
213
+ rb_aref(self, INT2FIX(1)),
214
+ rb_aref(self, INT2FIX(2)),
215
+ rb_aref(self, INT2FIX(3)));
216
+ }
217
+
218
+ VALUE
219
+ new_object()
220
+ {
221
+ VALUE object = rb_allocate(rb_klass);
222
+ *CVSCALAR(object) = cvScalar(0);
223
+ return object;
224
+ }
225
+
226
+ VALUE
227
+ new_object(CvScalar scalar)
228
+ {
229
+ VALUE object = rb_allocate(rb_klass);
230
+ *CVSCALAR(object) = scalar;
231
+ return object;
232
+ }
233
+
234
+ __NAMESPACE_END_CVSCALAR
235
+ __NAMESPACE_END_OPENCV
236
+
@@ -0,0 +1,71 @@
1
+ /************************************************************
2
+
3
+ cvscalar.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_CVSCALAR_H
11
+ #define RUBY_OPENCV_CVSCALAR_H
12
+
13
+ #include "opencv.h"
14
+
15
+ #define __NAMESPACE_BEGIN_CVSCALAR namespace cCvScalar {
16
+ #define __NAMESPACE_END_CVSCALAR }
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_CVSCALAR
20
+
21
+ VALUE rb_class();
22
+
23
+ void define_ruby_class();
24
+
25
+ VALUE rb_compatible_q(VALUE klass, VALUE object);
26
+
27
+ VALUE rb_allocate(VALUE klass);
28
+ VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
29
+
30
+ VALUE rb_aref(VALUE self, VALUE index);
31
+ VALUE rb_aset(VALUE self, VALUE index, VALUE value);
32
+ VALUE rb_sub(int argc, VALUE *argv, VALUE self);
33
+
34
+ VALUE rb_to_s(VALUE self);
35
+ VALUE rb_to_ary(VALUE self);
36
+
37
+ VALUE new_object();
38
+ VALUE new_object(CvScalar scalar);
39
+
40
+ __NAMESPACE_END_CVSCALAR
41
+
42
+ inline CvScalar*
43
+ CVSCALAR(VALUE object)
44
+ {
45
+ CvScalar *ptr;
46
+ Data_Get_Struct(object, CvScalar, ptr);
47
+ return ptr;
48
+ }
49
+
50
+ inline CvScalar
51
+ VALUE_TO_CVSCALAR(VALUE object)
52
+ {
53
+ ID aref_id;
54
+ if (FIXNUM_P(object)) {
55
+ return cvScalarAll(FIX2INT(object));
56
+ }
57
+ else if (rb_respond_to(object, (aref_id = rb_intern("[]")))) {
58
+ return cvScalar(NUM2DBL(rb_funcall(object, aref_id, 1, INT2FIX(0))),
59
+ NUM2DBL(rb_funcall(object, aref_id, 1, INT2FIX(1))),
60
+ NUM2DBL(rb_funcall(object, aref_id, 1, INT2FIX(2))),
61
+ NUM2DBL(rb_funcall(object, aref_id, 1, INT2FIX(3))));
62
+ }
63
+ else {
64
+ raise_compatible_typeerror(object, cCvScalar::rb_class());
65
+ }
66
+ throw "Should never reach here";
67
+ }
68
+
69
+ __NAMESPACE_END_OPENCV
70
+
71
+ #endif // RUBY_OPENCV_CVSCALAR_H
@@ -0,0 +1,599 @@
1
+ /************************************************************
2
+
3
+ cvseq.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvseq.h"
11
+ /*
12
+ * Document-class: OpenCV::CvSeq
13
+ *
14
+ * Generic Sequence class. CvSeq has the method like Array (push, pop, select, etc...).
15
+ * But, CvSeq cannot store the object of a different class.
16
+ * When storing object in CvSeq, conversion is automatically tried,
17
+ * and the object occurs the error if it cannot be done.
18
+ *
19
+ * e.g.
20
+ * seq = CvSeq.new(CvPoint) # Argument mean "this sequence contain only this class's object"
21
+ * seq.push(CvPoint.new(0, 0)) # => it's ok
22
+ * seq.push("hello") # => try convert "hello" to CvPoint. but can't do it. raise error.
23
+ *
24
+ * If the sequecne contain object of class A.
25
+ * When storing object(named "obj") of class B to the sequence.
26
+ * Try automatically : A.from_B(obj) => object of class A.
27
+ *
28
+ * The sequence might have another sequence outside. see below.
29
+ * Each sequece has h_prev, h_next, v_prev, v_next method.
30
+ * If the adjoining sequence exists, each method return the adjoining sequence.
31
+ * Otherwise return nil.
32
+ *
33
+ * link:../images/CvSeq_relationmap.png
34
+ */
35
+ __NAMESPACE_BEGIN_OPENCV
36
+ __NAMESPACE_BEGIN_CVSEQ
37
+
38
+ VALUE rb_allocate(VALUE klass);
39
+ void cvseq_free(void *ptr);
40
+
41
+ VALUE rb_klass;
42
+ // contain sequence-block class
43
+ st_table *seqblock_klass_table = st_init_numtable();
44
+
45
+ VALUE
46
+ rb_class()
47
+ {
48
+ return rb_klass;
49
+ }
50
+
51
+ VALUE
52
+ seqblock_class(void *ptr)
53
+ {
54
+ VALUE klass;
55
+ if (!st_lookup(seqblock_klass_table, (st_data_t)ptr, (st_data_t*)&klass)) {
56
+ rb_raise(rb_eTypeError, "Invalid sequence error.");
57
+ }
58
+ return klass;
59
+ }
60
+
61
+ void
62
+ register_elem_class(CvSeq *seq, VALUE klass)
63
+ {
64
+ st_insert(seqblock_klass_table, (st_data_t)seq, (st_data_t)klass);
65
+ }
66
+
67
+ void
68
+ unregister_elem_class(void *ptr)
69
+ {
70
+ if (ptr) {
71
+ st_delete(seqblock_klass_table, (st_data_t*)&ptr, NULL);
72
+ unregister_object(ptr);
73
+ }
74
+ }
75
+
76
+ void
77
+ define_ruby_class()
78
+ {
79
+ if (rb_klass)
80
+ return;
81
+ /*
82
+ * opencv = rb_define_module("OpenCV");
83
+ *
84
+ * note: this comment is used by rdoc.
85
+ */
86
+ VALUE opencv = rb_module_opencv();
87
+ rb_klass = rb_define_class_under(opencv, "CvSeq", rb_cObject);
88
+ rb_include_module(rb_klass, rb_mEnumerable);
89
+ rb_define_alloc_func(rb_klass, rb_allocate);
90
+ rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
91
+ rb_define_method(rb_klass, "total", RUBY_METHOD_FUNC(rb_total), 0);
92
+ rb_define_alias(rb_klass, "length", "total");
93
+ rb_define_alias(rb_klass, "size", "total");
94
+ rb_define_method(rb_klass, "empty?", RUBY_METHOD_FUNC(rb_empty_q), 0);
95
+ rb_define_method(rb_klass, "[]", RUBY_METHOD_FUNC(rb_aref), 1);
96
+ rb_define_method(rb_klass, "first", RUBY_METHOD_FUNC(rb_first), 0);
97
+ rb_define_method(rb_klass, "last", RUBY_METHOD_FUNC(rb_last), 0);
98
+
99
+ rb_define_method(rb_klass, "h_prev", RUBY_METHOD_FUNC(rb_h_prev), 0);
100
+ rb_define_method(rb_klass, "h_next", RUBY_METHOD_FUNC(rb_h_next), 0);
101
+ rb_define_method(rb_klass, "v_prev", RUBY_METHOD_FUNC(rb_v_prev), 0);
102
+ rb_define_method(rb_klass, "v_next", RUBY_METHOD_FUNC(rb_v_next), 0);
103
+
104
+ rb_define_method(rb_klass, "push", RUBY_METHOD_FUNC(rb_push), -2);
105
+ rb_define_alias(rb_klass, "<<", "push");
106
+ rb_define_method(rb_klass, "pop", RUBY_METHOD_FUNC(rb_pop), 0);
107
+ rb_define_method(rb_klass, "unshift", RUBY_METHOD_FUNC(rb_unshift), -2);
108
+ rb_define_alias(rb_klass, "push_front", "unshift");
109
+ rb_define_method(rb_klass, "shift", RUBY_METHOD_FUNC(rb_shift), 0);
110
+ rb_define_alias(rb_klass, "pop_front", "shift");
111
+ rb_define_method(rb_klass, "each", RUBY_METHOD_FUNC(rb_each), 0);
112
+ rb_define_method(rb_klass, "each_index", RUBY_METHOD_FUNC(rb_each_index), 0);
113
+ rb_define_method(rb_klass, "insert", RUBY_METHOD_FUNC(rb_insert), 2);
114
+ rb_define_method(rb_klass, "remove", RUBY_METHOD_FUNC(rb_remove), 1);
115
+ rb_define_alias(rb_klass, "delete_at", "remove");
116
+ rb_define_method(rb_klass, "clear", RUBY_METHOD_FUNC(rb_clear), 0);
117
+ }
118
+
119
+ VALUE
120
+ rb_allocate(VALUE klass)
121
+ {
122
+ CvSeq *ptr = ALLOC(CvSeq);
123
+ return Data_Wrap_Struct(klass, 0, unregister_elem_class, ptr);
124
+ }
125
+
126
+ /*
127
+ * call-seq:
128
+ * CvSeq.new(<i>type[,storage]</i>)
129
+ *
130
+ * Return a new CvSeq. <i>type</i> should be following classes.
131
+ *
132
+ * * CvIndex
133
+ * * CvPoint
134
+ */
135
+ VALUE
136
+ rb_initialize(int argc, VALUE *argv, VALUE self)
137
+ {
138
+ VALUE klass, storage_value;
139
+
140
+ rb_scan_args(argc, argv, "11", &klass, &storage_value);
141
+ if (!rb_obj_is_kind_of(klass, rb_cClass))
142
+ raise_typeerror(klass, rb_cClass);
143
+
144
+ int type = 0, size = 0;
145
+ if (klass == rb_cFixnum) {
146
+ type = CV_SEQ_ELTYPE_INDEX;
147
+ size = sizeof(int);
148
+ }
149
+ else if (klass == cCvPoint::rb_class()) {
150
+ type = CV_SEQ_ELTYPE_POINT;
151
+ size = sizeof(CvPoint);
152
+ }
153
+ else if (klass == cCvPoint2D32f::rb_class()) {
154
+ type = CV_SEQ_ELTYPE_POINT;
155
+ size = sizeof(CvPoint2D32f);
156
+ }
157
+ else if (klass == cCvPoint3D32f::rb_class()) {
158
+ type = CV_SEQ_ELTYPE_POINT3D;
159
+ size = sizeof(CvPoint3D32f);
160
+ }
161
+ else
162
+ rb_raise(rb_eArgError, "unsupport %s class for sequence-block.", rb_class2name(klass));
163
+
164
+ CvSeq* seq = NULL;
165
+ if (NIL_P(storage_value)) {
166
+ storage_value = cCvMemStorage::new_object(0);
167
+ }
168
+ else {
169
+ storage_value = CHECK_CVMEMSTORAGE(storage_value);
170
+ }
171
+
172
+ try {
173
+ seq = cvCreateSeq(type, sizeof(CvSeq), size, CVMEMSTORAGE(storage_value));
174
+ }
175
+ catch (cv::Exception& e) {
176
+ raise_cverror(e);
177
+ }
178
+ DATA_PTR(self) = seq;
179
+ register_elem_class(seq, klass);
180
+ register_root_object(seq, storage_value);
181
+
182
+ return self;
183
+ }
184
+
185
+ /*
186
+ * call-seq:
187
+ * total -> int
188
+ *
189
+ * Return total number of sequence-block.
190
+ */
191
+ VALUE
192
+ rb_total(VALUE self)
193
+ {
194
+ return INT2NUM(CVSEQ(self)->total);
195
+ }
196
+
197
+ /*
198
+ * call-seq:
199
+ * empty? -> true or false.
200
+ *
201
+ * Return <tt>true</tt> if contain no object, otherwize return <tt>false</tt>.
202
+ */
203
+ VALUE
204
+ rb_empty_q(VALUE self)
205
+ {
206
+ return CVSEQ(self)->total == 0 ? Qtrue : Qfalse;
207
+ }
208
+
209
+ /*
210
+ * call-seq:
211
+ * [<i>index</i>] -> obj or nil
212
+ *
213
+ * Return sequence-block at <i>index</i>.
214
+ */
215
+ VALUE
216
+ rb_aref(VALUE self, VALUE index)
217
+ {
218
+ CvSeq *seq = CVSEQ(self);
219
+ int idx = NUM2INT(index);
220
+ if (seq->total == 0)
221
+ return Qnil;
222
+ if (idx >= seq->total)
223
+ rb_raise(rb_eIndexError, "index %d out of sequence", idx);
224
+
225
+ VALUE result = Qnil;
226
+ try {
227
+ if (seqblock_class(seq) == rb_cFixnum)
228
+ result = INT2NUM(*CV_GET_SEQ_ELEM(int, seq, idx));
229
+ else
230
+ result = REFER_OBJECT(seqblock_class(seq), cvGetSeqElem(seq, idx), self);
231
+ }
232
+ catch (cv::Exception& e) {
233
+ raise_cverror(e);
234
+ }
235
+ return result;
236
+ }
237
+
238
+ /*
239
+ * call-seq:
240
+ * first -> obj or nil
241
+ *
242
+ * Return first sequence-block.
243
+ */
244
+ VALUE
245
+ rb_first(VALUE self)
246
+ {
247
+ return rb_aref(self, INT2FIX(0));
248
+ }
249
+
250
+ /*
251
+ * call-seq:
252
+ * last -> obj or nil
253
+ *
254
+ * Return last sequence-block.
255
+ */
256
+ VALUE
257
+ rb_last(VALUE self)
258
+ {
259
+ return rb_aref(self, INT2FIX(-1));
260
+ }
261
+
262
+ /*
263
+ * call-seq:
264
+ * h_prev -> seq or nil
265
+ *
266
+ * Return the sequence horizontally located in previous.
267
+ * Return <tt>nil</tt> if not existing.
268
+ */
269
+ VALUE
270
+ rb_h_prev(VALUE self)
271
+ {
272
+ CvSeq *seq = CVSEQ(self);
273
+ if (seq->h_prev)
274
+ return new_sequence(CLASS_OF(self), seq->h_prev, seqblock_class(seq), lookup_root_object(seq));
275
+ else
276
+ return Qnil;
277
+ }
278
+
279
+ /*
280
+ * call-seq:
281
+ * h_next -> seq or nil
282
+ *
283
+ * Return the sequence horizontally located in next.
284
+ * Return <tt>nil</tt> if not existing.
285
+ */
286
+ VALUE
287
+ rb_h_next(VALUE self)
288
+ {
289
+ CvSeq *seq = CVSEQ(self);
290
+ if (seq->h_next)
291
+ return new_sequence(CLASS_OF(self), seq->h_next, seqblock_class(seq), lookup_root_object(seq));
292
+ else
293
+ return Qnil;
294
+ }
295
+
296
+ /*
297
+ * call-seq:
298
+ * v_prev -> seq or nil
299
+ *
300
+ * Return the sequence vertically located in previous.
301
+ * Return <tt>nil</tt> if not existing.
302
+ */
303
+ VALUE
304
+ rb_v_prev(VALUE self)
305
+ {
306
+ CvSeq *seq = CVSEQ(self);
307
+ if (seq->v_prev)
308
+ return new_sequence(CLASS_OF(self), seq->v_prev, seqblock_class(seq), lookup_root_object(seq));
309
+ else
310
+ return Qnil;
311
+ }
312
+
313
+ /*
314
+ * call-seq:
315
+ * v_next -> seq or nil
316
+ *
317
+ * Return the sequence vertically located in next.
318
+ * Return <tt>nil</tt> if not existing.
319
+ */
320
+ VALUE
321
+ rb_v_next(VALUE self)
322
+ {
323
+ CvSeq *seq = CVSEQ(self);
324
+ if (seq->v_next)
325
+ return new_sequence(CLASS_OF(self), seq->v_next, seqblock_class(seq), lookup_root_object(seq));
326
+ else
327
+ return Qnil;
328
+ }
329
+
330
+ VALUE
331
+ rb_seq_push(VALUE self, VALUE args, int flag)
332
+ {
333
+ CvSeq *seq = CVSEQ(self);
334
+ VALUE klass = seqblock_class(seq), object;
335
+ void *elem = NULL;
336
+ int len = RARRAY_LEN(args);
337
+ for (int i = 0; i < len; ++i) {
338
+ object = RARRAY_PTR(args)[i];
339
+ if (CLASS_OF(object) == klass) {
340
+ if (TYPE(object) == T_FIXNUM) {
341
+ int int_elem = FIX2INT(object);
342
+ elem = &int_elem;
343
+ }
344
+ else {
345
+ elem = (void*)DATA_PTR(object);
346
+ }
347
+ try {
348
+ if (flag == CV_FRONT)
349
+ cvSeqPushFront(seq, elem);
350
+ else
351
+ cvSeqPush(seq, elem);
352
+ }
353
+ catch (cv::Exception& e) {
354
+ raise_cverror(e);
355
+ }
356
+ }
357
+ else if (rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(rb_first(object)) == klass) { // object is CvSeq
358
+ void *buffer = NULL;
359
+ try {
360
+ buffer = cvCvtSeqToArray(CVSEQ(object), rb_cvAlloc(CVSEQ(object)->total * CVSEQ(object)->elem_size));
361
+ cvSeqPushMulti(seq, buffer, CVSEQ(object)->total, flag);
362
+ cvFree(&buffer);
363
+ }
364
+ catch (cv::Exception& e) {
365
+ if (buffer != NULL)
366
+ cvFree(&buffer);
367
+ raise_cverror(e);
368
+ }
369
+ }
370
+ else {
371
+ rb_raise(rb_eTypeError, "arguments should be %s or %s which includes %s.",
372
+ rb_class2name(klass), rb_class2name(rb_klass), rb_class2name(klass));
373
+ }
374
+ }
375
+
376
+ return self;
377
+ }
378
+
379
+ /*
380
+ * call-seq:
381
+ * push(<i>obj, ...</i>) -> self
382
+ *
383
+ * Append - Pushes the given object(s) on the end of this sequence. This expression return the sequence itself,
384
+ * so several append may be chained together.
385
+ */
386
+ VALUE
387
+ rb_push(VALUE self, VALUE args)
388
+ {
389
+ return rb_seq_push(self, args, CV_BACK);
390
+ }
391
+
392
+ /*
393
+ * call-seq:
394
+ * pop -> obj or nil
395
+ *
396
+ * Remove the last sequence-block from <i>self</i> and return it,
397
+ * or <tt>nil</tt> if the sequence is empty.
398
+ */
399
+ VALUE
400
+ rb_pop(VALUE self)
401
+ {
402
+ CvSeq *seq = CVSEQ(self);
403
+ if (seq->total == 0)
404
+ return Qnil;
405
+
406
+ VALUE object = Qnil;
407
+ VALUE klass = seqblock_class(seq);
408
+ try {
409
+ if (klass == rb_cFixnum) {
410
+ int n = 0;
411
+ cvSeqPop(seq, &n);
412
+ object = INT2FIX(n);
413
+ }
414
+ else {
415
+ object = GENERIC_OBJECT(klass, malloc(seq->elem_size));
416
+ cvSeqPop(seq, DATA_PTR(object));
417
+ }
418
+ }
419
+ catch (cv::Exception& e) {
420
+ raise_cverror(e);
421
+ }
422
+ return object;
423
+ }
424
+
425
+ /*
426
+ * call-seq:
427
+ * clear -> self
428
+ *
429
+ * Clears sequence. Removes all elements from the sequence.
430
+ */
431
+ VALUE
432
+ rb_clear(VALUE self)
433
+ {
434
+ try {
435
+ cvClearSeq(CVSEQ(self));
436
+ }
437
+ catch (cv::Exception& e) {
438
+ raise_cverror(e);
439
+ }
440
+ return self;
441
+ }
442
+
443
+ /*
444
+ * call-seq:
445
+ * unshift -> self
446
+ *
447
+ * Prepends objects to the front of sequence. other elements up one.
448
+ */
449
+ VALUE
450
+ rb_unshift(VALUE self, VALUE args)
451
+ {
452
+ VALUE result = Qnil;
453
+ try {
454
+ result = rb_seq_push(self, args, CV_FRONT);
455
+ }
456
+ catch (cv::Exception& e) {
457
+ raise_cverror(e);
458
+ }
459
+ return result;
460
+ }
461
+
462
+ /*
463
+ * call-seq:
464
+ * shift -> obj or nil
465
+ *
466
+ * Returns the first element of <i>self</i> and removes it (shifting all other elements down by one). Returns <tt>nil</tt> if the array is empty.
467
+ */
468
+ VALUE
469
+ rb_shift(VALUE self)
470
+ {
471
+ CvSeq *seq = CVSEQ(self);
472
+ if (seq->total == 0)
473
+ return Qnil;
474
+
475
+ VALUE object = Qnil;
476
+ try {
477
+ if (seqblock_class(seq) == rb_cFixnum) {
478
+ int n = 0;
479
+ cvSeqPopFront(seq, &n);
480
+ object = INT2NUM(n);
481
+ }
482
+ else {
483
+ object = GENERIC_OBJECT(seqblock_class(seq), malloc(seq->elem_size));
484
+ cvSeqPopFront(seq, DATA_PTR(object));
485
+ }
486
+ }
487
+ catch (cv::Exception& e) {
488
+ raise_cverror(e);
489
+ }
490
+
491
+ return object;
492
+ }
493
+
494
+ /*
495
+ * call-seq:
496
+ * each {|obj| ... } -> self
497
+ *
498
+ * Calls block once for each sequence-block in <i>self</i>,
499
+ * passing that sequence-block as a parameter.
500
+ * seq = CvSeq.new(CvIndex)
501
+ * seq.push(5, 6, 7)
502
+ * seq.each {|x| print x, " -- " }
503
+ * produces:
504
+ * 5 -- 6 -- 7 --
505
+ */
506
+ VALUE
507
+ rb_each(VALUE self)
508
+ {
509
+ CvSeq *seq = CVSEQ(self);
510
+ if (seq->total > 0) {
511
+ VALUE klass = seqblock_class(seq);
512
+ try {
513
+ if (klass == rb_cFixnum)
514
+ for (int i = 0; i < seq->total; ++i)
515
+ rb_yield(INT2NUM(*CV_GET_SEQ_ELEM(int, seq, i)));
516
+ else
517
+ for (int i = 0; i < seq->total; ++i)
518
+ rb_yield(REFER_OBJECT(klass, cvGetSeqElem(seq, i), self));
519
+ }
520
+ catch (cv::Exception& e) {
521
+ raise_cverror(e);
522
+ }
523
+ }
524
+ return self;
525
+ }
526
+
527
+ /*
528
+ * call-seq:
529
+ * each_index {|index| ... } -> self
530
+ *
531
+ * Same as CvSeq#each, but passes the index of the element instead of the element itself.
532
+ */
533
+ VALUE
534
+ rb_each_index(VALUE self)
535
+ {
536
+ CvSeq *seq = CVSEQ(self);
537
+ for(int i = 0; i < seq->total; ++i)
538
+ rb_yield(INT2NUM(i));
539
+ return self;
540
+ }
541
+
542
+
543
+ /*
544
+ * call-seq:
545
+ * insert(<i>index,obj</i>) -> self
546
+ *
547
+ * Inserts the given values before element with the given index (which may be negative).
548
+ */
549
+ VALUE
550
+ rb_insert(VALUE self, VALUE index, VALUE object)
551
+ {
552
+ Check_Type(index, T_FIXNUM);
553
+ CvSeq *seq = CVSEQ(self);
554
+ VALUE klass = seqblock_class(seq);
555
+ if (CLASS_OF(object) != klass)
556
+ rb_raise(rb_eTypeError, "arguments should be %s.", rb_class2name(klass));
557
+ try {
558
+ if (klass == rb_cFixnum) {
559
+ int n = NUM2INT(object);
560
+ cvSeqInsert(seq, NUM2INT(index), &n);
561
+ }
562
+ else
563
+ cvSeqInsert(seq, NUM2INT(index), DATA_PTR(object));
564
+ }
565
+ catch (cv::Exception& e) {
566
+ raise_cverror(e);
567
+ }
568
+ return self;
569
+ }
570
+
571
+ /*
572
+ * call-seq:
573
+ * remove(<i>index</i>) -> obj or nil
574
+ *
575
+ * Deletes the elements at the specified index.
576
+ */
577
+ VALUE
578
+ rb_remove(VALUE self, VALUE index)
579
+ {
580
+ try {
581
+ cvSeqRemove(CVSEQ(self), NUM2INT(index));
582
+ }
583
+ catch (cv::Exception& e) {
584
+ raise_cverror(e);
585
+ }
586
+ return self;
587
+ }
588
+
589
+ VALUE
590
+ new_sequence(VALUE klass, CvSeq *seq, VALUE element_klass, VALUE storage)
591
+ {
592
+ register_root_object(seq, storage);
593
+ if (!NIL_P(element_klass))
594
+ register_elem_class(seq, element_klass);
595
+ return Data_Wrap_Struct(klass, mark_root_object, unregister_elem_class, seq);
596
+ }
597
+
598
+ __NAMESPACE_END_CVSEQ
599
+ __NAMESPACE_END_OPENCV