spyglass 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -1
- data/examples/background_subtractor.rb +1 -1
- data/ext/spyglass/background_subtractor.cc +84 -29
- data/ext/spyglass/background_subtractor.h +11 -4
- data/ext/spyglass/bgslib_bgs.h +67 -0
- data/ext/spyglass/bgslib_bgs_params.h +60 -0
- data/ext/spyglass/bgslib_dp_prati_mediod_bgs.cc +79 -0
- data/ext/spyglass/bgslib_dp_prati_mediod_bgs.h +53 -0
- data/ext/spyglass/bgslib_image.cc +77 -0
- data/ext/spyglass/bgslib_image.h +365 -0
- data/ext/spyglass/bgslib_prati_mediod_bgs.cc +276 -0
- data/ext/spyglass/bgslib_prati_mediod_bgs.h +142 -0
- data/ext/spyglass/contour.cc +0 -2
- data/ext/spyglass/extconf.rb +1 -0
- data/ext/spyglass/prelude.h +1 -1
- data/ext/spyglass/spyglass.cc +1 -1
- data/lib/spyglass/version.rb +2 -2
- data/spec/spyglass/background_subtractor_spec.rb +23 -18
- metadata +10 -2
@@ -0,0 +1,142 @@
|
|
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
|
+
* PratiMediodBGS.hpp
|
20
|
+
*
|
21
|
+
* Purpose: Implementation of the temporal median background
|
22
|
+
* subtraction algorithm described in:
|
23
|
+
*
|
24
|
+
* [1] "Detecting Moving Objects, Shosts, and Shadows in Video Stream"
|
25
|
+
* by R. Cucchiara et al (2003)
|
26
|
+
*
|
27
|
+
* [2] "Reliable Background Suppression for Complex Scenes"
|
28
|
+
* by S. Calderara et al (2006)
|
29
|
+
*
|
30
|
+
* Author: Donovan Parks, September 2007
|
31
|
+
*
|
32
|
+
* Please note that this is not an implementation of the complete system
|
33
|
+
* given in the above papers. It simply implements the temporal media background
|
34
|
+
* subtraction algorithm.
|
35
|
+
|
36
|
+
Example:
|
37
|
+
Algorithms::BackgroundSubtraction::PratiParams params;
|
38
|
+
params.SetFrameSize(width, height);
|
39
|
+
params.LowThreshold() = 30;
|
40
|
+
params.HighThreshold() = 2*params.LowThreshold(); // Note: high threshold is used by post-processing
|
41
|
+
params.SamplingRate() = 5;
|
42
|
+
params.HistorySize() = 16;
|
43
|
+
params.Weight() = 5;
|
44
|
+
|
45
|
+
Algorithms::BackgroundSubtraction::PratiMediodBGS bgs;
|
46
|
+
bgs.Initalize(params);
|
47
|
+
******************************************************************************/
|
48
|
+
|
49
|
+
#ifndef PRATI_MEDIA_BGS_H
|
50
|
+
#define PRATI_MEDIA_BGS_H
|
51
|
+
|
52
|
+
#include <vector>
|
53
|
+
#include "bgslib_bgs.h"
|
54
|
+
|
55
|
+
namespace Algorithms
|
56
|
+
{
|
57
|
+
namespace BackgroundSubtraction
|
58
|
+
{
|
59
|
+
// --- Parameters used by the Prati Mediod BGS algorithm ---
|
60
|
+
class PratiParams : public BgsParams
|
61
|
+
{
|
62
|
+
public:
|
63
|
+
unsigned int &LowThreshold() { return m_low_threshold; }
|
64
|
+
unsigned int &HighThreshold() { return m_high_threshold; }
|
65
|
+
|
66
|
+
int &Weight() { return m_weight; }
|
67
|
+
int &SamplingRate() { return m_sampling_rate; }
|
68
|
+
int &HistorySize() { return m_history_size; }
|
69
|
+
|
70
|
+
private:
|
71
|
+
// The low threshold is used to supress noise. The high thresohld is used
|
72
|
+
// to find pixels highly likely to be foreground. This implementation uses an L-inf
|
73
|
+
// distance measure and a pixel p is considered F/G if D(I(p), B(p)) > threshold.
|
74
|
+
// The two threshold maps are combined as in [2].
|
75
|
+
unsigned int m_low_threshold;
|
76
|
+
unsigned int m_high_threshold;
|
77
|
+
|
78
|
+
// The weight parameter controls the amount of influence given to previous background samples
|
79
|
+
// see w_b in equation (2) of [1]
|
80
|
+
// in [2] this value is set to 1
|
81
|
+
int m_weight;
|
82
|
+
|
83
|
+
// Number of samples to consider when calculating temporal mediod value
|
84
|
+
int m_history_size;
|
85
|
+
|
86
|
+
// Rate at which to obtain new samples
|
87
|
+
int m_sampling_rate;
|
88
|
+
};
|
89
|
+
|
90
|
+
// --- Prati Mediod BGS algorithm ---
|
91
|
+
class PratiMediodBGS : public Bgs
|
92
|
+
{
|
93
|
+
private:
|
94
|
+
// sum of L-inf distances from a sample point to all other sample points
|
95
|
+
struct MEDIAN_BUFFER
|
96
|
+
{
|
97
|
+
std::vector<RgbPixel> pixels; // vector of pixels at give location in image
|
98
|
+
std::vector<int> dist; // distance from pixel to all other pixels
|
99
|
+
int pos; // current position in circular buffer
|
100
|
+
|
101
|
+
RgbPixel median; // median at this pixel location
|
102
|
+
int medianDist; // distance from median pixel to all other pixels
|
103
|
+
};
|
104
|
+
|
105
|
+
public:
|
106
|
+
PratiMediodBGS();
|
107
|
+
~PratiMediodBGS();
|
108
|
+
|
109
|
+
void Initalize(const BgsParams& param);
|
110
|
+
|
111
|
+
void InitModel(const RgbImage& data);
|
112
|
+
void Subtract(int frame_num, const RgbImage& data,
|
113
|
+
BwImage& low_threshold_mask, BwImage& high_threshold_mask);
|
114
|
+
void Update(int frame_num, const RgbImage& data, const BwImage& update_mask);
|
115
|
+
|
116
|
+
RgbImage* Background() { return &m_background; }
|
117
|
+
|
118
|
+
private:
|
119
|
+
MEDIAN_BUFFER* m_median_buffer;
|
120
|
+
|
121
|
+
void CalculateMasks(int r, int c, const RgbPixel& pixel);
|
122
|
+
void Combine(const BwImage& low_mask, const BwImage& high_mask, BwImage& output);
|
123
|
+
void UpdateMediod(int r, int c, const RgbImage& new_frame, int& dist);
|
124
|
+
|
125
|
+
PratiParams m_params;
|
126
|
+
|
127
|
+
RgbImage m_background;
|
128
|
+
|
129
|
+
BwImage m_mask_low_threshold;
|
130
|
+
BwImage m_mask_high_threshold;
|
131
|
+
};
|
132
|
+
|
133
|
+
};
|
134
|
+
};
|
135
|
+
|
136
|
+
#endif
|
137
|
+
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
142
|
+
|
data/ext/spyglass/contour.cc
CHANGED
@@ -66,8 +66,6 @@ namespace Spyglass {
|
|
66
66
|
std::vector<cv::Point> contour = to_value_vector(SG_GET_CONTOUR(self));
|
67
67
|
cv::approxPolyDP(contour, contour, cv::arcLength(cv::Mat(contour), true) * 0.02, true);
|
68
68
|
if (contour.size() != 4) {
|
69
|
-
// TODO: Throw exception here?
|
70
|
-
std::cout << "The object is not quadrilateral!" << std::endl;
|
71
69
|
return Qnil;
|
72
70
|
}
|
73
71
|
|
data/ext/spyglass/extconf.rb
CHANGED
data/ext/spyglass/prelude.h
CHANGED
@@ -15,7 +15,7 @@
|
|
15
15
|
return ptr; \
|
16
16
|
}
|
17
17
|
|
18
|
-
SG_GEN_GET_OBJECT_FUNCTION(SG_GET_BG_SUBTRACTOR, cv::
|
18
|
+
SG_GEN_GET_OBJECT_FUNCTION(SG_GET_BG_SUBTRACTOR, cv::BackgroundSubtractor);
|
19
19
|
SG_GEN_GET_OBJECT_FUNCTION(SG_GET_CLASSIFIER, cv::CascadeClassifier);
|
20
20
|
SG_GEN_GET_OBJECT_FUNCTION(SG_GET_COLOR, cv::Scalar);
|
21
21
|
SG_GEN_GET_OBJECT_FUNCTION(SG_GET_CONTOUR, std::vector<cv::Point *>);
|
data/ext/spyglass/spyglass.cc
CHANGED
@@ -19,7 +19,7 @@ extern "C" {
|
|
19
19
|
Spyglass::define_ruby_module();
|
20
20
|
Spyglass::ColorSpace::define_ruby_module();
|
21
21
|
|
22
|
-
Spyglass::BackgroundSubtractor::
|
22
|
+
Spyglass::BackgroundSubtractor::define_ruby_types();
|
23
23
|
Spyglass::CascadeClassifier::define_ruby_class();
|
24
24
|
Spyglass::Color::define_ruby_class();
|
25
25
|
Spyglass::Contour::define_ruby_class();
|
data/lib/spyglass/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Spyglass
|
2
|
-
VERSION = "0.0.
|
1
|
+
module Spyglass
|
2
|
+
VERSION = "0.0.4"
|
3
3
|
end
|
@@ -1,23 +1,28 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
[Spyglass::BackgroundSubtractor::MOG,
|
4
|
+
Spyglass::BackgroundSubtractor::MOG2,
|
5
|
+
Spyglass::BackgroundSubtractor::GMG,
|
6
|
+
Spyglass::BackgroundSubtractor::PratiMediod].each do |algorithm|
|
7
|
+
describe algorithm do
|
8
|
+
let(:bg) { described_class.new }
|
9
|
+
let(:lena) { Spyglass::Image.load(fixture_path('lena.jpg')) }
|
6
10
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
+
describe '.new' do
|
12
|
+
it 'should work with no arguments' do
|
13
|
+
expect( bg ).to be_a described_class
|
14
|
+
end
|
11
15
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
+
it 'should work with an options hash' do
|
17
|
+
expect { algorithm.new(history: 50, threshold: 64) }.not_to raise_error
|
18
|
+
end
|
19
|
+
end
|
16
20
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
end
|
21
|
+
describe '#subtract' do
|
22
|
+
it 'should return a Spyglass::Image' do
|
23
|
+
delta = bg.subtract(lena, 1)
|
24
|
+
expect( delta ).to be_a Spyglass::Image
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spyglass
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- André Medeiros
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-10-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -91,6 +91,14 @@ files:
|
|
91
91
|
- examples/video_capture.rb
|
92
92
|
- ext/spyglass/background_subtractor.cc
|
93
93
|
- ext/spyglass/background_subtractor.h
|
94
|
+
- ext/spyglass/bgslib_bgs.h
|
95
|
+
- ext/spyglass/bgslib_bgs_params.h
|
96
|
+
- ext/spyglass/bgslib_dp_prati_mediod_bgs.cc
|
97
|
+
- ext/spyglass/bgslib_dp_prati_mediod_bgs.h
|
98
|
+
- ext/spyglass/bgslib_image.cc
|
99
|
+
- ext/spyglass/bgslib_image.h
|
100
|
+
- ext/spyglass/bgslib_prati_mediod_bgs.cc
|
101
|
+
- ext/spyglass/bgslib_prati_mediod_bgs.h
|
94
102
|
- ext/spyglass/cascade_classifier.cc
|
95
103
|
- ext/spyglass/cascade_classifier.h
|
96
104
|
- ext/spyglass/color.cc
|