spyglass 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea79e0513a6b15e030d22e55cb61f1281976d6d2
4
- data.tar.gz: a90524336749778e856803ace12316f3ae0fd88e
3
+ metadata.gz: 1a505d1ba6c6d3a5e1016de5742b9c2d5f3eeef3
4
+ data.tar.gz: 78af742887c6c26220f8890fa499c2a513157e0b
5
5
  SHA512:
6
- metadata.gz: a103029705c35d69271b0f527d7bd94e19eaff10218677ec4c70d715164696df900a837b0feea5d4dab35dec14dfff2044c98d2fdf3a38c393a31556d9e2dfad
7
- data.tar.gz: e01a216cab093e8db29631f3db6224d6525a2197a38e24c7ac6d2ef169b6a10f42437e0dc2102ba8803d5f06528faf342390443ca57fc0f19408a4be68d320c8
6
+ metadata.gz: e65815ef5337b98bfed88b0099c7e9866134f3d7d0eb4a70438f547d32b6e2ff5d5e183661a72e86866aeb7b0e60eb7149b6c69f8f48904394471bcaec494452
7
+ data.tar.gz: 0e5d01e6e0087b0f01379071c32c27c16eb467081d7bd5d28c59db6bbd7439be4ef4b5e51ee3ca180efd5789d2c66367c2b38814cfe01cb846e7dee1556a90f3
data/README.md CHANGED
@@ -21,8 +21,13 @@ There is a lot left to implement, as I've really only ported the parts of the AP
21
21
  * Documentation
22
22
  * More tests
23
23
 
24
+ Acknowledgements
25
+ ================
26
+
27
+ This code contains parts of [BGSLibrary](code.google.com/p/bgslibrary)
28
+
24
29
  Code Status
25
30
  ===========
26
31
 
