bonanza-ruby-opencv 0.0.13.20140330211753

Sign up to get free protection for your applications and to get access to all the features.
Files changed (240) 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 +98 -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 +607 -0
  50. data/ext/opencv/cvcapture.h +72 -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 +332 -0
  58. data/ext/opencv/cvcontour.h +48 -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 +715 -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 +6829 -0
  78. data/ext/opencv/cvmat.h +323 -0
  79. data/ext/opencv/cvmemstorage.cpp +73 -0
  80. data/ext/opencv/cvmemstorage.h +53 -0
  81. data/ext/opencv/cvmoments.cpp +293 -0
  82. data/ext/opencv/cvmoments.h +75 -0
  83. data/ext/opencv/cvpoint.cpp +265 -0
  84. data/ext/opencv/cvpoint.h +67 -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 +441 -0
  90. data/ext/opencv/cvrect.h +88 -0
  91. data/ext/opencv/cvscalar.cpp +301 -0
  92. data/ext/opencv/cvscalar.h +76 -0
  93. data/ext/opencv/cvseq.cpp +605 -0
  94. data/ext/opencv/cvseq.h +74 -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 +279 -0
  104. data/ext/opencv/cvsurfpoint.h +54 -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 +221 -0
  110. data/ext/opencv/cvutils.h +31 -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 +82 -0
  116. data/ext/opencv/facerecognizer.cpp +181 -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 +666 -0
  125. data/ext/opencv/iplimage.h +75 -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 +833 -0
  131. data/ext/opencv/opencv.h +405 -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 +4 -0
  144. data/test/eigenfaces_save.xml +7524 -0
  145. data/test/fisherfaces_save.xml +7530 -0
  146. data/test/helper.rb +166 -0
  147. data/test/lbph_save.xml +4304 -0
  148. data/test/runner.rb +30 -0
  149. data/test/samples/airplane.jpg +0 -0
  150. data/test/samples/baboon.jpg +0 -0
  151. data/test/samples/baboon200.jpg +0 -0
  152. data/test/samples/baboon200_rotated.jpg +0 -0
  153. data/test/samples/blank0.jpg +0 -0
  154. data/test/samples/blank1.jpg +0 -0
  155. data/test/samples/blank2.jpg +0 -0
  156. data/test/samples/blank3.jpg +0 -0
  157. data/test/samples/blank4.jpg +0 -0
  158. data/test/samples/blank5.jpg +0 -0
  159. data/test/samples/blank6.jpg +0 -0
  160. data/test/samples/blank7.jpg +0 -0
  161. data/test/samples/blank8.jpg +0 -0
  162. data/test/samples/blank9.jpg +0 -0
  163. data/test/samples/cat.jpg +0 -0
  164. data/test/samples/chessboard.jpg +0 -0
  165. data/test/samples/contours.jpg +0 -0
  166. data/test/samples/fruits.jpg +0 -0
  167. data/test/samples/haarcascade_frontalface_alt.xml.gz +0 -0
  168. data/test/samples/inpaint-mask.bmp +0 -0
  169. data/test/samples/lena-256x256.jpg +0 -0
  170. data/test/samples/lena-32x32.jpg +0 -0
  171. data/test/samples/lena-eyes.jpg +0 -0
  172. data/test/samples/lena-inpaint.jpg +0 -0
  173. data/test/samples/lena.jpg +0 -0
  174. data/test/samples/lines.jpg +0 -0
  175. data/test/samples/messy0.jpg +0 -0
  176. data/test/samples/messy1.jpg +0 -0
  177. data/test/samples/movie_sample.avi +0 -0
  178. data/test/samples/one_way_train_0000.jpg +0 -0
  179. data/test/samples/one_way_train_0001.jpg +0 -0
  180. data/test/samples/partially_blank0.jpg +0 -0
  181. data/test/samples/partially_blank1.jpg +0 -0
  182. data/test/samples/smooth0.jpg +0 -0
  183. data/test/samples/smooth1.jpg +0 -0
  184. data/test/samples/smooth2.jpg +0 -0
  185. data/test/samples/smooth3.jpg +0 -0
  186. data/test/samples/smooth4.jpg +0 -0
  187. data/test/samples/smooth5.jpg +0 -0
  188. data/test/samples/smooth6.jpg +0 -0
  189. data/test/samples/str-cv-rotated.jpg +0 -0
  190. data/test/samples/str-cv.jpg +0 -0
  191. data/test/samples/str-ov.jpg +0 -0
  192. data/test/samples/stuff.jpg +0 -0
  193. data/test/test_curve.rb +43 -0
  194. data/test/test_cvavgcomp.rb +24 -0
  195. data/test/test_cvbox2d.rb +76 -0
  196. data/test/test_cvcapture.rb +183 -0
  197. data/test/test_cvchain.rb +108 -0
  198. data/test/test_cvcircle32f.rb +41 -0
  199. data/test/test_cvconnectedcomp.rb +61 -0
  200. data/test/test_cvcontour.rb +150 -0
  201. data/test/test_cvcontourtree.rb +43 -0
  202. data/test/test_cverror.rb +50 -0
  203. data/test/test_cvfeaturetree.rb +65 -0
  204. data/test/test_cvfont.rb +58 -0
  205. data/test/test_cvhaarclassifiercascade.rb +63 -0
  206. data/test/test_cvhistogram.rb +271 -0
  207. data/test/test_cvhumoments.rb +83 -0
  208. data/test/test_cvline.rb +50 -0
  209. data/test/test_cvmat.rb +3003 -0
  210. data/test/test_cvmat_drawing.rb +349 -0
  211. data/test/test_cvmat_dxt.rb +150 -0
  212. data/test/test_cvmat_imageprocessing.rb +2085 -0
  213. data/test/test_cvmoments.rb +180 -0
  214. data/test/test_cvpoint.rb +75 -0
  215. data/test/test_cvpoint2d32f.rb +75 -0
  216. data/test/test_cvpoint3d32f.rb +93 -0
  217. data/test/test_cvrect.rb +144 -0
  218. data/test/test_cvscalar.rb +113 -0
  219. data/test/test_cvseq.rb +295 -0
  220. data/test/test_cvsize.rb +75 -0
  221. data/test/test_cvsize2d32f.rb +75 -0
  222. data/test/test_cvslice.rb +31 -0
  223. data/test/test_cvsurfparams.rb +57 -0
  224. data/test/test_cvsurfpoint.rb +66 -0
  225. data/test/test_cvtermcriteria.rb +56 -0
  226. data/test/test_cvtwopoints.rb +40 -0
  227. data/test/test_cvvideowriter.rb +58 -0
  228. data/test/test_eigenfaces.rb +93 -0
  229. data/test/test_fisherfaces.rb +93 -0
  230. data/test/test_iplconvkernel.rb +54 -0
  231. data/test/test_iplimage.rb +232 -0
  232. data/test/test_lbph.rb +152 -0
  233. data/test/test_mouseevent.rb +17 -0
  234. data/test/test_opencv.rb +360 -0
  235. data/test/test_pointset.rb +128 -0
  236. data/test/test_preliminary.rb +130 -0
  237. data/test/test_trackbar.rb +47 -0
  238. data/test/test_window.rb +115 -0
  239. data/yard_extension.rb +5 -0
  240. metadata +399 -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,332 @@
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
+ * @overload new(storage = nil)
46
+ * @param [CvMemStorage] storage Sequence location
47
+ * @return [CvContour] self
48
+ * @opencv_func cvCreateSeq
49
+ */
50
+ VALUE
51
+ rb_initialize(int argc, VALUE *argv, VALUE self)
52
+ {
53
+ VALUE storage;
54
+ rb_scan_args(argc, argv, "01", &storage);
55
+
56
+ if (NIL_P(storage))
57
+ storage = cCvMemStorage::new_object(0);
58
+ else
59
+ storage = CHECK_CVMEMSTORAGE(storage);
60
+
61
+ try {
62
+ DATA_PTR(self) = (CvContour*)cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvContour),
63
+ sizeof(CvPoint), CVMEMSTORAGE(storage));
64
+ }
65
+ catch (cv::Exception& e) {
66
+ raise_cverror(e);
67
+ }
68
+ cCvSeq::register_elem_class(CVSEQ(self), cCvPoint::rb_class());
69
+ register_root_object(CVSEQ(self), storage);
70
+
71
+ return self;
72
+ }
73
+
74
+ /*
75
+ * Returns bounding box of the contour
76
+ * @overload rect
77
+ * @return [CvRect] Bounding box of the contour
78
+ */
79
+ VALUE
80
+ rb_rect(VALUE self)
81
+ {
82
+ return cCvRect::new_object(CVCONTOUR(self)->rect);
83
+ }
84
+
85
+ /*
86
+ * Returns color of the contour
87
+ * @overload color
88
+ * @return [Number] Color of the contour
89
+ */
90
+ VALUE
91
+ rb_color(VALUE self)
92
+ {
93
+ return INT2NUM(CVCONTOUR(self)->color);
94
+ }
95
+
96
+ /*
97
+ * Set color of the contour
98
+ * @overload color=value
99
+ * @param value [Number] Color of the contour
100
+ */
101
+ VALUE
102
+ rb_set_color(VALUE self, VALUE color)
103
+ {
104
+ CVCONTOUR(self)->color = NUM2INT(color);
105
+ return self;
106
+ }
107
+
108
+ /*
109
+ * Returns reserved region values of the contour
110
+ * @overload reserved
111
+ * @return [Array<Number>] Reserved region values of the contour
112
+ */
113
+ VALUE
114
+ rb_reserved(VALUE self)
115
+ {
116
+ return rb_ary_new3(3,
117
+ INT2NUM(CVCONTOUR(self)->reserved[0]),
118
+ INT2NUM(CVCONTOUR(self)->reserved[1]),
119
+ INT2NUM(CVCONTOUR(self)->reserved[2]));
120
+ }
121
+
122
+ /*
123
+ * Approximates polygonal curves with desired precision
124
+ * @overload approx_poly(options)
125
+ * @param options [Hash] Parameters
126
+ * @option options [Symbol] :method Approximation method (default :dp)
127
+ * * :dp - Douglas-Peucker algorithm.
128
+ * @option options [Number] :accuracy Parameter specifying the approximation accuracy.
129
+ * This is the maximum distance between the original curve and its approximation.
130
+ * @option options [Boolean] :recursive Recursion flag. If true, the function approximates
131
+ * all the contours accessible from curve by h_next and v_next links.
132
+ * @return [CvContour] Result of the approximation
133
+ * @return [nil] Approximation faied
134
+ * @opencv_func cvApproxPoly
135
+ */
136
+ VALUE
137
+ rb_approx_poly(int argc, VALUE *argv, VALUE self)
138
+ {
139
+ VALUE approx_poly_option;
140
+ rb_scan_args(argc, argv, "01", &approx_poly_option);
141
+ approx_poly_option = APPROX_POLY_OPTION(approx_poly_option);
142
+ VALUE storage = cCvMemStorage::new_object();
143
+ CvSeq *contour = cvApproxPoly(CVCONTOUR(self), sizeof(CvContour), CVMEMSTORAGE(storage),
144
+ APPROX_POLY_METHOD(approx_poly_option),
145
+ APPROX_POLY_ACCURACY(approx_poly_option),
146
+ APPROX_POLY_RECURSIVE(approx_poly_option));
147
+
148
+ if (contour && contour->total > 0) {
149
+ return cCvSeq::new_sequence(cCvContour::rb_class(), contour, cCvPoint::rb_class(), storage);
150
+ }
151
+ return Qnil;
152
+ }
153
+
154
+ /*
155
+ * Calculates up-right bounding rectangle of point set.
156
+ * @overload bounding_rect
157
+ * @return [CvRect] Bounding rectangle
158
+ * @opencv_func cvBoundingRect
159
+ */
160
+ VALUE
161
+ rb_bounding_rect(VALUE self)
162
+ {
163
+ CvRect rect;
164
+ try {
165
+ rect = cvBoundingRect(CVCONTOUR(self), 1);
166
+ }
167
+ catch (cv::Exception& e) {
168
+ raise_cverror(e);
169
+ }
170
+ return cCvRect::new_object(rect);
171
+ }
172
+
173
+ /*
174
+ * Creates hierarchical representation of contour
175
+ * @overload create_tree(threshold = 0.0)
176
+ * @param threshold [Number] If <= 0, the method creates full binary tree representation.
177
+ * If > 0, the method creates representation with the precision threshold.
178
+ * @return [CvContourTree] Hierarchical representation of the contour
179
+ * @opencv_func cvCreateContourTree
180
+ */
181
+ VALUE
182
+ rb_create_tree(int argc, VALUE *argv, VALUE self)
183
+ {
184
+ VALUE threshold, storage;
185
+ rb_scan_args(argc, argv, "01", &threshold);
186
+ storage = cCvMemStorage::new_object();
187
+ CvContourTree *tree = NULL;
188
+ try {
189
+ tree = cvCreateContourTree(CVSEQ(self), CVMEMSTORAGE(storage), IF_DBL(threshold, 0.0));
190
+ }
191
+ catch (cv::Exception& e) {
192
+ raise_cverror(e);
193
+ }
194
+ return cCvSeq::new_sequence(cCvContourTree::rb_class(), (CvSeq*)tree, cCvPoint::rb_class(), storage);
195
+ }
196
+
197
+ /*
198
+ * Performs a point-in-contour test.
199
+ * The method determines whether the point is inside a contour, outside,
200
+ * or lies on an edge (or coincides with a vertex).
201
+ * @overload in?(point)
202
+ * @param point [CvPoint2D32f] Point tested against the contour
203
+ * @return [Boolean] If the point is inside, returns true. If outside, returns false.
204
+ * If lies on an edge, returns nil.
205
+ * @opencv_func cvPointPolygonTest
206
+ */
207
+ VALUE
208
+ rb_in_q(VALUE self, VALUE point)
209
+ {
210
+ double n = 0;
211
+ try {
212
+ n = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 0);
213
+ }
214
+ catch (cv::Exception& e) {
215
+ raise_cverror(e);
216
+ }
217
+ return n == 0 ? Qnil : n > 0 ? Qtrue : Qfalse;
218
+ }
219
+
220
+ /*
221
+ * Calculates distance between a point and the nearest contour edgex
222
+ * @overload measure_distance(point)
223
+ * @param point [CvPoint2D32f] Point tested against the contour
224
+ * @return Signed distance between the point and the nearest contour edge
225
+ * @opencv_func cvPointPolygonTest
226
+ */
227
+ VALUE
228
+ rb_measure_distance(VALUE self, VALUE point)
229
+ {
230
+ double distance = 0;
231
+ try {
232
+ distance = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), 1);
233
+ }
234
+ catch (cv::Exception& e) {
235
+ raise_cverror(e);
236
+ }
237
+ return rb_float_new(distance);
238
+ }
239
+
240
+ /*
241
+ * Determines whether the point is inside a contour, outside, or lies on an edge (or coinsides with a vertex).
242
+ * @overload point_polygon_test(point, measure_dist)
243
+ * @param point [CvPoint2D32f] Point tested against the contour
244
+ * @param measure_dist [Boolean] If true, the method estimates the signed distance from the point to
245
+ * the nearest contour edge. Otherwise, the function only checks if the point is inside a contour or not.
246
+ * @return [Number] When measure_dist = false, the return value is +1, -1 and 0, respectively.
247
+ * When measure_dist = true, it is a signed distance between the point and the nearest contour edge.
248
+ * @opencv_func cvPointPolygonTest
249
+ */
250
+ VALUE
251
+ rb_point_polygon_test(VALUE self, VALUE point, VALUE measure_dist)
252
+ {
253
+ int measure_dist_flag;
254
+
255
+ if (measure_dist == Qtrue)
256
+ measure_dist_flag = 1;
257
+ else if (measure_dist == Qfalse)
258
+ measure_dist_flag = 0;
259
+ else
260
+ measure_dist_flag = NUM2INT(measure_dist);
261
+
262
+ double dist = Qnil;
263
+ try {
264
+ dist = cvPointPolygonTest(CVARR(self), VALUE_TO_CVPOINT2D32F(point), measure_dist_flag);
265
+ }
266
+ catch (cv::Exception& e) {
267
+ raise_cverror(e);
268
+ }
269
+
270
+ /* cvPointPolygonTest returns 100, -100 or 0 when measure_dist = 0 */
271
+ if ((!measure_dist_flag) && ((int)dist) != 0)
272
+ dist = (dist > 0) ? 1 : -1;
273
+
274
+ return rb_float_new(dist);
275
+ }
276
+
277
+ VALUE new_object()
278
+ {
279
+ VALUE object = rb_allocate(rb_klass);
280
+ rb_initialize(0, NULL, object);
281
+ return object;
282
+ }
283
+
284
+
285
+ void
286
+ init_ruby_class()
287
+ {
288
+ #if 0
289
+ // For documentation using YARD
290
+ VALUE opencv = rb_define_module("OpenCV");
291
+ VALUE cvseq = rb_define_class_under(opencv, "CvSeq");
292
+ VALUE curve = rb_define_module_under(opencv, "Curve");
293
+ VALUE pointset = rb_define_module_under(opencv, "PointSet");
294
+ #endif
295
+
296
+ if (rb_klass)
297
+ return;
298
+
299
+ VALUE opencv = rb_module_opencv();
300
+ VALUE cvseq = cCvSeq::rb_class();
301
+ VALUE curve = mCurve::rb_module();
302
+ VALUE pointset = mPointSet::rb_module();
303
+
304
+ rb_klass = rb_define_class_under(opencv, "CvContour", cvseq);
305
+ rb_include_module(rb_klass, curve);
306
+ rb_include_module(rb_klass, pointset);
307
+
308
+ rb_define_alloc_func(rb_klass, rb_allocate);
309
+
310
+ VALUE approx_option = rb_hash_new();
311
+ rb_define_const(rb_klass, "APPROX_OPTION", approx_option);
312
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("method")), INT2FIX(CV_POLY_APPROX_DP));
313
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("accuracy")), rb_float_new(1.0));
314
+ rb_hash_aset(approx_option, ID2SYM(rb_intern("recursive")), Qfalse);
315
+
316
+ rb_define_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
317
+ rb_define_method(rb_klass, "rect", RUBY_METHOD_FUNC(rb_rect), 0);
318
+ rb_define_method(rb_klass, "color", RUBY_METHOD_FUNC(rb_color), 0);
319
+ rb_define_method(rb_klass, "color=", RUBY_METHOD_FUNC(rb_set_color), 1);
320
+ rb_define_method(rb_klass, "reserved", RUBY_METHOD_FUNC(rb_reserved), 0);
321
+ rb_define_method(rb_klass, "approx_poly", RUBY_METHOD_FUNC(rb_approx_poly), -1);
322
+ rb_define_alias(rb_klass, "approx", "approx_poly");
323
+ rb_define_method(rb_klass, "bounding_rect", RUBY_METHOD_FUNC(rb_bounding_rect), 0);
324
+ rb_define_method(rb_klass, "create_tree", RUBY_METHOD_FUNC(rb_create_tree), -1);
325
+ rb_define_method(rb_klass, "in?", RUBY_METHOD_FUNC(rb_in_q), 1);
326
+ rb_define_method(rb_klass, "measure_distance", RUBY_METHOD_FUNC(rb_measure_distance), 1);
327
+ rb_define_method(rb_klass, "point_polygon_test", RUBY_METHOD_FUNC(rb_point_polygon_test), 2);
328
+ }
329
+
330
+ __NAMESPACE_END_CVCONTOUR
331
+ __NAMESPACE_END_OPENCV
332
+