-
Notifications
You must be signed in to change notification settings - Fork 0
/
BackgroundDetection.cpp
99 lines (73 loc) · 2.17 KB
/
BackgroundDetection.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include "cv.hpp" //여기에 필요한 거 다 있음
#include "opencv2/opencv.hpp" //이렇게만 하면 다 뜬다 다 뜬다
#include "RoadDetection.h"
using namespace std;
using namespace cv;
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;
using namespace cv;
int main(void) {
Mat frame, poly, copy;
Mat back, fore, canny, gray;
Mat Final, Norm;
int key, frame_rate=30;
int blue=0, green=0, red=0;
//VideoCapture cap("example2.avi");
VideoCapture cap("C:\\Users\\Administrator\\Desktop\\Study\\4학년\\공프기\\OpenCV\\TrafficExample\\traffic2.mp4");
int start = 0;
BackgroundSubtractorMOG2 bg;
while (1) {
cap >> frame;
if (!frame.data) {
cout << "end" << endl;
break;
}
//foreground 계산
bg(frame, fore);
bg.getBackgroundImage(back);
imshow("BACK", back);
//Background-illumination-normalization.
//Norm = Normalization(back);
//GaussianBlur(Norm, Norm, Size(1,1), 0,0);
// imshow("BACK", Norm);
/*
if (start == 0) {
oldfore = fore.clone();
start = 1;
}
else
oldfore += fore.clone(); //oldfore는 그냥 차들이 지나간 영역을 계속 더하는 역할.
*/
cvtColor(back, gray, CV_BGR2GRAY);
Canny(gray, canny, 10, 30, 3);
imshow("CANNY", canny);
threshold(canny, canny, 127, 255, CV_THRESH_BINARY);
poly = nonedge_area(canny, 0.3, 8);
Final = FindLargestArea(back, poly);// MASK 영상
//cvtColor(Norm, Norm, CV_BGR2HSV);
Scalar value = mean(Norm, Final);
printf("Blue = %d, Green = %d, Red = %d\n", int(value.val[0]), int(value.val[1]), int(value.val[2]));
// for (int i = 0; i < 3; i++)
// printf("The result[%d] = %d", i, result[i]);
Final = roadFilter(int(value.val[0]), int(value.val[1]), int(value.val[2]), 1.2, Norm);
imshow("Frame", frame);
//imshow("Canny", canny);
//imshow("Background", back);
// imshow("COPY", copy);
//imshow("NORM", Norm);
//imshow("final", Final);
key = waitKey(frame_rate);
if (key == 27)
break;
else if (key == 32){
if (frame_rate == 30)
frame_rate = 0;
else if (frame_rate == 0)
frame_rate = 30;
}
}
return 0;
}