jf-ruby-opencv 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +28 -0
  3. data/.yardopts +3 -0
  4. data/DEVELOPERS_NOTE.md +137 -0
  5. data/Gemfile +9 -0
  6. data/History.txt +5 -0
  7. data/License.txt +30 -0
  8. data/Manifest.txt +239 -0
  9. data/README.md +94 -0
  10. data/Rakefile +99 -0
  11. data/config.yml +7 -0
  12. data/examples/alpha_blend.rb +21 -0
  13. data/examples/contours/bitmap-contours-with-labels.png +0 -0
  14. data/examples/contours/bitmap-contours.png +0 -0
  15. data/examples/contours/bounding-box-detect-canny.rb +62 -0
  16. data/examples/contours/contour_retrieval_modes.rb +139 -0
  17. data/examples/contours/rotated-boxes.jpg +0 -0
  18. data/examples/convexhull.rb +47 -0
  19. data/examples/face_detect.rb +20 -0
  20. data/examples/facerec/create_csv.rb +43 -0
  21. data/examples/facerec/facerec_eigenfaces.rb +132 -0
  22. data/examples/facerec/facerec_fisherfaces.rb +131 -0
  23. data/examples/facerec/facerec_lbph.rb +116 -0
  24. data/examples/facerec/readme.md +111 -0
  25. data/examples/find_obj.rb +169 -0
  26. data/examples/houghcircle.rb +22 -0
  27. data/examples/images/box.png +0 -0
  28. data/examples/images/box_in_scene.png +0 -0
  29. data/examples/images/inpaint.png +0 -0
  30. data/examples/images/lena-256x256.jpg +0 -0
  31. data/examples/images/lena-eyes.jpg +0 -0
  32. data/examples/images/lenna-rotated.jpg +0 -0
  33. data/examples/images/lenna.jpg +0 -0
  34. data/examples/images/stuff.jpg +0 -0
  35. data/examples/images/tiffany.jpg +0 -0
  36. data/examples/inpaint.rb +57 -0
  37. data/examples/match_kdtree.rb +88 -0
  38. data/examples/match_template.rb +26 -0
  39. data/examples/paint.rb +70 -0
  40. data/examples/snake.rb +43 -0
  41. data/ext/opencv/algorithm.cpp +291 -0
  42. data/ext/opencv/algorithm.h +38 -0
  43. data/ext/opencv/curve.cpp +127 -0
  44. data/ext/opencv/curve.h +34 -0
  45. data/ext/opencv/cvavgcomp.cpp +64 -0
  46. data/ext/opencv/cvavgcomp.h +39 -0
  47. data/ext/opencv/cvbox2d.cpp +195 -0
  48. data/ext/opencv/cvbox2d.h +61 -0
  49. data/ext/opencv/cvcapture.cpp +633 -0
  50. data/ext/opencv/cvcapture.h +82 -0
  51. data/ext/opencv/cvchain.cpp +233 -0
  52. data/ext/opencv/cvchain.h +46 -0
  53. data/ext/opencv/cvcircle32f.cpp +126 -0
  54. data/ext/opencv/cvcircle32f.h +52 -0
  55. data/ext/opencv/cvconnectedcomp.cpp +156 -0
  56. data/ext/opencv/cvconnectedcomp.h +49 -0
  57. data/ext/opencv/cvcontour.cpp +384 -0
  58. data/ext/opencv/cvcontour.h +51 -0
  59. data/ext/opencv/cvcontourtree.cpp +96 -0
  60. data/ext/opencv/cvcontourtree.h +41 -0
  61. data/ext/opencv/cvconvexitydefect.cpp +92 -0
  62. data/ext/opencv/cvconvexitydefect.h +42 -0
  63. data/ext/opencv/cverror.cpp +115 -0
  64. data/ext/opencv/cverror.h +28 -0
  65. data/ext/opencv/cvfeaturetree.cpp +123 -0
  66. data/ext/opencv/cvfeaturetree.h +55 -0
  67. data/ext/opencv/cvfont.cpp +228 -0
  68. data/ext/opencv/cvfont.h +64 -0
  69. data/ext/opencv/cvhaarclassifiercascade.cpp +148 -0
  70. data/ext/opencv/cvhaarclassifiercascade.h +39 -0
  71. data/ext/opencv/cvhistogram.cpp +717 -0
  72. data/ext/opencv/cvhistogram.h +73 -0
  73. data/ext/opencv/cvhumoments.cpp +178 -0
  74. data/ext/opencv/cvhumoments.h +51 -0
  75. data/ext/opencv/cvline.cpp +159 -0
  76. data/ext/opencv/cvline.h +54 -0
  77. data/ext/opencv/cvmat.cpp +6086 -0
  78. data/ext/opencv/cvmat.h +290 -0
  79. data/ext/opencv/cvmemstorage.cpp +73 -0
  80. data/ext/opencv/cvmemstorage.h +50 -0
  81. data/ext/opencv/cvmoments.cpp +293 -0
  82. data/ext/opencv/cvmoments.h +75 -0
  83. data/ext/opencv/cvpoint.cpp +234 -0
  84. data/ext/opencv/cvpoint.h +64 -0
  85. data/ext/opencv/cvpoint2d32f.cpp +216 -0
  86. data/ext/opencv/cvpoint2d32f.h +63 -0
  87. data/ext/opencv/cvpoint3d32f.cpp +252 -0
  88. data/ext/opencv/cvpoint3d32f.h +66 -0
  89. data/ext/opencv/cvrect.cpp +338 -0
  90. data/ext/opencv/cvrect.h +79 -0
  91. data/ext/opencv/cvscalar.cpp +241 -0
  92. data/ext/opencv/cvscalar.h +71 -0
  93. data/ext/opencv/cvseq.cpp +663 -0
  94. data/ext/opencv/cvseq.h +75 -0
  95. data/ext/opencv/cvsize.cpp +227 -0
  96. data/ext/opencv/cvsize.h +65 -0
  97. data/ext/opencv/cvsize2d32f.cpp +215 -0
  98. data/ext/opencv/cvsize2d32f.h +64 -0
  99. data/ext/opencv/cvslice.cpp +126 -0
  100. data/ext/opencv/cvslice.h +61 -0
  101. data/ext/opencv/cvsurfparams.cpp +208 -0
  102. data/ext/opencv/cvsurfparams.h +58 -0
  103. data/ext/opencv/cvsurfpoint.cpp +246 -0
  104. data/ext/opencv/cvsurfpoint.h +52 -0
  105. data/ext/opencv/cvtermcriteria.cpp +198 -0
  106. data/ext/opencv/cvtermcriteria.h +71 -0
  107. data/ext/opencv/cvtwopoints.cpp +122 -0
  108. data/ext/opencv/cvtwopoints.h +51 -0
  109. data/ext/opencv/cvutils.cpp +192 -0
  110. data/ext/opencv/cvutils.h +30 -0
  111. data/ext/opencv/cvvideowriter.cpp +142 -0
  112. data/ext/opencv/cvvideowriter.h +43 -0
  113. data/ext/opencv/eigenfaces.cpp +75 -0
  114. data/ext/opencv/eigenfaces.h +30 -0
  115. data/ext/opencv/extconf.rb +77 -0
  116. data/ext/opencv/facerecognizer.cpp +219 -0
  117. data/ext/opencv/facerecognizer.h +46 -0
  118. data/ext/opencv/fisherfaces.cpp +75 -0
  119. data/ext/opencv/fisherfaces.h +30 -0
  120. data/ext/opencv/gui.cpp +71 -0
  121. data/ext/opencv/gui.h +30 -0
  122. data/ext/opencv/iplconvkernel.cpp +198 -0
  123. data/ext/opencv/iplconvkernel.h +71 -0
  124. data/ext/opencv/iplimage.cpp +651 -0
  125. data/ext/opencv/iplimage.h +73 -0
  126. data/ext/opencv/lbph.cpp +78 -0
  127. data/ext/opencv/lbph.h +30 -0
  128. data/ext/opencv/mouseevent.cpp +186 -0
  129. data/ext/opencv/mouseevent.h +56 -0
  130. data/ext/opencv/opencv.cpp +819 -0
  131. data/ext/opencv/opencv.h +408 -0
  132. data/ext/opencv/pointset.cpp +280 -0
  133. data/ext/opencv/pointset.h +68 -0
  134. data/ext/opencv/trackbar.cpp +127 -0
  135. data/ext/opencv/trackbar.h +69 -0
  136. data/ext/opencv/window.cpp +377 -0
  137. data/ext/opencv/window.h +66 -0
  138. data/images/CvMat_sobel.png +0 -0
  139. data/images/CvMat_sub_rect.png +0 -0
  140. data/images/CvSeq_relationmap.png +0 -0
  141. data/lib/opencv.rb +12 -0
  142. data/lib/opencv/psyched_yaml.rb +22 -0
  143. data/lib/opencv/version.rb +3 -0
  144. data/ruby-opencv.gemspec +44 -0
  145. data/test/eigenfaces_save.xml +7524 -0
  146. data/test/fisherfaces_save.xml +7530 -0
  147. data/test/helper.rb +167 -0
  148. data/test/lbph_save.xml +4304 -0
  149. data/test/runner.rb +30 -0
  150. data/test/samples/airplane.jpg +0 -0
  151. data/test/samples/baboon.jpg +0 -0
  152. data/test/samples/baboon200.jpg +0 -0
  153. data/test/samples/baboon200_rotated.jpg +0 -0
  154. data/test/samples/blank0.jpg +0 -0
  155. data/test/samples/blank1.jpg +0 -0
  156. data/test/samples/blank2.jpg +0 -0
  157. data/test/samples/blank3.jpg +0 -0
  158. data/test/samples/blank4.jpg +0 -0
  159. data/test/samples/blank5.jpg +0 -0
  160. data/test/samples/blank6.jpg +0 -0
  161. data/test/samples/blank7.jpg +0 -0
  162. data/test/samples/blank8.jpg +0 -0
  163. data/test/samples/blank9.jpg +0 -0
  164. data/test/samples/cat.jpg +0 -0
  165. data/test/samples/chessboard.jpg +0 -0
  166. data/test/samples/contours.jpg +0 -0
  167. data/test/samples/fruits.jpg +0 -0
  168. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  169. data/test/samples/inpaint-mask.bmp +0 -0
  170. data/test/samples/lena-256x256.jpg +0 -0
  171. data/test/samples/lena-32x32.jpg +0 -0
  172. data/test/samples/lena-eyes.jpg +0 -0
  173. data/test/samples/lena-inpaint.jpg +0 -0
  174. data/test/samples/lena.jpg +0 -0
  175. data/test/samples/lines.jpg +0 -0
  176. data/test/samples/messy0.jpg +0 -0
  177. data/test/samples/messy1.jpg +0 -0
  178. data/test/samples/movie_sample.avi +0 -0
  179. data/test/samples/one_way_train_0000.jpg +0 -0
  180. data/test/samples/one_way_train_0001.jpg +0 -0
  181. data/test/samples/partially_blank0.jpg +0 -0
  182. data/test/samples/partially_blank1.jpg +0 -0
  183. data/test/samples/smooth0.jpg +0 -0
  184. data/test/samples/smooth1.jpg +0 -0
  185. data/test/samples/smooth2.jpg +0 -0
  186. data/test/samples/smooth3.jpg +0 -0
  187. data/test/samples/smooth4.jpg +0 -0
  188. data/test/samples/smooth5.jpg +0 -0
  189. data/test/samples/smooth6.jpg +0 -0
  190. data/test/samples/str-cv-rotated.jpg +0 -0
  191. data/test/samples/str-cv.jpg +0 -0
  192. data/test/samples/str-ov.jpg +0 -0
  193. data/test/samples/stuff.jpg +0 -0
  194. data/test/test_curve.rb +43 -0
  195. data/test/test_cvavgcomp.rb +24 -0
  196. data/test/test_cvbox2d.rb +76 -0
  197. data/test/test_cvcapture.rb +191 -0
  198. data/test/test_cvchain.rb +108 -0
  199. data/test/test_cvcircle32f.rb +41 -0
  200. data/test/test_cvconnectedcomp.rb +61 -0
  201. data/test/test_cvcontour.rb +171 -0
  202. data/test/test_cvcontourtree.rb +43 -0
  203. data/test/test_cverror.rb +50 -0
  204. data/test/test_cvfeaturetree.rb +65 -0
  205. data/test/test_cvfont.rb +58 -0
  206. data/test/test_cvhaarclassifiercascade.rb +63 -0
  207. data/test/test_cvhistogram.rb +271 -0
  208. data/test/test_cvhumoments.rb +83 -0
  209. data/test/test_cvline.rb +50 -0
  210. data/test/test_cvmat.rb +3036 -0
  211. data/test/test_cvmat_drawing.rb +349 -0
  212. data/test/test_cvmat_dxt.rb +150 -0
  213. data/test/test_cvmat_imageprocessing.rb +2085 -0
  214. data/test/test_cvmoments.rb +180 -0
  215. data/test/test_cvpoint.rb +75 -0
  216. data/test/test_cvpoint2d32f.rb +75 -0
  217. data/test/test_cvpoint3d32f.rb +93 -0
  218. data/test/test_cvrect.rb +144 -0
  219. data/test/test_cvscalar.rb +113 -0
  220. data/test/test_cvseq.rb +311 -0
  221. data/test/test_cvsize.rb +75 -0
  222. data/test/test_cvsize2d32f.rb +75 -0
  223. data/test/test_cvslice.rb +31 -0
  224. data/test/test_cvsurfparams.rb +57 -0
  225. data/test/test_cvsurfpoint.rb +66 -0
  226. data/test/test_cvtermcriteria.rb +56 -0
  227. data/test/test_cvtwopoints.rb +40 -0
  228. data/test/test_cvvideowriter.rb +58 -0
  229. data/test/test_eigenfaces.rb +93 -0
  230. data/test/test_fisherfaces.rb +93 -0
  231. data/test/test_iplconvkernel.rb +54 -0
  232. data/test/test_iplimage.rb +232 -0
  233. data/test/test_lbph.rb +166 -0
  234. data/test/test_mouseevent.rb +17 -0
  235. data/test/test_opencv.rb +360 -0
  236. data/test/test_pointset.rb +128 -0
  237. data/test/test_preliminary.rb +130 -0
  238. data/test/test_trackbar.rb +47 -0
  239. data/test/test_window.rb +115 -0
  240. data/yard_extension.rb +5 -0
  241. metadata +352 -0
@@ -0,0 +1,52 @@
1
+ /************************************************************
2
+
3
+ cvcircle32f.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2007 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_CVCIRCLE32F_H
11
+ #define RUBY_OPENCV_CVCIRCLE32F_H
12
+
13
+ #include "opencv.h"
14
+
15
+ #define __NAMESPACE_BEGIN_CVCIRCLE32F namespace cCvCircle32f {
16
+ #define __NAMESPACE_END_CVCIRCLE32F }
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+
20
+ typedef struct CvCircle32f {
21
+ CvPoint2D32f center;
22
+ float radius;
23
+ } CvCircle32f;
24
+
25
+ __NAMESPACE_BEGIN_CVCIRCLE32F
26
+
27
+ VALUE rb_class();
28
+
29
+ void init_ruby_class();
30
+
31
+ VALUE rb_allocate(VALUE klass);
32
+ VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
33
+ VALUE rb_center(VALUE self);
34
+ VALUE rb_radius(VALUE self);
35
+ VALUE rb_aref(VALUE self, VALUE index);
36
+ VALUE rb_to_ary(VALUE self);
37
+
38
+ VALUE new_object(CvCircle32f circle32f);
39
+
40
+ __NAMESPACE_END_CVCIRCLE32F
41
+
42
+ inline CvCircle32f*
43
+ CVCIRCLE32F(VALUE object)
44
+ {
45
+ CvCircle32f *ptr;
46
+ Data_Get_Struct(object, CvCircle32f, ptr);
47
+ return ptr;
48
+ }
49
+
50
+ __NAMESPACE_END_OPENCV
51
+
52
+ #endif // RUBY_OPENCV_CVCIRCLE32F_H
@@ -0,0 +1,156 @@
1
+ /************************************************************
2
+
3
+ cvconnectedcomp.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2007 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvconnectedcomp.h"
11
+ /*
12
+ * Document-class: OpenCV::CvConnectedComp
13
+ *
14
+ * Connected component
15
+ * see CvMat#flood_fill
16
+ */
17
+ __NAMESPACE_BEGIN_OPENCV
18
+ __NAMESPACE_BEGIN_CVCONNECTEDCOMP
19
+
20
+ VALUE rb_klass;
21
+
22
+ VALUE
23
+ rb_class()
24
+ {
25
+ return rb_klass;
26
+ }
27
+
28
+ VALUE
29
+ rb_allocate(VALUE klass)
30
+ {
31
+ CvConnectedComp *ptr;
32
+ return Data_Make_Struct(klass, CvConnectedComp, 0, -1, ptr);
33
+ }
34
+
35
+ /*
36
+ * Constructor
37
+ * @overload new(area = nil, value = nil, rect = nil, contour = nil)
38
+ * @param area [Number] Area of the segmented component
39
+ * @param value [CvScalar] Average color of the connected component
40
+ * @param rect [CvRect] ROI of the segmented component
41
+ * @param contour [CvSeq] Optional component boundary
42
+ * @return [CvConnectedComp] self
43
+ */
44
+ VALUE
45
+ rb_initialize(int argc, VALUE *argv, VALUE self)
46
+ {
47
+ VALUE area, value, rect, contour;
48
+ rb_scan_args(argc, argv, "04", &area, &value, &rect, &contour);
49
+
50
+ if (!NIL_P(area))
51
+ CVCONNECTEDCOMP(self)->area = NUM2DBL(area);
52
+ if (!NIL_P(value))
53
+ CVCONNECTEDCOMP(self)->value = *CVSCALAR(value);
54
+ if (!NIL_P(rect))
55
+ CVCONNECTEDCOMP(self)->rect = *CVRECT(rect);
56
+ if (!NIL_P(contour))
57
+ CVCONNECTEDCOMP(self)->contour = CVSEQ(contour);
58
+ return self;
59
+ }
60
+
61
+ /*
62
+ * Returns area of connected component
63
+ * @overload area
64
+ * @return [Number] Area of the connected component
65
+ */
66
+ VALUE
67
+ rb_area(VALUE self)
68
+ {
69
+ return rb_float_new(CVCONNECTEDCOMP(self)->area);
70
+ }
71
+
72
+ /*
73
+ * Return average color of the connected component.
74
+ * @overload value
75
+ * @return [CvScalar] Average color of the connected component
76
+ */
77
+ VALUE
78
+ rb_value(VALUE self)
79
+ {
80
+ return REFER_OBJECT(cCvScalar::rb_class(), &CVCONNECTEDCOMP(self)->value, self);
81
+ }
82
+
83
+ /*
84
+ * Return ROI of the segmented component
85
+ * @overload rect
86
+ * @return [CvRect] ROI of the segmented component
87
+ */
88
+ VALUE
89
+ rb_rect(VALUE self)
90
+ {
91
+ return REFER_OBJECT(cCvRect::rb_class(), &CVCONNECTEDCOMP(self)->rect, self);
92
+ }
93
+
94
+ /*
95
+ * Set ROI of the segmented component
96
+ * @overload rect=value
97
+ * @param value [CvRect] ROI to set
98
+ * @return [CvRect] ROI of the segmented component
99
+ */
100
+ VALUE
101
+ rb_set_rect(VALUE self, VALUE rect)
102
+ {
103
+ CVCONNECTEDCOMP(self)->rect = VALUE_TO_CVRECT(rect);
104
+ return self;
105
+ }
106
+
107
+ /*
108
+ * Returns optional component boundary
109
+ * @overload contour
110
+ * @return [CvContour] Optional component boundary
111
+ */
112
+ VALUE
113
+ rb_contour(VALUE self)
114
+ {
115
+ return REFER_OBJECT(cCvContour::rb_class(), &CVCONNECTEDCOMP(self)->contour, self);
116
+ }
117
+
118
+ VALUE
119
+ new_object()
120
+ {
121
+ return rb_allocate(rb_klass);
122
+ }
123
+
124
+ VALUE
125
+ new_object(CvConnectedComp comp)
126
+ {
127
+ VALUE object = rb_allocate(rb_klass);
128
+ *CVCONNECTEDCOMP(object) = comp;
129
+ return object;
130
+ }
131
+
132
+ void
133
+ init_ruby_class()
134
+ {
135
+ #if 0
136
+ // For documentation using YARD
137
+ VALUE opencv = rb_define_module("OpenCV");
138
+ #endif
139
+
140
+ if (rb_klass)
141
+ return;
142
+
143
+ VALUE opencv = rb_module_opencv();
144
+
145
+ rb_klass = rb_define_class_under(opencv, "CvConnectedComp", rb_cObject);
146
+ rb_define_alloc_func(rb_klass, rb_allocate);
147
+ rb_define_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
148
+ rb_define_method(rb_klass, "area", RUBY_METHOD_FUNC(rb_area), 0);
149
+ rb_define_method(rb_klass, "value", RUBY_METHOD_FUNC(rb_value), 0);
150
+ rb_define_method(rb_klass, "rect", RUBY_METHOD_FUNC(rb_rect), 0);
151
+ rb_define_method(rb_klass, "rect=", RUBY_METHOD_FUNC(rb_set_rect), 1);
152
+ rb_define_method(rb_klass, "contour", RUBY_METHOD_FUNC(rb_contour), 0);
153
+ }
154
+
155
+ __NAMESPACE_END_CVCONNECTEDCOMP
156
+ __NAMESPACE_END_OPENCV
@@ -0,0 +1,49 @@
1
+ /************************************************************
2
+
3
+ cvconnectedcomp.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2007 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_CVCONNECTEDCOMP_H
11
+ #define RUBY_OPENCV_CVCONNECTEDCOMP_H
12
+
13
+ #include "opencv.h"
14
+
15
+ #define __NAMESPACE_BEGIN_CVCONNECTEDCOMP namespace cCvConnectedComp {
16
+ #define __NAMESPACE_END_CVCONNECTEDCOMP }
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_CVCONNECTEDCOMP
20
+
21
+ VALUE rb_class();
22
+
23
+ void init_ruby_class();
24
+
25
+ VALUE rb_allocate(VALUE klass);
26
+
27
+ VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
28
+ VALUE rb_area(VALUE self);
29
+ VALUE rb_value(VALUE self);
30
+ VALUE rb_rect(VALUE self);
31
+ VALUE rb_set_rect(VALUE self, VALUE rect);
32
+ VALUE rb_contour(VALUE self);
33
+
34
+ VALUE new_object();
35
+ VALUE new_object(CvConnectedComp comp);
36
+
37
+ __NAMESPACE_END_CVCONNECTEDCOMP
38
+
39
+ inline CvConnectedComp*
40
+ CVCONNECTEDCOMP(VALUE object)
41
+ {
42
+ CvConnectedComp *ptr;
43
+ Data_Get_Struct(object, CvConnectedComp, ptr);
44
+ return ptr;
45
+ }
46
+
47
+ __NAMESPACE_END_OPENCV
48
+
49
+ #endif // RUBY_OPENCV_CVCONNECTEDCOMP_H
@@ -0,0 +1,384 @@
1
+ /************************************************************
2
+
3
+ cvcontour.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2007 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "cvcontour.h"
11
+ /*
12
+ * Document-class: OpenCV::CvContour
13
+ *
14
+ * Contour
15
+ *
16
+ * @see CvMat#find_contours
17
+ */
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_CVCONTOUR
20
+
21
+ #define APPROX_POLY_OPTION(op) rb_get_option_table(rb_klass, "APPROX_OPTION", op)
22
+ #define APPROX_POLY_METHOD(op) CVMETHOD("APPROX_POLY_METHOD", LOOKUP_HASH(op, "method"), CV_POLY_APPROX_DP)
23
+ #define APPROX_POLY_ACCURACY(op) NUM2DBL(LOOKUP_HASH(op, "accuracy"))
24
+ #define APPROX_POLY_RECURSIVE(op) TRUE_OR_FALSE(LOOKUP_HASH(op, "recursive"))
25
+
26
+ VALUE rb_allocate(VALUE klass);
27
+ void cvcontour_free(void *ptr);
28
+
29
+ VALUE rb_klass;
30
+
31
+ VALUE
32
+ rb_class()
33
+ {
34
+ return rb_klass;
35
+ }
36
+
37
+ VALUE
38
+ rb_allocate(VALUE klass)
39
+ {
40
+ return Data_Wrap_Struct(klass, mark_root_object, unregister_object, NULL);
41
+ }
42
+
43
+ /*
44
+ * Constructor
45
+ *
46
+ * @overload new(seq_flags = CV_SEQ_ELTYPE_POINT | CV_SEQ_KIND_GENERIC, storage = nil)
47
+ * @param [Integer] seq_flags Flags of the created sequence, which are combinations of
48
+ * the element types and sequence types.
49
+ * - Element type:
50
+ * - <tt>CV_SEQ_ELTYPE_POINT</tt>: {CvPoint}
51
+ * - <tt>CV_32FC2</tt>: {CvPoint2D32f}
52
+ * - <tt>CV_SEQ_ELTYPE_POINT3D</tt>: {CvPoint3D32f}
53
+ * - <tt>CV_SEQ_ELTYPE_INDEX</tt>: Integer
54
+ * - <tt>CV_SEQ_ELTYPE_CODE</tt>: Integer (Freeman code)
55
+ * - Sequence type:
56
+ * - <tt>CV_SEQ_KIND_GENERIC</tt>: Generic sequence
57
+ * - <tt>CV_SEQ_KIND_CURVE</tt>: Curve
58
+ * @param [CvMemStorage] storage Sequence location
59
+ * @return [CvContour] self
60
+ * @opencv_func cvCreateSeq
61
+ * @example
62
+ * seq = CvContour.new(CV_SEQ_ELTYPE_POINT | CV_SEQ_KIND_CURVE)
63
+ * seq << CvPoint.new(1, 2)
64
+ * seq << 3 #=> TypeError
65
+ */
66
+ VALUE
67
+ rb_initialize(int argc, VALUE *argv, VALUE self)
68
+ {
69
+ VALUE seq_flags_value, storage_value;
70
+ rb_scan_args(argc, argv, "02", &seq_flags_value, &storage_value);
71
+
72
+ int seq_flags = 0;
73
+ if (NIL_P(seq_flags_value)) {
74
+ seq_flags = CV_SEQ_ELTYPE_POINT | CV_SEQ_KIND_GENERIC;
75
+ }
76
+ else {
77
+ Check_Type(seq_flags_value, T_FIXNUM);
78
+ seq_flags = FIX2INT(seq_flags_value);
79
+ }
80
+ storage_value = CHECK_CVMEMSTORAGE(storage_value);
81
+
82
+ try {
83
+ DATA_PTR(self) = (CvContour*)cCvSeq::create_seq(seq_flags, sizeof(CvContour), storage_value);
84
+ }
85
+ catch (cv::Exception& e) {
86
+ raise_cverror(e);
87
+ }
88
+
89
+ return self;
90
+ }
91
+
92
+ /*
93
+ * Returns bounding box of the contour
94
+ * @overload rect
95
+ * @return [CvRect] Bounding box of the contour
96
+ */
97
+ VALUE
98
+ rb_rect(VALUE self)
99
+ {
100
+ return cCvRect::new_object(CVCONTOUR(self)->rect);
101
+ }
102
+
103
+ /*
104
+ * Returns color of the contour
105
+ * @overload color
106
+ * @return [Number] Color of the contour
107
+ */
108
+ VALUE
109
+ rb_color(VALUE self)
110
+ {
111
+ return INT2NUM(CVCONTOUR(self)->color);
112
+ }
113
+
114
+ /*
115
+ * Set color of the contour
116
+ * @overload color=value
117
+ * @param value [Number] Color of the contour
118
+ */
119
+ VALUE
120
+ rb_set_color(VALUE self, VALUE color)
121
+ {
122
+ CVCONTOUR(self)->color = NUM2INT(color);
123
+ return self;
124
+ }
125
+
126
+ /*
127
+ * Returns reserved region values of the contour
128
+ * @overload reserved
129
+ * @return [Array<Number>] Reserved region values of the contour
130
+ */
131
+ VALUE
132
+ rb_reserved(VALUE self)
133
+ {
134
+ return rb_ary_new3(3,
135
+ INT2NUM(CVCONTOUR(self)->reserved[0]),
136
+ INT2NUM(CVCONTOUR(self)->reserved[1]),
137
+ INT2NUM(CVCONTOUR(self)->reserved[2]));
138
+ }
139
+
140
+ /*
141
+ * Approximates polygonal curves with desired precision
142
+ * @overload approx_poly(options)
143
+ * @param options [Hash] Parameters
144
+ * @option options [Symbol] :method Approximation method (default :dp)
145
+ * * :dp - Douglas-Peucker algorithm.
146
+ * @option options [Number] :accuracy Parameter specifying the approximation accuracy.
147
+ * This is the maximum distance between the original curve and its approximation.
148
+ * @option options [Boolean] :recursive Recursion flag. If true, the function approximates
149
+ * all the contours accessible from curve by h_next and v_next links.
150
+ * @return [CvContour] Result of the approximation
151
+ * @return [nil] Approximation faied
152
+ * @opencv_func cvApproxPoly
153
+ */
154
+ VALUE
155
+ rb_approx_poly(int argc, VALUE *argv, VALUE self)
156
+ {
157
+ VALUE approx_poly_option;
158
+ rb_scan_args(argc, argv, "01", &approx_poly_option);
159
+ approx_poly_option = APPROX_POLY_OPTION(approx_poly_option);
160
+ VALUE storage = cCvMemStorage::new_object();
161
+ CvSeq *contour = cvApproxPoly(CVCONTOUR(self), sizeof(CvContour), CVMEMSTORAGE(storage),
162
+ APPROX_POLY_METHOD(approx_poly_option),
163
+ APPROX_POLY_ACCURACY(approx_poly_option),
164
+ APPROX_POLY_RECURSIVE(approx_poly_option));
165
+
166
+ if (contour && contour->total > 0) {
167
+ return cCvSeq::new_sequence(cCvContour::rb_class(), contour, cCvPoint::rb_class(), storage);
168
+ }
169
+ return Qnil;
170
+ }
171
+
172
+ /*
173
+ * Calculates up-right bounding rectangle of point set.
174
+ * @overload bounding_rect
175
+ * @return [CvRect] Bounding rectangle
176
+ * @opencv_func cvBoundingRect
177
+ */
178
+ VALUE
179
+ rb_bounding_rect(VALUE self)
180
+ {
181
+ CvRect rect;
182
+ try {
183
+ rect = cvBoundingRect(CVCONTOUR(self), 1);
184
+ }
185
+ catch (cv::Exception& e) {
186
+ raise_cverror(e);
187
+ }
188
+ return cCvRect::new_object(rect);
189
+ }
190
+
191
+ /*
192
+ * Creates hierarchical representation of contour
193
+ * @overload create_tree(threshold = 0.0)
194
+ * @param threshold [Number] If <= 0, the method creates full binary tree representation.
195
+ * If > 0, the method creates representation with the precision threshold.
196
+ * @return [CvContourTree] Hierarchical representation of the contour
197
+ * @opencv_func cvCreateContourTree
198
+ */
199
+ VALUE
200
+ rb_create_tree(int argc, VALUE *argv, VALUE self)
201
+ {
202
+ VALUE threshold, storage;
203
+ rb_scan_args(argc, argv, "01", &threshold);
204
+ storage = cCvMemStorage::new_object();
205
+ CvContourTree *tree = NULL;
206
+ try {
207
+ tree = cvCreateContourTree(CVSEQ(self), CVMEMSTORAGE(storage), IF_DBL(threshold, 0.0));
208
+ }
209
+ catch (cv::Exception& e) {
210
+ raise_cverror(e);
211
+ }
212
+ return cCvSeq::new_sequence(cCvContourTree::rb_class(), (CvSeq*)tree, cCvPoint::rb_class(), storage);
213
+ }
214
+
215
+ /*
216
+ * Performs a point-in-contour test.
217
+ * The method determines whether the point is inside a contour, outside,
218
+ * or lies on an edge (or coincides with a vertex).
219
+ * @overload in?(point)
220
+ * @param point [CvPoint2D32f] Point tested against the contour
221
+ * @return [Boolean] If the point is inside, returns true. If outside, returns false.
222
+ * If lies on an edge, returns nil.
223
+ * @opencv_func cvPointPolygonTest
224
+ */
225
+ VALUE
226
+ rb_in_q(VALUE self, VALUE point)
227
+ {
228
+ double n = 0;
229
+ try {
230
+ n = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 0);
231
+ }
232
+ catch (cv::Exception& e) {
233
+ raise_cverror(e);
234
+ }
235
+ return n == 0 ? Qnil : n > 0 ? Qtrue : Qfalse;
236
+ }
237
+
238
+ /*
239
+ * Calculates distance between a point and the nearest contour edgex
240
+ * @overload measure_distance(point)
241
+ * @param point [CvPoint2D32f] Point tested against the contour
242
+ * @return Signed distance between the point and the nearest contour edge
243
+ * @opencv_func cvPointPolygonTest
244
+ */
245
+ VALUE
246
+ rb_measure_distance(VALUE self, VALUE point)
247
+ {
248
+ double distance = 0;
249
+ try {
250
+ distance = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 1);
251
+ }
252
+ catch (cv::Exception& e) {
253
+ raise_cverror(e);
254
+ }
255
+ return rb_float_new(distance);
256
+ }
257
+
258
+ /*
259
+ * Determines whether the point is inside a contour, outside, or lies on an edge (or coinsides with a vertex).
260
+ * @overload point_polygon_test(point, measure_dist)
261
+ * @param point [CvPoint2D32f] Point tested against the contour
262
+ * @param measure_dist [Boolean] If true, the method estimates the signed distance from the point to
263
+ * the nearest contour edge. Otherwise, the function only checks if the point is inside a contour or not.
264
+ * @return [Number] When measure_dist = false, the return value is +1, -1 and 0, respectively.
265
+ * When measure_dist = true, it is a signed distance between the point and the nearest contour edge.
266
+ * @opencv_func cvPointPolygonTest
267
+ */
268
+ VALUE
269
+ rb_point_polygon_test(VALUE self, VALUE point, VALUE measure_dist)
270
+ {
271
+ int measure_dist_flag;
272
+
273
+ if (measure_dist == Qtrue)
274
+ measure_dist_flag = 1;
275
+ else if (measure_dist == Qfalse)
276
+ measure_dist_flag = 0;
277
+ else
278
+ measure_dist_flag = NUM2INT(measure_dist);
279
+
280
+ double dist = Qnil;
281
+ try {
282
+ dist = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), measure_dist_flag);
283
+ }
284
+ catch (cv::Exception& e) {
285
+ raise_cverror(e);
286
+ }
287
+
288
+ /* cvPointPolygonTest returns 100, -100 or 0 when measure_dist = 0 */
289
+ if ((!measure_dist_flag) && ((int)dist) != 0)
290
+ dist = (dist > 0) ? 1 : -1;
291
+
292
+ return rb_float_new(dist);
293
+ }
294
+
295
+ /*
296
+ * call-seq:
297
+ * match_shapes(object, method) -> float
298
+ *
299
+ * Compares two shapes(self and object). <i>object</i> should be CvContour.
300
+ *
301
+ * A - object1, B - object2:
302
+ * * method=CV_CONTOURS_MATCH_I1
303
+ * I1(A,B)=sumi=1..7abs(1/mAi - 1/mBi)
304
+ * * method=CV_CONTOURS_MATCH_I2
305
+ * I2(A,B)=sumi=1..7abs(mAi - mBi)
306
+ * * method=CV_CONTOURS_MATCH_I3
307
+ * I3(A,B)=sumi=1..7abs(mAi - mBi)/abs(mAi)
308
+ */
309
+ VALUE
310
+ rb_match_shapes(int argc, VALUE *argv, VALUE self)
311
+ {
312
+ VALUE object, method, param;
313
+ rb_scan_args(argc, argv, "21", &object, &method, &param);
314
+ int method_flag = CVMETHOD("COMPARISON_METHOD", method);
315
+ if (!rb_obj_is_kind_of(object, cCvContour::rb_class()))
316
+ rb_raise(rb_eTypeError, "argument 1 (shape) should be %s",
317
+ rb_class2name(cCvContour::rb_class()));
318
+ double result = 0;
319
+ try {
320
+ result = cvMatchShapes(CVARR(self), CVARR(object), method_flag);
321
+ }
322
+ catch (cv::Exception& e) {
323
+ raise_cverror(e);
324
+ }
325
+ return rb_float_new(result);
326
+ }
327
+
328
+ VALUE new_object()
329
+ {
330
+ VALUE object = rb_allocate(rb_klass);
331
+ rb_initialize(0, NULL, object);
332
+ return object;
333
+ }
334
+
335
+
336
+ void
337
+ init_ruby_class()
338
+ {
339
+ #if 0
340
+ // For documentation using YARD
341
+ VALUE opencv = rb_define_module("OpenCV");
342
+ VALUE cvseq = rb_define_class_under(opencv, "CvSeq");
343
+ VALUE curve = rb_define_module_under(opencv, "Curve");
344
+ VALUE pointset = rb_define_module_under(opencv, "PointSet");
345
+ #endif
346
+
347
+ if (rb_klass)
348
+ return;
349
+
350
+ VALUE opencv = rb_module_opencv();
351
+ VALUE cvseq = cCvSeq::rb_class();
352
+ VALUE curve = mCurve::rb_module();
353
+ VALUE pointset = mPointSet::rb_module();
354
+
355
+ rb_klass = rb_define_class_under(opencv, "CvContour", cvseq);
356
+ rb_include_module(rb_klass, curve);
357
+ rb_include_module(rb_klass, pointset);
358
+
359
+ rb_define_alloc_func(rb_klass, rb_allocate);
360
+
361
+ VALUE approx_option = rb_hash_new();
362
+ rb_define_const(rb_klass, "APPROX_OPTION", approx_option);
363
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("method")), INT2FIX(CV_POLY_APPROX_DP));
364
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("accuracy")), rb_float_new(1.0));
365
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("recursive")), Qfalse);
366
+
367
+ rb_define_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
368
+ rb_define_method(rb_klass, "rect", RUBY_METHOD_FUNC(rb_rect), 0);
369
+ rb_define_method(rb_klass, "color", RUBY_METHOD_FUNC(rb_color), 0);
370
+ rb_define_method(rb_klass, "color=", RUBY_METHOD_FUNC(rb_set_color), 1);
371
+ rb_define_method(rb_klass, "reserved", RUBY_METHOD_FUNC(rb_reserved), 0);
372
+ rb_define_method(rb_klass, "approx_poly", RUBY_METHOD_FUNC(rb_approx_poly), -1);
373
+ rb_define_alias(rb_klass, "approx", "approx_poly");
374
+ rb_define_method(rb_klass, "bounding_rect", RUBY_METHOD_FUNC(rb_bounding_rect), 0);
375
+ rb_define_method(rb_klass, "create_tree", RUBY_METHOD_FUNC(rb_create_tree), -1);
376
+ rb_define_method(rb_klass, "in?", RUBY_METHOD_FUNC(rb_in_q), 1);
377
+ rb_define_method(rb_klass, "measure_distance", RUBY_METHOD_FUNC(rb_measure_distance), 1);
378
+ rb_define_method(rb_klass, "point_polygon_test", RUBY_METHOD_FUNC(rb_point_polygon_test), 2);
379
+ rb_define_method(rb_klass, "match_shapes", RUBY_METHOD_FUNC(rb_match_shapes), -1);
380
+ }
381
+
382
+ __NAMESPACE_END_CVCONTOUR
383
+ __NAMESPACE_END_OPENCV
384
+