Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Movement detection.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include <opencv2/imgproc/imgproc.hpp>
- #include <opencv2/core.hpp>
- #include <opencv2/imgcodecs.hpp>
- #include <opencv2/highgui.hpp>
- #include <iostream>
- #include <deque>
- #include <opencv2/video/background_segm.hpp>
- #include <windows.h>
- using namespace cv;
- using namespace std;
- int main(int argc, char** argv)
- {
- auto source = "C:\\Users\\alior\\Downloads\\Video.mp4";
- VideoCapture inputVideo(source);
- if (!inputVideo.isOpened())
- {
- cout << "Could not open the input video: " << source << endl;
- return -1;
- }
- Mat frame;
- auto substractor = createBackgroundSubtractorMOG2(500, 16, false);
- while (inputVideo.read(frame))
- {
- Mat substracted;
- substractor->apply(frame, substracted);
- auto closingKernel = getStructuringElement(MORPH_ELLIPSE, Size(2, 2));
- auto openingKernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));
- Mat opened;
- morphologyEx(substracted, opened, MORPH_OPEN, openingKernel);
- Mat closed;
- morphologyEx(opened, closed, MORPH_CLOSE, closingKernel);
- Mat dilated;
- dilate(closed, dilated, closingKernel, Point(-1, -1), 2);
- vector<vector<Point> > contours;
- vector<Vec4i> hierarchy;
- findContours(dilated, contours, noArray(), CV_RETR_LIST, CV_CHAIN_APPROX_TC89_KCOS);
- float areaThreshold = 5;
- for (int i = 0; i < contours.size(); i++)
- {
- Scalar color = Scalar(255, 0, 0);
- auto rect = boundingRect(contours[i]);
- if (rect.area() < areaThreshold)
- continue;
- rectangle(frame, rect, color, 1);
- //drawContours(frame, contours, i, color, 2, 8, noArray(), 0, Point());
- }
- imshow("Opened", opened);
- imshow("Result", dilated);
- imshow("Movement", substracted);
- imshow("Frame", frame);
- while ((char)waitKey(30) != 'q');
- }
- inputVideo.release();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement