forked from sameernilupul/BackgroundSubstraction
/
main.cpp
82 lines (64 loc) · 2.38 KB
/
main.cpp
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
#include<opencv2/opencv.hpp>
#include<iostream>
#include<vector>
#include "SelectROI.h"
#define path "/Volumes/WINDOWS/TestData/5.avi"
#define min_size 500
#define max_size 10000
using namespace cv;
using namespace std;
RNG rng(12345);
int main(int argc, char *argv[])
{
Mat frame;
Mat back;
Mat fore;
Mat frame_roi;
Rect roi_rect;
VideoCapture cap(path);
BackgroundSubtractorMOG2 bg;
//bg.set("bShadowDetection", false);
//bg.bShadowDetection = false;
vector<vector<Point> > contours;
namedWindow("Frame");
namedWindow("Background");
cap >> frame;
Rect rect_roi = getSelected(frame);
for(;;)
{
cap >> frame; // capture frame from video
frame_roi = frame(rect_roi);
bg.operator ()(frame_roi,fore);
bg.getBackgroundImage(back); // get background image
erode(fore,fore,Mat());
dilate(fore,fore,Mat());
findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE); // find contours
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
}
/// Draw polygonal contour + bonding rects + circles
Mat drawing = Mat::zeros( fore.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
if(boundRect[i].area() <min_size || boundRect[i].area() >max_size){
continue;
}
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
//drawContours( frame, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( frame_roi, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
circle( frame_roi, center[i], (int)radius[i], color, 2, 8, 0 );
}
/////
imshow("Frame",frame);
imshow("Background",fore);
if(waitKey(1) >= 0) break;
}
return 0;
}