Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

int main(int argc, char *argv[]) { cv::Mat frame;
cv::Mat fg;
cv::Mat blurred; cv::Mat thresholded; cv::Mat thresholded2; cv::Mat result; cv::Mat bgmodel;
cv::namedWindow("Frame");
cv::namedWindow("Background Model"); cv::namedWindow("Blob"); cv::VideoCapture cap("campus3.avi");

cv::BackgroundSubtractorMOG2 bgs;                           

    bgs.nmixtures = 3;
    bgs.history = 1000;
    bgs.varThresholdGen = 15;
    bgs.bShadowDetection = true;                            
    bgs.nShadowDetection = 0;                               
    bgs.fTau = 0.5;                                         

std::vector<std::vector<cv::Point>> contours;               

for(;;)
{
    cap >> frame;                                           

    cv::GaussianBlur(frame,blurred,cv::Size(3,3),0,0,cv::BORDER_DEFAULT);

    bgs.operator()(blurred,fg);                         
    bgs.getBackgroundImage(bgmodel);                                

    cv::threshold(fg,thresholded,70.0f,255,CV_THRESH_BINARY);
    cv::threshold(fg,thresholded2,70.0f,255,CV_THRESH_BINARY);

    cv::Mat elementCLOSE(5,5,CV_8U,cv::Scalar(1));
    cv::morphologyEx(thresholded,thresholded,cv::MORPH_CLOSE,elementCLOSE);
    cv::morphologyEx(thresholded2,thresholded2,cv::MORPH_CLOSE,elementCLOSE);

    cv::findContours(thresholded,contours,CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);
    cv::cvtColor(thresholded2,result,CV_GRAY2RGB);

    int cmin = 50; 
    int cmax = 1000;

    for(int cnum = 0; cnum < contours.size(); cnum++){

    if(contours[cnum].size() > cmin && contours[cnum].size() < cmax){       

            for(int cpos = 0; cpos < contours[cnum].size(); cpos++){
                              cv::Point3_ <uchar>* p = frame.ptr<cv::Point3_ <uchar> >(contours[cnum][cpos].y, contours[cnum][cpos].x); //tracking blob per pixel here :D
                            }
             }

    }

    cv::imshow("Frame",frame);
    cv::imshow("Background Model",bgmodel);
    cv::imshow("Blob",result);
    if(cv::waitKey(30) >= 0) break;
}
return 0;

}