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,69 @@
1
+ /************************************************************
2
+
3
+ pointset.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_POINTSET_H
11
+ #define RUBY_OPENCV_POINTSET_H
12
+
13
+ #define __NAMESPACE_BEGIN_POINT_SET namespace mPointSet {
14
+ #define __NAMESPACE_END_POINT_SET }
15
+
16
+ #include "opencv.h"
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_POINT_SET
20
+
21
+ VALUE rb_module();
22
+
23
+ void define_ruby_module();
24
+ VALUE rb_contour_area(int argc, VALUE *argv, VALUE self);
25
+ VALUE rb_fit_ellipse2(VALUE self);
26
+ VALUE rb_convex_hull2(int argc, VALUE *argv, VALUE self);
27
+ VALUE rb_fit_line(int argc, VALUE *argv, VALUE self);
28
+ VALUE rb_check_contour_convexity(VALUE self);
29
+ VALUE rb_convexity_defects(VALUE self, VALUE hull);
30
+ VALUE rb_min_area_rect2(VALUE self);
31
+ VALUE rb_min_enclosing_circle(VALUE self);
32
+ VALUE rb_calc_pgh(VALUE self);
33
+
34
+ __NAMESPACE_END_POINT_SET
35
+
36
+ #define POINT_SET_P(object) rb_obj_is_kind_of(object, cCvSeq::rb_class()) && CV_IS_SEQ_POINT_SET(CVSEQ(object))
37
+
38
+ /*
39
+ inline CvPoint*
40
+ POINTSET(VALUE object)
41
+ {
42
+ CvPoint *pointset = (CvPoint*)cvAlloc(CVSEQ(object)->total * sizeof(CvPoint));
43
+ cvCvtSeqToArray(CVSEQ(object), pointset, CV_WHOLE_SEQ);
44
+ if (cCvSeq::seqblock_class(CVSEQ(object)) == cCvPoint2D32f::rb_class()) {
45
+ for(int i =0; i < CVSEQ(object)->total; i++)
46
+ pointset[i] = cvPointFrom32f(((CvPoint2D32f*)pointset)[i]);
47
+ }
48
+ return pointset;
49
+ }
50
+
51
+ inline CvPoint2D32f*
52
+ POINTSET2D32f(VALUE object)
53
+ {
54
+ CvPoint2D32f *pointset = (CvPoint2D32f*)cvAlloc(CVSEQ(object)->total * sizeof(CvPoint2D32f));
55
+ cvCvtSeqToArray(CVSEQ(object), pointset, CV_WHOLE_SEQ);
56
+ if (cCvSeq::seqblock_class(CVSEQ(object)) == cCvPoint::rb_class()) {
57
+ for(int i = 0; i < CVSEQ(object)->total; i++)
58
+ pointset[i] = cvPointTo32f(((CvPoint*)pointset)[i]);
59
+ }
60
+ return pointset;
61
+ }
62
+ */
63
+
64
+ int CVPOINTS_FROM_POINT_SET(VALUE object, CvPoint **pointset);
65
+ CvSeq* VALUE_TO_POINT_SET(VALUE object);
66
+
67
+ __NAMESPACE_END_OPENCV
68
+
69
+ #endif // RUBY_OPENCV_POINTSET_H
@@ -0,0 +1,121 @@
1
+ /************************************************************
2
+
3
+ trackbar.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "trackbar.h"
11
+ /*
12
+ * Document-class: OpenCV::GUI::Trackbar
13
+ *
14
+ * Simple Trackbar wedget. OpenCV::GUI::Window can treat trackbar.
15
+ * Trackbar can treat only positive-integer value.
16
+ */
17
+
18
+ __NAMESPACE_BEGIN_OPENCV
19
+ __NAMESPACE_BEGIN_GUI
20
+ __NAMESPACE_BEGIN_TRACKBAR
21
+
22
+ VALUE rb_klass;
23
+
24
+ VALUE rb_class() {
25
+ return rb_klass;
26
+ }
27
+
28
+ void define_ruby_class() {
29
+ if (rb_klass)
30
+ return;
31
+ /*
32
+ * opencv = rb_define_module("OpenCV");
33
+ * GUI = rb_define_module_under(opencv, "GUI");
34
+ *
35
+ * note: this comment is used by rdoc.
36
+ */
37
+ VALUE GUI = rb_module_GUI();
38
+ rb_klass = rb_define_class_under(GUI, "Trackbar", rb_cObject);
39
+ rb_define_alloc_func(rb_klass, rb_allocate);
40
+ rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
41
+ rb_define_method(rb_klass, "name", RUBY_METHOD_FUNC(rb_name), 0);
42
+ rb_define_method(rb_klass, "max", RUBY_METHOD_FUNC(rb_max), 0);
43
+ rb_define_method(rb_klass, "value", RUBY_METHOD_FUNC(rb_value), 0);
44
+ rb_define_method(rb_klass, "value=", RUBY_METHOD_FUNC(rb_set_value), 1);
45
+ }
46
+
47
+ VALUE rb_allocate(VALUE klass) {
48
+ Trackbar *ptr;
49
+ return Data_Make_Struct(klass, Trackbar, trackbar_mark, trackbar_free, ptr);
50
+ }
51
+
52
+ void trackbar_mark(void *ptr) {
53
+ rb_gc_mark(((Trackbar*)ptr)->block);
54
+ }
55
+
56
+ void trackbar_free(void *ptr) {
57
+ Trackbar *trackbar = (Trackbar*)ptr;
58
+ free(trackbar->name);
59
+ free(trackbar);
60
+ }
61
+
62
+ /*
63
+ * call-seq:
64
+ * new(<i>name,maxval[,val],&block</i>)
65
+ * new(<i>name,maxval[,val]</i>){|value| ... }
66
+ *
67
+ * Create new Trackbar.
68
+ * <i>name</i> should be String.
69
+ * <i>maxval</i> and <i>val</i> should be Fixnum.
70
+ * When Trackbar adjuster changed, block will be called.
71
+ */
72
+ VALUE rb_initialize(int argc, VALUE *argv, VALUE self) {
73
+ VALUE name, maxval, val, block;
74
+ rb_scan_args(argc, argv, "21&", &name, &maxval, &val, &block);
75
+ if (NIL_P(block))
76
+ rb_raise(rb_eArgError, "block not given.");
77
+ Check_Type(name, T_STRING);
78
+ Trackbar *trackbar = TRACKBAR(self);
79
+ trackbar->name = strcpy(ALLOC_N(char, RSTRING_LEN(name) + 1), StringValueCStr(name));
80
+ trackbar->maxval = NUM2INT(maxval);
81
+ trackbar->val = IF_INT(val, 0);
82
+ trackbar->block = block;
83
+ return self;
84
+ }
85
+
86
+ /*
87
+ * Return trackbar name.
88
+ */
89
+ VALUE rb_name(VALUE self) {
90
+ return rb_str_new2(TRACKBAR(self)->name);
91
+ }
92
+
93
+ /*
94
+ * Return the maximum value that can be taken this trackbar.
95
+ */
96
+ VALUE rb_max(VALUE self) {
97
+ return INT2NUM(TRACKBAR(self)->maxval);
98
+ }
99
+
100
+ /*
101
+ * Return the value of this trackbar.
102
+ */
103
+ VALUE rb_value(VALUE self) {
104
+ return INT2NUM(TRACKBAR(self)->val);
105
+ }
106
+
107
+ /*
108
+ * call-seq:
109
+ * value = <i>val</i>
110
+ *
111
+ * Set trackbar value.
112
+ */
113
+ VALUE rb_set_value(VALUE self, VALUE val) {
114
+ TRACKBAR(self)->val = NUM2INT(val);
115
+ return self;
116
+ }
117
+
118
+ __NAMESPACE_END_TRACKBAR
119
+ __NAMESPACE_END_GUI
120
+ __NAMESPACE_END_OPENCV
121
+
@@ -0,0 +1,69 @@
1
+ /************************************************************
2
+
3
+ trackbar.h -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #ifndef RUBY_OPENCV_GUI_H
11
+ #include "gui.h"
12
+ #endif
13
+
14
+ #ifndef RUBY_OPENCV_GUI_TRACKBAR_H
15
+ #define RUBY_OPENCV_GUI_TRACKBAR_H
16
+
17
+ #include "opencv.h"
18
+
19
+ #define __NAMESPACE_BEGIN_TRACKBAR namespace cTrackbar {
20
+ #define __NAMESPACE_END_TRACKBAR }
21
+
22
+ __NAMESPACE_BEGIN_OPENCV
23
+ __NAMESPACE_BEGIN_GUI
24
+
25
+ typedef struct Trackbar {
26
+ char *name;
27
+ int maxval;
28
+ int val;
29
+ VALUE block;
30
+ } Trackbar;
31
+
32
+ __NAMESPACE_BEGIN_TRACKBAR
33
+
34
+ VALUE rb_class();
35
+
36
+ void define_ruby_class();
37
+ VALUE rb_allocate(VALUE klass);
38
+
39
+ void trackbar_mark(void *ptr);
40
+ void trackbar_free(void *ptr);
41
+
42
+ VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
43
+ VALUE rb_name(VALUE self);
44
+ VALUE rb_max(VALUE self);
45
+ VALUE rb_value(VALUE self);
46
+ VALUE rb_set_value(VALUE self, VALUE val);
47
+
48
+ __NAMESPACE_END_TRACKBAR
49
+
50
+ inline Trackbar*
51
+ TRACKBAR(VALUE object) {
52
+ Trackbar *ptr;
53
+ Data_Get_Struct(object, Trackbar, ptr);
54
+ return ptr;
55
+ }
56
+
57
+ inline Trackbar*
58
+ TRACKBAR_WITH_CHECK(VALUE object) {
59
+ if (!rb_obj_is_kind_of(object, cTrackbar::rb_class())) {
60
+ raise_typeerror(object, cTrackbar::rb_class());
61
+ }
62
+ return TRACKBAR(object);
63
+ }
64
+
65
+ __NAMESPACE_END_GUI
66
+ __NAMESPACE_END_OPENCV
67
+
68
+ #endif // RUBY_OPENCV_GUI_TRACKBAR_H
69
+
@@ -0,0 +1,357 @@
1
+ /************************************************************
2
+
3
+ window.cpp -
4
+
5
+ $Author: lsxi $
6
+
7
+ Copyright (C) 2005-2006 Masakazu Yonekura
8
+
9
+ ************************************************************/
10
+ #include "window.h"
11
+
12
+ /*
13
+ * Document-class: OpenCV::GUI::Window
14
+ *
15
+ * Simple Window wedget to show images(CvMat/IplImage).
16
+ *
17
+ * Sample:
18
+ * image = OpenCV::IplImage::load("opencv.bmp") #=> load image
19
+ * window = OpenCV::GUI::Window.new("simple viewer")#=> create new window named "simaple viewer"
20
+ * window.show(image) #=> show image
21
+ */
22
+ __NAMESPACE_BEGIN_OPENCV
23
+ __NAMESPACE_BEGIN_GUI
24
+ __NAMESPACE_BEGIN_WINDOW
25
+
26
+ int num_windows = 0;
27
+ VALUE rb_klass;
28
+
29
+ VALUE
30
+ rb_class()
31
+ {
32
+ return rb_klass;
33
+ }
34
+
35
+ void
36
+ define_ruby_class()
37
+ {
38
+ if (rb_klass)
39
+ return;
40
+ /*
41
+ * opencv = rb_define_module("OpenCV");
42
+ * GUI = rb_define_module_under(opencv, "GUI");
43
+ *
44
+ * note: this comment is used by rdoc.
45
+ */
46
+ VALUE GUI = rb_module_GUI();
47
+ rb_klass = rb_define_class_under(GUI, "Window", rb_cObject);
48
+ rb_define_alloc_func(rb_klass, rb_allocate);
49
+ rb_define_private_method(rb_klass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
50
+ rb_define_method(rb_klass, "alive?", RUBY_METHOD_FUNC(rb_alive_q), 0);
51
+ rb_define_method(rb_klass, "destroy", RUBY_METHOD_FUNC(rb_destroy), 0);
52
+ rb_define_singleton_method(rb_klass, "destroy_all", RUBY_METHOD_FUNC(rb_destroy_all), 0);
53
+ rb_define_method(rb_klass, "resize", RUBY_METHOD_FUNC(rb_resize), -1);
54
+ rb_define_method(rb_klass, "move", RUBY_METHOD_FUNC(rb_move), -1);
55
+ rb_define_method(rb_klass, "show_image", RUBY_METHOD_FUNC(rb_show_image), 1);
56
+ rb_define_alias(rb_klass, "show", "show_image");
57
+ rb_define_method(rb_klass, "set_trackbar", RUBY_METHOD_FUNC(rb_set_trackbar), -1);
58
+ rb_define_method(rb_klass, "set_mouse_callback", RUBY_METHOD_FUNC(rb_set_mouse_callback), -1);
59
+ rb_define_alias(rb_klass, "on_mouse", "set_mouse_callback");
60
+ }
61
+
62
+ VALUE
63
+ rb_allocate(VALUE klass)
64
+ {
65
+ Window *ptr;
66
+ return Data_Make_Struct(klass, Window, window_mark, window_free, ptr);
67
+ }
68
+
69
+ void
70
+ window_mark(void *ptr)
71
+ {
72
+ Window* window_ptr = (Window*)ptr;
73
+ rb_gc_mark(window_ptr->name);
74
+ rb_gc_mark(window_ptr->image);
75
+ rb_gc_mark(window_ptr->trackbars);
76
+ rb_gc_mark(window_ptr->blocks);
77
+ }
78
+
79
+ void
80
+ window_free(void *ptr)
81
+ {
82
+ free(ptr);
83
+ }
84
+
85
+ /*
86
+ * call-seq:
87
+ * new(<i>name[, flags]</i>)
88
+ *
89
+ * Create new window named <i>name</i>.
90
+ * If <i>flags</i> is CV_WINDOW_AUTOSIZE (default), window size automatically resize when image given.
91
+ */
92
+ VALUE
93
+ rb_initialize(int argc, VALUE *argv, VALUE self)
94
+ {
95
+ VALUE name, flags;
96
+ rb_scan_args(argc, argv, "11", &name, &flags);
97
+ Check_Type(name, T_STRING);
98
+ char* name_str = StringValueCStr(name);
99
+ if (cvGetWindowHandle(name_str) != NULL) {
100
+ rb_raise(rb_eStandardError, "window name should be unique.");
101
+ }
102
+
103
+ int mode = CV_WINDOW_AUTOSIZE;
104
+ if (argc == 2) {
105
+ Check_Type(flags, T_FIXNUM);
106
+ mode = FIX2INT(flags);
107
+ }
108
+
109
+ Window* self_ptr = WINDOW(self);
110
+ self_ptr->name = name;
111
+ self_ptr->trackbars = rb_ary_new();
112
+ self_ptr->blocks = rb_ary_new();
113
+ try {
114
+ cvNamedWindow(name_str, mode);
115
+ }
116
+ catch (cv::Exception& e) {
117
+ raise_cverror(e);
118
+ }
119
+ num_windows++;
120
+ return self;
121
+ }
122
+
123
+ /*
124
+ * Return alive status of window. Return true if alive, otherwise return false.
125
+ */
126
+ VALUE
127
+ rb_alive_q(VALUE self)
128
+ {
129
+ const char* name_str = GET_WINDOW_NAME(self);
130
+ return (cvGetWindowHandle(name_str) == NULL) ? Qfalse : Qtrue;
131
+ }
132
+
133
+ /*
134
+ * Destroys a window. alive status of window be false.
135
+ */
136
+ VALUE
137
+ rb_destroy(VALUE self)
138
+ {
139
+ const char* name_str = GET_WINDOW_NAME(self);
140
+ try {
141
+ cvDestroyWindow(name_str);
142
+ }
143
+ catch (cv::Exception& e) {
144
+ raise_cverror(e);
145
+ }
146
+ num_windows--;
147
+ return self;
148
+ }
149
+
150
+ /*
151
+ * Destorys all the windows.
152
+ */
153
+ VALUE
154
+ rb_destroy_all(VALUE klass)
155
+ {
156
+ if (num_windows > 0) {
157
+ try {
158
+ cvDestroyAllWindows();
159
+ }
160
+ catch (cv::Exception& e) {
161
+ raise_cverror(e);
162
+ }
163
+ num_windows = 0;
164
+ }
165
+ return Qnil;
166
+ }
167
+
168
+ /*
169
+ * call-seq:
170
+ * resize(<i>size</i>)
171
+ * resize(<i>width, height</i>)
172
+ *
173
+ * Set window size.
174
+ */
175
+ VALUE
176
+ rb_resize(int argc, VALUE *argv, VALUE self)
177
+ {
178
+ int width = 0;
179
+ int height = 0;
180
+ switch (argc) {
181
+ case 1: {
182
+ CvSize size = VALUE_TO_CVSIZE(argv[0]);
183
+ width = size.width;
184
+ height = size.height;
185
+ break;
186
+ }
187
+ case 2:
188
+ width = NUM2INT(argv[0]);
189
+ height = NUM2INT(argv[1]);
190
+ break;
191
+ default:
192
+ rb_raise(rb_eArgError, "wrong number of arguments (1 or 2)");
193
+ break;
194
+ }
195
+ try {
196
+ cvResizeWindow(GET_WINDOW_NAME(self), width, height);
197
+ }
198
+ catch (cv::Exception& e) {
199
+ raise_cverror(e);
200
+ }
201
+ return self;
202
+ }
203
+
204
+ /*
205
+ * call-seq:
206
+ * move(<i>point</i>)
207
+ * move(<i>x, y</i>)
208
+ *
209
+ * Set window position.
210
+ */
211
+ VALUE
212
+ rb_move(int argc, VALUE *argv, VALUE self)
213
+ {
214
+ int x = 0;
215
+ int y = 0;
216
+ switch (argc) {
217
+ case 1: {
218
+ CvPoint point = VALUE_TO_CVPOINT(argv[0]);
219
+ x = point.x;
220
+ y = point.y;
221
+ break;
222
+ }
223
+ case 2:
224
+ x = NUM2INT(argv[0]);
225
+ y = NUM2INT(argv[1]);
226
+ break;
227
+ default:
228
+ rb_raise(rb_eArgError, "wrong number of arguments (1 or 2)");
229
+ break;
230
+ }
231
+ try {
232
+ cvMoveWindow(GET_WINDOW_NAME(self), x, y);
233
+ }
234
+ catch (cv::Exception& e) {
235
+ raise_cverror(e);
236
+ }
237
+ return self;
238
+ }
239
+
240
+ /*
241
+ * call-seq:
242
+ * show_image(<i>image</i>)
243
+ *
244
+ * Show the image. If the window was created with <i>flags</i> = CV_WINDOW_AUTOSIZE then the image is shown
245
+ * with its original size, otherwize the image is scaled to fit the window.
246
+ */
247
+ VALUE
248
+ rb_show_image(VALUE self, VALUE img)
249
+ {
250
+ CvArr* image = CVARR_WITH_CHECK(img);
251
+ WINDOW(self)->image = img;
252
+ try {
253
+ cvShowImage(GET_WINDOW_NAME(self), image);
254
+ }
255
+ catch (cv::Exception& e) {
256
+ raise_cverror(e);
257
+ }
258
+ return self;
259
+ }
260
+
261
+ void
262
+ trackbar_callback(int value, void* block)
263
+ {
264
+ rb_funcall((VALUE)block, rb_intern("call"), 1, INT2NUM(value));
265
+ }
266
+
267
+ /*
268
+ * call-seq:
269
+ * set_trackbar(<i>trackbar</i>)
270
+ * set_trackbar(<i>name,maxval[,val],&block</i>)
271
+ * set_trackbar(<i>name,maxval[,val]</i>){|value| ... }
272
+ *
273
+ * Create Trackbar on this window. Return new Trackbar.
274
+ * see Trackbar.new
275
+ */
276
+ VALUE
277
+ rb_set_trackbar(int argc, VALUE *argv, VALUE self)
278
+ {
279
+ VALUE trackbar;
280
+ if (argc == 1) {
281
+ trackbar = argv[0];
282
+ }
283
+ else {
284
+ trackbar = cTrackbar::rb_initialize(argc, argv, cTrackbar::rb_allocate(cTrackbar::rb_class()));
285
+ }
286
+ Trackbar *trackbar_ptr = TRACKBAR_WITH_CHECK(trackbar);
287
+ try {
288
+ cv::createTrackbar(trackbar_ptr->name, GET_WINDOW_NAME(self), &(trackbar_ptr->val), trackbar_ptr->maxval,
289
+ (cv::TrackbarCallback)trackbar_callback, (void*)(trackbar_ptr->block));
290
+ }
291
+ catch (cv::Exception& e) {
292
+ raise_cverror(e);
293
+ }
294
+ rb_ary_push(WINDOW(self)->trackbars, trackbar);
295
+
296
+ return trackbar;
297
+ }
298
+
299
+ void
300
+ on_mouse(int event, int x, int y, int flags, void* param)
301
+ {
302
+ VALUE block = (VALUE)param;
303
+ if (rb_obj_is_kind_of(block, rb_cProc)) {
304
+ rb_funcall(block, rb_intern("call"), 1, cMouseEvent::new_object(event, x, y, flags));
305
+ }
306
+ }
307
+
308
+ /*
309
+ * call-seq:
310
+ * set_mouse_callback(&block)
311
+ * set_mouse_callback {|mouse_event| ... }
312
+ *
313
+ * Set mouse callback.
314
+ * When the mouse is operated on the window, block will be called.
315
+ * Return Proc object.
316
+ * block given mouse event object, see GUI::Window::MouseEvent
317
+ *
318
+ * e.g. display mouse event on console.
319
+ * window = OpenCV::GUI::Window.new "sample window"
320
+ * image = OpenCV::IplImage::load "sample.png"
321
+ * window.show(image)
322
+ * window.set_mouse_callback {|mouse|
323
+ * e = "#{mouse.x}, #{mouse.y} : #{mouse.event} : "
324
+ * e << "<L>" if mouse.left_button?
325
+ * e << "<R>" if mouse.right_button?
326
+ * e << "<M>" if mouse.middle_button?
327
+ * e << "[CTRL]" if mouse.ctrl_key?
328
+ * e << "[SHIFT]" if mouse.shift_key?
329
+ * e << "[ALT]" if mouse.alt_key?
330
+ * puts e
331
+ * }
332
+ * OpenCV::GUI::wait_key
333
+ */
334
+ VALUE
335
+ rb_set_mouse_callback(int argc, VALUE* argv, VALUE self)
336
+ {
337
+ if (!rb_block_given_p()) {
338
+ rb_raise(rb_eArgError, "block not given.");
339
+ }
340
+
341
+ VALUE block = Qnil;
342
+ rb_scan_args(argc, argv, "0&", &block);
343
+ try {
344
+ cvSetMouseCallback(GET_WINDOW_NAME(self), on_mouse, (void*)block);
345
+ }
346
+ catch (cv::Exception& e) {
347
+ raise_cverror(e);
348
+ }
349
+
350
+ rb_ary_push(WINDOW(self)->blocks, block);
351
+ return block;
352
+ }
353
+
354
+ __NAMESPACE_END_WINDOW
355
+ __NAMESPACE_END_GUI
356
+ __NAMESPACE_END_OPENCV
357
+