spyglass 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.rspec +6 -0
  4. data/.travis.yml +11 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +20 -0
  7. data/README.md +28 -0
  8. data/Rakefile +5 -0
  9. data/examples/background_subtractor.rb +35 -0
  10. data/examples/cascade_classifier.rb +21 -0
  11. data/examples/contours.rb +24 -0
  12. data/examples/images/apple.jpg +0 -0
  13. data/examples/images/beach.jpg +0 -0
  14. data/examples/video_capture.rb +15 -0
  15. data/ext/spyglass/background_subtractor.cc +78 -0
  16. data/ext/spyglass/background_subtractor.h +18 -0
  17. data/ext/spyglass/cascade_classifier.cc +70 -0
  18. data/ext/spyglass/cascade_classifier.h +18 -0
  19. data/ext/spyglass/color.cc +83 -0
  20. data/ext/spyglass/color.h +22 -0
  21. data/ext/spyglass/color_space.cc +39 -0
  22. data/ext/spyglass/color_space.h +13 -0
  23. data/ext/spyglass/contour.cc +92 -0
  24. data/ext/spyglass/contour.h +22 -0
  25. data/ext/spyglass/extconf.rb +6 -0
  26. data/ext/spyglass/gui.cc +27 -0
  27. data/ext/spyglass/gui.h +15 -0
  28. data/ext/spyglass/image.cc +482 -0
  29. data/ext/spyglass/image.h +46 -0
  30. data/ext/spyglass/point.cc +78 -0
  31. data/ext/spyglass/point.h +23 -0
  32. data/ext/spyglass/prelude.h +42 -0
  33. data/ext/spyglass/rect.cc +131 -0
  34. data/ext/spyglass/rect.h +30 -0
  35. data/ext/spyglass/size.cc +89 -0
  36. data/ext/spyglass/size.h +25 -0
  37. data/ext/spyglass/spyglass.cc +35 -0
  38. data/ext/spyglass/spyglass.h +29 -0
  39. data/ext/spyglass/video_capture.cc +96 -0
  40. data/ext/spyglass/video_capture.h +20 -0
  41. data/ext/spyglass/window.cc +93 -0
  42. data/ext/spyglass/window.h +23 -0
  43. data/lib/spyglass.rb +6 -0
  44. data/lib/spyglass/color_space.rb +12 -0
  45. data/lib/spyglass/version.rb +3 -0
  46. data/spec/fixtures/haarcascade_frontalface_default.xml +35712 -0
  47. data/spec/fixtures/lena.jpg +0 -0
  48. data/spec/matchers/close_to.rb +6 -0
  49. data/spec/spec_helper.rb +8 -0
  50. data/spec/spyglass/background_subtractor_spec.rb +23 -0
  51. data/spec/spyglass/cascade_classifier_spec.rb +28 -0
  52. data/spec/spyglass/color_space_spec.rb +13 -0
  53. data/spec/spyglass/color_spec.rb +54 -0
  54. data/spec/spyglass/contour_spec.rb +26 -0
  55. data/spec/spyglass/gui/window_spec.rb +21 -0
  56. data/spec/spyglass/image_spec.rb +116 -0
  57. data/spec/spyglass/point_spec.rb +41 -0
  58. data/spec/spyglass/rect_spec.rb +103 -0
  59. data/spec/spyglass/size_spec.rb +52 -0
  60. data/spyglass.gemspec +26 -0
  61. data/tasks/compile.rake +6 -0
  62. data/tasks/gem.rake +2 -0
  63. data/tasks/rspec.rake +21 -0
  64. metadata +177 -0
@@ -0,0 +1,46 @@
1
+ #ifndef SPYGLASS_IMAGE_H_
2
+ #define SPYGLASS_IMAGE_H_
3
+
4
+ #include "spyglass.h"
5
+
6
+ namespace Spyglass {
7
+ namespace Image {
8
+ void define_ruby_class();
9
+ VALUE get_ruby_class();
10
+
11
+ static VALUE rb_alloc(VALUE self);
12
+ static void rb_free(cv::Mat *mat);
13
+ static VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
14
+ static VALUE rb_canny(VALUE self, VALUE threshold1, VALUE threshold2);
15
+ static VALUE rb_canny_inplace(VALUE self, VALUE threshold1, VALUE threshold2);
16
+ static VALUE rb_convert(VALUE self, VALUE color_space);
17
+ static VALUE rb_convert_inplace(VALUE self, VALUE color_space);
18
+ static VALUE rb_copy_inplace(int argc, VALUE *argv, VALUE self);
19
+ static VALUE rb_crop(VALUE self, VALUE rect);
20
+ static VALUE rb_crop_inplace(VALUE self, VALUE rect);
21
+ static VALUE rb_dilate(int argc, VALUE *argv, VALUE self);
22
+ static VALUE rb_dilate_inplace(int argc, VALUE *argv, VALUE self);
23
+ static VALUE rb_draw_contours(VALUE self, VALUE contours, VALUE color);
24
+ static VALUE rb_draw_rectangle(VALUE self, VALUE rect, VALUE color);
25
+ static VALUE rb_erode(int argc, VALUE *argv, VALUE self);
26
+ static VALUE rb_erode_inplace(int argc, VALUE *argv, VALUE self);
27
+ static VALUE rb_fill(int argc, VALUE *argv, VALUE self);
28
+ static VALUE rb_fill_inplace(int argc, VALUE *argv, VALUE self);
29
+ static VALUE rb_load(int argc, VALUE *argv, VALUE klass);
30
+ static VALUE rb_mean(int argc, VALUE *argv, VALUE klass);
31
+ static VALUE rb_threshold(int argc, VALUE *argv, VALUE klass);
32
+ static VALUE rb_threshold_inplace(int argc, VALUE *argv, VALUE klass);
33
+ static VALUE rb_threshold_inv(int argc, VALUE *argv, VALUE klass);
34
+ static VALUE rb_threshold_inv_inplace(int argc, VALUE *argv, VALUE klass);
35
+ static VALUE rb_get_cols(VALUE self);
36
+ static VALUE rb_get_contours(VALUE self);
37
+ static VALUE rb_get_rows(VALUE self);
38
+ static VALUE rb_get_size(VALUE self);
39
+ static VALUE rb_write(VALUE self, VALUE filename);
40
+ static VALUE rb_zeros(int argc, VALUE *argv, VALUE klass);
41
+
42
+ VALUE from_cvmat(cv::Mat *mat);
43
+ }
44
+ }
45
+
46
+ #endif // SPYGLASS_IMAGE_H_
@@ -0,0 +1,78 @@
1
+ #include "point.h"
2
+
3
+ static VALUE PointClass;
4
+
5
+ namespace Spyglass {
6
+ namespace Point {
7
+ void define_ruby_class() {
8
+ // Class definition
9
+ PointClass = rb_define_class_under(Spyglass::get_ruby_module(), "Point", rb_cObject);
10
+ rb_define_alloc_func(PointClass, rb_alloc);
11
+ rb_define_method(PointClass, "initialize", RUBY_METHOD_FUNC(rb_initialize), 2);
12
+
13
+ // Instance methods
14
+ rb_define_method(PointClass, "x", RUBY_METHOD_FUNC(rb_get_x), 0);
15
+ rb_define_method(PointClass, "x=", RUBY_METHOD_FUNC(rb_set_x), 1);
16
+ rb_define_method(PointClass, "y", RUBY_METHOD_FUNC(rb_get_y), 0);
17
+ rb_define_method(PointClass, "y=", RUBY_METHOD_FUNC(rb_set_y), 1);
18
+ }
19
+
20
+ VALUE get_ruby_class() {
21
+ return PointClass;
22
+ }
23
+
24
+ static VALUE rb_alloc(VALUE self) {
25
+ cv::Point *point = new cv::Point();
26
+ return Data_Wrap_Struct(PointClass, NULL, rb_free, point);
27
+ }
28
+
29
+ static void rb_free(cv::Point *point) {
30
+ delete point;
31
+ }
32
+
33
+ static VALUE rb_initialize(VALUE self, VALUE x, VALUE y) {
34
+ Check_Type(x, T_FIXNUM);
35
+ Check_Type(y, T_FIXNUM);
36
+
37
+ cv::Point *point = SG_GET_POINT(self);
38
+
39
+ point->x = FIX2INT(x);
40
+ point->y = FIX2INT(y);
41
+
42
+ return self;
43
+ }
44
+
45
+ static VALUE rb_get_x(VALUE self) {
46
+ cv::Point *point = SG_GET_POINT(self);
47
+ return INT2FIX(point->x);
48
+ }
49
+
50
+ static VALUE rb_set_x(VALUE self, VALUE x) {
51
+ Check_Type(x, T_FIXNUM);
52
+
53
+ cv::Point *point = SG_GET_POINT(self);
54
+
55
+ point->x = FIX2INT(x);
56
+ return x;
57
+ }
58
+
59
+ static VALUE rb_get_y(VALUE self) {
60
+ cv::Point *point = SG_GET_POINT(self);
61
+ return INT2FIX(point->y);
62
+ }
63
+
64
+ static VALUE rb_set_y(VALUE self, VALUE y) {
65
+ Check_Type(y, T_FIXNUM);
66
+
67
+ cv::Point *point = SG_GET_POINT(self);
68
+
69
+ point->y = FIX2INT(y);
70
+ return y;
71
+ }
72
+
73
+ VALUE from_cvrect(cv::Rect *rect) {
74
+ cv::Point *point = new cv::Point(rect->tl());
75
+ return Data_Wrap_Struct(PointClass, NULL, rb_free, point);
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,23 @@
1
+ #ifndef SPYGLASS_POINT_H_
2
+ #define SPYGLASS_POINT_H_
3
+
4
+ #include "spyglass.h"
5
+
6
+ namespace Spyglass {
7
+ namespace Point {
8
+ void define_ruby_class();
9
+ VALUE get_ruby_class();
10
+
11
+ static VALUE rb_alloc(VALUE self);
12
+ static void rb_free(cv::Point *point);
13
+ static VALUE rb_initialize(VALUE self, VALUE x, VALUE y);
14
+ static VALUE rb_get_x(VALUE self);
15
+ static VALUE rb_get_y(VALUE self);
16
+ static VALUE rb_set_x(VALUE self, VALUE x);
17
+ static VALUE rb_set_y(VALUE self, VALUE y);
18
+
19
+ VALUE from_cvrect(cv::Rect *rect);
20
+ }
21
+ }
22
+
23
+ #endif // SPYGLASS_POINT_H_
@@ -0,0 +1,42 @@
1
+ #ifndef SPYGLASS_PRELUDE_H_
2
+ #define SPYGLASS_PRELUDE_H_
3
+
4
+ #ifndef Data_Set_Struct
5
+ #define Data_Set_Struct(obj,ptr) do {\
6
+ Check_Type(obj, T_DATA); \
7
+ DATA_PTR(obj) = ptr; \
8
+ } while (0);
9
+ #endif
10
+
11
+ #define SG_GEN_GET_OBJECT_FUNCTION(func, type) \
12
+ inline type *func(VALUE obj) { \
13
+ type *ptr; \
14
+ Data_Get_Struct(obj, type, ptr); \
15
+ return ptr; \
16
+ }
17
+
18
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_BG_SUBTRACTOR, cv::BackgroundSubtractorMOG2);
19
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_CLASSIFIER, cv::CascadeClassifier);
20
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_COLOR, cv::Scalar);
21
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_CONTOUR, std::vector<cv::Point *>);
22
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_IMAGE, cv::Mat);
23
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_POINT, cv::Point);
24
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_RECT, cv::Rect);
25
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_SIZE, cv::Size);
26
+ SG_GEN_GET_OBJECT_FUNCTION(SG_GET_VIDEO_CAPTURE, cv::VideoCapture);
27
+
28
+ #define SG_OPTION(opts, type, key, default, expr) \
29
+ type key = default; \
30
+ if(RTEST(opts)) { \
31
+ Check_Type(opts, T_HASH); \
32
+ do { \
33
+ VALUE option; \
34
+ option = rb_hash_aref(opts, rb_str_new2(#key)); \
35
+ if(!RTEST(option)) \
36
+ option = rb_hash_aref(opts, ID2SYM(rb_intern(#key))); \
37
+ if(RTEST(option)) \
38
+ key = expr(option); \
39
+ } while(0); \
40
+ }
41
+
42
+ #endif // SPYGLASS_PRELUDE_H_
@@ -0,0 +1,131 @@
1
+ #include "rect.h"
2
+
3
+ static VALUE RectClass;
4
+
5
+ namespace Spyglass {
6
+ namespace Rect {
7
+ void define_ruby_class() {
8
+ // Class definition
9
+ RectClass = rb_define_class_under(Spyglass::get_ruby_module(), "Rect", rb_cObject);
10
+ rb_define_alloc_func(RectClass, rb_alloc);
11
+ rb_define_method(RectClass, "initialize", RUBY_METHOD_FUNC(rb_initialize), 4);
12
+
13
+ // Instance methods
14
+ rb_define_method(RectClass, "area", RUBY_METHOD_FUNC(rb_get_area), 0);
15
+ rb_define_method(RectClass, "height", RUBY_METHOD_FUNC(rb_get_height), 0);
16
+ rb_define_method(RectClass, "height=", RUBY_METHOD_FUNC(rb_set_height), 1);
17
+ rb_define_method(RectClass, "point", RUBY_METHOD_FUNC(rb_get_point), 0);
18
+ rb_define_method(RectClass, "size", RUBY_METHOD_FUNC(rb_get_size), 0);
19
+ rb_define_method(RectClass, "width", RUBY_METHOD_FUNC(rb_get_width), 0);
20
+ rb_define_method(RectClass, "width=", RUBY_METHOD_FUNC(rb_set_width), 1);
21
+ rb_define_method(RectClass, "x", RUBY_METHOD_FUNC(rb_get_x), 0);
22
+ rb_define_method(RectClass, "x=", RUBY_METHOD_FUNC(rb_set_x), 1);
23
+ rb_define_method(RectClass, "y", RUBY_METHOD_FUNC(rb_get_y), 0);
24
+ rb_define_method(RectClass, "y=", RUBY_METHOD_FUNC(rb_set_y), 1);
25
+ }
26
+
27
+ VALUE get_ruby_class() {
28
+ return RectClass;
29
+ }
30
+
31
+ static VALUE rb_alloc(VALUE self) {
32
+ cv::Rect *rect = new cv::Rect();
33
+ return Data_Wrap_Struct(RectClass, NULL, rb_free, rect);
34
+ }
35
+
36
+ static void rb_free(cv::Rect *rect) {
37
+ delete rect;
38
+ }
39
+
40
+ static VALUE rb_initialize(VALUE self, VALUE x, VALUE y, VALUE width, VALUE height) {
41
+ Check_Type(x, T_FIXNUM);
42
+ Check_Type(y, T_FIXNUM);
43
+ Check_Type(width, T_FIXNUM);
44
+ Check_Type(height, T_FIXNUM);
45
+
46
+ cv::Rect *rect = SG_GET_RECT(self);
47
+
48
+ rect->x = FIX2INT(x);
49
+ rect->y = FIX2INT(y);
50
+ rect->width = FIX2INT(width);
51
+ rect->height = FIX2INT(height);
52
+
53
+ return self;
54
+ }
55
+
56
+ static VALUE rb_get_area(VALUE self) {
57
+ cv::Rect *rect = SG_GET_RECT(self);
58
+ return INT2FIX(rect->area());
59
+ }
60
+
61
+ static VALUE rb_get_height(VALUE self) {
62
+ cv::Rect *rect = SG_GET_RECT(self);
63
+ return INT2FIX(rect->height);
64
+ }
65
+
66
+ static VALUE rb_set_height(VALUE self, VALUE height) {
67
+ Check_Type(height, T_FIXNUM);
68
+
69
+ cv::Rect *rect = SG_GET_RECT(self);
70
+
71
+ rect->height = FIX2INT(height);
72
+ return height;
73
+ }
74
+
75
+ static VALUE rb_get_point(VALUE self) {
76
+ cv::Rect *rect = SG_GET_RECT(self);
77
+ return Point::from_cvrect(rect);
78
+ }
79
+
80
+ static VALUE rb_get_size(VALUE self) {
81
+ cv::Rect *rect = SG_GET_RECT(self);
82
+ return Size::from_cvrect(rect);
83
+ }
84
+
85
+ static VALUE rb_get_width(VALUE self) {
86
+ cv::Rect *rect = SG_GET_RECT(self);
87
+ return INT2FIX(rect->width);
88
+ }
89
+
90
+ static VALUE rb_set_width(VALUE self, VALUE width) {
91
+ Check_Type(width, T_FIXNUM);
92
+
93
+ cv::Rect *rect = SG_GET_RECT(self);
94
+
95
+ rect->width = FIX2INT(width);
96
+ return width;
97
+ }
98
+
99
+ static VALUE rb_get_x(VALUE self) {
100
+ cv::Rect *rect = SG_GET_RECT(self);
101
+ return INT2FIX(rect->x);
102
+ }
103
+
104
+ static VALUE rb_set_x(VALUE self, VALUE x) {
105
+ Check_Type(x, T_FIXNUM);
106
+
107
+ cv::Rect *rect = SG_GET_RECT(self);
108
+
109
+ rect->x = FIX2INT(x);
110
+ return x;
111
+ }
112
+
113
+ static VALUE rb_get_y(VALUE self) {
114
+ cv::Rect *rect = SG_GET_RECT(self);
115
+ return INT2FIX(rect->y);
116
+ }
117
+
118
+ static VALUE rb_set_y(VALUE self, VALUE y) {
119
+ Check_Type(y, T_FIXNUM);
120
+
121
+ cv::Rect *rect = SG_GET_RECT(self);
122
+
123
+ rect->y = FIX2INT(y);
124
+ return y;
125
+ }
126
+
127
+ VALUE from_cvrect(cv::Rect *rect) {
128
+ return Data_Wrap_Struct(RectClass, NULL, rb_free, rect);
129
+ }
130
+ }
131
+ }
@@ -0,0 +1,30 @@
1
+ #ifndef SPYGLASS_RECT_H_
2
+ #define SPYGLASS_RECT_H_
3
+
4
+ #include "spyglass.h"
5
+
6
+ namespace Spyglass {
7
+ namespace Rect {
8
+ void define_ruby_class();
9
+ VALUE get_ruby_class();
10
+
11
+ static VALUE rb_alloc(VALUE self);
12
+ static void rb_free(cv::Rect *rect);
13
+ static VALUE rb_initialize(VALUE self, VALUE x, VALUE y, VALUE width, VALUE height);
14
+ static VALUE rb_get_area(VALUE self);
15
+ static VALUE rb_get_height(VALUE self);
16
+ static VALUE rb_get_point(VALUE self);
17
+ static VALUE rb_get_size(VALUE self);
18
+ static VALUE rb_get_width(VALUE self);
19
+ static VALUE rb_get_x(VALUE self);
20
+ static VALUE rb_get_y(VALUE self);
21
+ static VALUE rb_set_height(VALUE self, VALUE height);
22
+ static VALUE rb_set_width(VALUE self, VALUE width);
23
+ static VALUE rb_set_x(VALUE self, VALUE x);
24
+ static VALUE rb_set_y(VALUE self, VALUE y);
25
+
26
+ VALUE from_cvrect(cv::Rect *rect);
27
+ }
28
+ }
29
+
30
+ #endif // SPYGLASS_RECT_H_
@@ -0,0 +1,89 @@
1
+ #include "size.h"
2
+
3
+ static VALUE SizeClass;
4
+
5
+ namespace Spyglass {
6
+ namespace Size {
7
+ void define_ruby_class() {
8
+ // Class definition
9
+ SizeClass = rb_define_class_under(Spyglass::get_ruby_module(), "Size", rb_cObject);
10
+ rb_define_alloc_func(SizeClass, rb_alloc);
11
+ rb_define_method(SizeClass, "initialize", RUBY_METHOD_FUNC(rb_initialize), 2);
12
+
13
+ // Instance methods
14
+ rb_define_method(SizeClass, "area", RUBY_METHOD_FUNC(rb_get_area), 0);
15
+ rb_define_method(SizeClass, "height", RUBY_METHOD_FUNC(rb_get_height), 0);
16
+ rb_define_method(SizeClass, "height=", RUBY_METHOD_FUNC(rb_set_height), 1);
17
+ rb_define_method(SizeClass, "width", RUBY_METHOD_FUNC(rb_get_width), 0);
18
+ rb_define_method(SizeClass, "width=", RUBY_METHOD_FUNC(rb_set_width), 1);
19
+ }
20
+
21
+ VALUE get_ruby_class() {
22
+ return SizeClass;
23
+ }
24
+
25
+ static VALUE rb_alloc(VALUE self) {
26
+ cv::Size *size = new cv::Size();
27
+ return Data_Wrap_Struct(SizeClass, NULL, rb_free, size);
28
+ }
29
+
30
+ static void rb_free(cv::Size *size) {
31
+ delete size;
32
+ }
33
+
34
+ static VALUE rb_initialize(VALUE self, VALUE width, VALUE height) {
35
+ Check_Type(width, T_FIXNUM);
36
+ Check_Type(height, T_FIXNUM);
37
+
38
+ cv::Size *size = SG_GET_SIZE(self);
39
+
40
+ size->width = FIX2INT(width);
41
+ size->height = FIX2INT(height);
42
+
43
+ return self;
44
+ }
45
+
46
+ static VALUE rb_get_area(VALUE self) {
47
+ cv::Size *size = SG_GET_SIZE(self);
48
+ return INT2FIX(size->area());
49
+ }
50
+
51
+ static VALUE rb_get_height(VALUE self) {
52
+ cv::Size *size = SG_GET_SIZE(self);
53
+ return INT2FIX(size->height);
54
+ }
55
+
56
+ static VALUE rb_set_height(VALUE self, VALUE height) {
57
+ Check_Type(height, T_FIXNUM);
58
+
59
+ cv::Size *size = SG_GET_SIZE(self);
60
+
61
+ size->height = FIX2INT(height);
62
+ return height;
63
+ }
64
+
65
+ static VALUE rb_get_width(VALUE self) {
66
+ cv::Size *size = SG_GET_SIZE(self);
67
+ return INT2FIX(size->width);
68
+ }
69
+
70
+ static VALUE rb_set_width(VALUE self, VALUE width) {
71
+ Check_Type(width, T_FIXNUM);
72
+
73
+ cv::Size *size = SG_GET_SIZE(self);
74
+
75
+ size->width = FIX2INT(width);
76
+ return width;
77
+ }
78
+
79
+ VALUE from_cvmat(cv::Mat *mat) {
80
+ cv::Size *size = new cv::Size(mat->size());
81
+ return Data_Wrap_Struct(SizeClass, NULL, rb_free, size);
82
+ }
83
+
84
+ VALUE from_cvrect(cv::Rect *rect) {
85
+ cv::Size *size = new cv::Size(rect->size());
86
+ return Data_Wrap_Struct(SizeClass, NULL, rb_free, size);
87
+ }
88
+ }
89
+ }