Repository navigation
Expand file tree
/
Copy pathmotiondetector.h
More file actions
179 lines (139 loc) · 4.11 KB
/
Copy pathmotiondetector.h
File metadata and controls
179 lines (139 loc) · 4.11 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Face manager 0.1
* Implementation of motion detector algorithms
*
* Copyright (c) 2018 David Snowdon. All rights reserved.
*
* Distributed under the Boost Software License, Version 1.0. (See accompanying
* file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*/
#ifndef MOTION_DETECTOR_H_
#define MOTION_DETECTOR_H_
#include "imagelogger.h"
#include <opencv2/opencv.hpp>
/**
* Motion detectors are stateful classes that detect motion in a sequence of images
*/
class MotionDetector {
public:
/*
* How many frames does the detector need before it can start detecting motion
*/
virtual int numInitFrames() = 0;
/*
* Pass an initialization frame. This must be called numInitFrames() times
*/
virtual void initFrame(cv::Mat frame) = 0;
/*
* After initialisation determine if this frame contains motion
*/
virtual bool detectMotion(cv::Mat frame) = 0;
};
/**
* Implement a no-op detector to make it possible to create timings without motion detection
*/
class ConstantMotionDetector : public MotionDetector {
public:
ConstantMotionDetector(bool detect_motion_result) {
detect_motion_result_ = detect_motion_result;
}
virtual int numInitFrames() {
return 0;
}
virtual void initFrame(cv::Mat frame) {
}
virtual bool detectMotion(cv::Mat frame) {
return detect_motion_result_;
}
private:
bool detect_motion_result_;
};
/**
* Does not detect motion, just signals motion every specified number of frames to reduce overall processing time
*/
class FrameSkipMotionDetector : public MotionDetector {
public:
FrameSkipMotionDetector(int frame_skip_count) {
frame_skip_count_ = frame_skip_count;
frame_count_ = 0;
}
virtual int numInitFrames() {
return 0;
}
virtual void initFrame(cv::Mat frame) {
}
virtual bool detectMotion(cv::Mat frame) {
return (++frame_count_ % frame_skip_count_) == 0;
}
private:
int frame_skip_count_;
int frame_count_;
};
/**
* Based on algorithm described in https://www.pyimagesearch.com/2015/05/25/basic-motion-detection-and-tracking-with-python-and-opencv/
*/
class ContourMotionDetector : public MotionDetector {
public:
ContourMotionDetector(int image_width, int motion_detected_area) {
image_width_ = image_width;
motion_dectected_area_ = motion_detected_area;
}
virtual int numInitFrames() {
return 1;
}
virtual void initFrame(cv::Mat frame);
virtual bool detectMotion(cv::Mat frame);
private:
cv::Mat preProcessImage(cv::Mat frame);
int image_width_;
int motion_dectected_area_;
cv::Mat accumulator_;
};
/**
* Based on algorithm described in https://www.pyimagesearch.com/2014/09/15/python-compare-two-images/
*/
class MeanSquaredErrorMotionDetector : public MotionDetector {
public:
MeanSquaredErrorMotionDetector(int image_width, double threshold, bool use_blur) {
image_width_ = image_width;
threshold_ = threshold;
use_blur_ = use_blur;
}
virtual int numInitFrames() {
return 1;
}
virtual void initFrame(cv::Mat frame);
virtual bool detectMotion(cv::Mat frame);
private:
cv::Mat preProcessImage(cv::Mat frame);
int image_width_;
double threshold_;
bool use_blur_;
cv::Mat accumulator_;
};
/**
* Based on algorithm and code from
* http://www.steinm.com/blog/motion-detection-webcam-python-opencv-differential-images/
* https://github.com/cedricve/motion-detection
*/
class FrameDifferenceMotionDetector : public MotionDetector {
public:
FrameDifferenceMotionDetector(int image_width, double threshold, bool use_blur) {
image_width_ = image_width;
threshold_ = threshold;
use_blur_ = use_blur;
}
virtual int numInitFrames() {
return 2;
}
virtual void initFrame(cv::Mat frame);
virtual bool detectMotion(cv::Mat frame);
private:
cv::Mat preProcessImage(cv::Mat frame);
int image_width_;
double threshold_;
bool use_blur_;
cv::Mat prev_frame_;
cv::Mat current_frame_;
};
#endif // MOTION_DETECTOR_H_