Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cv.h>
- #include "opencv2/core/core.hpp"
- #include "opencv2/imgproc/imgproc.hpp"
- #include "opencv2/video/background_segm.hpp"
- #include "opencv2/highgui/highgui.hpp"
- using namespace cv;
- using namespace std;
- int main (int argc, char** argv) {
- // Read in our stuff
- if (argc != 3) {
- cout << "Usage: ./Test input.avi output.avi" << endl;
- exit(4);
- }
- // Attempt to read in the input file
- VideoCapture inVid;
- if (!inVid.open(argv[1])) {
- cout << "Failed to open the input video" << endl;
- exit(5);
- }
- VideoWriter outVid;
- if (!outVid.open(argv[2], inVid.get(CV_CAP_PROP_FOURCC), inVid.get(CV_CAP_PROP_FPS), Size2f(inVid.get(CV_CAP_PROP_FRAME_WIDTH), inVid.get(CV_CAP_PROP_FRAME_HEIGHT)))) {
- cout << "Failed top open the output video" << endl;
- exit(6);
- }
- // Lets create our basic "background". Unfortunatly, we have to assume our first image is "clean" in this regard (dunno how to fix... *shrug*)
- BackgroundSubtractorMOG2 mog;
- Mat img, fgmask;
- vector<vector<Point> > v;
- int idx, area = 0;
- while (inVid.read(img)) {
- // We have a frame to process
- // Update our mog
- mog(img, fgmask);
- // Find the contours of the fgmask
- findContours(fgmask, v, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
- for (int i = 0; i < v.size(); i++) {
- if (area < v[i].size()) {
- idx = i;
- }
- }
- // Calculate and draw our bonding box
- Rect rect = boundingRect(v[idx]);
- Point pt1, pt2;
- pt1.x = rect.x;
- pt1.y = rect.y;
- pt2.x = rect.x + rect.width;
- pt2.y = rect.y + rect.height;
- rectangle(img, pt1, pt2, CV_RGB(255,0,0), 1);
- outVid.write(img);
- }
- mog(img, fgmask, 0);
- return 0;
- }
Add Comment
Please, Sign In to add comment