-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground_subtraction.cpp
More file actions
69 lines (51 loc) · 1.42 KB
/
Copy pathbackground_subtraction.cpp
File metadata and controls
69 lines (51 loc) · 1.42 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
#include <iostream>
#include <string>
#include <sstream>
#include <opencv2/features2d.hpp>
#include "opencv2/highgui.hpp"
#include <opencv2/imgcodecs.hpp>
#include "opencv2/imgproc.hpp"
#include <opencv2/video.hpp>
#include <opencv2/videoio.hpp>
using namespace std;
using namespace cv;
string WIN_SRC = "Camera";
string WIN_MOG2 = "MOG2";
const int thresh = 50;
int main() {
VideoCapture cap(1);
namedWindow(WIN_SRC, WINDOW_AUTOSIZE);
moveWindow(WIN_SRC, 20, 20);
namedWindow(WIN_MOG2, WINDOW_AUTOSIZE);
moveWindow(WIN_MOG2, 820, 20);
Ptr<BackgroundSubtractor> mog2 = createBackgroundSubtractorMOG2();
Mat frame, mog2Mask, mog2Thresh;
while (true) {
cap >> frame;
if (frame.empty())
break;
mog2->apply(frame, mog2Mask);
threshold(mog2Mask, mog2Thresh, thresh, 255, THRESH_TOZERO);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(mog2Mask, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
double max_area = 0, max_i = -1;
for (int i=0; i<contours.size(); i++) {
double area = contourArea(contours[i]);
if (area > max_area) {
max_area = area;
max_i = i;
}
}
if (max_i != -1) {
drawContours(frame, contours, max_i, Scalar(0, 0, 255), 2, LINE_8, hierarchy, 0);
rectangle(frame, boundingRect(contours[max_i]), Scalar(0, 255, 0));
}
imshow(WIN_SRC, frame);
imshow(WIN_MOG2, mog2Mask);
if (char key = waitKey(30) == 27) {
break;
}
}
return 0;
}