Skip to content

Instantly share code, notes, and snippets.

@erogol
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erogol/f2aa111eebe9cc75168b to your computer and use it in GitHub Desktop.
Save erogol/f2aa111eebe9cc75168b to your computer and use it in GitHub Desktop.
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc.hpp>
#include<opencv2/imgcodecs/imgcodecs.hpp>
#include<opencv2/videoio.hpp>
#include<iostream>
#include<vector>
int main(int argc, char *argv[])
{
cv::Mat frame;
cv::Mat back;
cv::Mat fore;
cv::VideoCapture cap(0); //define video source
cv::Ptr<cv::BackgroundSubtractor> bg = cv::createBackgroundSubtractorMOG2(500, 16, false);
std::vector<std::vector<cv::Point> > contours;
cv::namedWindow("Frame");
cv::namedWindow("Background");
for(;;)
{
cap >> frame;
bg->apply(frame,fore);
bg->getBackgroundImage(back);
cv::erode(fore,fore,cv::Mat());
cv::dilate(fore,fore,cv::Mat());
cv::findContours(fore.clone(),contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
cv::imshow("Foreground", fore);
cv::imshow("Frame",frame);
cv::imshow("Background",back);
if(cv::waitKey(30) >= 0) break;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment