SHOW:
|
|
- or go back to the newest paste.
1 | /** | |
2 | - | *http://answers.opencv.org/question/38774/background-substraction-using-opencv-mog-from-live/ |
2 | + | *http://stackoverflow.com/questions/25161269/background-substraction-using-opencv-mog-from-live-camera-feed/ |
3 | */ | |
4 | ||
5 | #include <opencv2/opencv.hpp> | |
6 | #include <opencv2/highgui/highgui.hpp> | |
7 | #include <time.h> | |
8 | #include <iostream> | |
9 | #include <iomanip> | |
10 | ||
11 | using namespace std; | |
12 | using namespace cv; | |
13 | ||
14 | const static int SENSITIVITY_VALUE = 20; | |
15 | const static int BLUR_SIZE = 10; | |
16 | ||
17 | bool detectMotion(Mat thresholdImage); | |
18 | string intToString(int number); | |
19 | string getDateTimeForFile(); | |
20 | string getDateTime(); | |
21 | ||
22 | int main(int argc, char** argv) | |
23 | { | |
24 | initModule_video(); | |
25 | setUseOptimized(true); | |
26 | setNumThreads(4); | |
27 | ||
28 | VideoCapture cap; | |
29 | if (argc > 1) | |
30 | cap.open(argv[1]); | |
31 | else | |
32 | cap.open(0); | |
33 | cap.set(CV_CAP_PROP_FOURCC ,CV_FOURCC('D', 'I', 'V', '3') ); | |
34 | ||
35 | if (!cap.isOpened()) | |
36 | { | |
37 | std::cerr << "Cannot read video. Try moving video file to sample directory." << std::endl; | |
38 | return -1; | |
39 | } | |
40 | VideoWriter oVideoWriter; | |
41 | double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); | |
42 | double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); | |
43 | Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight)); | |
44 | ||
45 | std::string l_fname = ""; | |
46 | ||
47 | namedWindow("Image", WINDOW_NORMAL); | |
48 | namedWindow("Debug", WINDOW_NORMAL); | |
49 | ||
50 | Mat frame, fgMaskMOG; | |
51 | ||
52 | Ptr<BackgroundSubtractor> pMOG = new BackgroundSubtractorMOG(); | |
53 | for (;;) | |
54 | { | |
55 | if(!cap.read(frame)) { | |
56 | cerr << "Unable to read next frame." << endl; | |
57 | continue; | |
58 | } | |
59 | // process the image to obtain a mask image. | |
60 | pMOG->operator()(frame, fgMaskMOG); | |
61 | ||
62 | std::string time = getDateTime(); | |
63 | cv::rectangle(frame,cv::Rect(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-25,290,20),cv::Scalar(255,255,255),-1); | |
64 | cv::putText(frame,time,cv::Point(0,cap.get(CV_CAP_PROP_FRAME_HEIGHT)-5),1,1.5,cv::Scalar(0,0,0),2); | |
65 | ||
66 | // detect contours in the final threshold image. | |
67 | if(false){ | |
68 | string fname = "C:/Users/Familjen-Styren/Videos/VIDEO" + getDateTimeForFile() + ".avi"; | |
69 | if(l_fname == "" || l_fname != fname){ | |
70 | oVideoWriter = VideoWriter(fname, CV_FOURCC('D', 'I', 'V', '3'), 10, frameSize, true); | |
71 | if ( !oVideoWriter.isOpened() ) | |
72 | { | |
73 | std::cerr << "ERROR: Failed to initialize video writing" << std::endl; | |
74 | return -1; | |
75 | } | |
76 | } | |
77 | l_fname = fname; | |
78 | oVideoWriter.write(frame); | |
79 | putText(frame,"REC",Point(0,60),2,2,Scalar(0,0,255),2); | |
80 | } | |
81 | // show image. | |
82 | imshow("Image", frame); | |
83 | imshow("Debug",fgMaskMOG); | |
84 | int c = waitKey(30); | |
85 | if (c == 'q' || c == 'Q' || (c & 255) == 27) | |
86 | break; | |
87 | } | |
88 | return 0; | |
89 | } | |
90 | ||
91 | bool detectMotion(Mat thresholdImage){ | |
92 | //create motionDetected variable. | |
93 | bool motionDetected = false; | |
94 | //create temp Mat for threshold image | |
95 | Mat temp; | |
96 | thresholdImage.copyTo(temp); | |
97 | //these two vectors needed for output of findContours | |
98 | vector< vector<Point> > contours; | |
99 | vector<Vec4i> hierarchy; | |
100 | //find contours of filtered image using openCV findContours function | |
101 | //findContours(temp,contours,hierarchy,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE );// retrieves all contours | |
102 | findContours(temp,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE );// retrieves external contours | |
103 | ||
104 | //if contours vector is not empty, we have found some objects | |
105 | //we can simply say that if the vector is not empty, motion in the video feed has been detected. | |
106 | if(contours.size()>0)motionDetected=true; | |
107 | else motionDetected = false; | |
108 | ||
109 | return motionDetected; | |
110 | ||
111 | } | |
112 | ||
113 | string intToString(int number){ | |
114 | ||
115 | //this function has a number input and string output | |
116 | std::stringstream ss; | |
117 | ss << number; | |
118 | return ss.str(); | |
119 | } | |
120 | ||
121 | string getDateTime(){ | |
122 | //get the system time | |
123 | time_t theTime = time(NULL); | |
124 | struct tm *aTime = localtime(&theTime); | |
125 | //create string to store the date and time | |
126 | string dateTime; | |
127 | //convert year to string | |
128 | string year = intToString(aTime->tm_year + 1900); | |
129 | //use stringstream to add a leading '0' to the month (ie. 3 -> 03) | |
130 | //we use 'setw(2)' so that we force the string 2 characters wide with a zero in front of it. | |
131 | //if the month is '10' then it will remain '10' | |
132 | std::stringstream m; | |
133 | m<<std::setfill('0')<<std::setw(2)<< aTime->tm_mon + 1; | |
134 | string month = m.str(); | |
135 | //day | |
136 | std::stringstream d; | |
137 | d<<std::setfill('0')<<std::setw(2)<< aTime->tm_mday; | |
138 | string day = d.str(); | |
139 | //hour | |
140 | std::stringstream hr; | |
141 | hr<<setfill('0')<<std::setw(2)<<aTime->tm_hour; | |
142 | string hour = hr.str(); | |
143 | //minute | |
144 | std::stringstream min; | |
145 | min<<setfill('0')<<std::setw(2)<<aTime->tm_min; | |
146 | string minute = min.str(); | |
147 | //second | |
148 | std::stringstream sec; | |
149 | sec<<setfill('0')<<std::setw(2)<<aTime->tm_sec; | |
150 | string second = sec.str(); | |
151 | ||
152 | //here we use the year, month, day, hour, minute info to create a custom string | |
153 | //this will be displayed in the bottom left corner of our video feed. | |
154 | dateTime = year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second; | |
155 | ||
156 | return dateTime; | |
157 | } | |
158 | ||
159 | string getDateTimeForFile(){ | |
160 | //get the system time | |
161 | time_t theTime = time(NULL); | |
162 | struct tm *aTime = localtime(&theTime); | |
163 | //create string to store the date and time | |
164 | string dateTime; | |
165 | //convert year to string | |
166 | string year = intToString(aTime->tm_year + 1900); | |
167 | //use stringstream to add a leading '0' to the month (ie. 3 -> 03) | |
168 | //we use 'setw(2)' so that we force the string 2 characters wide with a zero in front of it. | |
169 | //if the month is '10' then it will remain '10' | |
170 | std::stringstream m; | |
171 | m<<std::setfill('0')<<std::setw(2)<< aTime->tm_mon + 1; | |
172 | string month = m.str(); | |
173 | //day | |
174 | std::stringstream d; | |
175 | d<<std::setfill('0')<<std::setw(2)<< aTime->tm_mday; | |
176 | string day = d.str(); | |
177 | //hour | |
178 | std::stringstream hr; | |
179 | hr<<setfill('0')<<std::setw(2)<<aTime->tm_hour; | |
180 | string hour = hr.str(); | |
181 | ||
182 | dateTime = year + "_" + month + "_" + day; | |
183 | return dateTime; | |
184 | } |