/
motion_detector.hpp
131 lines (112 loc) · 3.49 KB
/
motion_detector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/core/mat.hpp>
#include <opencv2/core/operations.hpp>
#include <opencv2/core/types_c.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include <vector>
#include "config.hpp"
using namespace cv;
RNG rng(12345);
class MotionDetector
{
public:
Mat steady;
Mat current;
Mat difference;
float score, minimum_area, motion_score_threshold, acculum_weight, max_allowed_area;
bool initialized;
Config config;
public:
MotionDetector() :
initialized(false),
score(0)
{
config.load(); // FIXME
minimum_area = config.minimum_area;
max_allowed_area = config.max_allowed_area;
acculum_weight = config.acculum_weight;
motion_score_threshold = config.motion_score_threshold;
}
void setSize(Size size)
{
steady = Mat::zeros(size, CV_32F);
}
void addFrame(Mat img)
{
cvtColor(img, current, CV_BGR2GRAY);
current.convertTo(current, CV_32F, 1 / 255.0);
//blur(current, current, Size(5, 5));
calculate_diff();
accumulateWeighted(current, steady, acculum_weight);
}
void calculate_diff()
{
if (!initialized)
{
initialized = true;
reset();
return;
}
absdiff(steady, current, difference);
difference.convertTo(difference, CV_8U, 255);
threshold(difference, difference, 5, 255, CV_THRESH_BINARY);
//adaptiveThreshold(difference, difference, 255, ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, 7, 8);
int size = 10;
Mat kernel = getStructuringElement(
MORPH_ELLIPSE,
Size(2*size + 1, 2*size + 1),
Point(size, size)
);
//dilate(difference, difference, kernel);
//erode(difference, difference, kernel);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(difference, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));
//Mat out = Mat::zeros(difference.size(), CV_8UC3);
//Mat out = current;
float max_move = 0;
for (auto contour : contours)
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
//drawContours(out, contours, i, color, 2, 8, hierarchy, 0, Point());
Rect bndRect = boundingRect(contour);
Point pt1, pt2;
pt1.x = bndRect.x;
pt1.y = bndRect.y;
pt2.x = bndRect.x + bndRect.width;
pt2.y = bndRect.y + bndRect.height;
//rectangle(out, pt1, pt2, CV_RGB(255,255,255), 2);
float area_proportion = (float)(bndRect.width * bndRect.height) / (float)(steady.rows * steady.cols);
if (area_proportion > max_move && area_proportion < max_allowed_area)
{
max_move = area_proportion;
}
}
score -= .05;
if (max_move > minimum_area)
{
if (config.debug) {
cerr << "Largest moving area: " << max_move << endl;
}
score += .3;
}
if (score < 0)
{
score = 0;
}
if (config.debug) {
cerr << "Motion score = " << score << endl;
}
}
bool isMotion()
{
return score > motion_score_threshold;
}
void reset()
{
score = 0;
current.copyTo(steady);
}
};