27
32
  * [![Build Status](https://travis-ci.org/andremedeiros/spyglass.png?branch=master)](https://travis-ci.org/andremedeiros/spyglass)
28
- * [![Dependency Status](https://gemnasium.com/andremedeiros/spyglass.png)](https://gemnasium.com/andremedeiros/spyglass)
33
+ * [![Dependency Status](https://gemnasium.com/andremedeiros/spyglass.png)](https://gemnasium.com/andremedeiros/spyglass)
@@ -9,7 +9,7 @@ WARNING: This demo might not work well with cameras that do auto-exposure.
9
9
 
10
10
  eos
11
11
 
12
- bg = BackgroundSubtractor.new
12
+ bg = BackgroundSubtractor::MOG2.new
13
13
  window = GUI::Window.new("Beach!")
14
14
  cap = VideoCapture.new 0, width: 640, height: 480
15
15
  frame = Image.new
@@ -1,30 +1,67 @@
1
1
  #include "background_subtractor.h"
2
2
 
3
- static VALUE BackgroundSubtractorClass;
3
+ static VALUE BackgroundSubtractorModule;
4
+ static VALUE BGSMOGClass;
5
+ static VALUE BGSMOG2Class;
6
+ static VALUE BGSGMGClass;
7
+ static VALUE BGSPratiMediodClass;
4
8
 
5
9
  namespace Spyglass {
6
10
  namespace BackgroundSubtractor {
7
- void define_ruby_class() {
8
- // Class definition
9
- BackgroundSubtractorClass = rb_define_class_under(Spyglass::get_ruby_module(), "BackgroundSubtractor", rb_cObject);
10
- rb_define_alloc_func(BackgroundSubtractorClass, rb_alloc);
11
- rb_define_method(BackgroundSubtractorClass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
12
-
13
- // Instance methods
14
- rb_define_method(BackgroundSubtractorClass, "subtract", RUBY_METHOD_FUNC(rb_subtract), -1);
11
+ void define_ruby_types() {
12
+ // Module definition
13
+ BackgroundSubtractorModule = rb_define_module_under(Spyglass::get_ruby_module(), "BackgroundSubtractor");
14
+
15
+ // Class definition: MOG
16
+ BGSMOGClass = rb_define_class_under(BackgroundSubtractorModule, "MOG", rb_cObject);
17
+ rb_define_alloc_func(BGSMOGClass, rb_mog_alloc);
18
+ rb_define_method(BGSMOGClass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
19
+ rb_define_method(BGSMOGClass, "subtract", RUBY_METHOD_FUNC(rb_subtract), -1);
20
+
21
+ // Class definition: MOG2
22
+ BGSMOG2Class = rb_define_class_under(BackgroundSubtractorModule, "MOG2", rb_cObject);
23
+ rb_define_alloc_func(BGSMOG2Class, rb_mog2_alloc);
24
+ rb_define_method(BGSMOG2Class, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
25
+ rb_define_method(BGSMOG2Class, "subtract", RUBY_METHOD_FUNC(rb_subtract), -1);
26
+
27
+ // Class definition: GMG
28
+ BGSGMGClass = rb_define_class_under(BackgroundSubtractorModule, "GMG", rb_cObject);
29
+ rb_define_alloc_func(BGSGMGClass, rb_gmg_alloc);
30
+ rb_define_method(BGSGMGClass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
31
+ rb_define_method(BGSGMGClass, "subtract", RUBY_METHOD_FUNC(rb_subtract), -1);
32
+
33
+ // Class definition: PratiMediod
34
+ BGSPratiMediodClass = rb_define_class_under(BackgroundSubtractorModule, "PratiMediod", rb_cObject);
35
+ rb_define_alloc_func(BGSPratiMediodClass, rb_prati_mediod_alloc);
36
+ rb_define_method(BGSPratiMediodClass, "initialize", RUBY_METHOD_FUNC(rb_initialize), -1);
37
+ rb_define_method(BGSPratiMediodClass, "subtract", RUBY_METHOD_FUNC(rb_subtract), -1);
15
38
  }
16
39
 
17
- VALUE get_ruby_class() {
18
- return BackgroundSubtractorClass;
40
+ VALUE get_ruby_module() {
41
+ return BackgroundSubtractorModule;
19
42
  }
20
43
 
21
- static VALUE rb_alloc(VALUE self) {
22
- cv::BackgroundSubtractorMOG2 *bg = new cv::BackgroundSubtractorMOG2();
23
- return Data_Wrap_Struct(BackgroundSubtractorClass, NULL, rb_free, bg);
44
+ static VALUE rb_mog_alloc(VALUE self) {
45
+ cv::BackgroundSubtractor *bg = new cv::BackgroundSubtractorMOG();
46
+ return Data_Wrap_Struct(BGSMOGClass, NULL, rb_free, bg);
24
47
  }
25
48
 
26
- static void rb_free(cv::BackgroundSubtractorMOG2 *bg) {
27
- bg->~BackgroundSubtractorMOG2();
49
+ static VALUE rb_mog2_alloc(VALUE self) {
50
+ cv::BackgroundSubtractor *bg = new cv::BackgroundSubtractorMOG2();
51
+ return Data_Wrap_Struct(BGSMOG2Class, NULL, rb_free, bg);
52
+ }
53
+
54
+ static VALUE rb_gmg_alloc(VALUE self) {
55
+ cv::BackgroundSubtractor *bg = new cv::BackgroundSubtractorGMG();
56
+ return Data_Wrap_Struct(BGSGMGClass, NULL, rb_free, bg);
57
+ }
58
+
59
+ static VALUE rb_prati_mediod_alloc(VALUE self) {
60
+ cv::BackgroundSubtractor *bg = new DPPratiMediodBGS();
61
+ return Data_Wrap_Struct(BGSPratiMediodClass, NULL, rb_free, bg);
62
+ }
63
+
64
+ static void rb_free(cv::BackgroundSubtractor *bg) {
28
65
  delete bg;
29
66
  }
30
67
 
@@ -35,15 +72,33 @@ namespace Spyglass {
35
72
  if(!RTEST(opts))
36
73
  return self;
37
74
 
38
- cv::BackgroundSubtractorMOG2 *bg = SG_GET_BG_SUBTRACTOR(self);
39
-
40
- SG_OPTION(opts, int, history, 500, NUM2INT);
41
- SG_OPTION(opts, double, threshold, 8.0, NUM2DBL);
42
- SG_OPTION(opts, bool, shadow_detection, true, RTEST);
43
-
44
- bg->set("history", history);
45
- bg->set("varThreshold", threshold);
46
- bg->set("detectShadows", shadow_detection);
75
+ cv::BackgroundSubtractor *bg = SG_GET_BG_SUBTRACTOR(self);
76
+
77
+ if(CLASS_OF(self) == BGSMOGClass) {
78
+ SG_OPTION(opts, int, history, 200, NUM2INT);
79
+ SG_OPTION(opts, int, mixtures, 5, NUM2INT);
80
+ SG_OPTION(opts, double, background_ratio, 0.7, NUM2DBL);
81
+
82
+ bg->set("history", history);
83
+ bg->set("nmixtures", mixtures);
84
+ bg->set("backgroundRatio", background_ratio);
85
+ } else if(CLASS_OF(self) == BGSMOG2Class) {
86
+ SG_OPTION(opts, int, history, 500, NUM2INT);
87
+ SG_OPTION(opts, double, threshold, 8.0, NUM2DBL);
88
+ SG_OPTION(opts, bool, shadow_detection, true, RTEST);
89
+
90
+ bg->set("history", history);
91
+ bg->set("varThreshold", threshold);
92
+ bg->set("detectShadows", shadow_detection);
93
+ } else if(CLASS_OF(self) == BGSPratiMediodClass) {
94
+ SG_OPTION(opts, int, history, 16, NUM2INT);
95
+ SG_OPTION(opts, int, threshold, 30, NUM2INT);
96
+ SG_OPTION(opts, int, sampling_rate, 5, NUM2INT);
97
+
98
+ bg->set("historySize", history);
99
+ bg->set("threshold", threshold);
100
+ bg->set("samplingRate", sampling_rate);
101
+ }
47
102
 
48
103
  return self;
49
104
  }
@@ -61,13 +116,13 @@ namespace Spyglass {
61
116
 
62
117
 
63
118
  if(RTEST(learn_rate) && TYPE(learn_rate) != T_FLOAT && TYPE(learn_rate) != T_FIXNUM)
64
- rb_raise(rb_eTypeError, "wrong argument type %s (expected Float or Fixnum)",
65
- rb_obj_classname(learn_rate));
119
+ rb_raise(rb_eTypeError, "wrong argument type %s (expected Float or Fixnum)",
120
+ rb_obj_classname(learn_rate));
66
121
 
67
122
  rate = RTEST(learn_rate) ? NUM2DBL(learn_rate) : -1.0;
68
123
 
69
- cv::BackgroundSubtractorMOG2 *bg = SG_GET_BG_SUBTRACTOR(self);
70
- cv::Mat *img = SG_GET_IMAGE(image);
124
+ cv::BackgroundSubtractor *bg = SG_GET_BG_SUBTRACTOR(self);
125
+ cv::Mat *img = SG_GET_IMAGE(image);
71
126
 
72
127
  cv::Mat *delta = new cv::Mat();
73
128
  (*bg)(*img, *delta, rate);
@@ -3,15 +3,22 @@
3
3
 
4
4
  #include "spyglass.h"
5
5
 
6
+ #include "bgslib_dp_prati_mediod_bgs.h"
7
+
6
8
  namespace Spyglass {
7
9
  namespace BackgroundSubtractor {
8
- void define_ruby_class();
9
- VALUE get_ruby_class();
10
+ void define_ruby_types();
11
+ VALUE get_ruby_module();
10
12
 
11
- static VALUE rb_alloc(VALUE self);
12
- static void rb_free(cv::BackgroundSubtractorMOG2 *bg);
13
+ static void rb_free(cv::BackgroundSubtractor *bg);
13
14
  static VALUE rb_initialize(int argc, VALUE *argv, VALUE self);
14
15
  static VALUE rb_subtract(int argc, VALUE *argv, VALUE self);
16
+
17
+ // Class allocators
18
+ static VALUE rb_mog_alloc(VALUE self);
19
+ static VALUE rb_mog2_alloc(VALUE self);
20
+ static VALUE rb_gmg_alloc(VALUE self);
21
+ static VALUE rb_prati_mediod_alloc(VALUE self);
15
22
  }
16
23
  }
17
24
 
@@ -0,0 +1,67 @@
1
+ /*
2
+ This file is part of BGSLibrary.
3
+
4
+ BGSLibrary is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ BGSLibrary is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ /****************************************************************************
18
+ *
19
+ * Bgs.hpp
20
+ *
21
+ * Purpose: Base class for BGS algorithms.
22
+ *
23
+ * Author: Donovan Parks, October 2007
24
+ *
25
+ ******************************************************************************/
26
+
27
+ #ifndef BGS_H_
28
+ #define BGS_H_
29
+
30
+ #include "bgslib_image.h"
31
+ #include "bgslib_bgs_params.h"
32
+
33
+ namespace Algorithms
34
+ {
35
+ namespace BackgroundSubtraction
36
+ {
37
+ class Bgs
38
+ {
39
+ public:
40
+ static const int BACKGROUND = 0;
41
+ static const int FOREGROUND = 255;
42
+
43
+ virtual ~Bgs() {}
44
+
45
+ // Initialize any data required by the BGS algorithm. Should be called once before calling
46
+ // any of the following functions.
47
+ virtual void Initalize(const BgsParams& param) = 0;
48
+
49
+ // Initialize the background model. Typically, the background model is initialized using the first
50
+ // frame of the incoming video stream, but alternatives are possible.
51
+ virtual void InitModel(const RgbImage& data) = 0;
52
+
53
+ // Subtract the current frame from the background model and produce a binary foreground mask using
54
+ // both a low and high threshold value.
55
+ virtual void Subtract(int frame_num, const RgbImage& data,
56
+ BwImage& low_threshold_mask, BwImage& high_threshold_mask) = 0;
57
+
58
+ // Update the background model. Only pixels set to background in update_mask are updated.
59
+ virtual void Update(int frame_num, const RgbImage& data, const BwImage& update_mask) = 0;
60
+
61
+ // Return the current background model.
62
+ virtual RgbImage *Background() = 0;
63
+ };
64
+ };
65
+ };
66
+
67
+ #endif
@@ -0,0 +1,60 @@
1
+ /*
2
+ This file is part of BGSLibrary.
3
+
4
+ BGSLibrary is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ BGSLibrary is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ /****************************************************************************
18
+ *
19
+ * BgsParams.hpp
20
+ *
21
+ * Purpose: Base class for BGS parameters. Any parameters common to all BGS
22
+ * algorithms should be specified directly in this class.
23
+ *
24
+ * Author: Donovan Parks, May 2008
25
+ *
26
+ ******************************************************************************/
27
+
28
+ #ifndef BGS_PARAMS_H_
29
+ #define BGS_PARAMS_H_
30
+
31
+ namespace Algorithms
32
+ {
33
+ namespace BackgroundSubtraction
34
+ {
35
+ class BgsParams
36
+ {
37
+ public:
38
+ virtual ~BgsParams() {}
39
+
40
+ virtual void SetFrameSize(unsigned int width, unsigned int height)
41
+ {
42
+ m_width = width;
43
+ m_height = height;
44
+ m_size = width*height;
45
+ }
46
+
47
+ unsigned int &Width() { return m_width; }
48
+ unsigned int &Height() { return m_height; }
49
+ unsigned int &Size() { return m_size; }
50
+
51
+ protected:
52
+ unsigned int m_width;
53
+ unsigned int m_height;
54
+ unsigned int m_size;
55
+ };
56
+ };
57
+ };
58
+
59
+ #endif
60
+
@@ -0,0 +1,79 @@
1
+ /*
2
+ This file is part of BGSLibrary.
3
+
4
+ BGSLibrary is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ BGSLibrary is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ #include "bgslib_dp_prati_mediod_bgs.h"
18
+
19
+ DPPratiMediodBGS::DPPratiMediodBGS() :
20
+ firstTime(true), frameNumber(0), threshold(30), samplingRate(5), historySize(16), weight(5), showOutput(false)
21
+ {}
22
+
23
+ DPPratiMediodBGS::~DPPratiMediodBGS() {}
24
+
25
+ void DPPratiMediodBGS::operator()(cv::InputArray _image, cv::OutputArray _fgmask, double learningRate)
26
+ {
27
+ cv::Mat img_input = _image.getMat();
28
+ if(img_input.empty())
29
+ return;
30
+
31
+ frame = new IplImage(img_input);
32
+
33
+ if(firstTime)
34
+ frame_data.ReleaseMemory(false);
35
+ frame_data = frame;
36
+
37
+ if(firstTime)
38
+ {
39
+ int width = img_input.size().width;
40
+ int height = img_input.size().height;
41
+
42
+ lowThresholdMask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
43
+ lowThresholdMask.Ptr()->origin = IPL_ORIGIN_BL;
44
+
45
+ highThresholdMask = cvCreateImage(cvSize(width, height), IPL_DEPTH_8U, 1);
46
+ highThresholdMask.Ptr()->origin = IPL_ORIGIN_BL;
47
+
48
+ params.SetFrameSize(width, height);
49
+ params.LowThreshold() = threshold;
50
+ params.HighThreshold() = 2*params.LowThreshold(); // Note: high threshold is used by post-processing
51
+ params.SamplingRate() = samplingRate;
52
+ params.HistorySize() = historySize;
53
+ params.Weight() = weight;
54
+
55
+ bgs.Initalize(params);
56
+ bgs.InitModel(frame_data);
57
+ }
58
+
59
+ bgs.Subtract(frameNumber, frame_data, lowThresholdMask, highThresholdMask);
60
+ lowThresholdMask.Clear();
61
+ bgs.Update(frameNumber, frame_data, lowThresholdMask);
62
+
63
+ cv::Mat foreground(highThresholdMask.Ptr());
64
+
65
+ if(showOutput)
66
+ cv::imshow("Temporal Median (Cucchiara&Calderara)", foreground);
67
+
68
+ foreground.copyTo(_fgmask);
69
+
70
+ delete frame;
71
+ firstTime = false;
72
+ frameNumber++;
73
+ }
74
+
75
+ CV_INIT_ALGORITHM(DPPratiMediodBGS, "BGSLibrary.DPPratiMediod",
76
+ obj.info()->addParam(obj, "threshold", obj.threshold);
77
+ obj.info()->addParam(obj, "samplingRate", obj.samplingRate);
78
+ obj.info()->addParam(obj, "historySize", obj.historySize);
79
+ obj.info()->addParam(obj, "showOutput", obj.showOutput));
@@ -0,0 +1,53 @@
1
+ /*
2
+ This file is part of BGSLibrary.
3
+
4
+ BGSLibrary is free software: you can redistribute it and/or modify
5
+ it under the terms of the GNU General Public License as published by
6
+ the Free Software Foundation, either version 3 of the License, or
7
+ (at your option) any later version.
8
+
9
+ BGSLibrary is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with BGSLibrary. If not, see <http://www.gnu.org/licenses/>.
16
+ */
17
+ #pragma once
18
+
19
+ #include <iostream>
20
+ #include <opencv2/opencv.hpp>
21
+ #include <opencv2/core/internal.hpp>
22
+
23
+ #include "bgslib_prati_mediod_bgs.h"
24
+
25
+ using namespace Algorithms::BackgroundSubtraction;
26
+
27
+ class DPPratiMediodBGS : public cv::BackgroundSubtractor
28
+ {
29
+ private:
30
+ bool firstTime;
31
+ long frameNumber;
32
+ IplImage* frame;
33
+ RgbImage frame_data;
34
+
35
+ PratiParams params;
36
+ PratiMediodBGS bgs;
37
+ BwImage lowThresholdMask;
38
+ BwImage highThresholdMask;
39
+
40
+ int threshold;
41
+ int samplingRate;
42
+ int historySize;
43
+ int weight;
44
+ bool showOutput;
45
+
46
+ public:
47
+ DPPratiMediodBGS();
48
+ ~DPPratiMediodBGS();
49
+
50
+ virtual cv::AlgorithmInfo* info() const;
51
+ void operator()(cv::InputArray _image, cv::OutputArray _fgmask, double learningRate);
52
+ };
53
+